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.

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