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.

152 lines
3.5 KiB

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