diff --git a/examples/HttpClient/HttpClient.ino b/examples/HttpClient/HttpClient.ino new file mode 100644 index 0000000..1b8029b --- /dev/null +++ b/examples/HttpClient/HttpClient.ino @@ -0,0 +1,151 @@ +/************************************************************** + * + * This sketch connects to a website and downloads a page. + * It can be used to perform HTTP/RESTful API calls. + * + * For this example, you need to install ArduinoHttpClient library: + * https://github.com/arduino-libraries/ArduinoHttpClient + * or from http://librarymanager/all#ArduinoHttpClient + * + * TinyGSM Getting Started guide: + * http://tiny.cc/tiny-gsm-readme + * + **************************************************************/ + +// Select your modem: +#define TINY_GSM_MODEM_SIM800 +// #define TINY_GSM_MODEM_SIM900 +// #define TINY_GSM_MODEM_A6 +// #define TINY_GSM_MODEM_A7 +// #define TINY_GSM_MODEM_M590 +// #define TINY_GSM_MODEM_ESP8266 + +// Increase RX buffer +#define TINY_GSM_RX_BUFFER 512 + +// Use Hardware Serial on Mega, Leonardo, Micro +#define SerialAT Serial1 + +// or Software Serial on Uno, Nano +//#include +//SoftwareSerial SerialAT(2, 3); // RX, TX + +//#define DUMP_AT_COMMANDS +//#define TINY_GSM_DEBUG Serial + + +// Your GPRS credentials +// Leave empty, if missing user or pass +const char apn[] = "YourAPN"; +const char user[] = ""; +const char pass[] = ""; + +// Name of the server we want to connect to +const char server[] = "cdn.rawgit.com"; +const int port = 80; +// Path to download (this is the bit after the hostname in the URL) +const char resource[] = "/vshymanskyy/tinygsm/master/extras/logo.txt"; + +#include +#include + +#ifdef DUMP_AT_COMMANDS + #include + StreamDebugger debugger(SerialAT, Serial); + TinyGsm modem(debugger); +#else + TinyGsm modem(SerialAT); +#endif + +TinyGsmClient client(modem); +HttpClient http(client, server, port); + +void setup() { + // Set console baud rate + Serial.begin(115200); + delay(10); + + // Set GSM module baud rate + SerialAT.begin(115200); + delay(3000); + + // Restart takes quite some time + // To skip it, call init() instead of restart() + Serial.println("Initializing modem..."); + modem.restart(); + + String modemInfo = modem.getModemInfo(); + Serial.print("Modem: "); + Serial.println(modemInfo); + + // Unlock your SIM card with a PIN + //modem.simUnlock("1234"); +} + +void loop() { + Serial.print(F("Waiting for network...")); + if (!modem.waitForNetwork()) { + Serial.println(" fail"); + delay(10000); + return; + } + Serial.println(" OK"); + + Serial.print(F("Connecting to ")); + Serial.print(apn); + if (!modem.gprsConnect(apn, user, pass)) { + Serial.println(" fail"); + delay(10000); + return; + } + Serial.println(" OK"); + + + Serial.print(F("Performing HTTP GET request... ")); + int err = http.get(resource); + if (err != 0) { + Serial.println("failed to connect"); + delay(10000); + return; + } + + int status = http.responseStatusCode(); + Serial.println(status); + if (!status) { + delay(10000); + return; + } + + while (http.headerAvailable()) { + String headerName = http.readHeaderName(); + String headerValue = http.readHeaderValue(); + //Serial.println(headerName + " : " + headerValue); + } + + int length = http.contentLength(); + if (length >= 0) { + Serial.println(String("Content length is: ") + length); + } + if (http.isResponseChunked()) { + Serial.println("This response is chunked"); + } + + String body = http.responseBody(); + Serial.println("Response:"); + Serial.println(body); + + Serial.println(String("Body length is: ") + body.length()); + + // Shutdown + + http.stop(); + + modem.gprsDisconnect(); + Serial.println("GPRS disconnected"); + + // Do nothing forevermore + while (true) { + delay(1000); + } +} +