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.1 KiB

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