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.

233 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, if needed
  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. // Set your reset, enable, power pins here
  83. pinMode(20, OUTPUT);
  84. digitalWrite(20, HIGH);
  85. pinMode(23, OUTPUT);
  86. digitalWrite(23, HIGH);
  87. SerialMon.println("Wait...");
  88. // Set GSM module baud rate
  89. SerialAT.begin(115200);
  90. delay(3000);
  91. // Restart takes quite some time
  92. // To skip it, call init() instead of restart()
  93. SerialMon.println("Initializing modem...");
  94. modem.restart();
  95. // modem.init();
  96. String modemInfo = modem.getModemInfo();
  97. SerialMon.print("Modem: ");
  98. SerialMon.println(modemInfo);
  99. #if TINY_GSM_USE_GPRS
  100. // Unlock your SIM card with a PIN if needed
  101. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  102. modem.simUnlock(GSM_PIN);
  103. }
  104. #endif
  105. if (!modem.hasSSL()) {
  106. SerialMon.println(F("SSL is not supported by this modem"));
  107. while(true) { delay(1000); }
  108. }
  109. }
  110. void loop() {
  111. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  112. SerialMon.print(F("Setting SSID/password..."));
  113. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  114. SerialMon.println(" fail");
  115. delay(10000);
  116. return;
  117. }
  118. SerialMon.println(" success");
  119. #endif
  120. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  121. // The XBee must run the gprsConnect function BEFORE waiting for network!
  122. modem.gprsConnect(apn, gprsUser, gprsPass);
  123. #endif
  124. SerialMon.print("Waiting for network...");
  125. if (!modem.waitForNetwork()) {
  126. SerialMon.println(" fail");
  127. delay(10000);
  128. return;
  129. }
  130. SerialMon.println(" success");
  131. if (modem.isNetworkConnected()) {
  132. SerialMon.println("Network connected");
  133. }
  134. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  135. SerialMon.print(F("Connecting to "));
  136. SerialMon.print(apn);
  137. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  138. SerialMon.println(" fail");
  139. delay(10000);
  140. return;
  141. }
  142. SerialMon.println(" success");
  143. #endif
  144. SerialMon.print(F("Performing HTTPS GET request... "));
  145. http.connectionKeepAlive(); // Currently, this is needed for HTTPS
  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. }