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.

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