You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
4.5 KiB

6 years ago
8 years ago
6 years ago
6 years ago
8 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**************************************************************
  2. *
  3. * This sketch connects to a website and downloads a page.
  4. * It can be used to perform HTTP/RESTful API calls.
  5. *
  6. * TinyGSM Getting Started guide:
  7. * https://tiny.cc/tinygsm-readme
  8. *
  9. **************************************************************/
  10. // Select your modem:
  11. #define TINY_GSM_MODEM_SIM800
  12. // #define TINY_GSM_MODEM_SIM900
  13. // #define TINY_GSM_MODEM_SIM808
  14. // #define TINY_GSM_MODEM_SIM868
  15. // #define TINY_GSM_MODEM_SIM7000
  16. // #define TINY_GSM_MODEM_UBLOX
  17. // #define TINY_GSM_MODEM_M95
  18. // #define TINY_GSM_MODEM_BG96
  19. // #define TINY_GSM_MODEM_A6
  20. // #define TINY_GSM_MODEM_A7
  21. // #define TINY_GSM_MODEM_M590
  22. // #define TINY_GSM_MODEM_MC60
  23. // #define TINY_GSM_MODEM_MC60E
  24. // #define TINY_GSM_MODEM_ESP8266
  25. // #define TINY_GSM_MODEM_XBEE
  26. // Increase RX buffer if needed
  27. //#define TINY_GSM_RX_BUFFER 512
  28. // See the debugging, if wanted
  29. //#define TINY_GSM_DEBUG Serial
  30. //#define LOGGING
  31. // Add a reception delay, if needed
  32. //#define TINY_GSM_YIELD() { delay(1); }
  33. #include <TinyGsmClient.h>
  34. // Uncomment this if you want to see all AT commands
  35. //#define DUMP_AT_COMMANDS
  36. // Uncomment this if you want to use SSL
  37. //#define USE_SSL
  38. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  39. #define SerialMon Serial
  40. // Use Hardware Serial on Mega, Leonardo, Micro
  41. #define SerialAT Serial1
  42. // or Software Serial on Uno, Nano
  43. //#include <SoftwareSerial.h>
  44. //SoftwareSerial SerialAT(2, 3); // RX, TX
  45. // Your GPRS credentials
  46. // Leave empty, if missing user or pass
  47. const char apn[] = "YourAPN";
  48. const char gprsUser[] = "";
  49. const char gprsPass[] = "";
  50. const char wifiSSID[] = "YourSSID";
  51. const char wifiPass[] = "SSIDpw";
  52. // Server details
  53. const char server[] = "vsh.pp.ua";
  54. const char resource[] = "/TinyGSM/logo.txt";
  55. #ifdef DUMP_AT_COMMANDS
  56. #include <StreamDebugger.h>
  57. StreamDebugger debugger(SerialAT, SerialMon);
  58. TinyGsm modem(debugger);
  59. #else
  60. TinyGsm modem(SerialAT);
  61. #endif
  62. #ifdef USE_SSL
  63. TinyGsmClientSecure client(modem);
  64. const int port = 443;
  65. #else
  66. TinyGsmClient client(modem);
  67. const int port = 80;
  68. #endif
  69. void setup() {
  70. // Set console baud rate
  71. SerialMon.begin(115200);
  72. delay(10);
  73. SerialMon.println(F("Wait..."));
  74. pinMode(23, OUTPUT);
  75. digitalWrite(23, LOW);
  76. // Set GSM module baud rate
  77. SerialAT.begin(9600);
  78. delay(3000);
  79. // Restart takes quite some time
  80. // To skip it, call init() instead of restart()
  81. SerialMon.println(F("Initializing modem..."));
  82. modem.restart();
  83. String modemInfo = modem.getModemInfo();
  84. SerialMon.print(F("Modem: "));
  85. SerialMon.println(modemInfo);
  86. // Unlock your SIM card with a PIN
  87. //modem.simUnlock("1234");
  88. }
  89. void loop() {
  90. if (modem.hasWifi()) {
  91. SerialMon.print(F("Setting SSID/password..."));
  92. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  93. SerialMon.println(" fail");
  94. delay(10000);
  95. return;
  96. }
  97. SerialMon.println(" OK");
  98. }
  99. else if (modem.getModemName().indexOf("XBee") >= 0) {
  100. SerialMon.print(F("Setting APN"));
  101. if (!modem.gprsConnect(apn)) {
  102. SerialMon.println(" fail");
  103. delay(10000);
  104. return;
  105. }
  106. SerialMon.println(" OK");
  107. }
  108. SerialMon.print(F("Waiting for network..."));
  109. if (!modem.waitForNetwork()) {
  110. SerialMon.println(" fail");
  111. delay(10000);
  112. return;
  113. }
  114. SerialMon.println(" OK");
  115. if (modem.hasGPRS()) {
  116. SerialMon.print(F("Connecting to "));
  117. SerialMon.print(apn);
  118. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  119. SerialMon.println(" fail");
  120. delay(10000);
  121. return;
  122. }
  123. SerialMon.println(" OK");
  124. }
  125. SerialMon.print(F("Connecting to "));
  126. SerialMon.print(server);
  127. if (!client.connect(server, port)) {
  128. SerialMon.println(" fail");
  129. delay(10000);
  130. return;
  131. }
  132. SerialMon.println(" OK");
  133. // Make a HTTP GET request:
  134. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  135. client.print(String("Host: ") + server + "\r\n");
  136. client.print("Connection: close\r\n\r\n");
  137. unsigned long timeout = millis();
  138. while (client.connected() && millis() - timeout < 10000L) {
  139. // Print available data
  140. while (client.available()) {
  141. char c = client.read();
  142. SerialMon.print(c);
  143. timeout = millis();
  144. }
  145. }
  146. SerialMon.println();
  147. // Shutdown
  148. client.stop();
  149. SerialMon.println(F("Server disconnected"));
  150. if (modem.hasWifi()) {
  151. modem.networkDisconnect();
  152. SerialMon.println(F("WiFi disconnected"));
  153. }
  154. else {
  155. modem.gprsDisconnect();
  156. SerialMon.println(F("GPRS disconnected"));
  157. }
  158. // Do nothing forevermore
  159. while (true) {
  160. delay(1000);
  161. }
  162. }