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.

155 lines
3.6 KiB

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