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.

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