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.

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