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.

159 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. //#define TINY_GSM_RX_BUFFER 512
  20. #include <TinyGsmClient.h>
  21. #include <ArduinoHttpClient.h>
  22. // Uncomment this if you want to see all AT commands
  23. //#define DUMP_AT_COMMANDS
  24. // Uncomment this if you want to use SSL
  25. //#define USE_SSL
  26. // Set serial for debug console (to the Serial Monitor, speed 115200)
  27. #define SerialMon SerialUSB
  28. // Select Serial1 or Serial depending on your module configuration
  29. #define SerialAT Serial1
  30. // Your GPRS credentials
  31. // Leave empty, if missing user or pass
  32. const char apn[] = "YourAPN";
  33. const char user[] = "";
  34. const char pass[] = "";
  35. // Server details
  36. const char server[] = "vsh.pp.ua";
  37. const char resource[] = "/TinyGSM/logo.txt";
  38. #ifdef DUMP_AT_COMMANDS
  39. #include <StreamDebugger.h>
  40. StreamDebugger debugger(SerialAT, SerialMon);
  41. TinyGsm modem(debugger);
  42. #else
  43. TinyGsm modem(SerialAT);
  44. #endif
  45. #ifdef USE_SSL
  46. TinyGsmClientSecure client(modem);
  47. HttpClient http(client, server, 443);
  48. #else
  49. TinyGsmClient client(modem);
  50. HttpClient http(client, server, 80);
  51. #endif
  52. void setup() {
  53. // Turn on modem with 1 second pulse on D6
  54. pinMode(6, OUTPUT);
  55. digitalWrite(6, HIGH);
  56. delay(1000);
  57. digitalWrite(6, LOW);
  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(" success");
  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(" success");
  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. }