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.

230 lines
6.0 KiB

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