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.

259 lines
6.9 KiB

7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
5 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
5 years ago
5 years ago
5 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
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
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
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. * For more HTTP API examples, see ArduinoHttpClient library
  14. *
  15. * NOTE: This example may NOT work with the XBee because the
  16. * HttpClient library does not empty to serial buffer fast enough
  17. * and the buffer overflow causes the HttpClient library to stall.
  18. * Boards with faster processors may work, 8MHz boards will not.
  19. **************************************************************/
  20. // Select your modem:
  21. #define TINY_GSM_MODEM_SIM800
  22. // #define TINY_GSM_MODEM_SIM808
  23. // #define TINY_GSM_MODEM_SIM868
  24. // #define TINY_GSM_MODEM_SIM900
  25. // #define TINY_GSM_MODEM_SIM7000
  26. // #define TINY_GSM_MODEM_SIM5360
  27. // #define TINY_GSM_MODEM_SIM7600
  28. // #define TINY_GSM_MODEM_UBLOX
  29. // #define TINY_GSM_MODEM_SARAR4
  30. // #define TINY_GSM_MODEM_M95
  31. // #define TINY_GSM_MODEM_BG96
  32. // #define TINY_GSM_MODEM_A6
  33. // #define TINY_GSM_MODEM_A7
  34. // #define TINY_GSM_MODEM_M590
  35. // #define TINY_GSM_MODEM_MC60
  36. // #define TINY_GSM_MODEM_MC60E
  37. // #define TINY_GSM_MODEM_ESP8266
  38. // #define TINY_GSM_MODEM_XBEE
  39. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  40. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  41. #define SerialMon Serial
  42. // Set serial for AT commands (to the module)
  43. // Use Hardware Serial on Mega, Leonardo, Micro
  44. #define SerialAT Serial1
  45. // or Software Serial on Uno, Nano
  46. //#include <SoftwareSerial.h>
  47. //SoftwareSerial SerialAT(2, 3); // RX, TX
  48. // Increase RX buffer to capture the entire response
  49. // Chips without internal buffering (A6/A7, ESP8266, M590)
  50. // need enough space in the buffer for the entire response
  51. // else data will be lost (and the http library will fail).
  52. #define TINY_GSM_RX_BUFFER 650
  53. // See all AT commands, if wanted
  54. //#define DUMP_AT_COMMANDS
  55. // Define the serial console for debug prints, if needed
  56. #define TINY_GSM_DEBUG SerialMon
  57. //#define LOGGING // <- Logging is for the HTTP library
  58. // Range to attempt to autobaud
  59. #define GSM_AUTOBAUD_MIN 9600
  60. #define GSM_AUTOBAUD_MAX 115200
  61. // Add a reception delay - may be needed for a fast processor at a slow baud rate
  62. //#define TINY_GSM_YIELD() { delay(2); }
  63. // Define how you're planning to connect to the internet
  64. #define TINY_GSM_USE_GPRS true
  65. #define TINY_GSM_USE_WIFI false
  66. // set GSM PIN, if any
  67. #define GSM_PIN ""
  68. // Your GPRS credentials, if any
  69. const char apn[] = "YourAPN";
  70. const char gprsUser[] = "";
  71. const char gprsPass[] = "";
  72. // Your WiFi connection credentials, if applicable
  73. const char wifiSSID[] = "YourSSID";
  74. const char wifiPass[] = "YourWiFiPass";
  75. // Server details
  76. const char server[] = "vsh.pp.ua";
  77. const char resource[] = "/TinyGSM/logo.txt";
  78. const int port = 80;
  79. #include <TinyGsmClient.h>
  80. #include <ArduinoHttpClient.h>
  81. // Just in case someone defined the wrong thing..
  82. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  83. #undef TINY_GSM_USE_GPRS
  84. #undef TINY_GSM_USE_WIFI
  85. #define TINY_GSM_USE_GPRS false
  86. #define TINY_GSM_USE_WIFI true
  87. #endif
  88. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  89. #undef TINY_GSM_USE_GPRS
  90. #undef TINY_GSM_USE_WIFI
  91. #define TINY_GSM_USE_GPRS true
  92. #define TINY_GSM_USE_WIFI false
  93. #endif
  94. #ifdef DUMP_AT_COMMANDS
  95. #include <StreamDebugger.h>
  96. StreamDebugger debugger(SerialAT, SerialMon);
  97. TinyGsm modem(debugger);
  98. #else
  99. TinyGsm modem(SerialAT);
  100. #endif
  101. TinyGsmClient client(modem);
  102. HttpClient http(client, server, port);
  103. void setup() {
  104. // Set console baud rate
  105. SerialMon.begin(115200);
  106. delay(10);
  107. // !!!!!!!!!!!
  108. // Set your reset, enable, power pins here
  109. // !!!!!!!!!!!
  110. SerialMon.println("Wait...");
  111. // Set GSM module baud rate
  112. // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  113. SerialAT.begin(9600);
  114. delay(3000);
  115. // Restart takes quite some time
  116. // To skip it, call init() instead of restart()
  117. SerialMon.println("Initializing modem...");
  118. modem.restart();
  119. // modem.init();
  120. String modemInfo = modem.getModemInfo();
  121. SerialMon.print("Modem Info: ");
  122. SerialMon.println(modemInfo);
  123. #if TINY_GSM_USE_GPRS
  124. // Unlock your SIM card with a PIN if needed
  125. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  126. modem.simUnlock(GSM_PIN);
  127. }
  128. #endif
  129. }
  130. void loop() {
  131. #if TINY_GSM_USE_WIFI
  132. // Wifi connection parameters must be set before waiting for the network
  133. SerialMon.print(F("Setting SSID/password..."));
  134. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  135. SerialMon.println(" fail");
  136. delay(10000);
  137. return;
  138. }
  139. SerialMon.println(" success");
  140. #endif
  141. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  142. // The XBee must run the gprsConnect function BEFORE waiting for network!
  143. modem.gprsConnect(apn, gprsUser, gprsPass);
  144. #endif
  145. SerialMon.print("Waiting for network...");
  146. if (!modem.waitForNetwork()) {
  147. SerialMon.println(" fail");
  148. delay(10000);
  149. return;
  150. }
  151. SerialMon.println(" success");
  152. if (modem.isNetworkConnected()) {
  153. SerialMon.println("Network connected");
  154. }
  155. #if TINY_GSM_USE_GPRS
  156. // GPRS connection parameters are usually set after network registration
  157. SerialMon.print(F("Connecting to "));
  158. SerialMon.print(apn);
  159. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  160. SerialMon.println(" fail");
  161. delay(10000);
  162. return;
  163. }
  164. SerialMon.println(" success");
  165. if (modem.isGprsConnected()) {
  166. SerialMon.println("GPRS connected");
  167. }
  168. #endif
  169. SerialMon.print(F("Performing HTTP GET request... "));
  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) {
  215. delay(1000);
  216. }
  217. }