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.

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