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.

192 lines
4.8 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
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 the debugging, if wanted
  31. //#define TINY_GSM_DEBUG Serial
  32. //#define LOGGING
  33. // Add a reception delay, if needed
  34. //#define TINY_GSM_YIELD() { delay(1); }
  35. #include <TinyGsmClient.h>
  36. #include <ArduinoHttpClient.h>
  37. // Uncomment this if you want to see all AT commands
  38. //#define DUMP_AT_COMMANDS
  39. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  40. #define SerialMon Serial
  41. // Use Hardware Serial on Mega, Leonardo, Micro
  42. #define SerialAT Serial1
  43. // or Software Serial on Uno, Nano
  44. //#include <SoftwareSerial.h>
  45. //SoftwareSerial SerialAT(2, 3); // RX, TX
  46. // Your GPRS credentials
  47. // Leave empty, if missing user or pass
  48. const char apn[] = "YourAPN";
  49. const char gprsUser[] = "";
  50. const char gprsPass[] = "";
  51. const char wifiSSID[] = "YourSSID";
  52. const char wifiPass[] = "SSIDpw";
  53. // Server details
  54. const char server[] = "vsh.pp.ua";
  55. const char resource[] = "/TinyGSM/logo.txt";
  56. const int port = 443;
  57. #ifdef DUMP_AT_COMMANDS
  58. #include <StreamDebugger.h>
  59. StreamDebugger debugger(SerialAT, SerialMon);
  60. TinyGsm modem(debugger);
  61. #else
  62. TinyGsm modem(SerialAT);
  63. #endif
  64. TinyGsmClientSecure client(modem);
  65. HttpClient http(client, server, port);
  66. void setup() {
  67. // Set console baud rate
  68. SerialMon.begin(115200);
  69. delay(10);
  70. SerialMon.println(F("Wait..."));
  71. // Set GSM module baud rate
  72. SerialAT.begin(115200);
  73. delay(3000);
  74. // Restart takes quite some time
  75. // To skip it, call init() instead of restart()
  76. SerialMon.println(F("Initializing modem..."));
  77. modem.restart();
  78. String modemInfo = modem.getModemInfo();
  79. SerialMon.print(F("Modem: "));
  80. SerialMon.println(modemInfo);
  81. // Unlock your SIM card with a PIN
  82. //modem.simUnlock("1234");
  83. if (!modem.hasSSL()) {
  84. SerialMon.println(F("SSL is not supported by this modem"));
  85. while(true) { delay(1000); }
  86. }
  87. }
  88. void loop() {
  89. if (modem.hasWifi()) {
  90. SerialMon.print(F("Setting SSID/password..."));
  91. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  92. SerialMon.println(" fail");
  93. delay(10000);
  94. return;
  95. }
  96. SerialMon.println(" OK");
  97. }
  98. SerialMon.print(F("Waiting for network..."));
  99. if (!modem.waitForNetwork()) {
  100. SerialMon.println(" fail");
  101. delay(10000);
  102. return;
  103. }
  104. SerialMon.println(" OK");
  105. if (modem.hasGPRS()) {
  106. SerialMon.print(F("Connecting to "));
  107. SerialMon.print(apn);
  108. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  109. SerialMon.println(" fail");
  110. delay(10000);
  111. return;
  112. }
  113. SerialMon.println(" OK");
  114. }
  115. SerialMon.print(F("Performing HTTPS GET request... "));
  116. http.connectionKeepAlive(); // Currently, this is needed for HTTPS
  117. int err = http.get(resource);
  118. if (err != 0) {
  119. SerialMon.println(F("failed to connect"));
  120. delay(10000);
  121. return;
  122. }
  123. int status = http.responseStatusCode();
  124. SerialMon.print(F("Response status code: "));
  125. SerialMon.println(status);
  126. if (!status) {
  127. delay(10000);
  128. return;
  129. }
  130. SerialMon.println(F("Response Headers:"));
  131. while (http.headerAvailable()) {
  132. String headerName = http.readHeaderName();
  133. String headerValue = http.readHeaderValue();
  134. SerialMon.println(" " + headerName + " : " + headerValue);
  135. }
  136. int length = http.contentLength();
  137. if (length >= 0) {
  138. SerialMon.print(F("Content length is: "));
  139. SerialMon.println(length);
  140. }
  141. if (http.isResponseChunked()) {
  142. SerialMon.println(F("The response is chunked"));
  143. }
  144. String body = http.responseBody();
  145. SerialMon.println(F("Response:"));
  146. SerialMon.println(body);
  147. SerialMon.print(F("Body length is: "));
  148. SerialMon.println(body.length());
  149. // Shutdown
  150. http.stop();
  151. SerialMon.println(F("Server disconnected"));
  152. modem.gprsDisconnect();
  153. SerialMon.println(F("GPRS disconnected"));
  154. // Do nothing forevermore
  155. while (true) {
  156. delay(1000);
  157. }
  158. }