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.

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