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.

161 lines
3.8 KiB

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