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.

231 lines
6.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
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_UBLOX
  28. // #define TINY_GSM_MODEM_SARAR4
  29. // #define TINY_GSM_MODEM_M95
  30. // #define TINY_GSM_MODEM_BG96
  31. // #define TINY_GSM_MODEM_A6
  32. // #define TINY_GSM_MODEM_A7
  33. // #define TINY_GSM_MODEM_M590
  34. // #define TINY_GSM_MODEM_MC60
  35. // #define TINY_GSM_MODEM_MC60E
  36. // #define TINY_GSM_MODEM_ESP8266
  37. // #define TINY_GSM_MODEM_XBEE
  38. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  39. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  40. #define SerialMon Serial
  41. // Set serial for AT commands (to the module)
  42. // Use Hardware Serial on Mega, Leonardo, Micro
  43. #define SerialAT Serial1
  44. // or Software Serial on Uno, Nano
  45. //#include <SoftwareSerial.h>
  46. //SoftwareSerial SerialAT(2, 3); // RX, TX
  47. // Increase RX buffer to capture the entire response
  48. // Chips without internal buffering (A6/A7, ESP8266, M590)
  49. // need enough space in the buffer for the entire response
  50. // else data will be lost (and the http library will fail).
  51. #define TINY_GSM_RX_BUFFER 650
  52. // See all AT commands, if wanted
  53. //#define DUMP_AT_COMMANDS
  54. // Define the serial console for debug prints, if needed
  55. #define TINY_GSM_DEBUG SerialMon
  56. //#define LOGGING // <- Logging is for the HTTP library
  57. // Add a reception delay, if needed
  58. //#define TINY_GSM_YIELD() { delay(2); }
  59. #define TINY_GSM_USE_GPRS true
  60. #define TINY_GSM_USE_WIFI false
  61. // set GSM PIN, if any
  62. #define GSM_PIN ""
  63. // Your GPRS credentials
  64. // Leave empty, if missing user or pass
  65. const char apn[] = "YourAPN";
  66. const char gprsUser[] = "";
  67. const char gprsPass[] = "";
  68. const char wifiSSID[] = "YourSSID";
  69. const char wifiPass[] = "YourWiFiPass";
  70. // Server details
  71. const char server[] = "vsh.pp.ua";
  72. const char resource[] = "/TinyGSM/logo.txt";
  73. const int port = 80;
  74. #include <TinyGsmClient.h>
  75. #include <ArduinoHttpClient.h>
  76. #ifdef DUMP_AT_COMMANDS
  77. #include <StreamDebugger.h>
  78. StreamDebugger debugger(SerialAT, SerialMon);
  79. TinyGsm modem(debugger);
  80. #else
  81. TinyGsm modem(SerialAT);
  82. #endif
  83. TinyGsmClient client(modem);
  84. HttpClient http(client, server, port);
  85. void setup() {
  86. // Set console baud rate
  87. SerialMon.begin(115200);
  88. delay(10);
  89. // !!!!!!!!!!!
  90. // Set your reset, enable, power pins here
  91. // !!!!!!!!!!!
  92. SerialMon.println("Wait...");
  93. // Set GSM module baud rate
  94. SerialAT.begin(115200);
  95. delay(3000);
  96. // Restart takes quite some time
  97. // To skip it, call init() instead of restart()
  98. SerialMon.println("Initializing modem...");
  99. modem.restart();
  100. // modem.init();
  101. String modemInfo = modem.getModemInfo();
  102. SerialMon.print("Modem: ");
  103. SerialMon.println(modemInfo);
  104. #if TINY_GSM_USE_GPRS
  105. // Unlock your SIM card with a PIN if needed
  106. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  107. modem.simUnlock(GSM_PIN);
  108. }
  109. #endif
  110. }
  111. void loop() {
  112. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  113. SerialMon.print(F("Setting SSID/password..."));
  114. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  115. SerialMon.println(" fail");
  116. delay(10000);
  117. return;
  118. }
  119. SerialMon.println(" success");
  120. #endif
  121. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  122. // The XBee must run the gprsConnect function BEFORE waiting for network!
  123. modem.gprsConnect(apn, gprsUser, gprsPass);
  124. #endif
  125. SerialMon.print("Waiting for network...");
  126. if (!modem.waitForNetwork()) {
  127. SerialMon.println(" fail");
  128. delay(10000);
  129. return;
  130. }
  131. SerialMon.println(" success");
  132. if (modem.isNetworkConnected()) {
  133. SerialMon.println("Network connected");
  134. }
  135. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  136. SerialMon.print(F("Connecting to "));
  137. SerialMon.print(apn);
  138. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  139. SerialMon.println(" fail");
  140. delay(10000);
  141. return;
  142. }
  143. SerialMon.println(" success");
  144. #endif
  145. SerialMon.print(F("Performing HTTP GET request... "));
  146. int err = http.get(resource);
  147. if (err != 0) {
  148. SerialMon.println(F("failed to connect"));
  149. delay(10000);
  150. return;
  151. }
  152. int status = http.responseStatusCode();
  153. SerialMon.print(F("Response status code: "));
  154. SerialMon.println(status);
  155. if (!status) {
  156. delay(10000);
  157. return;
  158. }
  159. SerialMon.println(F("Response Headers:"));
  160. while (http.headerAvailable()) {
  161. String headerName = http.readHeaderName();
  162. String headerValue = http.readHeaderValue();
  163. SerialMon.println(" " + headerName + " : " + headerValue);
  164. }
  165. int length = http.contentLength();
  166. if (length >= 0) {
  167. SerialMon.print(F("Content length is: "));
  168. SerialMon.println(length);
  169. }
  170. if (http.isResponseChunked()) {
  171. SerialMon.println(F("The response is chunked"));
  172. }
  173. String body = http.responseBody();
  174. SerialMon.println(F("Response:"));
  175. SerialMon.println(body);
  176. SerialMon.print(F("Body length is: "));
  177. SerialMon.println(body.length());
  178. // Shutdown
  179. http.stop();
  180. SerialMon.println(F("Server disconnected"));
  181. #if TINY_GSM_USE_WIFI
  182. modem.networkDisconnect();
  183. SerialMon.println(F("WiFi disconnected"));
  184. #endif
  185. #if TINY_GSM_USE_GPRS
  186. modem.gprsDisconnect();
  187. SerialMon.println(F("GPRS disconnected"));
  188. #endif
  189. // Do nothing forevermore
  190. while (true) {
  191. delay(1000);
  192. }
  193. }