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