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.

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