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.

218 lines
5.4 KiB

6 years ago
8 years ago
5 years ago
5 years ago
6 years ago
6 years ago
8 years ago
5 years ago
5 years ago
5 years ago
7 years ago
6 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
6 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
5 years ago
5 years ago
5 years ago
8 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_SIM808
  13. // #define TINY_GSM_MODEM_SIM868
  14. // #define TINY_GSM_MODEM_SIM900
  15. // #define TINY_GSM_MODEM_SIM7000
  16. // #define TINY_GSM_MODEM_SIM5360
  17. // #define TINY_GSM_MODEM_UBLOX
  18. // #define TINY_GSM_MODEM_SARAR4
  19. // #define TINY_GSM_MODEM_M95
  20. // #define TINY_GSM_MODEM_BG96
  21. // #define TINY_GSM_MODEM_A6
  22. // #define TINY_GSM_MODEM_A7
  23. // #define TINY_GSM_MODEM_M590
  24. // #define TINY_GSM_MODEM_MC60
  25. // #define TINY_GSM_MODEM_MC60E
  26. // #define TINY_GSM_MODEM_ESP8266
  27. // #define TINY_GSM_MODEM_XBEE
  28. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  29. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  30. #define SerialMon Serial
  31. // Set serial for AT commands (to the module)
  32. // Use Hardware Serial on Mega, Leonardo, Micro
  33. #define SerialAT Serial1
  34. // or Software Serial on Uno, Nano
  35. //#include <SoftwareSerial.h>
  36. //SoftwareSerial SerialAT(2, 3); // RX, TX
  37. // Increase RX buffer to capture the entire response
  38. // Chips without internal buffering (A6/A7, ESP8266, M590)
  39. // need enough space in the buffer for the entire response
  40. // else data will be lost (and the http library will fail).
  41. #define TINY_GSM_RX_BUFFER 650
  42. // See all AT commands, if wanted
  43. //#define DUMP_AT_COMMANDS
  44. // Define the serial console for debug prints, if needed
  45. #define TINY_GSM_DEBUG SerialMon
  46. // Range to attempt to autobaud
  47. #define GSM_AUTOBAUD_MIN 9600
  48. #define GSM_AUTOBAUD_MAX 115200
  49. // Add a reception delay, if needed
  50. //#define TINY_GSM_YIELD() { delay(2); }
  51. // Uncomment this if you want to use SSL
  52. //#define USE_SSL
  53. #define TINY_GSM_USE_GPRS true
  54. #define TINY_GSM_USE_WIFI false
  55. // set GSM PIN, if any
  56. #define GSM_PIN ""
  57. // Your GPRS credentials
  58. // Leave empty, if missing user or pass
  59. const char apn[] = "YourAPN";
  60. const char gprsUser[] = "";
  61. const char gprsPass[] = "";
  62. const char wifiPass[] = "YourWiFiPass";
  63. // Server details
  64. const char server[] = "vsh.pp.ua";
  65. const char resource[] = "/TinyGSM/logo.txt";
  66. #include <TinyGsmClient.h>
  67. #ifdef DUMP_AT_COMMANDS
  68. #include <StreamDebugger.h>
  69. StreamDebugger debugger(SerialAT, SerialMon);
  70. TinyGsm modem(debugger);
  71. #else
  72. TinyGsm modem(SerialAT);
  73. #endif
  74. #ifdef USE_SSL
  75. TinyGsmClientSecure client(modem);
  76. const int port = 443;
  77. #else
  78. TinyGsmClient client(modem);
  79. const int port = 80;
  80. #endif
  81. void setup() {
  82. // Set console baud rate
  83. SerialMon.begin(115200);
  84. delay(10);
  85. // !!!!!!!!!!!
  86. // Set your reset, enable, power pins here
  87. // !!!!!!!!!!!
  88. SerialMon.println("Wait...");
  89. // Set GSM module baud rate
  90. // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  91. SerialAT.begin(9600);
  92. delay(3000);
  93. // Restart takes quite some time
  94. // To skip it, call init() instead of restart()
  95. SerialMon.println("Initializing modem...");
  96. modem.restart();
  97. // modem.init();
  98. String modemInfo = modem.getModemInfo();
  99. SerialMon.print("Modem: ");
  100. SerialMon.println(modemInfo);
  101. #if TINY_GSM_USE_GPRS
  102. // Unlock your SIM card with a PIN if needed
  103. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  104. modem.simUnlock(GSM_PIN);
  105. }
  106. #endif
  107. }
  108. void loop() {
  109. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  110. SerialMon.print(F("Setting SSID/password..."));
  111. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  112. SerialMon.println(" fail");
  113. delay(10000);
  114. return;
  115. }
  116. SerialMon.println(" success");
  117. #endif
  118. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  119. // The XBee must run the gprsConnect function BEFORE waiting for network!
  120. modem.gprsConnect(apn, gprsUser, gprsPass);
  121. #endif
  122. SerialMon.print("Waiting for network...");
  123. if (!modem.waitForNetwork()) {
  124. SerialMon.println(" fail");
  125. delay(10000);
  126. return;
  127. }
  128. SerialMon.println(" success");
  129. if (modem.isNetworkConnected()) {
  130. SerialMon.println("Network connected");
  131. }
  132. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  133. SerialMon.print(F("Connecting to "));
  134. SerialMon.print(apn);
  135. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  136. SerialMon.println(" fail");
  137. delay(10000);
  138. return;
  139. }
  140. SerialMon.println(" success");
  141. #endif
  142. SerialMon.print("Connecting to ");
  143. SerialMon.println(server);
  144. if (!client.connect(server, port)) {
  145. SerialMon.println(" fail");
  146. delay(10000);
  147. return;
  148. }
  149. SerialMon.println(" success");
  150. // Make a HTTP GET request:
  151. SerialMon.println("Performing HTTP GET request...");
  152. client.print(String("GET ") + resource + " HTTP/1.1\r\n");
  153. client.print(String("Host: ") + server + "\r\n");
  154. client.print("Connection: close\r\n\r\n");
  155. client.println();
  156. unsigned long timeout = millis();
  157. while (client.connected() && millis() - timeout < 10000L) {
  158. // Print available data
  159. while (client.available()) {
  160. char c = client.read();
  161. SerialMon.print(c);
  162. timeout = millis();
  163. }
  164. }
  165. SerialMon.println();
  166. // Shutdown
  167. client.stop();
  168. SerialMon.println(F("Server disconnected"));
  169. #if TINY_GSM_USE_WIFI
  170. modem.networkDisconnect();
  171. SerialMon.println(F("WiFi disconnected"));
  172. #endif
  173. #if TINY_GSM_USE_GPRS
  174. modem.gprsDisconnect();
  175. SerialMon.println(F("GPRS disconnected"));
  176. #endif
  177. // Do nothing forevermore
  178. while (true) {
  179. delay(1000);
  180. }
  181. }