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.

229 lines
5.9 KiB

7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 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
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_UBLOX
  27. // #define TINY_GSM_MODEM_SARAR4
  28. // #define TINY_GSM_MODEM_M95
  29. // #define TINY_GSM_MODEM_BG96
  30. // #define TINY_GSM_MODEM_A6
  31. // #define TINY_GSM_MODEM_A7
  32. // #define TINY_GSM_MODEM_M590
  33. // #define TINY_GSM_MODEM_MC60
  34. // #define TINY_GSM_MODEM_MC60E
  35. // #define TINY_GSM_MODEM_ESP8266
  36. // #define TINY_GSM_MODEM_XBEE
  37. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  38. // Increase RX buffer to capture the entire response
  39. // Chips without internal buffering (A6/A7, ESP8266, M590)
  40. // need enough space in the buffer for the entire response
  41. // else data will be lost (and the http library will fail).
  42. #define TINY_GSM_RX_BUFFER 650
  43. // See all AT commands, if wanted
  44. //#define DUMP_AT_COMMANDS
  45. // See the debugging, if wanted
  46. //#define TINY_GSM_DEBUG Serial
  47. //#define LOGGING // <- Logging is for the HTTP library
  48. // Add a reception delay, if needed
  49. //#define TINY_GSM_YIELD() { delay(1); }
  50. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  51. #define SerialMon Serial
  52. // Set serial for AT commands (to the module)
  53. // Use Hardware Serial on Mega, Leonardo, Micro
  54. #define SerialAT Serial1
  55. // or Software Serial on Uno, Nano
  56. //#include <SoftwareSerial.h>
  57. //SoftwareSerial SerialAT(2, 3); // RX, TX
  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
  63. // Leave empty, if missing user or pass
  64. const char apn[] = "YourAPN";
  65. const char gprsUser[] = "";
  66. const char gprsPass[] = "";
  67. const char wifiSSID[] = "YourSSID";
  68. const char wifiPass[] = "SSIDpw";
  69. // Server details
  70. const char server[] = "vsh.pp.ua";
  71. const char resource[] = "/TinyGSM/logo.txt";
  72. const int port = 80;
  73. #include <TinyGsmClient.h>
  74. #include <ArduinoHttpClient.h>
  75. #ifdef DUMP_AT_COMMANDS
  76. #include <StreamDebugger.h>
  77. StreamDebugger debugger(SerialAT, SerialMon);
  78. TinyGsm modem(debugger);
  79. #else
  80. TinyGsm modem(SerialAT);
  81. #endif
  82. TinyGsmClient client(modem);
  83. HttpClient http(client, server, port);
  84. void setup() {
  85. // Set console baud rate
  86. SerialMon.begin(115200);
  87. delay(10);
  88. // Set your reset, enable, power pins here
  89. pinMode(20, OUTPUT);
  90. digitalWrite(20, HIGH);
  91. pinMode(23, OUTPUT);
  92. digitalWrite(23, HIGH);
  93. SerialMon.println("Wait...");
  94. // Set GSM module baud rate
  95. SerialAT.begin(115200);
  96. delay(3000);
  97. // Restart takes quite some time
  98. // To skip it, call init() instead of restart()
  99. SerialMon.println("Initializing modem...");
  100. modem.restart();
  101. // modem.init();
  102. String modemInfo = modem.getModemInfo();
  103. SerialMon.print("Modem: ");
  104. SerialMon.println(modemInfo);
  105. // Unlock your SIM card with a PIN
  106. //modem.simUnlock("1234");
  107. }
  108. void loop() {
  109. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  110. SerialMon.print(F("Setting SSID/password..."));
  111. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  112. SerialMon.println(" fail");
  113. delay(10000);
  114. return;
  115. }
  116. SerialMon.println(" OK");
  117. #endif
  118. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  119. // The XBee must run the gprsConnect function BEFORE waiting for network!
  120. modem.gprsConnect(apn, gprsUser, gprsPass);
  121. #endif
  122. SerialMon.print("Waiting for network...");
  123. if (!modem.waitForNetwork()) {
  124. SerialMon.println(" fail");
  125. delay(10000);
  126. return;
  127. }
  128. SerialMon.println(" OK");
  129. if (modem.isNetworkConnected()) {
  130. SerialMon.println("Network connected");
  131. }
  132. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  133. SerialMon.print(F("Connecting to "));
  134. SerialMon.print(apn);
  135. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  136. SerialMon.println(" fail");
  137. delay(10000);
  138. return;
  139. }
  140. SerialMon.println(" OK");
  141. #endif
  142. SerialMon.print(F("Performing HTTP GET request... "));
  143. int err = http.get(resource);
  144. if (err != 0) {
  145. SerialMon.println(F("failed to connect"));
  146. delay(10000);
  147. return;
  148. }
  149. int status = http.responseStatusCode();
  150. SerialMon.print(F("Response status code: "));
  151. SerialMon.println(status);
  152. if (!status) {
  153. delay(10000);
  154. return;
  155. }
  156. SerialMon.println(F("Response Headers:"));
  157. while (http.headerAvailable()) {
  158. String headerName = http.readHeaderName();
  159. String headerValue = http.readHeaderValue();
  160. SerialMon.println(" " + headerName + " : " + headerValue);
  161. }
  162. int length = http.contentLength();
  163. if (length >= 0) {
  164. SerialMon.print(F("Content length is: "));
  165. SerialMon.println(length);
  166. }
  167. if (http.isResponseChunked()) {
  168. SerialMon.println(F("The response is chunked"));
  169. }
  170. String body = http.responseBody();
  171. SerialMon.println(F("Response:"));
  172. SerialMon.println(body);
  173. SerialMon.print(F("Body length is: "));
  174. SerialMon.println(body.length());
  175. // Shutdown
  176. http.stop();
  177. SerialMon.println(F("Server disconnected"));
  178. #if TINY_GSM_USE_WIFI
  179. modem.networkDisconnect();
  180. SerialMon.println(F("WiFi disconnected"));
  181. #endif
  182. #if TINY_GSM_USE_GPRS
  183. modem.gprsDisconnect();
  184. SerialMon.println(F("GPRS disconnected"));
  185. #endif
  186. // Do nothing forevermore
  187. while (true) {
  188. delay(1000);
  189. }
  190. }