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

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_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. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  34. #define SerialMon Serial
  35. // Set serial for AT commands (to the module)
  36. // Use Hardware Serial on Mega, Leonardo, Micro
  37. #define SerialAT Serial1
  38. // or Software Serial on Uno, Nano
  39. //#include <SoftwareSerial.h>
  40. //SoftwareSerial SerialAT(2, 3); // RX, TX
  41. // Increase RX buffer to capture the entire response
  42. // Chips without internal buffering (A6/A7, ESP8266, M590)
  43. // need enough space in the buffer for the entire response
  44. // else data will be lost (and the http library will fail).
  45. #define TINY_GSM_RX_BUFFER 650
  46. // See all AT commands, if wanted
  47. //#define DUMP_AT_COMMANDS
  48. // Define the serial console for debug prints, if needed
  49. #define TINY_GSM_DEBUG SerialMon
  50. //#define LOGGING // <- Logging is for the HTTP library
  51. // Add a reception delay, if needed
  52. //#define TINY_GSM_YIELD() { delay(2); }
  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[] = "YourWiFiPass";
  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. #if TINY_GSM_USE_GPRS
  101. // Unlock your SIM card with a PIN if needed
  102. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  103. modem.simUnlock(GSM_PIN);
  104. }
  105. #endif
  106. if (!modem.hasSSL()) {
  107. SerialMon.println(F("SSL is not supported by this modem"));
  108. while(true) { delay(1000); }
  109. }
  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 HTTPS GET request... "));
  146. http.connectionKeepAlive(); // Currently, this is needed for HTTPS
  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. }