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.

158 lines
3.7 KiB

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