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.

198 lines
5.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
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 the debugging, if wanted
  43. //#define TINY_GSM_DEBUG Serial
  44. //#define LOGGING
  45. // Add a reception delay, if needed
  46. //#define TINY_GSM_YIELD() { delay(1); }
  47. #include <TinyGsmClient.h>
  48. #include <ArduinoHttpClient.h>
  49. // Uncomment this if you want to see all AT commands
  50. //#define DUMP_AT_COMMANDS
  51. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  52. #define SerialMon Serial
  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. // Your GPRS credentials
  59. // Leave empty, if missing user or pass
  60. const char apn[] = "YourAPN";
  61. const char gprsUser[] = "";
  62. const char gprsPass[] = "";
  63. const char wifiSSID[] = "YourSSID";
  64. const char wifiPass[] = "SSIDpw";
  65. // Server details
  66. const char server[] = "vsh.pp.ua";
  67. const char resource[] = "/TinyGSM/logo.txt";
  68. const int port = 80;
  69. #ifdef DUMP_AT_COMMANDS
  70. #include <StreamDebugger.h>
  71. StreamDebugger debugger(SerialAT, SerialMon);
  72. TinyGsm modem(debugger);
  73. #else
  74. TinyGsm modem(SerialAT);
  75. #endif
  76. TinyGsmClient client(modem);
  77. HttpClient http(client, server, port);
  78. void setup() {
  79. // Set console baud rate
  80. SerialMon.begin(115200);
  81. delay(10);
  82. SerialMon.println(F("Wait..."));
  83. // Set GSM module baud rate
  84. SerialAT.begin(115200);
  85. delay(3000);
  86. // Restart takes quite some time
  87. // To skip it, call init() instead of restart()
  88. SerialMon.println(F("Initializing modem..."));
  89. modem.restart();
  90. String modemInfo = modem.getModemInfo();
  91. SerialMon.print(F("Modem: "));
  92. SerialMon.println(modemInfo);
  93. // Unlock your SIM card with a PIN
  94. //modem.simUnlock("1234");
  95. }
  96. void loop() {
  97. if (modem.hasWifi()) {
  98. SerialMon.print(F("Setting SSID/password..."));
  99. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  100. SerialMon.println(" fail");
  101. delay(10000);
  102. return;
  103. }
  104. SerialMon.println(" OK");
  105. }
  106. SerialMon.print(F("Waiting for network..."));
  107. if (!modem.waitForNetwork()) {
  108. SerialMon.println(" fail");
  109. delay(10000);
  110. return;
  111. }
  112. SerialMon.println(" OK");
  113. if (modem.hasGPRS()) {
  114. SerialMon.print(F("Connecting to "));
  115. SerialMon.print(apn);
  116. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  117. SerialMon.println(" fail");
  118. delay(10000);
  119. return;
  120. }
  121. SerialMon.println(" OK");
  122. }
  123. SerialMon.print(F("Performing HTTP GET request... "));
  124. int err = http.get(resource);
  125. if (err != 0) {
  126. SerialMon.println(F("failed to connect"));
  127. delay(10000);
  128. return;
  129. }
  130. int status = http.responseStatusCode();
  131. SerialMon.print(F("Response status code: "));
  132. SerialMon.println(status);
  133. if (!status) {
  134. delay(10000);
  135. return;
  136. }
  137. SerialMon.println(F("Response Headers:"));
  138. while (http.headerAvailable()) {
  139. String headerName = http.readHeaderName();
  140. String headerValue = http.readHeaderValue();
  141. SerialMon.println(" " + headerName + " : " + headerValue);
  142. }
  143. int length = http.contentLength();
  144. if (length >= 0) {
  145. SerialMon.print(F("Content length is: "));
  146. SerialMon.println(length);
  147. }
  148. if (http.isResponseChunked()) {
  149. SerialMon.println(F("The response is chunked"));
  150. }
  151. String body = http.responseBody();
  152. SerialMon.println(F("Response:"));
  153. SerialMon.println(body);
  154. SerialMon.print(F("Body length is: "));
  155. SerialMon.println(body.length());
  156. // Shutdown
  157. http.stop();
  158. SerialMon.println(F("Server disconnected"));
  159. modem.gprsDisconnect();
  160. SerialMon.println(F("GPRS disconnected"));
  161. // Do nothing forevermore
  162. while (true) {
  163. delay(1000);
  164. }
  165. }