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.

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