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.

160 lines
3.8 KiB

7 years ago
7 years ago
7 years ago
7 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
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. * http://tiny.cc/tiny-gsm-readme
  12. *
  13. * For more HTTP API examples, see ArduinoHttpClient library
  14. *
  15. **************************************************************/
  16. // Select your modem:
  17. #define TINY_GSM_MODEM_SIM800
  18. // #define TINY_GSM_MODEM_SIM808
  19. // #define TINY_GSM_MODEM_SIM900
  20. // #define TINY_GSM_MODEM_UBLOX
  21. // #define TINY_GSM_MODEM_BG96
  22. // #define TINY_GSM_MODEM_A6
  23. // #define TINY_GSM_MODEM_A7
  24. // #define TINY_GSM_MODEM_M590
  25. // #define TINY_GSM_MODEM_ESP8266
  26. // Increase RX buffer if needed
  27. //#define TINY_GSM_RX_BUFFER 512
  28. #include <TinyGsmClient.h>
  29. #include <ArduinoHttpClient.h>
  30. // Uncomment this if you want to see all AT commands
  31. //#define DUMP_AT_COMMANDS
  32. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  33. #define SerialMon Serial
  34. // Use Hardware Serial on Mega, Leonardo, Micro
  35. #define SerialAT Serial1
  36. // or Software Serial on Uno, Nano
  37. //#include <SoftwareSerial.h>
  38. //SoftwareSerial SerialAT(2, 3); // RX, TX
  39. // Your GPRS credentials
  40. // Leave empty, if missing user or pass
  41. const char apn[] = "YourAPN";
  42. const char user[] = "";
  43. const char pass[] = "";
  44. // Server details
  45. const char server[] = "vsh.pp.ua";
  46. const char resource[] = "/TinyGSM/logo.txt";
  47. const int port = 80;
  48. #ifdef DUMP_AT_COMMANDS
  49. #include <StreamDebugger.h>
  50. StreamDebugger debugger(SerialAT, SerialMon);
  51. TinyGsm modem(debugger);
  52. #else
  53. TinyGsm modem(SerialAT);
  54. #endif
  55. TinyGsmClient client(modem);
  56. HttpClient http(client, server, port);
  57. void setup() {
  58. // Set console baud rate
  59. SerialMon.begin(115200);
  60. delay(10);
  61. // Set GSM module baud rate
  62. SerialAT.begin(115200);
  63. delay(3000);
  64. // Restart takes quite some time
  65. // To skip it, call init() instead of restart()
  66. SerialMon.println(F("Initializing modem..."));
  67. modem.restart();
  68. String modemInfo = modem.getModemInfo();
  69. SerialMon.print(F("Modem: "));
  70. SerialMon.println(modemInfo);
  71. // Unlock your SIM card with a PIN
  72. //modem.simUnlock("1234");
  73. }
  74. void loop() {
  75. SerialMon.print(F("Waiting for network..."));
  76. if (!modem.waitForNetwork()) {
  77. SerialMon.println(" fail");
  78. delay(10000);
  79. return;
  80. }
  81. SerialMon.println(" OK");
  82. SerialMon.print(F("Connecting to "));
  83. SerialMon.print(apn);
  84. if (!modem.gprsConnect(apn, user, pass)) {
  85. SerialMon.println(" fail");
  86. delay(10000);
  87. return;
  88. }
  89. SerialMon.println(" OK");
  90. SerialMon.print(F("Performing HTTP GET request... "));
  91. int err = http.get(resource);
  92. if (err != 0) {
  93. SerialMon.println(F("failed to connect"));
  94. delay(10000);
  95. return;
  96. }
  97. int status = http.responseStatusCode();
  98. SerialMon.println(status);
  99. if (!status) {
  100. delay(10000);
  101. return;
  102. }
  103. while (http.headerAvailable()) {
  104. String headerName = http.readHeaderName();
  105. String headerValue = http.readHeaderValue();
  106. //SerialMon.println(headerName + " : " + headerValue);
  107. }
  108. int length = http.contentLength();
  109. if (length >= 0) {
  110. SerialMon.print(F("Content length is: "));
  111. SerialMon.println(length);
  112. }
  113. if (http.isResponseChunked()) {
  114. SerialMon.println(F("The response is chunked"));
  115. }
  116. String body = http.responseBody();
  117. SerialMon.println(F("Response:"));
  118. SerialMon.println(body);
  119. SerialMon.print(F("Body length is: "));
  120. SerialMon.println(body.length());
  121. // Shutdown
  122. http.stop();
  123. SerialMon.println(F("Server disconnected"));
  124. modem.gprsDisconnect();
  125. SerialMon.println(F("GPRS disconnected"));
  126. // Do nothing forevermore
  127. while (true) {
  128. delay(1000);
  129. }
  130. }