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.

232 lines
6.1 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
5 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
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. #define TINY_GSM_RX_BUFFER 650
  53. // See all AT commands, if wanted
  54. //#define DUMP_AT_COMMANDS
  55. // Define the serial console for debug prints, if needed
  56. #define TINY_GSM_DEBUG SerialMon
  57. //#define LOGGING // <- Logging is for the HTTP library
  58. // Add a reception delay - may be needed for a fast processor at a slow baud rate
  59. //#define TINY_GSM_YIELD() { delay(2); }
  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
  65. // Leave empty, if missing user or pass
  66. const char apn[] = "YourAPN";
  67. const char gprsUser[] = "";
  68. const char gprsPass[] = "";
  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. const int port = 80;
  75. #include <TinyGsmClient.h>
  76. #include <ArduinoHttpClient.h>
  77. #ifdef DUMP_AT_COMMANDS
  78. #include <StreamDebugger.h>
  79. StreamDebugger debugger(SerialAT, SerialMon);
  80. TinyGsm modem(debugger);
  81. #else
  82. TinyGsm modem(SerialAT);
  83. #endif
  84. TinyGsmClient client(modem);
  85. HttpClient http(client, server, port);
  86. void setup() {
  87. // Set console baud rate
  88. SerialMon.begin(115200);
  89. delay(10);
  90. // !!!!!!!!!!!
  91. // Set your reset, enable, power pins here
  92. // !!!!!!!!!!!
  93. SerialMon.println("Wait...");
  94. // Set GSM module baud rate
  95. SerialAT.begin(115200);
  96. delay(3000);
  97. // Restart takes quite some time
  98. // To skip it, call init() instead of restart()
  99. SerialMon.println("Initializing modem...");
  100. modem.restart();
  101. // modem.init();
  102. String modemInfo = modem.getModemInfo();
  103. SerialMon.print("Modem: ");
  104. SerialMon.println(modemInfo);
  105. #if TINY_GSM_USE_GPRS
  106. // Unlock your SIM card with a PIN if needed
  107. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  108. modem.simUnlock(GSM_PIN);
  109. }
  110. #endif
  111. }
  112. void loop() {
  113. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  114. SerialMon.print(F("Setting SSID/password..."));
  115. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  116. SerialMon.println(" fail");
  117. delay(10000);
  118. return;
  119. }
  120. SerialMon.println(" success");
  121. #endif
  122. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  123. // The XBee must run the gprsConnect function BEFORE waiting for network!
  124. modem.gprsConnect(apn, gprsUser, gprsPass);
  125. #endif
  126. SerialMon.print("Waiting for network...");
  127. if (!modem.waitForNetwork()) {
  128. SerialMon.println(" fail");
  129. delay(10000);
  130. return;
  131. }
  132. SerialMon.println(" success");
  133. if (modem.isNetworkConnected()) {
  134. SerialMon.println("Network connected");
  135. }
  136. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  137. SerialMon.print(F("Connecting to "));
  138. SerialMon.print(apn);
  139. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  140. SerialMon.println(" fail");
  141. delay(10000);
  142. return;
  143. }
  144. SerialMon.println(" success");
  145. #endif
  146. SerialMon.print(F("Performing HTTP GET request... "));
  147. int err = http.get(resource);
  148. if (err != 0) {
  149. SerialMon.println(F("failed to connect"));
  150. delay(10000);
  151. return;
  152. }
  153. int status = http.responseStatusCode();
  154. SerialMon.print(F("Response status code: "));
  155. SerialMon.println(status);
  156. if (!status) {
  157. delay(10000);
  158. return;
  159. }
  160. SerialMon.println(F("Response Headers:"));
  161. while (http.headerAvailable()) {
  162. String headerName = http.readHeaderName();
  163. String headerValue = http.readHeaderValue();
  164. SerialMon.println(" " + headerName + " : " + headerValue);
  165. }
  166. int length = http.contentLength();
  167. if (length >= 0) {
  168. SerialMon.print(F("Content length is: "));
  169. SerialMon.println(length);
  170. }
  171. if (http.isResponseChunked()) {
  172. SerialMon.println(F("The response is chunked"));
  173. }
  174. String body = http.responseBody();
  175. SerialMon.println(F("Response:"));
  176. SerialMon.println(body);
  177. SerialMon.print(F("Body length is: "));
  178. SerialMon.println(body.length());
  179. // Shutdown
  180. http.stop();
  181. SerialMon.println(F("Server disconnected"));
  182. #if TINY_GSM_USE_WIFI
  183. modem.networkDisconnect();
  184. SerialMon.println(F("WiFi disconnected"));
  185. #endif
  186. #if TINY_GSM_USE_GPRS
  187. modem.gprsDisconnect();
  188. SerialMon.println(F("GPRS disconnected"));
  189. #endif
  190. // Do nothing forevermore
  191. while (true) {
  192. delay(1000);
  193. }
  194. }