diff --git a/README.md b/README.md index 10a7823..f0e1490 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ This library is easy to integrate with lots of sketches, which use Ethernet or W ### TinyGSM is tiny The complete WebClient example for Arduino Uno (via Software Serial) takes little resources: ``` -Sketch uses 14094 bytes (43%) of program storage space. Maximum is 32256 bytes. -Global variables use 625 bytes (30%) of dynamic memory, leaving 1423 bytes for local variables. Maximum is 2048 bytes. +Sketch uses 15022 bytes (46%) of program storage space. Maximum is 32256 bytes. +Global variables use 574 bytes (28%) of dynamic memory, leaving 1474 bytes for local variables. Maximum is 2048 bytes. ``` Arduino GSM library uses 15868 bytes (49%) of Flash and 1113 bytes (54%) of RAM in a similar scenario. TinyGSM also pulls data gently from the modem (whenever possible), so it can operate on very little RAM. @@ -78,6 +78,7 @@ GPS/GNSS | ✔¹ | 🅧 | ◌¹ | 🅧 | - Microduino GSM - Adafruit FONA (Mini Cellular GSM Breakout) - Adafruit FONA 800/808 Shield +- Industruino GSM - ... other modules, based on supported modems More modems may be supported later: diff --git a/examples/BlynkClient/BlynkClient.ino b/examples/BlynkClient/BlynkClient.ino index 1fb969c..821e8b4 100644 --- a/examples/BlynkClient/BlynkClient.ino +++ b/examples/BlynkClient/BlynkClient.ino @@ -41,15 +41,8 @@ #include #include -// You should get Auth Token in the Blynk App. -// Go to the Project Settings (nut icon). -const char auth[] = "YourAuthToken"; - -// Your GPRS credentials -// Leave empty, if missing user or pass -const char apn[] = "YourAPN"; -const char user[] = ""; -const char pass[] = ""; +// Set serial for debug console (to the Serial Monitor, default speed 115200) +#define SerialMon Serial // Hardware Serial on Mega, Leonardo, Micro #define SerialAT Serial1 @@ -58,12 +51,23 @@ const char pass[] = ""; //#include //SoftwareSerial SerialAT(2, 3); // RX, TX + +// Your GPRS credentials +// Leave empty, if missing user or pass +const char apn[] = "YourAPN"; +const char user[] = ""; +const char pass[] = ""; + +// You should get Auth Token in the Blynk App. +// Go to the Project Settings (nut icon). +const char auth[] = "YourAuthToken"; + TinyGsm modem(SerialAT); void setup() { // Set console baud rate - Serial.begin(115200); + SerialMon.begin(115200); delay(10); // Set GSM module baud rate @@ -72,12 +76,12 @@ void setup() // Restart takes quite some time // To skip it, call init() instead of restart() - Serial.println("Initializing modem..."); + SerialMon.println("Initializing modem..."); modem.restart(); String modemInfo = modem.getModemInfo(); - Serial.print("Modem: "); - Serial.println(modemInfo); + SerialMon.print("Modem: "); + SerialMon.println(modemInfo); // Unlock your SIM card with a PIN //modem.simUnlock("1234"); diff --git a/examples/FileDownload/FileDownload.ino b/examples/FileDownload/FileDownload.ino index a9cebeb..f7eecc3 100644 --- a/examples/FileDownload/FileDownload.ino +++ b/examples/FileDownload/FileDownload.ino @@ -7,8 +7,9 @@ * TinyGSM Getting Started guide: * http://tiny.cc/tiny-gsm-readme * - * ATTENTION! Downloading big files requires quite a lot - * of knowledge - so this is for more experienced developers. + * ATTENTION! Downloading big files requires of knowledge of + * the TinyGSM internals and some modem specifics, + * so this is for more experienced developers. * **************************************************************/ @@ -22,17 +23,17 @@ // #define TINY_GSM_MODEM_ESP8266 // #define TINY_GSM_MODEM_XBEE -// Increase RX buffer -#define TINY_GSM_RX_BUFFER 1030 +// Increase RX buffer if needed +#define TINY_GSM_RX_BUFFER 1024 +#include +#include + +// Uncomment this if you want to see all AT commands //#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[] = ""; +// Set serial for debug console (to the Serial Monitor, default speed 115200) +#define SerialMon Serial // Use Hardware Serial on Mega, Leonardo, Micro #define SerialAT Serial1 @@ -41,28 +42,34 @@ const char pass[] = ""; //#include //SoftwareSerial SerialAT(2, 3); // RX, TX -#include -#include + +// Your GPRS credentials +// Leave empty, if missing user or pass +const char apn[] = "YourAPN"; +const char user[] = ""; +const char pass[] = ""; + +// Server details +const char server[] = "vsh.pp.ua"; +const int port = 80; + +const char resource[] = "/TinyGSM/test_1k.bin"; +uint32_t knownCRC32 = 0x6f50d767; +uint32_t knownFileSize = 1024; // In case server does not send it #ifdef DUMP_AT_COMMANDS #include - StreamDebugger debugger(SerialAT, Serial); + StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger); #else TinyGsm modem(SerialAT); #endif + TinyGsmClient client(modem); -const char server[] = "cdn.rawgit.com"; -const int port = 80; - -const char resource[] = "/vshymanskyy/tinygsm/master/extras/test_1k.bin"; -uint32_t knownCRC32 = 0x6f50d767; -uint32_t knownFileSize = 1024; // In case server does not send it - void setup() { // Set console baud rate - Serial.begin(115200); + SerialMon.begin(115200); delay(10); // Set GSM module baud rate @@ -71,12 +78,12 @@ void setup() { // Restart takes quite some time // To skip it, call init() instead of restart() - Serial.println("Initializing modem..."); + SerialMon.println(F("Initializing modem...")); modem.restart(); String modemInfo = modem.getModemInfo(); - Serial.print("Modem: "); - Serial.println(modemInfo); + SerialMon.print(F("Modem: ")); + SerialMon.println(modemInfo); // Unlock your SIM card with a PIN //modem.simUnlock("1234"); @@ -85,43 +92,42 @@ void setup() { void printPercent(uint32_t readLength, uint32_t contentLength) { // If we know the total length if (contentLength != -1) { - Serial.print("\r "); - Serial.print((100.0 * readLength) / contentLength); - Serial.print('%'); + SerialMon.print("\r "); + SerialMon.print((100.0 * readLength) / contentLength); + SerialMon.print('%'); } else { - Serial.println(readLength); + SerialMon.println(readLength); } } void loop() { - Serial.print("Waiting for network..."); + SerialMon.print(F("Waiting for network...")); if (!modem.waitForNetwork()) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); + SerialMon.println(" OK"); - Serial.print("Connecting to "); - Serial.print(apn); + SerialMon.print(F("Connecting to ")); + SerialMon.print(apn); if (!modem.gprsConnect(apn, user, pass)) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); + SerialMon.println(" OK"); - Serial.print("Connecting to "); - Serial.print(server); - - // if you get a connection, report back via serial: + SerialMon.print(F("Connecting to ")); + SerialMon.print(server); if (!client.connect(server, port)) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); - // Make a HTTP request: + SerialMon.println(" OK"); + + // Make a HTTP GET request: client.print(String("GET ") + resource + " HTTP/1.0\r\n"); client.print(String("Host: ") + server + "\r\n"); client.print("Connection: close\r\n\r\n"); @@ -129,20 +135,20 @@ void loop() { long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 5000L) { - Serial.println(">>> Client Timeout !"); + SerialMon.println(F(">>> Client Timeout !")); client.stop(); delay(10000L); return; } } - Serial.println("Reading response header"); + SerialMon.println(F("Reading response header")); uint32_t contentLength = knownFileSize; while (client.available()) { String line = client.readStringUntil('\n'); line.trim(); - //Serial.println(line); // Uncomment this to show response header + //SerialMon.println(line); // Uncomment this to show response header line.toLowerCase(); if (line.startsWith("content-length:")) { contentLength = line.substring(line.lastIndexOf(':') + 1).toInt(); @@ -151,7 +157,7 @@ void loop() { } } - Serial.println("Reading response data"); + SerialMon.println(F("Reading response data")); timeout = millis(); uint32_t readLength = 0; CRC32 crc; @@ -161,7 +167,7 @@ void loop() { while (readLength < contentLength && client.connected() && millis() - timeout < 10000L) { while (client.available()) { uint8_t c = client.read(); - //Serial.print((char)c); // Uncomment this to show data + //SerialMon.print((char)c); // Uncomment this to show data crc.update(c); readLength++; if (readLength % (contentLength / 13) == 0) { @@ -172,22 +178,24 @@ void loop() { } printPercent(readLength, contentLength); timeElapsed = millis() - timeElapsed; - Serial.println(); + SerialMon.println(); + + // Shutdown client.stop(); - Serial.println("Server disconnected"); + SerialMon.println(F("Server disconnected")); modem.gprsDisconnect(); - Serial.println("GPRS disconnected"); - Serial.println(); + SerialMon.println(F("GPRS disconnected")); float duration = float(timeElapsed) / 1000; - Serial.print("Content-Length: "); Serial.println(contentLength); - Serial.print("Actually read: "); Serial.println(readLength); - Serial.print("Calc. CRC32: 0x"); Serial.println(crc.finalize(), HEX); - Serial.print("Known CRC32: 0x"); Serial.println(knownCRC32, HEX); - Serial.print("Duration: "); Serial.print(duration); Serial.println("s"); + SerialMon.println(); + SerialMon.print("Content-Length: "); SerialMon.println(contentLength); + SerialMon.print("Actually read: "); SerialMon.println(readLength); + SerialMon.print("Calc. CRC32: 0x"); SerialMon.println(crc.finalize(), HEX); + SerialMon.print("Known CRC32: 0x"); SerialMon.println(knownCRC32, HEX); + SerialMon.print("Duration: "); SerialMon.print(duration); SerialMon.println("s"); // Do nothing forevermore while (true) { diff --git a/examples/HttpClient/HttpClient.ino b/examples/HttpClient/HttpClient.ino index 69e2e50..c52a664 100644 --- a/examples/HttpClient/HttpClient.ino +++ b/examples/HttpClient/HttpClient.ino @@ -10,6 +10,8 @@ * TinyGSM Getting Started guide: * http://tiny.cc/tiny-gsm-readme * + * For more HTTP API examples, see ArduinoHttpClient library + * **************************************************************/ // Select your modem: @@ -21,8 +23,17 @@ // #define TINY_GSM_MODEM_M590 // #define TINY_GSM_MODEM_ESP8266 -// Increase RX buffer -#define TINY_GSM_RX_BUFFER 600 +// Increase RX buffer if needed +//#define TINY_GSM_RX_BUFFER 512 + +#include +#include + +// Uncomment this if you want to see all AT commands +//#define DUMP_AT_COMMANDS + +// Set serial for debug console (to the Serial Monitor, default speed 115200) +#define SerialMon Serial // Use Hardware Serial on Mega, Leonardo, Micro #define SerialAT Serial1 @@ -31,9 +42,6 @@ //#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 @@ -41,18 +49,14 @@ 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 +// Server details +const char server[] = "vsh.pp.ua"; +const char resource[] = "/TinyGSM/logo.txt"; +const int port = 80; #ifdef DUMP_AT_COMMANDS #include - StreamDebugger debugger(SerialAT, Serial); + StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger); #else TinyGsm modem(SerialAT); @@ -63,7 +67,7 @@ HttpClient http(client, server, port); void setup() { // Set console baud rate - Serial.begin(115200); + SerialMon.begin(115200); delay(10); // Set GSM module baud rate @@ -72,46 +76,45 @@ void setup() { // Restart takes quite some time // To skip it, call init() instead of restart() - Serial.println("Initializing modem..."); + SerialMon.println(F("Initializing modem...")); modem.restart(); String modemInfo = modem.getModemInfo(); - Serial.print("Modem: "); - Serial.println(modemInfo); + SerialMon.print(F("Modem: ")); + SerialMon.println(modemInfo); // Unlock your SIM card with a PIN //modem.simUnlock("1234"); } void loop() { - Serial.print(F("Waiting for network...")); + SerialMon.print(F("Waiting for network...")); if (!modem.waitForNetwork()) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); + SerialMon.println(" OK"); - Serial.print(F("Connecting to ")); - Serial.print(apn); + SerialMon.print(F("Connecting to ")); + SerialMon.print(apn); if (!modem.gprsConnect(apn, user, pass)) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); + SerialMon.println(" OK"); - - Serial.print(F("Performing HTTP GET request... ")); + SerialMon.print(F("Performing HTTP GET request... ")); int err = http.get(resource); if (err != 0) { - Serial.println("failed to connect"); + SerialMon.println(F("failed to connect")); delay(10000); return; } int status = http.responseStatusCode(); - Serial.println(status); + SerialMon.println(status); if (!status) { delay(10000); return; @@ -120,29 +123,32 @@ void loop() { while (http.headerAvailable()) { String headerName = http.readHeaderName(); String headerValue = http.readHeaderValue(); - //Serial.println(headerName + " : " + headerValue); + //SerialMon.println(headerName + " : " + headerValue); } int length = http.contentLength(); if (length >= 0) { - Serial.println(String("Content length is: ") + length); + SerialMon.print(F("Content length is: ")); + SerialMon.println(length); } if (http.isResponseChunked()) { - Serial.println("This response is chunked"); + SerialMon.println(F("The response is chunked")); } String body = http.responseBody(); - Serial.println("Response:"); - Serial.println(body); + SerialMon.println(F("Response:")); + SerialMon.println(body); - Serial.println(String("Body length is: ") + body.length()); + SerialMon.print(F("Body length is: ")); + SerialMon.println(body.length()); // Shutdown http.stop(); + SerialMon.println(F("Server disconnected")); modem.gprsDisconnect(); - Serial.println("GPRS disconnected"); + SerialMon.println(F("GPRS disconnected")); // Do nothing forevermore while (true) { diff --git a/examples/HttpsClient/HttpsClient.ino b/examples/HttpsClient/HttpsClient.ino index dfe5bd5..73cd3be 100644 --- a/examples/HttpsClient/HttpsClient.ino +++ b/examples/HttpsClient/HttpsClient.ino @@ -10,15 +10,26 @@ * TinyGSM Getting Started guide: * http://tiny.cc/tiny-gsm-readme * + * SSL/TLS is currently supported only with SIM8xx series + * For more HTTP API examples, see ArduinoHttpClient library + * **************************************************************/ -// Select your modem -// SSL/TLS is currently supported only with SIM8xx series +// Select your modem: #define TINY_GSM_MODEM_SIM800 -//#define TINY_GSM_MODEM_SIM808 +// #define TINY_GSM_MODEM_SIM808 -// Increase RX buffer -#define TINY_GSM_RX_BUFFER 64 +// Increase RX buffer if needed +//#define TINY_GSM_RX_BUFFER 512 + +#include +#include + +// Uncomment this if you want to see all AT commands +//#define DUMP_AT_COMMANDS + +// Set serial for debug console (to the Serial Monitor, default speed 115200) +#define SerialMon Serial // Use Hardware Serial on Mega, Leonardo, Micro #define SerialAT Serial1 @@ -27,9 +38,6 @@ //#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 @@ -37,18 +45,14 @@ 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 = 443; -// Path to download (this is the bit after the hostname in the URL) -const char resource[] = "/vshymanskyy/tinygsm/master/extras/logo.txt"; - -#include -#include +// Server details +const char server[] = "vsh.pp.ua"; +const char resource[] = "/TinyGSM/logo.txt"; +const int port = 443; #ifdef DUMP_AT_COMMANDS #include - StreamDebugger debugger(SerialAT, Serial); + StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger); #else TinyGsm modem(SerialAT); @@ -59,7 +63,7 @@ HttpClient http(client, server, port); void setup() { // Set console baud rate - Serial.begin(115200); + SerialMon.begin(115200); delay(10); // Set GSM module baud rate @@ -68,53 +72,51 @@ void setup() { // Restart takes quite some time // To skip it, call init() instead of restart() - Serial.println("Initializing modem..."); + SerialMon.println(F("Initializing modem...")); modem.restart(); String modemInfo = modem.getModemInfo(); - Serial.print("Modem: "); - Serial.println(modemInfo); + SerialMon.print(F("Modem: ")); + SerialMon.println(modemInfo); // Unlock your SIM card with a PIN //modem.simUnlock("1234"); + + if (!modem.hasSSL()) { + SerialMon.println(F("SSL is not supported by this modem")); + while(true) { delay(1000); } + } } void loop() { - if (!modem.hasSSL()) { - Serial.println("SSL is not supported by this modem"); - delay(10000); - return; - } - - Serial.print(F("Waiting for network...")); + SerialMon.print(F("Waiting for network...")); if (!modem.waitForNetwork()) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); + SerialMon.println(" OK"); - Serial.print(F("Connecting to ")); - Serial.print(apn); + SerialMon.print(F("Connecting to ")); + SerialMon.print(apn); if (!modem.gprsConnect(apn, user, pass)) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); + SerialMon.println(" OK"); - - Serial.print(F("Performing HTTP GET request... ")); + SerialMon.print(F("Performing HTTPS GET request... ")); http.connectionKeepAlive(); // Currently, this is needed for HTTPS int err = http.get(resource); if (err != 0) { - Serial.println("failed to connect"); + SerialMon.println(F("failed to connect")); delay(10000); return; } int status = http.responseStatusCode(); - Serial.println(status); + SerialMon.println(status); if (!status) { delay(10000); return; @@ -123,29 +125,32 @@ void loop() { while (http.headerAvailable()) { String headerName = http.readHeaderName(); String headerValue = http.readHeaderValue(); - //Serial.println(headerName + " : " + headerValue); + //SerialMon.println(headerName + " : " + headerValue); } int length = http.contentLength(); if (length >= 0) { - Serial.println(String("Content length is: ") + length); + SerialMon.print(F("Content length is: ")); + SerialMon.println(length); } if (http.isResponseChunked()) { - Serial.println("This response is chunked"); + SerialMon.println(F("The response is chunked")); } String body = http.responseBody(); - Serial.println("Response:"); - Serial.println(body); + SerialMon.println(F("Response:")); + SerialMon.println(body); - Serial.println(String("Body length is: ") + body.length()); + SerialMon.print(F("Body length is: ")); + SerialMon.println(body.length()); // Shutdown http.stop(); + SerialMon.println(F("Server disconnected")); modem.gprsDisconnect(); - Serial.println("GPRS disconnected"); + SerialMon.println(F("GPRS disconnected")); // Do nothing forevermore while (true) { diff --git a/examples/MqttClient/MqttClient.ino b/examples/MqttClient/MqttClient.ino index 1557f21..b398d15 100644 --- a/examples/MqttClient/MqttClient.ino +++ b/examples/MqttClient/MqttClient.ino @@ -1,12 +1,14 @@ /************************************************************** * * For this example, you need to install PubSubClient library: - * https://github.com/knolleary/pubsubclient/releases/latest + * https://github.com/knolleary/pubsubclient * or from http://librarymanager/all#PubSubClient * * TinyGSM Getting Started guide: * http://tiny.cc/tiny-gsm-readme * + * For more MQTT examples, see PubSubClient library + * ************************************************************** * Use Mosquitto client tools to work with MQTT * Ubuntu/Linux: sudo apt-get install mosquitto-clients @@ -20,7 +22,7 @@ * You can use Node-RED for wiring together MQTT-enabled devices * https://nodered.org/ * Also, take a look at these additional Node-RED modules: - * node-red-contrib-blynk-websockets + * node-red-contrib-blynk-ws * node-red-dashboard * **************************************************************/ @@ -38,11 +40,8 @@ #include #include -// Your GPRS credentials -// Leave empty, if missing user or pass -const char apn[] = "YourAPN"; -const char user[] = ""; -const char pass[] = ""; +// Set serial for debug console (to the Serial Monitor, default speed 115200) +#define SerialMon Serial // Use Hardware Serial on Mega, Leonardo, Micro #define SerialAT Serial1 @@ -51,16 +50,24 @@ const char pass[] = ""; //#include //SoftwareSerial SerialAT(2, 3); // RX, TX -TinyGsm modem(SerialAT); -TinyGsmClient client(modem); -PubSubClient mqtt(client); +// Your GPRS credentials +// Leave empty, if missing user or pass +const char apn[] = "YourAPN"; +const char user[] = ""; +const char pass[] = ""; + +// MQTT details const char* broker = "test.mosquitto.org"; const char* topicLed = "GsmClientTest/led"; const char* topicInit = "GsmClientTest/init"; const char* topicLedStatus = "GsmClientTest/ledStatus"; +TinyGsm modem(SerialAT); +TinyGsmClient client(modem); +PubSubClient mqtt(client); + #define LED_PIN 13 int ledStatus = LOW; @@ -70,7 +77,7 @@ void setup() { pinMode(LED_PIN, OUTPUT); // Set console baud rate - Serial.begin(115200); + SerialMon.begin(115200); delay(10); // Set GSM module baud rate @@ -79,30 +86,30 @@ void setup() { // Restart takes quite some time // To skip it, call init() instead of restart() - Serial.println("Initializing modem..."); + SerialMon.println("Initializing modem..."); modem.restart(); String modemInfo = modem.getModemInfo(); - Serial.print("Modem: "); - Serial.println(modemInfo); + SerialMon.print("Modem: "); + SerialMon.println(modemInfo); // Unlock your SIM card with a PIN //modem.simUnlock("1234"); - Serial.print("Waiting for network..."); + SerialMon.print("Waiting for network..."); if (!modem.waitForNetwork()) { - Serial.println(" fail"); + SerialMon.println(" fail"); while (true); } - Serial.println(" OK"); + SerialMon.println(" OK"); - Serial.print("Connecting to "); - Serial.print(apn); + SerialMon.print("Connecting to "); + SerialMon.print(apn); if (!modem.gprsConnect(apn, user, pass)) { - Serial.println(" fail"); + SerialMon.println(" fail"); while (true); } - Serial.println(" OK"); + SerialMon.println(" OK"); // MQTT Broker setup mqtt.setServer(broker, 1883); @@ -110,13 +117,13 @@ void setup() { } boolean mqttConnect() { - Serial.print("Connecting to "); - Serial.print(broker); + SerialMon.print("Connecting to "); + SerialMon.print(broker); if (!mqtt.connect("GsmClientTest")) { - Serial.println(" fail"); + SerialMon.println(" fail"); return false; } - Serial.println(" OK"); + SerialMon.println(" OK"); mqtt.publish(topicInit, "GsmClientTest started"); mqtt.subscribe(topicLed); return mqtt.connected(); @@ -140,11 +147,11 @@ void loop() { } void mqttCallback(char* topic, byte* payload, unsigned int len) { - Serial.print("Message arrived ["); - Serial.print(topic); - Serial.print("]: "); - Serial.write(payload, len); - Serial.println(); + SerialMon.print("Message arrived ["); + SerialMon.print(topic); + SerialMon.print("]: "); + SerialMon.write(payload, len); + SerialMon.println(); // Only proceed if incoming message's topic matches if (String(topic) == topicLed) { diff --git a/examples/WebClient/WebClient.ino b/examples/WebClient/WebClient.ino index 6f65c93..e764242 100644 --- a/examples/WebClient/WebClient.ino +++ b/examples/WebClient/WebClient.ino @@ -17,13 +17,16 @@ // #define TINY_GSM_MODEM_M590 // #define TINY_GSM_MODEM_ESP8266 +// Increase RX buffer if needed +//#define TINY_GSM_RX_BUFFER 512 + #include -// Your GPRS credentials -// Leave empty, if missing user or pass -const char apn[] = "YourAPN"; -const char user[] = ""; -const char pass[] = ""; +// Uncomment this if you want to see all AT commands +//#define DUMP_AT_COMMANDS + +// Set serial for debug console (to the Serial Monitor, default speed 115200) +#define SerialMon Serial // Use Hardware Serial on Mega, Leonardo, Micro #define SerialAT Serial1 @@ -32,17 +35,31 @@ const char pass[] = ""; //#include //SoftwareSerial SerialAT(2, 3); // RX, TX -TinyGsm modem(SerialAT); + +// Your GPRS credentials +// Leave empty, if missing user or pass +const char apn[] = "YourAPN"; +const char user[] = ""; +const char pass[] = ""; + +// Server details +const char server[] = "vsh.pp.ua"; +const char resource[] = "/TinyGSM/logo.txt"; +const int port = 80; + +#ifdef DUMP_AT_COMMANDS + #include + StreamDebugger debugger(SerialAT, SerialMon); + TinyGsm modem(debugger); +#else + TinyGsm modem(SerialAT); +#endif + TinyGsmClient client(modem); -const char server[] = "cdn.rawgit.com"; -const char resource[] = "/vshymanskyy/tinygsm/master/extras/logo.txt"; - -const int port = 80; - void setup() { // Set console baud rate - Serial.begin(115200); + SerialMon.begin(115200); delay(10); // Set GSM module baud rate @@ -51,43 +68,43 @@ void setup() { // Restart takes quite some time // To skip it, call init() instead of restart() - Serial.println(F("Initializing modem...")); + SerialMon.println(F("Initializing modem...")); modem.restart(); String modemInfo = modem.getModemInfo(); - Serial.print("Modem: "); - Serial.println(modemInfo); + SerialMon.print(F("Modem: ")); + SerialMon.println(modemInfo); // Unlock your SIM card with a PIN //modem.simUnlock("1234"); } void loop() { - Serial.print(F("Waiting for network...")); + SerialMon.print(F("Waiting for network...")); if (!modem.waitForNetwork()) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); + SerialMon.println(" OK"); - Serial.print(F("Connecting to ")); - Serial.print(apn); + SerialMon.print(F("Connecting to ")); + SerialMon.print(apn); if (!modem.gprsConnect(apn, user, pass)) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); + SerialMon.println(" OK"); - Serial.print(F("Connecting to ")); - Serial.print(server); + SerialMon.print(F("Connecting to ")); + SerialMon.print(server); if (!client.connect(server, port)) { - Serial.println(" fail"); + SerialMon.println(" fail"); delay(10000); return; } - Serial.println(" OK"); + SerialMon.println(" OK"); // Make a HTTP GET request: client.print(String("GET ") + resource + " HTTP/1.0\r\n"); @@ -99,17 +116,19 @@ void loop() { // Print available data while (client.available()) { char c = client.read(); - Serial.print(c); + SerialMon.print(c); timeout = millis(); } } - Serial.println(); + SerialMon.println(); + + // Shutdown client.stop(); - Serial.println("Server disconnected"); + SerialMon.println(F("Server disconnected")); modem.gprsDisconnect(); - Serial.println("GPRS disconnected"); + SerialMon.println(F("GPRS disconnected")); // Do nothing forevermore while (true) { diff --git a/examples/more/Industruino/Industruino.ino b/examples/more/Industruino/Industruino.ino new file mode 100644 index 0000000..5e30f86 --- /dev/null +++ b/examples/more/Industruino/Industruino.ino @@ -0,0 +1,160 @@ +/************************************************************** + * + * 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 + * + * For more HTTP API examples, see ArduinoHttpClient library + * + **************************************************************/ + +// Industruino uses SIM800H +#define TINY_GSM_MODEM_SIM800 + +// Increase RX buffer if needed +//#define TINY_GSM_RX_BUFFER 512 + +#include +#include + +// Uncomment this if you want to see all AT commands +//#define DUMP_AT_COMMANDS + +// Uncomment this if you want to use SSL +//#define USE_SSL + +// Set serial for debug console (to the Serial Monitor, speed 115200) +#define SerialMon SerialUSB + +// Select Serial1 or Serial depending on your module configuration +#define SerialAT Serial1 + +// Your GPRS credentials +// Leave empty, if missing user or pass +const char apn[] = "YourAPN"; +const char user[] = ""; +const char pass[] = ""; + +// Server details +const char server[] = "vsh.pp.ua"; +const char resource[] = "/TinyGSM/logo.txt"; + +#ifdef DUMP_AT_COMMANDS + #include + StreamDebugger debugger(SerialAT, SerialMon); + TinyGsm modem(debugger); +#else + TinyGsm modem(SerialAT); +#endif + +#ifdef USE_SSL + TinyGsmClientSecure client(modem); + HttpClient http(client, server, 443); +#else + TinyGsmClient client(modem); + HttpClient http(client, server, 80); +#endif + +void setup() { + // Turn on modem with 1 second pulse on D6 + pinMode(6, OUTPUT); + digitalWrite(6, HIGH); + delay(1000); + digitalWrite(6, LOW); + + // Set console baud rate + SerialMon.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() + SerialMon.println(F("Initializing modem...")); + modem.restart(); + + String modemInfo = modem.getModemInfo(); + SerialMon.print(F("Modem: ")); + SerialMon.println(modemInfo); + + // Unlock your SIM card with a PIN + //modem.simUnlock("1234"); +} + +void loop() { + SerialMon.print(F("Waiting for network...")); + if (!modem.waitForNetwork()) { + SerialMon.println(" fail"); + delay(10000); + return; + } + SerialMon.println(" OK"); + + SerialMon.print(F("Connecting to ")); + SerialMon.print(apn); + if (!modem.gprsConnect(apn, user, pass)) { + SerialMon.println(" fail"); + delay(10000); + return; + } + SerialMon.println(" OK"); + + SerialMon.print(F("Performing HTTP GET request... ")); + int err = http.get(resource); + if (err != 0) { + SerialMon.println(F("failed to connect")); + delay(10000); + return; + } + + int status = http.responseStatusCode(); + SerialMon.println(status); + if (!status) { + delay(10000); + return; + } + + while (http.headerAvailable()) { + String headerName = http.readHeaderName(); + String headerValue = http.readHeaderValue(); + //SerialMon.println(headerName + " : " + headerValue); + } + + int length = http.contentLength(); + if (length >= 0) { + SerialMon.print(F("Content length is: ")); + SerialMon.println(length); + } + if (http.isResponseChunked()) { + SerialMon.println(F("The response is chunked")); + } + + String body = http.responseBody(); + SerialMon.println(F("Response:")); + SerialMon.println(body); + + SerialMon.print(F("Body length is: ")); + SerialMon.println(body.length()); + + // Shutdown + + http.stop(); + SerialMon.println(F("Server disconnected")); + + modem.gprsDisconnect(); + SerialMon.println(F("GPRS disconnected")); + + // Do nothing forevermore + while (true) { + delay(1000); + } +} + diff --git a/examples/more/SIM800_SslSetCert/COMODORSACertificationAuthority.h b/examples/more/SIM800_SslSetCert/COMODORSACertificationAuthority.h new file mode 100644 index 0000000..514db99 --- /dev/null +++ b/examples/more/SIM800_SslSetCert/COMODORSACertificationAuthority.h @@ -0,0 +1,36 @@ +const char cert[] PROGMEM = +"-----BEGIN CERTIFICATE-----\n" +"MIIF2DCCA8CgAwIBAgIQTKr5yttjb+Af907YWwOGnTANBgkqhkiG9w0BAQwFADCB\n" +"hTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4G\n" +"A1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNV\n" +"BAMTIkNPTU9ETyBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAwMTE5\n" +"MDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgT\n" +"EkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMR\n" +"Q09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBSU0EgQ2VydGlmaWNh\n" +"dGlvbiBBdXRob3JpdHkwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCR\n" +"6FSS0gpWsawNJN3Fz0RndJkrN6N9I3AAcbxT38T6KhKPS38QVr2fcHK3YX/JSw8X\n" +"pz3jsARh7v8Rl8f0hj4K+j5c+ZPmNHrZFGvnnLOFoIJ6dq9xkNfs/Q36nGz637CC\n" +"9BR++b7Epi9Pf5l/tfxnQ3K9DADWietrLNPtj5gcFKt+5eNu/Nio5JIk2kNrYrhV\n" +"/erBvGy2i/MOjZrkm2xpmfh4SDBF1a3hDTxFYPwyllEnvGfDyi62a+pGx8cgoLEf\n" +"Zd5ICLqkTqnyg0Y3hOvozIFIQ2dOciqbXL1MGyiKXCJ7tKuY2e7gUYPDCUZObT6Z\n" +"+pUX2nwzV0E8jVHtC7ZcryxjGt9XyD+86V3Em69FmeKjWiS0uqlWPc9vqv9JWL7w\n" +"qP/0uK3pN/u6uPQLOvnoQ0IeidiEyxPx2bvhiWC4jChWrBQdnArncevPDt09qZah\n" +"SL0896+1DSJMwBGB7FY79tOi4lu3sgQiUpWAk2nojkxl8ZEDLXB0AuqLZxUpaVIC\n" +"u9ffUGpVRr+goyhhf3DQw6KqLCGqR84onAZFdr+CGCe01a60y1Dma/RMhnEw6abf\n" +"Fobg2P9A3fvQQoh/ozM6LlweQRGBY84YcWsr7KaKtzFcOmpH4MN5WdYgGq/yapiq\n" +"crxXStJLnbsQ/LBMQeXtHT1eKJ2czL+zUdqnR+WEUwIDAQABo0IwQDAdBgNVHQ4E\n" +"FgQUu69+Aj36pvE8hI6t7jiY7NkyMtQwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB\n" +"/wQFMAMBAf8wDQYJKoZIhvcNAQEMBQADggIBAArx1UaEt65Ru2yyTUEUAJNMnMvl\n" +"wFTPoCWOAvn9sKIN9SCYPBMtrFaisNZ+EZLpLrqeLppysb0ZRGxhNaKatBYSaVqM\n" +"4dc+pBroLwP0rmEdEBsqpIt6xf4FpuHA1sj+nq6PK7o9mfjYcwlYRm6mnPTXJ9OV\n" +"2jeDchzTc+CiR5kDOF3VSXkAKRzH7JsgHAckaVd4sjn8OoSgtZx8jb8uk2Intzna\n" +"FxiuvTwJaP+EmzzV1gsD41eeFPfR60/IvYcjt7ZJQ3mFXLrrkguhxuhoqEwWsRqZ\n" +"CuhTLJK7oQkYdQxlqHvLI7cawiiFwxv/0Cti76R7CZGYZ4wUAc1oBmpjIXUDgIiK\n" +"boHGhfKppC3n9KUkEEeDys30jXlYsQab5xoq2Z0B15R97QNKyvDb6KkBPvVWmcke\n" +"jkk9u+UJueBPSZI9FoJAzMxZxuY67RIuaTxslbH9qh17f4a+Hg4yRvv7E491f0yL\n" +"S0Zj/gA0QHDBw7mh3aZw4gSzQbzpgJHqZJx64SIDqZxubw5lT2yHh17zbqD5daWb\n" +"QOhTsiedSrnAdyGN/4fy3ryM7xfft0kL0fJuMAsaDk527RH89elWsn2/x20Kk4yl\n" +"0MC2Hb46TpSi125sC8KKfPog88Tk5c0NqMuRkrF8hey1FGlmDoLnzc7ILaZRfyHB\n" +"NVOFBkpdn627G190\n" +"-----END CERTIFICATE-----\n"; + diff --git a/examples/more/SIM800_SslSetCert/DSTRootCAX3.der.h b/examples/more/SIM800_SslSetCert/DSTRootCAX3.der.h new file mode 100644 index 0000000..15063a3 --- /dev/null +++ b/examples/more/SIM800_SslSetCert/DSTRootCAX3.der.h @@ -0,0 +1,74 @@ +const char cert[] PROGMEM = +{ + 0x30, 0x82, 0x03, 0x4a, 0x30, 0x82, 0x02, 0x32, 0xa0, 0x03, 0x02, 0x01, + 0x02, 0x02, 0x10, 0x44, 0xaf, 0xb0, 0x80, 0xd6, 0xa3, 0x27, 0xba, 0x89, + 0x30, 0x39, 0x86, 0x2e, 0xf8, 0x40, 0x6b, 0x30, 0x0d, 0x06, 0x09, 0x2a, + 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x3f, + 0x31, 0x24, 0x30, 0x22, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x1b, 0x44, + 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c, 0x20, 0x53, 0x69, 0x67, 0x6e, 0x61, + 0x74, 0x75, 0x72, 0x65, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x43, + 0x6f, 0x2e, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, + 0x0e, 0x44, 0x53, 0x54, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, + 0x20, 0x58, 0x33, 0x30, 0x1e, 0x17, 0x0d, 0x30, 0x30, 0x30, 0x39, 0x33, + 0x30, 0x32, 0x31, 0x31, 0x32, 0x31, 0x39, 0x5a, 0x17, 0x0d, 0x32, 0x31, + 0x30, 0x39, 0x33, 0x30, 0x31, 0x34, 0x30, 0x31, 0x31, 0x35, 0x5a, 0x30, + 0x3f, 0x31, 0x24, 0x30, 0x22, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x1b, + 0x44, 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c, 0x20, 0x53, 0x69, 0x67, 0x6e, + 0x61, 0x74, 0x75, 0x72, 0x65, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, + 0x43, 0x6f, 0x2e, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x03, + 0x13, 0x0e, 0x44, 0x53, 0x54, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, + 0x41, 0x20, 0x58, 0x33, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, + 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, + 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, + 0x00, 0xdf, 0xaf, 0xe9, 0x97, 0x50, 0x08, 0x83, 0x57, 0xb4, 0xcc, 0x62, + 0x65, 0xf6, 0x90, 0x82, 0xec, 0xc7, 0xd3, 0x2c, 0x6b, 0x30, 0xca, 0x5b, + 0xec, 0xd9, 0xc3, 0x7d, 0xc7, 0x40, 0xc1, 0x18, 0x14, 0x8b, 0xe0, 0xe8, + 0x33, 0x76, 0x49, 0x2a, 0xe3, 0x3f, 0x21, 0x49, 0x93, 0xac, 0x4e, 0x0e, + 0xaf, 0x3e, 0x48, 0xcb, 0x65, 0xee, 0xfc, 0xd3, 0x21, 0x0f, 0x65, 0xd2, + 0x2a, 0xd9, 0x32, 0x8f, 0x8c, 0xe5, 0xf7, 0x77, 0xb0, 0x12, 0x7b, 0xb5, + 0x95, 0xc0, 0x89, 0xa3, 0xa9, 0xba, 0xed, 0x73, 0x2e, 0x7a, 0x0c, 0x06, + 0x32, 0x83, 0xa2, 0x7e, 0x8a, 0x14, 0x30, 0xcd, 0x11, 0xa0, 0xe1, 0x2a, + 0x38, 0xb9, 0x79, 0x0a, 0x31, 0xfd, 0x50, 0xbd, 0x80, 0x65, 0xdf, 0xb7, + 0x51, 0x63, 0x83, 0xc8, 0xe2, 0x88, 0x61, 0xea, 0x4b, 0x61, 0x81, 0xec, + 0x52, 0x6b, 0xb9, 0xa2, 0xe2, 0x4b, 0x1a, 0x28, 0x9f, 0x48, 0xa3, 0x9e, + 0x0c, 0xda, 0x09, 0x8e, 0x3e, 0x17, 0x2e, 0x1e, 0xdd, 0x20, 0xdf, 0x5b, + 0xc6, 0x2a, 0x8a, 0xab, 0x2e, 0xbd, 0x70, 0xad, 0xc5, 0x0b, 0x1a, 0x25, + 0x90, 0x74, 0x72, 0xc5, 0x7b, 0x6a, 0xab, 0x34, 0xd6, 0x30, 0x89, 0xff, + 0xe5, 0x68, 0x13, 0x7b, 0x54, 0x0b, 0xc8, 0xd6, 0xae, 0xec, 0x5a, 0x9c, + 0x92, 0x1e, 0x3d, 0x64, 0xb3, 0x8c, 0xc6, 0xdf, 0xbf, 0xc9, 0x41, 0x70, + 0xec, 0x16, 0x72, 0xd5, 0x26, 0xec, 0x38, 0x55, 0x39, 0x43, 0xd0, 0xfc, + 0xfd, 0x18, 0x5c, 0x40, 0xf1, 0x97, 0xeb, 0xd5, 0x9a, 0x9b, 0x8d, 0x1d, + 0xba, 0xda, 0x25, 0xb9, 0xc6, 0xd8, 0xdf, 0xc1, 0x15, 0x02, 0x3a, 0xab, + 0xda, 0x6e, 0xf1, 0x3e, 0x2e, 0xf5, 0x5c, 0x08, 0x9c, 0x3c, 0xd6, 0x83, + 0x69, 0xe4, 0x10, 0x9b, 0x19, 0x2a, 0xb6, 0x29, 0x57, 0xe3, 0xe5, 0x3d, + 0x9b, 0x9f, 0xf0, 0x02, 0x5d, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x42, + 0x30, 0x40, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, + 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0e, 0x06, 0x03, 0x55, + 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04, 0x04, 0x03, 0x02, 0x01, 0x06, 0x30, + 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xc4, 0xa7, + 0xb1, 0xa4, 0x7b, 0x2c, 0x71, 0xfa, 0xdb, 0xe1, 0x4b, 0x90, 0x75, 0xff, + 0xc4, 0x15, 0x60, 0x85, 0x89, 0x10, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, + 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x82, 0x01, + 0x01, 0x00, 0xa3, 0x1a, 0x2c, 0x9b, 0x17, 0x00, 0x5c, 0xa9, 0x1e, 0xee, + 0x28, 0x66, 0x37, 0x3a, 0xbf, 0x83, 0xc7, 0x3f, 0x4b, 0xc3, 0x09, 0xa0, + 0x95, 0x20, 0x5d, 0xe3, 0xd9, 0x59, 0x44, 0xd2, 0x3e, 0x0d, 0x3e, 0xbd, + 0x8a, 0x4b, 0xa0, 0x74, 0x1f, 0xce, 0x10, 0x82, 0x9c, 0x74, 0x1a, 0x1d, + 0x7e, 0x98, 0x1a, 0xdd, 0xcb, 0x13, 0x4b, 0xb3, 0x20, 0x44, 0xe4, 0x91, + 0xe9, 0xcc, 0xfc, 0x7d, 0xa5, 0xdb, 0x6a, 0xe5, 0xfe, 0xe6, 0xfd, 0xe0, + 0x4e, 0xdd, 0xb7, 0x00, 0x3a, 0xb5, 0x70, 0x49, 0xaf, 0xf2, 0xe5, 0xeb, + 0x02, 0xf1, 0xd1, 0x02, 0x8b, 0x19, 0xcb, 0x94, 0x3a, 0x5e, 0x48, 0xc4, + 0x18, 0x1e, 0x58, 0x19, 0x5f, 0x1e, 0x02, 0x5a, 0xf0, 0x0c, 0xf1, 0xb1, + 0xad, 0xa9, 0xdc, 0x59, 0x86, 0x8b, 0x6e, 0xe9, 0x91, 0xf5, 0x86, 0xca, + 0xfa, 0xb9, 0x66, 0x33, 0xaa, 0x59, 0x5b, 0xce, 0xe2, 0xa7, 0x16, 0x73, + 0x47, 0xcb, 0x2b, 0xcc, 0x99, 0xb0, 0x37, 0x48, 0xcf, 0xe3, 0x56, 0x4b, + 0xf5, 0xcf, 0x0f, 0x0c, 0x72, 0x32, 0x87, 0xc6, 0xf0, 0x44, 0xbb, 0x53, + 0x72, 0x6d, 0x43, 0xf5, 0x26, 0x48, 0x9a, 0x52, 0x67, 0xb7, 0x58, 0xab, + 0xfe, 0x67, 0x76, 0x71, 0x78, 0xdb, 0x0d, 0xa2, 0x56, 0x14, 0x13, 0x39, + 0x24, 0x31, 0x85, 0xa2, 0xa8, 0x02, 0x5a, 0x30, 0x47, 0xe1, 0xdd, 0x50, + 0x07, 0xbc, 0x02, 0x09, 0x90, 0x00, 0xeb, 0x64, 0x63, 0x60, 0x9b, 0x16, + 0xbc, 0x88, 0xc9, 0x12, 0xe6, 0xd2, 0x7d, 0x91, 0x8b, 0xf9, 0x3d, 0x32, + 0x8d, 0x65, 0xb4, 0xe9, 0x7c, 0xb1, 0x57, 0x76, 0xea, 0xc5, 0xb6, 0x28, + 0x39, 0xbf, 0x15, 0x65, 0x1c, 0xc8, 0xf6, 0x77, 0x96, 0x6a, 0x0a, 0x8d, + 0x77, 0x0b, 0xd8, 0x91, 0x0b, 0x04, 0x8e, 0x07, 0xdb, 0x29, 0xb6, 0x0a, + 0xee, 0x9d, 0x82, 0x35, 0x35, 0x10 +}; diff --git a/examples/more/SIM800_SslSetCert/DSTRootCAX3.h b/examples/more/SIM800_SslSetCert/DSTRootCAX3.h new file mode 100644 index 0000000..55e79d9 --- /dev/null +++ b/examples/more/SIM800_SslSetCert/DSTRootCAX3.h @@ -0,0 +1,22 @@ +const char cert[] PROGMEM = +"-----BEGIN CERTIFICATE-----\n" +"MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n" +"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" +"DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n" +"PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n" +"Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n" +"AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n" +"rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n" +"OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n" +"xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n" +"7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\n" +"aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n" +"HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n" +"SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n" +"ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\n" +"AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n" +"R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n" +"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\n" +"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n" +"-----END CERTIFICATE-----\n"; + diff --git a/examples/more/SIM800_SslSetCert/SIM800_SslSetCert.ino b/examples/more/SIM800_SslSetCert/SIM800_SslSetCert.ino new file mode 100644 index 0000000..a25babd --- /dev/null +++ b/examples/more/SIM800_SslSetCert/SIM800_SslSetCert.ino @@ -0,0 +1,97 @@ +/************************************************************** + * + * This sketch uploads SSL certificates to the SIM8xx + * + * TinyGSM Getting Started guide: + * http://tiny.cc/tiny-gsm-readme + * + **************************************************************/ + +// This example is specific to SIM8xx +#define TINY_GSM_MODEM_SIM800 + +// Select your certificate: +#include "DSTRootCAX3.h" +//#include "DSTRootCAX3.der.h" +//#include "COMODORSACertificationAuthority.h" + +// Select the file you want to write into +// (the file is stored on the modem) +#define CERT_FILE "C:\\USER\\CERT.CRT" + +#include + +// Set serial for debug console (to the Serial Monitor, speed 115200) +#define SerialMon Serial + +// Use Hardware Serial for AT commands +#define SerialAT Serial1 + +// Uncomment this if you want to see all AT commands +//#define DUMP_AT_COMMANDS + + +#ifdef DUMP_AT_COMMANDS + #include + StreamDebugger debugger(SerialAT, SerialMon); + TinyGsm modem(debugger); +#else + TinyGsm modem(SerialAT); +#endif + +void setup() { + // Set console baud rate + SerialMon.begin(115200); + delay(10); + + // Set GSM module baud rate + SerialAT.begin(115200); + delay(3000); + + SerialMon.println(F("Initializing modem...")); + modem.init(); + + modem.sendAT(GF("+FSCREATE=" CERT_FILE)); + if (modem.waitResponse() != 1) return; + + const int cert_size = sizeof(cert); + + modem.sendAT(GF("+FSWRITE=" CERT_FILE ",0,"), cert_size, GF(",10")); + if (modem.waitResponse(GF(">")) != 1) { + return; + } + + for (int i = 0; i < cert_size; i++) { + char c = pgm_read_byte(&cert[i]); + modem.stream.write(c); + } + + modem.stream.write(GSM_NL); + modem.stream.flush(); + + if (modem.waitResponse(2000) != 1) return; + + modem.sendAT(GF("+SSLSETCERT=\"" CERT_FILE "\"")); + if (modem.waitResponse() != 1) return; + if (modem.waitResponse(5000L, GF(GSM_NL "+SSLSETCERT:")) != 1) return; + const int retCode = modem.stream.readStringUntil('\n').toInt(); + + + SerialMon.println(); + SerialMon.println(); + SerialMon.println(F("****************************")); + SerialMon.print(F("Setting Certificate: ")); + SerialMon.println((0 == retCode) ? "OK" : "FAILED"); + SerialMon.println(F("****************************")); +} + +void loop() { + if (SerialAT.available()) { + SerialMon.write(SerialAT.read()); + } + if (SerialMon.available()) { + SerialAT.write(SerialMon.read()); + } + delay(0); +} + diff --git a/extras/doc/SIM800 AT Commands v1.10.pdf b/extras/doc/SIM800 AT Commands v1.10.pdf new file mode 100644 index 0000000..0ec8983 Binary files /dev/null and b/extras/doc/SIM800 AT Commands v1.10.pdf differ diff --git a/extras/doc/SIM800 Series_SSL_Application Note_V1.01.pdf b/extras/doc/SIM800 Series_SSL_Application Note_V1.01.pdf new file mode 100644 index 0000000..7c456d0 Binary files /dev/null and b/extras/doc/SIM800 Series_SSL_Application Note_V1.01.pdf differ diff --git a/library.json b/library.json index 3dc2093..d491bea 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "TinyGSM", - "version": "0.3.13", + "version": "0.3.20", "description": "A small Arduino library for GPRS modules, that just works. Includes examples for Blynk, MQTT, File Download, and Web Client. Supports GSM modules with AT command interface: SIM800, SIM800A, SIM800C, SIM800L, SIM800H, SIM808, SIM868, SIM900, SIM900A, SIM900D, SIM908, SIM968", "keywords": "GSM, AT commands, AT, SIM800, SIM900, A6, A7, M590, ESP8266, SIM800A, SIM800C, SIM800L, SIM800H, SIM808, SIM868, SIM900A, SIM900D, SIM908, SIM968", "authors": diff --git a/library.properties b/library.properties index eb3f063..36a01da 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TinyGSM -version=0.3.13 +version=0.3.20 author=Volodymyr Shymanskyy maintainer=Volodymyr Shymanskyy sentence=A small Arduino library for GPRS modules, that just works. diff --git a/src/TinyGsmClientA6.h b/src/TinyGsmClientA6.h index e48351c..3d87d29 100644 --- a/src/TinyGsmClientA6.h +++ b/src/TinyGsmClientA6.h @@ -82,6 +82,7 @@ public: public: virtual int connect(const char *host, uint16_t port) { + stop(); TINY_GSM_YIELD(); rx.clear(); uint8_t newMux = -1; @@ -110,6 +111,7 @@ public: at->sendAT(GF("+CIPCLOSE="), mux); sock_connected = false; at->waitResponse(); + rx.clear(); } virtual size_t write(const uint8_t *buf, size_t size) { @@ -252,9 +254,7 @@ public: } void maintain() { - //while (stream.available()) { - waitResponse(10, NULL, NULL); - //} + waitResponse(10, NULL, NULL); } bool factoryDefault() { @@ -276,7 +276,9 @@ public: return res; } - bool hasSSL() { return false; } + bool hasSSL() { + return false; + } /* * Power functions @@ -427,7 +429,7 @@ public: /* * GPRS functions */ - bool gprsConnect(const char* apn, const char* user, const char* pwd) { + bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) { gprsDisconnect(); sendAT(GF("+CGATT=1")); @@ -458,8 +460,10 @@ public: } bool gprsDisconnect() { + // Shut the TCP/IP connection sendAT(GF("+CIPSHUT")); - waitResponse(5000L); + if (waitResponse(60000L) != 1) + return false; for (int i = 0; i<3; i++) { sendAT(GF("+CGATT=0")); @@ -572,12 +576,12 @@ protected: int modemSend(const void* buff, size_t len, uint8_t mux) { sendAT(GF("+CIPSEND="), mux, ',', len); if (waitResponse(2000L, GF(GSM_NL ">")) != 1) { - return -1; + return 0; } stream.write((uint8_t*)buff, len); stream.flush(); if (waitResponse(10000L, GFP(GSM_OK), GF(GSM_NL "FAIL")) != 1) { - return -1; + return 0; } return len; } @@ -713,8 +717,10 @@ finish: return waitResponse(1000, r1, r2, r3, r4, r5); } -protected: +public: Stream& stream; + +protected: GsmClient* sockets[TINY_GSM_MUX_COUNT]; }; diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index 6dc2a1c..4998de7 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -80,6 +80,7 @@ public: public: virtual int connect(const char *host, uint16_t port) { + stop(); TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); @@ -103,6 +104,7 @@ public: at->sendAT(GF("+CIPCLOSE="), mux); sock_connected = false; at->waitResponse(); + rx.clear(); } virtual size_t write(const uint8_t *buf, size_t size) { @@ -194,6 +196,7 @@ public: public: virtual int connect(const char *host, uint16_t port) { + stop(); TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux, true); @@ -255,7 +258,7 @@ public: } void maintain() { - waitResponse(10, NULL, NULL); + waitResponse(10, NULL, NULL); } bool factoryDefault() { @@ -443,12 +446,12 @@ protected: int modemSend(const void* buff, size_t len, uint8_t mux) { sendAT(GF("+CIPSEND="), mux, ',', len); if (waitResponse(GF(">")) != 1) { - return -1; + return 0; } stream.write((uint8_t*)buff, len); stream.flush(); if (waitResponse(10000L, GF(GSM_NL "SEND OK" GSM_NL)) != 1) { - return -1; + return 0; } return len; } @@ -584,8 +587,10 @@ finish: return waitResponse(1000, r1, r2, r3, r4, r5); } -protected: +public: Stream& stream; + +protected: GsmClient* sockets[TINY_GSM_MUX_COUNT]; }; diff --git a/src/TinyGsmClientM590.h b/src/TinyGsmClientM590.h index e01d0e9..cbe95b6 100644 --- a/src/TinyGsmClientM590.h +++ b/src/TinyGsmClientM590.h @@ -80,6 +80,7 @@ public: public: virtual int connect(const char *host, uint16_t port) { + stop(); TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); @@ -103,6 +104,7 @@ public: at->sendAT(GF("+TCPCLOSE="), mux); sock_connected = false; at->waitResponse(); + rx.clear(); } virtual size_t write(const uint8_t *buf, size_t size) { @@ -273,7 +275,9 @@ public: return res; } - bool hasSSL() { return false; } + bool hasSSL() { + return false; + } /* * Power functions @@ -428,7 +432,7 @@ public: /* * GPRS functions */ - bool gprsConnect(const char* apn, const char* user = "", const char* pwd = "") { + bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) { gprsDisconnect(); sendAT(GF("+XISP=0")); @@ -724,8 +728,10 @@ finish: return waitResponse(1000, r1, r2, r3, r4, r5); } -protected: +public: Stream& stream; + +protected: GsmClient* sockets[TINY_GSM_MUX_COUNT]; }; diff --git a/src/TinyGsmClientSIM800.h b/src/TinyGsmClientSIM800.h index 12af3f1..039218f 100644 --- a/src/TinyGsmClientSIM800.h +++ b/src/TinyGsmClientSIM800.h @@ -85,6 +85,7 @@ public: public: virtual int connect(const char *host, uint16_t port) { + stop(); TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); @@ -108,6 +109,7 @@ public: at->sendAT(GF("+CIPCLOSE="), mux); sock_connected = false; at->waitResponse(); + rx.clear(); } virtual size_t write(const uint8_t *buf, size_t size) { @@ -211,6 +213,7 @@ public: public: virtual int connect(const char *host, uint16_t port) { + stop(); TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux, true); @@ -320,11 +323,15 @@ public: } bool hasSSL() { +#if defined(TINY_GSM_MODEM_SIM900) + return false; +#else sendAT(GF("+CIPSSL=?")); if (waitResponse(GF(GSM_NL "+CIPSSL:")) != 1) { return false; } return waitResponse() == 1; +#endif } /* @@ -495,7 +502,7 @@ public: /* * GPRS functions */ - bool gprsConnect(const char* apn, const char* user = "", const char* pwd = "") { + bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) { gprsDisconnect(); // Set the Bearer for the IP @@ -583,7 +590,8 @@ public: } bool gprsDisconnect() { - sendAT(GF("+CIPSHUT")); // Shut the TCP/IP connection + // Shut the TCP/IP connection + sendAT(GF("+CIPSHUT")); if (waitResponse(60000L) != 1) return false; @@ -644,6 +652,7 @@ public: bool sendSMS(const String& number, const String& text) { sendAT(GF("+CMGF=1")); waitResponse(); + //Set GSM 7 bit default alphabet (3GPP TS 23.038) sendAT(GF("+CSCS=\"GSM\"")); waitResponse(); sendAT(GF("+CMGS=\""), number, GF("\"")); @@ -731,28 +740,33 @@ public: protected: bool modemConnect(const char* host, uint16_t port, uint8_t mux, bool ssl = false) { +#if !defined(TINY_GSM_MODEM_SIM900) sendAT(GF("+CIPSSL="), ssl); int rsp = waitResponse(); if (ssl && rsp != 1) { return false; } +#endif sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port); rsp = waitResponse(75000L, GF("CONNECT OK" GSM_NL), GF("CONNECT FAIL" GSM_NL), - GF("ALREADY CONNECT" GSM_NL)); + GF("ALREADY CONNECT" GSM_NL), + GF("ERROR" GSM_NL), + GF("CLOSE OK" GSM_NL) // Happens when HTTPS handshake fails + ); return (1 == rsp); } int modemSend(const void* buff, size_t len, uint8_t mux) { sendAT(GF("+CIPSEND="), mux, ',', len); if (waitResponse(GF(">")) != 1) { - return -1; + return 0; } stream.write((uint8_t*)buff, len); stream.flush(); if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) { - return -1; + return 0; } streamSkipUntil(','); // Skip mux return stream.readStringUntil('\n').toInt(); @@ -934,8 +948,10 @@ finish: return waitResponse(1000, r1, r2, r3, r4, r5); } -protected: +public: Stream& stream; + +protected: GsmClient* sockets[TINY_GSM_MUX_COUNT]; }; diff --git a/src/TinyGsmClientU201.h b/src/TinyGsmClientU201.h index d120be2..910f21b 100644 --- a/src/TinyGsmClientU201.h +++ b/src/TinyGsmClientU201.h @@ -81,6 +81,7 @@ public: public: virtual int connect(const char *host, uint16_t port) { + stop(); TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, &mux); @@ -105,6 +106,7 @@ public: at->sendAT(GF("+USOCL="), mux); sock_connected = false; at->waitResponse(); + rx.clear(); } virtual size_t write(const uint8_t *buf, size_t size) { @@ -119,7 +121,7 @@ public: virtual int available() { TINY_GSM_YIELD(); - if (!rx.size() && sock_connected) { // TODO + if (!rx.size() && sock_connected) { at->maintain(); } return rx.size() + sock_available; @@ -200,6 +202,7 @@ public: public: virtual int connect(const char *host, uint16_t port) { + stop(); TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, &mux, true); @@ -287,7 +290,9 @@ public: String getModemInfo() TINY_GSM_ATTR_NOT_IMPLEMENTED; - bool hasSSL() { return true; } + bool hasSSL() { + return true; + } /* * Power functions @@ -433,7 +438,7 @@ public: /* * GPRS functions */ - bool gprsConnect(const char* apn, const char* user = "", const char* pwd = "") { + bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) { gprsDisconnect(); sendAT(GF("+CGATT=1")); @@ -732,8 +737,10 @@ finish: return waitResponse(1000, r1, r2, r3, r4, r5); } -protected: +public: Stream& stream; + +protected: GsmClient* sockets[TINY_GSM_MUX_COUNT]; }; diff --git a/src/TinyGsmClientXBee.h b/src/TinyGsmClientXBee.h index bb576ff..ec050fb 100644 --- a/src/TinyGsmClientXBee.h +++ b/src/TinyGsmClientXBee.h @@ -9,7 +9,7 @@ #ifndef TinyGsmClientXBee_h #define TinyGsmClientXBee_h -// #define TINY_GSM_DEBUG Serial +//#define TINY_GSM_DEBUG Serial #define TINY_GSM_MUX_COUNT 1 // Multi-plexing isn't supported using command mode @@ -597,7 +597,7 @@ fail: /* * GPRS functions */ - bool gprsConnect(const char* apn, const char* user = "", const char* pwd = "") { + bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) { if (!commandMode()) return false; // Return immediately sendAT(GF("AN"), apn); // Set the APN waitResponse(); @@ -626,9 +626,7 @@ fail: * Messaging functions */ - void sendUSSD() TINY_GSM_ATTR_NOT_AVAILABLE; - - void sendSMS() TINY_GSM_ATTR_NOT_IMPLEMENTED; + String sendUSSD(const String& code) TINY_GSM_ATTR_NOT_IMPLEMENTED; bool sendSMS(const String& number, const String& text) { if (!commandMode()) return false; // Return immediately @@ -870,10 +868,12 @@ finish: return waitResponse(1000, r1, r2, r3, r4, r5); } +public: + Stream& stream; + protected: int guardTime; XBeeType beeType; - Stream& stream; GsmClient* sockets[TINY_GSM_MUX_COUNT]; }; diff --git a/tools/AT_Debug/AT_Debug.ino b/tools/AT_Debug/AT_Debug.ino index d495f5d..56b63f7 100644 --- a/tools/AT_Debug/AT_Debug.ino +++ b/tools/AT_Debug/AT_Debug.ino @@ -18,8 +18,6 @@ // #define TINY_GSM_MODEM_ESP8266 // #define TINY_GSM_MODEM_XBEE -#include - // Set serial for debug console (to the Serial Monitor, speed 115200) #define SerialMon Serial diff --git a/tools/AT_Spy/AT_Spy.ino b/tools/AT_Spy/AT_Spy.ino index 93ab3d6..a427918 100644 --- a/tools/AT_Spy/AT_Spy.ino +++ b/tools/AT_Spy/AT_Spy.ino @@ -1,7 +1,7 @@ /************************************************************** * - * This script just listens in on the communication between an Arduino and the - * modem. + * This script just listens in on the communication + * between an Arduino and the modem. * * TinyGSM Getting Started guide: * http://tiny.cc/tiny-gsm-readme @@ -28,18 +28,20 @@ AltSoftSerial BOARD_TX; void setup() { // Set console baud rate - SPY.begin(57600); + SPY.begin(115200); + MODEM_TX.begin(BAUD_RATE); BOARD_TX.begin(BAUD_RATE); - delay(5000); + delay(1000); } void loop() { - while (MODEM_TX.available()) { - SPY.write(MODEM_TX.read()); - } - while (BOARD_TX.available()) { - SPY.write(BOARD_TX.read()); - } + while (MODEM_TX.available()) { + SPY.write(MODEM_TX.read()); + } + while (BOARD_TX.available()) { + SPY.write(BOARD_TX.read()); + } } +