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.

234 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_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. // Set your reset, enable, power pins here
  90. pinMode(20, OUTPUT);
  91. digitalWrite(20, HIGH);
  92. pinMode(23, OUTPUT);
  93. digitalWrite(23, HIGH);
  94. SerialMon.println("Wait...");
  95. // Set GSM module baud rate
  96. SerialAT.begin(115200);
  97. delay(3000);
  98. // Restart takes quite some time
  99. // To skip it, call init() instead of restart()
  100. SerialMon.println("Initializing modem...");
  101. modem.restart();
  102. // modem.init();
  103. String modemInfo = modem.getModemInfo();
  104. SerialMon.print("Modem: ");
  105. SerialMon.println(modemInfo);
  106. #if TINY_GSM_USE_GPRS
  107. // Unlock your SIM card with a PIN if needed
  108. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  109. modem.simUnlock(GSM_PIN);
  110. }
  111. #endif
  112. }
  113. void loop() {
  114. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  115. SerialMon.print(F("Setting SSID/password..."));
  116. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  117. SerialMon.println(" fail");
  118. delay(10000);
  119. return;
  120. }
  121. SerialMon.println(" success");
  122. #endif
  123. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  124. // The XBee must run the gprsConnect function BEFORE waiting for network!
  125. modem.gprsConnect(apn, gprsUser, gprsPass);
  126. #endif
  127. SerialMon.print("Waiting for network...");
  128. if (!modem.waitForNetwork()) {
  129. SerialMon.println(" fail");
  130. delay(10000);
  131. return;
  132. }
  133. SerialMon.println(" success");
  134. if (modem.isNetworkConnected()) {
  135. SerialMon.println("Network connected");
  136. }
  137. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  138. SerialMon.print(F("Connecting to "));
  139. SerialMon.print(apn);
  140. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  141. SerialMon.println(" fail");
  142. delay(10000);
  143. return;
  144. }
  145. SerialMon.println(" success");
  146. #endif
  147. SerialMon.print(F("Performing HTTP GET request... "));
  148. int err = http.get(resource);
  149. if (err != 0) {
  150. SerialMon.println(F("failed to connect"));
  151. delay(10000);
  152. return;
  153. }
  154. int status = http.responseStatusCode();
  155. SerialMon.print(F("Response status code: "));
  156. SerialMon.println(status);
  157. if (!status) {
  158. delay(10000);
  159. return;
  160. }
  161. SerialMon.println(F("Response Headers:"));
  162. while (http.headerAvailable()) {
  163. String headerName = http.readHeaderName();
  164. String headerValue = http.readHeaderValue();
  165. SerialMon.println(" " + headerName + " : " + headerValue);
  166. }
  167. int length = http.contentLength();
  168. if (length >= 0) {
  169. SerialMon.print(F("Content length is: "));
  170. SerialMon.println(length);
  171. }
  172. if (http.isResponseChunked()) {
  173. SerialMon.println(F("The response is chunked"));
  174. }
  175. String body = http.responseBody();
  176. SerialMon.println(F("Response:"));
  177. SerialMon.println(body);
  178. SerialMon.print(F("Body length is: "));
  179. SerialMon.println(body.length());
  180. // Shutdown
  181. http.stop();
  182. SerialMon.println(F("Server disconnected"));
  183. #if TINY_GSM_USE_WIFI
  184. modem.networkDisconnect();
  185. SerialMon.println(F("WiFi disconnected"));
  186. #endif
  187. #if TINY_GSM_USE_GPRS
  188. modem.gprsDisconnect();
  189. SerialMon.println(F("GPRS disconnected"));
  190. #endif
  191. // Do nothing forevermore
  192. while (true) {
  193. delay(1000);
  194. }
  195. }