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.

191 lines
4.8 KiB

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