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.

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