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.

257 lines
7.2 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
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 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
  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_SIM7000SSL
  28. // #define TINY_GSM_MODEM_SIM7080
  29. // #define TINY_GSM_MODEM_UBLOX
  30. // #define TINY_GSM_MODEM_SARAR4
  31. // #define TINY_GSM_MODEM_ESP8266
  32. // #define TINY_GSM_MODEM_XBEE
  33. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  34. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  35. #define SerialMon Serial
  36. // Set serial for AT commands (to the module)
  37. // Use Hardware Serial on Mega, Leonardo, Micro
  38. #ifndef __AVR_ATmega328P__
  39. #define SerialAT Serial1
  40. // or Software Serial on Uno, Nano
  41. #else
  42. #include <SoftwareSerial.h>
  43. SoftwareSerial SerialAT(2, 3); // RX, TX
  44. #endif
  45. // Increase RX buffer to capture the entire response
  46. // Chips without internal buffering (A6/A7, ESP8266, M590)
  47. // need enough space in the buffer for the entire response
  48. // else data will be lost (and the http library will fail).
  49. #if !defined(TINY_GSM_RX_BUFFER)
  50. #define TINY_GSM_RX_BUFFER 650
  51. #endif
  52. // See all AT commands, if wanted
  53. // #define DUMP_AT_COMMANDS
  54. // Define the serial console for debug prints, if needed
  55. #define TINY_GSM_DEBUG SerialMon
  56. // #define LOGGING // <- Logging is for the HTTP library
  57. // Range to attempt to autobaud
  58. // NOTE: DO NOT AUTOBAUD in production code. Once you've established
  59. // communication, set a fixed baud rate using modem.setBaud(#).
  60. #define GSM_AUTOBAUD_MIN 9600
  61. #define GSM_AUTOBAUD_MAX 115200
  62. // Add a reception delay, if needed.
  63. // This may be needed for a fast processor at a slow baud rate.
  64. // #define TINY_GSM_YIELD() { delay(2); }
  65. // Define how you're planning to connect to the internet.
  66. // This is only needed for this example, not in other code.
  67. #define TINY_GSM_USE_GPRS true
  68. #define TINY_GSM_USE_WIFI false
  69. // set GSM PIN, if any
  70. #define GSM_PIN ""
  71. // flag to force SSL client authentication, if needed
  72. // #define TINY_GSM_SSL_CLIENT_AUTHENTICATION
  73. // Your GPRS credentials, if any
  74. const char apn[] = "YourAPN";
  75. const char gprsUser[] = "";
  76. const char gprsPass[] = "";
  77. // Your WiFi connection credentials, if applicable
  78. const char wifiSSID[] = "YourSSID";
  79. const char wifiPass[] = "YourWiFiPass";
  80. // Server details
  81. const char server[] = "vsh.pp.ua";
  82. const char resource[] = "/TinyGSM/logo.txt";
  83. const int port = 443;
  84. #include <TinyGsmClient.h>
  85. #include <ArduinoHttpClient.h>
  86. // Just in case someone defined the wrong thing..
  87. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  88. #undef TINY_GSM_USE_GPRS
  89. #undef TINY_GSM_USE_WIFI
  90. #define TINY_GSM_USE_GPRS false
  91. #define TINY_GSM_USE_WIFI true
  92. #endif
  93. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  94. #undef TINY_GSM_USE_GPRS
  95. #undef TINY_GSM_USE_WIFI
  96. #define TINY_GSM_USE_GPRS true
  97. #define TINY_GSM_USE_WIFI false
  98. #endif
  99. #ifdef DUMP_AT_COMMANDS
  100. #include <StreamDebugger.h>
  101. StreamDebugger debugger(SerialAT, SerialMon);
  102. TinyGsm modem(debugger);
  103. #else
  104. TinyGsm modem(SerialAT);
  105. #endif
  106. TinyGsmClientSecure client(modem);
  107. HttpClient http(client, server, port);
  108. void setup() {
  109. // Set console baud rate
  110. SerialMon.begin(115200);
  111. delay(10);
  112. // !!!!!!!!!!!
  113. // Set your reset, enable, power pins here
  114. // !!!!!!!!!!!
  115. SerialMon.println("Wait...");
  116. // Set GSM module baud rate
  117. TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  118. // SerialAT.begin(9600);
  119. delay(6000);
  120. // Restart takes quite some time
  121. // To skip it, call init() instead of restart()
  122. SerialMon.println("Initializing modem...");
  123. modem.restart();
  124. // modem.init();
  125. String modemInfo = modem.getModemInfo();
  126. SerialMon.print("Modem Info: ");
  127. SerialMon.println(modemInfo);
  128. #if TINY_GSM_USE_GPRS
  129. // Unlock your SIM card with a PIN if needed
  130. if (GSM_PIN && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }
  131. #endif
  132. }
  133. void loop() {
  134. #if TINY_GSM_USE_WIFI
  135. // Wifi connection parameters must be set before waiting for the network
  136. SerialMon.print(F("Setting SSID/password..."));
  137. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  138. SerialMon.println(" fail");
  139. delay(10000);
  140. return;
  141. }
  142. SerialMon.println(" success");
  143. #endif
  144. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  145. // The XBee must run the gprsConnect function BEFORE waiting for network!
  146. modem.gprsConnect(apn, gprsUser, gprsPass);
  147. #endif
  148. SerialMon.print("Waiting for network...");
  149. if (!modem.waitForNetwork()) {
  150. SerialMon.println(" fail");
  151. delay(10000);
  152. return;
  153. }
  154. SerialMon.println(" success");
  155. if (modem.isNetworkConnected()) { SerialMon.println("Network connected"); }
  156. #if TINY_GSM_USE_GPRS
  157. // GPRS connection parameters are usually set after network registration
  158. SerialMon.print(F("Connecting to "));
  159. SerialMon.print(apn);
  160. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  161. SerialMon.println(" fail");
  162. delay(10000);
  163. return;
  164. }
  165. SerialMon.println(" success");
  166. if (modem.isGprsConnected()) { SerialMon.println("GPRS connected"); }
  167. #endif
  168. SerialMon.print(F("Performing HTTPS GET request... "));
  169. http.connectionKeepAlive(); // Currently, this is needed for HTTPS
  170. int err = http.get(resource);
  171. if (err != 0) {
  172. SerialMon.println(F("failed to connect"));
  173. delay(10000);
  174. return;
  175. }
  176. int status = http.responseStatusCode();
  177. SerialMon.print(F("Response status code: "));
  178. SerialMon.println(status);
  179. if (!status) {
  180. delay(10000);
  181. return;
  182. }
  183. SerialMon.println(F("Response Headers:"));
  184. while (http.headerAvailable()) {
  185. String headerName = http.readHeaderName();
  186. String headerValue = http.readHeaderValue();
  187. SerialMon.println(" " + headerName + " : " + headerValue);
  188. }
  189. int length = http.contentLength();
  190. if (length >= 0) {
  191. SerialMon.print(F("Content length is: "));
  192. SerialMon.println(length);
  193. }
  194. if (http.isResponseChunked()) {
  195. SerialMon.println(F("The response is chunked"));
  196. }
  197. String body = http.responseBody();
  198. SerialMon.println(F("Response:"));
  199. SerialMon.println(body);
  200. SerialMon.print(F("Body length is: "));
  201. SerialMon.println(body.length());
  202. // Shutdown
  203. http.stop();
  204. SerialMon.println(F("Server disconnected"));
  205. #if TINY_GSM_USE_WIFI
  206. modem.networkDisconnect();
  207. SerialMon.println(F("WiFi disconnected"));
  208. #endif
  209. #if TINY_GSM_USE_GPRS
  210. modem.gprsDisconnect();
  211. SerialMon.println(F("GPRS disconnected"));
  212. #endif
  213. // Do nothing forevermore
  214. while (true) { delay(1000); }
  215. }