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.

172 lines
4.2 KiB

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