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.

162 lines
3.9 KiB

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