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.

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