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.

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