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.

233 lines
6.1 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
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
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_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. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  39. #define SerialMon Serial
  40. // Set serial for AT commands (to the module)
  41. // Use Hardware Serial on Mega, Leonardo, Micro
  42. #define SerialAT Serial1
  43. // or Software Serial on Uno, Nano
  44. //#include <SoftwareSerial.h>
  45. //SoftwareSerial SerialAT(2, 3); // RX, TX
  46. // Increase RX buffer to capture the entire response
  47. // Chips without internal buffering (A6/A7, ESP8266, M590)
  48. // need enough space in the buffer for the entire response
  49. // else data will be lost (and the http library will fail).
  50. #define TINY_GSM_RX_BUFFER 650
  51. // See all AT commands, if wanted
  52. //#define DUMP_AT_COMMANDS
  53. // Define the serial console for debug prints, if needed
  54. #define TINY_GSM_DEBUG SerialMon
  55. //#define LOGGING // <- Logging is for the HTTP library
  56. // Add a reception delay, if needed
  57. //#define TINY_GSM_YIELD() { delay(2); }
  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[] = "YourWiFiPass";
  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. #if TINY_GSM_USE_GPRS
  106. // Unlock your SIM card with a PIN if needed
  107. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  108. modem.simUnlock(GSM_PIN);
  109. }
  110. #endif
  111. }
  112. void loop() {
  113. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  114. SerialMon.print(F("Setting SSID/password..."));
  115. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  116. SerialMon.println(" fail");
  117. delay(10000);
  118. return;
  119. }
  120. SerialMon.println(" success");
  121. #endif
  122. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  123. // The XBee must run the gprsConnect function BEFORE waiting for network!
  124. modem.gprsConnect(apn, gprsUser, gprsPass);
  125. #endif
  126. SerialMon.print("Waiting for network...");
  127. if (!modem.waitForNetwork()) {
  128. SerialMon.println(" fail");
  129. delay(10000);
  130. return;
  131. }
  132. SerialMon.println(" success");
  133. if (modem.isNetworkConnected()) {
  134. SerialMon.println("Network connected");
  135. }
  136. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  137. SerialMon.print(F("Connecting to "));
  138. SerialMon.print(apn);
  139. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  140. SerialMon.println(" fail");
  141. delay(10000);
  142. return;
  143. }
  144. SerialMon.println(" success");
  145. #endif
  146. SerialMon.print(F("Performing HTTP GET request... "));
  147. int err = http.get(resource);
  148. if (err != 0) {
  149. SerialMon.println(F("failed to connect"));
  150. delay(10000);
  151. return;
  152. }
  153. int status = http.responseStatusCode();
  154. SerialMon.print(F("Response status code: "));
  155. SerialMon.println(status);
  156. if (!status) {
  157. delay(10000);
  158. return;
  159. }
  160. SerialMon.println(F("Response Headers:"));
  161. while (http.headerAvailable()) {
  162. String headerName = http.readHeaderName();
  163. String headerValue = http.readHeaderValue();
  164. SerialMon.println(" " + headerName + " : " + headerValue);
  165. }
  166. int length = http.contentLength();
  167. if (length >= 0) {
  168. SerialMon.print(F("Content length is: "));
  169. SerialMon.println(length);
  170. }
  171. if (http.isResponseChunked()) {
  172. SerialMon.println(F("The response is chunked"));
  173. }
  174. String body = http.responseBody();
  175. SerialMon.println(F("Response:"));
  176. SerialMon.println(body);
  177. SerialMon.print(F("Body length is: "));
  178. SerialMon.println(body.length());
  179. // Shutdown
  180. http.stop();
  181. SerialMon.println(F("Server disconnected"));
  182. #if TINY_GSM_USE_WIFI
  183. modem.networkDisconnect();
  184. SerialMon.println(F("WiFi disconnected"));
  185. #endif
  186. #if TINY_GSM_USE_GPRS
  187. modem.gprsDisconnect();
  188. SerialMon.println(F("GPRS disconnected"));
  189. #endif
  190. // Do nothing forevermore
  191. while (true) {
  192. delay(1000);
  193. }
  194. }