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.

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