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.

164 lines
4.0 KiB

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