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.

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