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.

160 lines
3.8 KiB

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