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.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
5 years ago
7 years ago
6 years ago
6 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 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_SIM7000SSL
  17. // #define TINY_GSM_MODEM_SIM7080
  18. // #define TINY_GSM_MODEM_SIM5360
  19. // #define TINY_GSM_MODEM_SIM7600
  20. // #define TINY_GSM_MODEM_UBLOX
  21. // #define TINY_GSM_MODEM_SARAR4
  22. // #define TINY_GSM_MODEM_M95
  23. // #define TINY_GSM_MODEM_BG96
  24. // #define TINY_GSM_MODEM_A6
  25. // #define TINY_GSM_MODEM_A7
  26. // #define TINY_GSM_MODEM_M590
  27. // #define TINY_GSM_MODEM_MC60
  28. // #define TINY_GSM_MODEM_MC60E
  29. // #define TINY_GSM_MODEM_ESP8266
  30. // #define TINY_GSM_MODEM_XBEE
  31. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  32. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  33. #define SerialMon Serial
  34. // Set serial for AT commands (to the module)
  35. // Use Hardware Serial on Mega, Leonardo, Micro
  36. #ifndef __AVR_ATmega328P__
  37. #define SerialAT Serial1
  38. // or Software Serial on Uno, Nano
  39. #else
  40. #include <SoftwareSerial.h>
  41. SoftwareSerial SerialAT(2, 3); // RX, TX
  42. #endif
  43. // Increase RX buffer to capture the entire response
  44. // Chips without internal buffering (A6/A7, ESP8266, M590)
  45. // need enough space in the buffer for the entire response
  46. // else data will be lost (and the http library will fail).
  47. #if !defined(TINY_GSM_RX_BUFFER)
  48. #define TINY_GSM_RX_BUFFER 650
  49. #endif
  50. // See all AT commands, if wanted
  51. // #define DUMP_AT_COMMANDS
  52. // Define the serial console for debug prints, if needed
  53. #define TINY_GSM_DEBUG SerialMon
  54. // Range to attempt to autobaud
  55. // NOTE: DO NOT AUTOBAUD in production code. Once you've established
  56. // communication, set a fixed baud rate using modem.setBaud(#).
  57. #define GSM_AUTOBAUD_MIN 9600
  58. #define GSM_AUTOBAUD_MAX 115200
  59. // Add a reception delay, if needed.
  60. // This may be needed for a fast processor at a slow baud rate.
  61. // #define TINY_GSM_YIELD() { delay(2); }
  62. // Uncomment this if you want to use SSL
  63. // #define USE_SSL
  64. // Define how you're planning to connect to the internet.
  65. // This is only needed for this example, not in other code.
  66. #define TINY_GSM_USE_GPRS true
  67. #define TINY_GSM_USE_WIFI false
  68. // set GSM PIN, if any
  69. #define GSM_PIN ""
  70. // Your GPRS credentials, if any
  71. const char apn[] = "YourAPN";
  72. const char gprsUser[] = "";
  73. const char gprsPass[] = "";
  74. // Your WiFi connection credentials, if applicable
  75. const char wifiSSID[] = "YourSSID";
  76. const char wifiPass[] = "YourWiFiPass";
  77. // Server details
  78. const char server[] = "vsh.pp.ua";
  79. const char resource[] = "/TinyGSM/logo.txt";
  80. #include <TinyGsmClient.h>
  81. // Just in case someone defined the wrong thing..
  82. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  83. #undef TINY_GSM_USE_GPRS
  84. #undef TINY_GSM_USE_WIFI
  85. #define TINY_GSM_USE_GPRS false
  86. #define TINY_GSM_USE_WIFI true
  87. #endif
  88. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  89. #undef TINY_GSM_USE_GPRS
  90. #undef TINY_GSM_USE_WIFI
  91. #define TINY_GSM_USE_GPRS true
  92. #define TINY_GSM_USE_WIFI false
  93. #endif
  94. #ifdef DUMP_AT_COMMANDS
  95. #include <StreamDebugger.h>
  96. StreamDebugger debugger(SerialAT, SerialMon);
  97. TinyGsm modem(debugger);
  98. #else
  99. TinyGsm modem(SerialAT);
  100. #endif
  101. #ifdef USE_SSL
  102. TinyGsmClientSecure client(modem);
  103. const int port = 443;
  104. #else
  105. TinyGsmClient client(modem);
  106. const int port = 80;
  107. #endif
  108. void setup() {
  109. // Set console baud rate
  110. SerialMon.begin(115200);
  111. delay(10);
  112. // !!!!!!!!!!!
  113. // Set your reset, enable, power pins here
  114. // !!!!!!!!!!!
  115. SerialMon.println("Wait...");
  116. // Set GSM module baud rate
  117. TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  118. // SerialAT.begin(9600);
  119. delay(6000);
  120. // Restart takes quite some time
  121. // To skip it, call init() instead of restart()
  122. SerialMon.println("Initializing modem...");
  123. modem.restart();
  124. // modem.init();
  125. String modemInfo = modem.getModemInfo();
  126. SerialMon.print("Modem Info: ");
  127. SerialMon.println(modemInfo);
  128. #if TINY_GSM_USE_GPRS
  129. // Unlock your SIM card with a PIN if needed
  130. if (GSM_PIN && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }
  131. #endif
  132. }
  133. void loop() {
  134. #if TINY_GSM_USE_WIFI
  135. // Wifi connection parameters must be set before waiting for the network
  136. SerialMon.print(F("Setting SSID/password..."));
  137. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  138. SerialMon.println(" fail");
  139. delay(10000);
  140. return;
  141. }
  142. SerialMon.println(" success");
  143. #endif
  144. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  145. // The XBee must run the gprsConnect function BEFORE waiting for network!
  146. modem.gprsConnect(apn, gprsUser, gprsPass);
  147. #endif
  148. SerialMon.print("Waiting for network...");
  149. if (!modem.waitForNetwork()) {
  150. SerialMon.println(" fail");
  151. delay(10000);
  152. return;
  153. }
  154. SerialMon.println(" success");
  155. if (modem.isNetworkConnected()) { SerialMon.println("Network connected"); }
  156. #if TINY_GSM_USE_GPRS
  157. // GPRS connection parameters are usually set after network registration
  158. SerialMon.print(F("Connecting to "));
  159. SerialMon.print(apn);
  160. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  161. SerialMon.println(" fail");
  162. delay(10000);
  163. return;
  164. }
  165. SerialMon.println(" success");
  166. if (modem.isGprsConnected()) { SerialMon.println("GPRS connected"); }
  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) { delay(1000); }
  205. }