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.

261 lines
7.0 KiB

7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
5 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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. * For this example, you need to install ArduinoHttpClient library:
  7. * https://github.com/arduino-libraries/ArduinoHttpClient
  8. * or from http://librarymanager/all#ArduinoHttpClient
  9. *
  10. * TinyGSM Getting Started guide:
  11. * https://tiny.cc/tinygsm-readme
  12. *
  13. * For more HTTP API examples, see ArduinoHttpClient library
  14. *
  15. * NOTE: This example may NOT work with the XBee because the
  16. * HttpClient library does not empty to serial buffer fast enough
  17. * and the buffer overflow causes the HttpClient library to stall.
  18. * Boards with faster processors may work, 8MHz boards will not.
  19. **************************************************************/
  20. // Select your modem:
  21. #define TINY_GSM_MODEM_SIM800
  22. // #define TINY_GSM_MODEM_SIM808
  23. // #define TINY_GSM_MODEM_SIM868
  24. // #define TINY_GSM_MODEM_SIM900
  25. // #define TINY_GSM_MODEM_SIM7000
  26. // #define TINY_GSM_MODEM_SIM5360
  27. // #define TINY_GSM_MODEM_SIM7600
  28. // #define TINY_GSM_MODEM_UBLOX
  29. // #define TINY_GSM_MODEM_SARAR4
  30. // #define TINY_GSM_MODEM_M95
  31. // #define TINY_GSM_MODEM_BG96
  32. // #define TINY_GSM_MODEM_A6
  33. // #define TINY_GSM_MODEM_A7
  34. // #define TINY_GSM_MODEM_M590
  35. // #define TINY_GSM_MODEM_MC60
  36. // #define TINY_GSM_MODEM_MC60E
  37. // #define TINY_GSM_MODEM_ESP8266
  38. // #define TINY_GSM_MODEM_XBEE
  39. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  40. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  41. #define SerialMon Serial
  42. // Set serial for AT commands (to the module)
  43. // Use Hardware Serial on Mega, Leonardo, Micro
  44. #define SerialAT Serial1
  45. // or Software Serial on Uno, Nano
  46. //#include <SoftwareSerial.h>
  47. //SoftwareSerial SerialAT(2, 3); // RX, TX
  48. // Increase RX buffer to capture the entire response
  49. // Chips without internal buffering (A6/A7, ESP8266, M590)
  50. // need enough space in the buffer for the entire response
  51. // else data will be lost (and the http library will fail).
  52. #if !defined(TINY_GSM_RX_BUFFER)
  53. #define TINY_GSM_RX_BUFFER 650
  54. #endif
  55. // See all AT commands, if wanted
  56. // #define DUMP_AT_COMMANDS
  57. // Define the serial console for debug prints, if needed
  58. #define TINY_GSM_DEBUG SerialMon
  59. // #define LOGGING // <- Logging is for the HTTP library
  60. // Range to attempt to autobaud
  61. #define GSM_AUTOBAUD_MIN 9600
  62. #define GSM_AUTOBAUD_MAX 115200
  63. // Add a reception delay - may be needed for a fast processor at a slow baud rate
  64. // #define TINY_GSM_YIELD() { delay(2); }
  65. // Define how you're planning to connect to the internet
  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. const int port = 80;
  81. #include <TinyGsmClient.h>
  82. #include <ArduinoHttpClient.h>
  83. // Just in case someone defined the wrong thing..
  84. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  85. #undef TINY_GSM_USE_GPRS
  86. #undef TINY_GSM_USE_WIFI
  87. #define TINY_GSM_USE_GPRS false
  88. #define TINY_GSM_USE_WIFI true
  89. #endif
  90. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  91. #undef TINY_GSM_USE_GPRS
  92. #undef TINY_GSM_USE_WIFI
  93. #define TINY_GSM_USE_GPRS true
  94. #define TINY_GSM_USE_WIFI false
  95. #endif
  96. #ifdef DUMP_AT_COMMANDS
  97. #include <StreamDebugger.h>
  98. StreamDebugger debugger(SerialAT, SerialMon);
  99. TinyGsm modem(debugger);
  100. #else
  101. TinyGsm modem(SerialAT);
  102. #endif
  103. TinyGsmClient client(modem);
  104. HttpClient http(client, server, port);
  105. void setup() {
  106. // Set console baud rate
  107. SerialMon.begin(115200);
  108. delay(10);
  109. // !!!!!!!!!!!
  110. // Set your reset, enable, power pins here
  111. // !!!!!!!!!!!
  112. SerialMon.println("Wait...");
  113. // Set GSM module baud rate
  114. // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  115. SerialAT.begin(9600);
  116. delay(6000);
  117. // Restart takes quite some time
  118. // To skip it, call init() instead of restart()
  119. SerialMon.println("Initializing modem...");
  120. modem.restart();
  121. // modem.init();
  122. String modemInfo = modem.getModemInfo();
  123. SerialMon.print("Modem Info: ");
  124. SerialMon.println(modemInfo);
  125. #if TINY_GSM_USE_GPRS
  126. // Unlock your SIM card with a PIN if needed
  127. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  128. modem.simUnlock(GSM_PIN);
  129. }
  130. #endif
  131. }
  132. void loop() {
  133. #if TINY_GSM_USE_WIFI
  134. // Wifi connection parameters must be set before waiting for the network
  135. SerialMon.print(F("Setting SSID/password..."));
  136. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  137. SerialMon.println(" fail");
  138. delay(10000);
  139. return;
  140. }
  141. SerialMon.println(" success");
  142. #endif
  143. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  144. // The XBee must run the gprsConnect function BEFORE waiting for network!
  145. modem.gprsConnect(apn, gprsUser, gprsPass);
  146. #endif
  147. SerialMon.print("Waiting for network...");
  148. if (!modem.waitForNetwork()) {
  149. SerialMon.println(" fail");
  150. delay(10000);
  151. return;
  152. }
  153. SerialMon.println(" success");
  154. if (modem.isNetworkConnected()) {
  155. SerialMon.println("Network connected");
  156. }
  157. #if TINY_GSM_USE_GPRS
  158. // GPRS connection parameters are usually set after network registration
  159. SerialMon.print(F("Connecting to "));
  160. SerialMon.print(apn);
  161. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  162. SerialMon.println(" fail");
  163. delay(10000);
  164. return;
  165. }
  166. SerialMon.println(" success");
  167. if (modem.isGprsConnected()) {
  168. SerialMon.println("GPRS connected");
  169. }
  170. #endif
  171. SerialMon.print(F("Performing HTTP GET request... "));
  172. int err = http.get(resource);
  173. if (err != 0) {
  174. SerialMon.println(F("failed to connect"));
  175. delay(10000);
  176. return;
  177. }
  178. int status = http.responseStatusCode();
  179. SerialMon.print(F("Response status code: "));
  180. SerialMon.println(status);
  181. if (!status) {
  182. delay(10000);
  183. return;
  184. }
  185. SerialMon.println(F("Response Headers:"));
  186. while (http.headerAvailable()) {
  187. String headerName = http.readHeaderName();
  188. String headerValue = http.readHeaderValue();
  189. SerialMon.println(" " + headerName + " : " + headerValue);
  190. }
  191. int length = http.contentLength();
  192. if (length >= 0) {
  193. SerialMon.print(F("Content length is: "));
  194. SerialMon.println(length);
  195. }
  196. if (http.isResponseChunked()) {
  197. SerialMon.println(F("The response is chunked"));
  198. }
  199. String body = http.responseBody();
  200. SerialMon.println(F("Response:"));
  201. SerialMon.println(body);
  202. SerialMon.print(F("Body length is: "));
  203. SerialMon.println(body.length());
  204. // Shutdown
  205. http.stop();
  206. SerialMon.println(F("Server disconnected"));
  207. #if TINY_GSM_USE_WIFI
  208. modem.networkDisconnect();
  209. SerialMon.println(F("WiFi disconnected"));
  210. #endif
  211. #if TINY_GSM_USE_GPRS
  212. modem.gprsDisconnect();
  213. SerialMon.println(F("GPRS disconnected"));
  214. #endif
  215. // Do nothing forevermore
  216. while (true) {
  217. delay(1000);
  218. }
  219. }