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.

242 lines
6.2 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
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
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 how you're planning to connect to the internet
  55. #define TINY_GSM_USE_GPRS true
  56. #define TINY_GSM_USE_WIFI false
  57. // set GSM PIN, if any
  58. #define GSM_PIN ""
  59. // Your GPRS credentials, if any
  60. const char apn[] = "YourAPN";
  61. const char gprsUser[] = "";
  62. const char gprsPass[] = "";
  63. // Your WiFi connection credentials, if applicable
  64. const char wifiSSID[] = "YourSSID";
  65. const char wifiPass[] = "YourWiFiPass";
  66. // Server details
  67. const char server[] = "vsh.pp.ua";
  68. const char resource[] = "/TinyGSM/logo.txt";
  69. #include <TinyGsmClient.h>
  70. // Just in case someone defined the wrong thing..
  71. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  72. #undef TINY_GSM_USE_GPRS
  73. #undef TINY_GSM_USE_WIFI
  74. #define TINY_GSM_USE_GPRS false
  75. #define TINY_GSM_USE_WIFI true
  76. #endif
  77. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  78. #undef TINY_GSM_USE_GPRS
  79. #undef TINY_GSM_USE_WIFI
  80. #define TINY_GSM_USE_GPRS true
  81. #define TINY_GSM_USE_WIFI false
  82. #endif
  83. #ifdef DUMP_AT_COMMANDS
  84. #include <StreamDebugger.h>
  85. StreamDebugger debugger(SerialAT, SerialMon);
  86. TinyGsm modem(debugger);
  87. #else
  88. TinyGsm modem(SerialAT);
  89. #endif
  90. #ifdef USE_SSL
  91. TinyGsmClientSecure client(modem);
  92. const int port = 443;
  93. #else
  94. TinyGsmClient client(modem);
  95. const int port = 80;
  96. #endif
  97. void setup() {
  98. // Set console baud rate
  99. SerialMon.begin(115200);
  100. delay(10);
  101. // !!!!!!!!!!!
  102. // Set your reset, enable, power pins here
  103. // !!!!!!!!!!!
  104. SerialMon.println("Wait...");
  105. // Set GSM module baud rate
  106. // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  107. SerialAT.begin(9600);
  108. delay(3000);
  109. // Restart takes quite some time
  110. // To skip it, call init() instead of restart()
  111. SerialMon.println("Initializing modem...");
  112. modem.restart();
  113. // modem.init();
  114. String modemInfo = modem.getModemInfo();
  115. SerialMon.print("Modem Info: ");
  116. SerialMon.println(modemInfo);
  117. #if TINY_GSM_USE_GPRS
  118. // Unlock your SIM card with a PIN if needed
  119. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  120. modem.simUnlock(GSM_PIN);
  121. }
  122. #endif
  123. }
  124. void loop() {
  125. #if TINY_GSM_USE_WIFI
  126. // Wifi connection parameters must be set before waiting for the network
  127. SerialMon.print(F("Setting SSID/password..."));
  128. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  129. SerialMon.println(" fail");
  130. delay(10000);
  131. return;
  132. }
  133. SerialMon.println(" success");
  134. #endif
  135. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  136. // The XBee must run the gprsConnect function BEFORE waiting for network!
  137. modem.gprsConnect(apn, gprsUser, gprsPass);
  138. #endif
  139. SerialMon.print("Waiting for network...");
  140. if (!modem.waitForNetwork()) {
  141. SerialMon.println(" fail");
  142. delay(10000);
  143. return;
  144. }
  145. SerialMon.println(" success");
  146. if (modem.isNetworkConnected()) {
  147. SerialMon.println("Network connected");
  148. }
  149. #if TINY_GSM_USE_GPRS
  150. // GPRS connection parameters are usually set after network registration
  151. SerialMon.print(F("Connecting to "));
  152. SerialMon.print(apn);
  153. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  154. SerialMon.println(" fail");
  155. delay(10000);
  156. return;
  157. }
  158. SerialMon.println(" success");
  159. if (modem.isGprsConnected()) {
  160. SerialMon.println("GPRS connected");
  161. }
  162. #endif
  163. SerialMon.print("Connecting to ");
  164. SerialMon.println(server);
  165. if (!client.connect(server, port)) {
  166. SerialMon.println(" fail");
  167. delay(10000);
  168. return;
  169. }
  170. SerialMon.println(" success");
  171. // Make a HTTP GET request:
  172. SerialMon.println("Performing HTTP GET request...");
  173. client.print(String("GET ") + resource + " HTTP/1.1\r\n");
  174. client.print(String("Host: ") + server + "\r\n");
  175. client.print("Connection: close\r\n\r\n");
  176. client.println();
  177. unsigned long timeout = millis();
  178. while (client.connected() && millis() - timeout < 10000L) {
  179. // Print available data
  180. while (client.available()) {
  181. char c = client.read();
  182. SerialMon.print(c);
  183. timeout = millis();
  184. }
  185. }
  186. SerialMon.println();
  187. // Shutdown
  188. client.stop();
  189. SerialMon.println(F("Server disconnected"));
  190. #if TINY_GSM_USE_WIFI
  191. modem.networkDisconnect();
  192. SerialMon.println(F("WiFi disconnected"));
  193. #endif
  194. #if TINY_GSM_USE_GPRS
  195. modem.gprsDisconnect();
  196. SerialMon.println(F("GPRS disconnected"));
  197. #endif
  198. // Do nothing forevermore
  199. while (true) {
  200. delay(1000);
  201. }
  202. }