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.

254 lines
6.8 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. // Your GPRS credentials, if any
  63. const char apn[] = "YourAPN";
  64. const char gprsUser[] = "";
  65. const char gprsPass[] = "";
  66. // Your WiFi connection credentials, if applicable
  67. const char wifiSSID[] = "YourSSID";
  68. const char wifiPass[] = "YourWiFiPass";
  69. // Server details
  70. const char server[] = "vsh.pp.ua";
  71. const char resource[] = "/TinyGSM/logo.txt";
  72. const int port = 443;
  73. #include <TinyGsmClient.h>
  74. #include <ArduinoHttpClient.h>
  75. // Just in case someone defined the wrong thing..
  76. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  77. #undef TINY_GSM_USE_GPRS
  78. #undef TINY_GSM_USE_WIFI
  79. #define TINY_GSM_USE_GPRS false
  80. #define TINY_GSM_USE_WIFI true
  81. #endif
  82. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  83. #undef TINY_GSM_USE_GPRS
  84. #undef TINY_GSM_USE_WIFI
  85. #define TINY_GSM_USE_GPRS true
  86. #define TINY_GSM_USE_WIFI false
  87. #endif
  88. #ifdef DUMP_AT_COMMANDS
  89. #include <StreamDebugger.h>
  90. StreamDebugger debugger(SerialAT, SerialMon);
  91. TinyGsm modem(debugger);
  92. #else
  93. TinyGsm modem(SerialAT);
  94. #endif
  95. TinyGsmClientSecure client(modem);
  96. HttpClient http(client, server, port);
  97. void setup() {
  98. // Set console baud rate
  99. SerialMon.begin(115200);
  100. delay(10);
  101. // !!!!!!!!!!!
  102. // Set your reset, enable, power pins here
  103. // !!!!!!!!!!!
  104. SerialMon.println("Wait...");
  105. // Set GSM module baud rate
  106. // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  107. SerialAT.begin(9600);
  108. delay(6000);
  109. // Restart takes quite some time
  110. // To skip it, call init() instead of restart()
  111. SerialMon.println("Initializing modem...");
  112. modem.restart();
  113. // modem.init();
  114. String modemInfo = modem.getModemInfo();
  115. SerialMon.print("Modem Info: ");
  116. SerialMon.println(modemInfo);
  117. #if TINY_GSM_USE_GPRS
  118. // Unlock your SIM card with a PIN if needed
  119. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  120. modem.simUnlock(GSM_PIN);
  121. }
  122. #endif
  123. }
  124. void loop() {
  125. #if TINY_GSM_USE_WIFI
  126. // Wifi connection parameters must be set before waiting for the network
  127. SerialMon.print(F("Setting SSID/password..."));
  128. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  129. SerialMon.println(" fail");
  130. delay(10000);
  131. return;
  132. }
  133. SerialMon.println(" success");
  134. #endif
  135. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  136. // The XBee must run the gprsConnect function BEFORE waiting for network!
  137. modem.gprsConnect(apn, gprsUser, gprsPass);
  138. #endif
  139. SerialMon.print("Waiting for network...");
  140. if (!modem.waitForNetwork()) {
  141. SerialMon.println(" fail");
  142. delay(10000);
  143. return;
  144. }
  145. SerialMon.println(" success");
  146. if (modem.isNetworkConnected()) {
  147. SerialMon.println("Network connected");
  148. }
  149. #if TINY_GSM_USE_GPRS
  150. // GPRS connection parameters are usually set after network registration
  151. SerialMon.print(F("Connecting to "));
  152. SerialMon.print(apn);
  153. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  154. SerialMon.println(" fail");
  155. delay(10000);
  156. return;
  157. }
  158. SerialMon.println(" success");
  159. if (modem.isGprsConnected()) {
  160. SerialMon.println("GPRS connected");
  161. }
  162. #endif
  163. SerialMon.print(F("Performing HTTPS GET request... "));
  164. http.connectionKeepAlive(); // Currently, this is needed for HTTPS
  165. int err = http.get(resource);
  166. if (err != 0) {
  167. SerialMon.println(F("failed to connect"));
  168. delay(10000);
  169. return;
  170. }
  171. int status = http.responseStatusCode();
  172. SerialMon.print(F("Response status code: "));
  173. SerialMon.println(status);
  174. if (!status) {
  175. delay(10000);
  176. return;
  177. }
  178. SerialMon.println(F("Response Headers:"));
  179. while (http.headerAvailable()) {
  180. String headerName = http.readHeaderName();
  181. String headerValue = http.readHeaderValue();
  182. SerialMon.println(" " + headerName + " : " + headerValue);
  183. }
  184. int length = http.contentLength();
  185. if (length >= 0) {
  186. SerialMon.print(F("Content length is: "));
  187. SerialMon.println(length);
  188. }
  189. if (http.isResponseChunked()) {
  190. SerialMon.println(F("The response is chunked"));
  191. }
  192. String body = http.responseBody();
  193. SerialMon.println(F("Response:"));
  194. SerialMon.println(body);
  195. SerialMon.print(F("Body length is: "));
  196. SerialMon.println(body.length());
  197. // Shutdown
  198. http.stop();
  199. SerialMon.println(F("Server disconnected"));
  200. #if TINY_GSM_USE_WIFI
  201. modem.networkDisconnect();
  202. SerialMon.println(F("WiFi disconnected"));
  203. #endif
  204. #if TINY_GSM_USE_GPRS
  205. modem.gprsDisconnect();
  206. SerialMon.println(F("GPRS disconnected"));
  207. #endif
  208. // Do nothing forevermore
  209. while (true) {
  210. delay(1000);
  211. }
  212. }