From cd0d7c16b6b43aee0d486ab5c62f64a6a1f09eaf Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Fri, 1 Feb 2019 19:10:19 +0200 Subject: [PATCH 1/6] Update links --- examples/AllFunctions/AllFunctions.ino | 2 +- examples/BlynkClient/BlynkClient.ino | 2 +- examples/FileDownload/FileDownload.ino | 2 +- examples/HttpClient/HttpClient.ino | 2 +- examples/HttpsClient/HttpsClient.ino | 2 +- examples/MqttClient/MqttClient.ino | 2 +- examples/WebClient/WebClient.ino | 2 +- examples/more/Hologram_Dash/Hologram_Dash.ino | 2 +- examples/more/Industruino/Industruino.ino | 2 +- examples/more/SIM800_SslSetCert/SIM800_SslSetCert.ino | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/AllFunctions/AllFunctions.ino b/examples/AllFunctions/AllFunctions.ino index 573e279..a17f39f 100644 --- a/examples/AllFunctions/AllFunctions.ino +++ b/examples/AllFunctions/AllFunctions.ino @@ -1,7 +1,7 @@ /************************************************************** * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * * NOTE: * Some of the functions may be unavailable for your modem. diff --git a/examples/BlynkClient/BlynkClient.ino b/examples/BlynkClient/BlynkClient.ino index d3d7ce3..c8f1af7 100644 --- a/examples/BlynkClient/BlynkClient.ino +++ b/examples/BlynkClient/BlynkClient.ino @@ -4,7 +4,7 @@ * https://github.com/blynkkk/blynk-library/releases/latest * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * ************************************************************** * diff --git a/examples/FileDownload/FileDownload.ino b/examples/FileDownload/FileDownload.ino index 19c73a6..b77c04d 100644 --- a/examples/FileDownload/FileDownload.ino +++ b/examples/FileDownload/FileDownload.ino @@ -5,7 +5,7 @@ * or from http://librarymanager/all#CRC32+checksum * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * * ATTENTION! Downloading big files requires of knowledge of * the TinyGSM internals and some modem specifics, diff --git a/examples/HttpClient/HttpClient.ino b/examples/HttpClient/HttpClient.ino index 95b3453..39da935 100644 --- a/examples/HttpClient/HttpClient.ino +++ b/examples/HttpClient/HttpClient.ino @@ -8,7 +8,7 @@ * or from http://librarymanager/all#ArduinoHttpClient * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * * For more HTTP API examples, see ArduinoHttpClient library * diff --git a/examples/HttpsClient/HttpsClient.ino b/examples/HttpsClient/HttpsClient.ino index bd892d7..fdef7b5 100644 --- a/examples/HttpsClient/HttpsClient.ino +++ b/examples/HttpsClient/HttpsClient.ino @@ -8,7 +8,7 @@ * or from http://librarymanager/all#ArduinoHttpClient * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * * SSL/TLS is currently supported only with: SIM8xx, uBlox * diff --git a/examples/MqttClient/MqttClient.ino b/examples/MqttClient/MqttClient.ino index da5933a..3ca89d5 100644 --- a/examples/MqttClient/MqttClient.ino +++ b/examples/MqttClient/MqttClient.ino @@ -5,7 +5,7 @@ * or from http://librarymanager/all#PubSubClient * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * * For more MQTT examples, see PubSubClient library * diff --git a/examples/WebClient/WebClient.ino b/examples/WebClient/WebClient.ino index e1127fd..bc1fd54 100644 --- a/examples/WebClient/WebClient.ino +++ b/examples/WebClient/WebClient.ino @@ -4,7 +4,7 @@ * It can be used to perform HTTP/RESTful API calls. * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * **************************************************************/ diff --git a/examples/more/Hologram_Dash/Hologram_Dash.ino b/examples/more/Hologram_Dash/Hologram_Dash.ino index 532fee4..2f195da 100644 --- a/examples/more/Hologram_Dash/Hologram_Dash.ino +++ b/examples/more/Hologram_Dash/Hologram_Dash.ino @@ -4,7 +4,7 @@ * It can be used to perform HTTP/RESTful API calls. * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * **************************************************************/ diff --git a/examples/more/Industruino/Industruino.ino b/examples/more/Industruino/Industruino.ino index 5e30f86..3757f0c 100644 --- a/examples/more/Industruino/Industruino.ino +++ b/examples/more/Industruino/Industruino.ino @@ -8,7 +8,7 @@ * or from http://librarymanager/all#ArduinoHttpClient * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * * For more HTTP API examples, see ArduinoHttpClient library * diff --git a/examples/more/SIM800_SslSetCert/SIM800_SslSetCert.ino b/examples/more/SIM800_SslSetCert/SIM800_SslSetCert.ino index a25babd..e2a679d 100644 --- a/examples/more/SIM800_SslSetCert/SIM800_SslSetCert.ino +++ b/examples/more/SIM800_SslSetCert/SIM800_SslSetCert.ino @@ -3,7 +3,7 @@ * This sketch uploads SSL certificates to the SIM8xx * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * **************************************************************/ From aabe6b3d0192b95698f0ff3a683515bcb69a042e Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Fri, 1 Feb 2019 19:10:44 +0200 Subject: [PATCH 2/6] Update links --- tools/AT_Debug/AT_Debug.ino | 2 +- tools/AT_Spy/AT_Spy.ino | 2 +- tools/Diagnostics/Diagnostics.ino | 2 +- tools/FactoryReset/FactoryReset.ino | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/AT_Debug/AT_Debug.ino b/tools/AT_Debug/AT_Debug.ino index e8e985d..d809a34 100644 --- a/tools/AT_Debug/AT_Debug.ino +++ b/tools/AT_Debug/AT_Debug.ino @@ -4,7 +4,7 @@ * and allows direct AT commands access * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * **************************************************************/ diff --git a/tools/AT_Spy/AT_Spy.ino b/tools/AT_Spy/AT_Spy.ino index a427918..c7c0759 100644 --- a/tools/AT_Spy/AT_Spy.ino +++ b/tools/AT_Spy/AT_Spy.ino @@ -4,7 +4,7 @@ * between an Arduino and the modem. * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * **************************************************************/ diff --git a/tools/Diagnostics/Diagnostics.ino b/tools/Diagnostics/Diagnostics.ino index a57f2b8..eee0655 100644 --- a/tools/Diagnostics/Diagnostics.ino +++ b/tools/Diagnostics/Diagnostics.ino @@ -5,7 +5,7 @@ * or from http://librarymanager/all#StreamDebugger * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * **************************************************************/ diff --git a/tools/FactoryReset/FactoryReset.ino b/tools/FactoryReset/FactoryReset.ino index 1570cfc..97d1627 100644 --- a/tools/FactoryReset/FactoryReset.ino +++ b/tools/FactoryReset/FactoryReset.ino @@ -5,7 +5,7 @@ * or from http://librarymanager/all#StreamDebugger * * TinyGSM Getting Started guide: - * http://tiny.cc/tiny-gsm-readme + * https://tiny.cc/tinygsm-readme * **************************************************************/ From 477b4f2d1b3fa8b6c3bc00b47f6592663a83fd72 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Fri, 1 Feb 2019 19:29:25 +0200 Subject: [PATCH 3/6] boost ver --- library.json | 2 +- library.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library.json b/library.json index 29ac518..3fbc503 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "TinyGSM", - "version": "0.3.5", + "version": "0.3.6", "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 7762874..d9727bd 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TinyGSM -version=0.3.5 +version=0.3.6 author=Volodymyr Shymanskyy maintainer=Volodymyr Shymanskyy sentence=A small Arduino library for GPRS modules, that just works. From c1badbff8ba12dce7f21a9f360c340c759d59cbd Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 13 Feb 2019 08:25:03 +0100 Subject: [PATCH 4/6] Added SIM7000(A/E/C/G) First version - clone of SIM800 with changed values including /example folder --- examples/AllFunctions/AllFunctions.ino | 6 +- .../SIM7000-ESP32/Diagnostics/Diagnostics.ino | 187 +++ src/TinyGsmClient.h | 11 +- src/TinyGsmClientSIM7000.h | 1000 +++++++++++++++++ 4 files changed, 1199 insertions(+), 5 deletions(-) create mode 100644 examples/SIM7000-ESP32/Diagnostics/Diagnostics.ino create mode 100644 src/TinyGsmClientSIM7000.h diff --git a/examples/AllFunctions/AllFunctions.ino b/examples/AllFunctions/AllFunctions.ino index a17f39f..0d3209f 100644 --- a/examples/AllFunctions/AllFunctions.ino +++ b/examples/AllFunctions/AllFunctions.ino @@ -10,9 +10,10 @@ **************************************************************/ // Select your modem: -#define TINY_GSM_MODEM_SIM800 +//#define TINY_GSM_MODEM_SIM800 // #define TINY_GSM_MODEM_SIM808 // #define TINY_GSM_MODEM_SIM900 +#define TINY_GSM_MODEM_SIM7000 // #define TINY_GSM_MODEM_UBLOX // #define TINY_GSM_MODEM_BG96 // #define TINY_GSM_MODEM_A6 @@ -110,7 +111,7 @@ void loop() { String cop = modem.getOperator(); DBG("Operator:", cop); - + IPAddress local = modem.localIP(); DBG("Local IP:", local); @@ -199,4 +200,3 @@ void loop() { modem.maintain(); } } - diff --git a/examples/SIM7000-ESP32/Diagnostics/Diagnostics.ino b/examples/SIM7000-ESP32/Diagnostics/Diagnostics.ino new file mode 100644 index 0000000..f17b925 --- /dev/null +++ b/examples/SIM7000-ESP32/Diagnostics/Diagnostics.ino @@ -0,0 +1,187 @@ +/************************************************************** + * + * To run this tool you need StreamDebugger library: + * https://github.com/vshymanskyy/StreamDebugger + * or from http://librarymanager/all#StreamDebugger + * + * TinyGSM Getting Started guide: + * https://tiny.cc/tinygsm-readme + * + **************************************************************/ + +// Select your modem: +//#define TINY_GSM_MODEM_SIM800 +// #define TINY_GSM_MODEM_SIM808 +// #define TINY_GSM_MODEM_SIM900 +#define TINY_GSM_MODEM_SIM7000 +// #define TINY_GSM_MODEM_UBLOX +// #define TINY_GSM_MODEM_BG96 +// #define TINY_GSM_MODEM_A6 +// #define TINY_GSM_MODEM_A7 +// #define TINY_GSM_MODEM_M590 +// #define TINY_GSM_MODEM_ESP8266 +// #define TINY_GSM_MODEM_XBEE + +// Increase the buffer +#define TINY_GSM_RX_BUFFER 512 + +// Define the serial console for debug prints, if needed +//#define TINY_GSM_DEBUG Serial + +#include + +// Your GPRS credentials +// Leave empty, if missing user or pass +const char apn[] = "drei.at"; +const char user[] = ""; +const char pass[] = ""; + +// Set serial for debug console (to the Serial Monitor, speed 115200) +#define SerialMon Serial + +// Set serial for AT commands (to the module) +// Use Hardware Serial on Mega, Leonardo, Micro, Esp32 +#include "HardwareSerial.h" +#define SerialAT Serial2 + +// or Software Serial on Uno, Nano +//#include +//SoftwareSerial SerialAT(2, 3); // RX, TX + +#include +StreamDebugger debugger(SerialAT, SerialMon); +TinyGsm modem(debugger); + +const char server[] = "vsh.pp.ua"; +const char resource[] = "/TinyGSM/logo.txt"; + +const int port = 80; +TinyGsmClient client(modem); + +// For SSL: +//const int port = 443; +//TinyGsmClientSecure client(modem); + +void setup() { + // Set console baud rate + Serial.println("Starting Serial"); + SerialMon.begin(115200); + delay(10); + + // Set GSM module baud rate + SerialAT.begin(9600); + delay(3000); +} + +void loop() { + // Restart takes quite some time + // To skip it, call init() instead of restart() + SerialMon.print("Initializing modem..."); + if (!modem.restart()) { + SerialMon.println(F(" [fail]")); + SerialMon.println(F("************************")); + SerialMon.println(F(" Is your modem connected properly?")); + SerialMon.println(F(" Is your serial speed (baud rate) correct?")); + SerialMon.println(F(" Is your modem powered on?")); + SerialMon.println(F(" Do you use a good, stable power source?")); + SerialMon.println(F(" Try useing File -> Examples -> TinyGSM -> tools -> AT_Debug to find correct configuration")); + SerialMon.println(F("************************")); + delay(10000); + return; + } + SerialMon.println(F(" [OK]")); + + String modemInfo = modem.getModemInfo(); + SerialMon.print("Modem: "); + SerialMon.println(modemInfo); + + // Unlock your SIM card with a PIN + //modem.simUnlock("1234"); + + SerialMon.print("Waiting for network..."); + if (!modem.waitForNetwork()) { + SerialMon.println(F(" [fail]")); + SerialMon.println(F("************************")); + SerialMon.println(F(" Is your sim card locked?")); + SerialMon.println(F(" Do you have a good signal?")); + SerialMon.println(F(" Is antenna attached?")); + SerialMon.println(F(" Does the SIM card work with your phone?")); + SerialMon.println(F("************************")); + delay(10000); + return; + } + SerialMon.println(F(" [OK]")); + + SerialMon.print("Connecting to "); + SerialMon.print(apn); + if (!modem.gprsConnect(apn, user, pass)) { + SerialMon.println(F(" [fail]")); + SerialMon.println(F("************************")); + SerialMon.println(F(" Is GPRS enabled by network provider?")); + SerialMon.println(F(" Try checking your card balance.")); + SerialMon.println(F("************************")); + delay(10000); + return; + } + SerialMon.println(F(" [OK]")); + + IPAddress local = modem.localIP(); + SerialMon.print("Local IP: "); + SerialMon.println(local); + + SerialMon.print(F("Connecting to ")); + SerialMon.print(server); + if (!client.connect(server, port)) { + SerialMon.println(F(" [fail]")); + delay(10000); + return; + } + SerialMon.println(F(" [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"); + + // Wait for data to arrive + while (client.connected() && !client.available()) { + delay(100); + SerialMon.print('.'); + }; + SerialMon.println(); + + // Skip all headers + client.find("\r\n\r\n"); + + // Read data + unsigned long timeout = millis(); + unsigned long bytesReceived = 0; + while (client.connected() && millis() - timeout < 10000L) { + while (client.available()) { + char c = client.read(); + //SerialMon.print(c); + bytesReceived += 1; + timeout = millis(); + } + } + + client.stop(); + SerialMon.println(F("Server disconnected")); + + modem.gprsDisconnect(); + SerialMon.println(F("GPRS disconnected")); + + SerialMon.println(); + SerialMon.println(F("************************")); + SerialMon.print (F(" Received: ")); + SerialMon.print(bytesReceived); + SerialMon.println(F(" bytes")); + SerialMon.print (F(" Test: ")); + SerialMon.println((bytesReceived == 121) ? "PASSED" : "FAILED"); + SerialMon.println(F("************************")); + + // Do nothing forevermore + while (true) { + delay(1000); + } +} diff --git a/src/TinyGsmClient.h b/src/TinyGsmClient.h index c352cbb..3d42e8b 100644 --- a/src/TinyGsmClient.h +++ b/src/TinyGsmClient.h @@ -9,11 +9,11 @@ #ifndef TinyGsmClient_h #define TinyGsmClient_h -#if defined(TINY_GSM_MODEM_SIM800) || defined(TINY_GSM_MODEM_SIM868) || defined(TINY_GSM_MODEM_UBLOX) || defined(TINY_GSM_MODEM_ESP8266) +#if defined(TINY_GSM_MODEM_SIM800) || defined(TINY_GSM_MODEM_SIM868) || defined(TINY_GSM_MODEM_UBLOX) || defined(TINY_GSM_MODEM_ESP8266) || defined(TINY_GSM_MODEM_SIM7000) #define TINY_GSM_MODEM_HAS_SSL #endif -#if defined(TINY_GSM_MODEM_SIM808) || defined(TINY_GSM_MODEM_SIM868) || defined(TINY_GSM_MODEM_A7) +#if defined(TINY_GSM_MODEM_SIM808) || defined(TINY_GSM_MODEM_SIM868) || defined(TINY_GSM_MODEM_A7) || defined(TINY_GSM_MODEM_SIM7000) #define TINY_GSM_MODEM_HAS_GPS #endif @@ -24,6 +24,13 @@ typedef TinyGsmSim800::GsmClient TinyGsmClient; typedef TinyGsmSim800::GsmClientSecure TinyGsmClientSecure; +#elif defined(TINY_GSM_MODEM_SIM7000) + #define TINY_GSM_MODEM_HAS_GPRS + #include + typedef TinyGsmSim7000 TinyGsm; + typedef TinyGsmSim7000::GsmClient TinyGsmClient; + typedef TinyGsmSim7000::GsmClientSecure TinyGsmClientSecure; + #elif defined(TINY_GSM_MODEM_SIM808) || defined(TINY_GSM_MODEM_SIM868) #define TINY_GSM_MODEM_HAS_GPRS #include diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h new file mode 100644 index 0000000..ae370db --- /dev/null +++ b/src/TinyGsmClientSIM7000.h @@ -0,0 +1,1000 @@ +/** + * @file TinyGsmClientSIM7000.h + * @author Volodymyr Shymanskyy + * @license LGPL-3.0 + * @copyright Copyright (c) 2016 Volodymyr Shymanskyy + * @date Nov 2016 + */ + +#ifndef TinyGsmClientSIM7000_h +#define TinyGsmClientSIM7000_h + +#define TINY_GSM_DEBUG Serial +//#define TINY_GSM_USE_HEX + +#if !defined(TINY_GSM_RX_BUFFER) + #define TINY_GSM_RX_BUFFER 64 +#endif + +#define TINY_GSM_MUX_COUNT 5 + +#include + +#define GSM_NL "\r\n" +static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL; +static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL; + +enum SimStatus { + SIM_ERROR = 0, + SIM_READY = 1, + SIM_LOCKED = 2, +}; + +enum RegStatus { + REG_UNREGISTERED = 0, + REG_SEARCHING = 2, + REG_DENIED = 3, + REG_OK_HOME = 1, + REG_OK_ROAMING = 5, + REG_UNKNOWN = 4, +}; + +enum TinyGSMDateTimeFormat { + DATE_FULL = 0, + DATE_TIME = 1, + DATE_DATE = 2 +}; + +class TinyGsmSim7000 +{ + +public: + +class GsmClient : public Client +{ + friend class TinyGsmSim7000; + typedef TinyGsmFifo RxFifo; + +public: + GsmClient() {} + + GsmClient(TinyGsmSim7000& modem, uint8_t mux = 1) { + init(&modem, mux); + } + + bool init(TinyGsmSim7000* modem, uint8_t mux = 1) { + this->at = modem; + this->mux = mux; + sock_available = 0; + prev_check = 0; + sock_connected = false; + got_data = false; + + at->sockets[mux] = this; + + return true; + } + +public: + virtual int connect(const char *host, uint16_t port) { + stop(); + TINY_GSM_YIELD(); + rx.clear(); + sock_connected = at->modemConnect(host, port, mux); + return sock_connected; + } + + virtual int connect(IPAddress ip, uint16_t port) { + String host; host.reserve(16); + host += ip[0]; + host += "."; + host += ip[1]; + host += "."; + host += ip[2]; + host += "."; + host += ip[3]; + return connect(host.c_str(), port); + } + + virtual void stop() { + TINY_GSM_YIELD(); + at->sendAT(GF("+CIPCLOSE="), mux); + sock_connected = false; + at->waitResponse(); + rx.clear(); + } + + virtual size_t write(const uint8_t *buf, size_t size) { + TINY_GSM_YIELD(); + at->maintain(); + return at->modemSend(buf, size, mux); + } + + virtual size_t write(uint8_t c) { + return write(&c, 1); + } + + virtual size_t write(const char *str) { + if (str == NULL) return 0; + return write((const uint8_t *)str, strlen(str)); + } + + virtual int available() { + TINY_GSM_YIELD(); + if (!rx.size() && sock_connected) { + // Workaround: sometimes SIM7000 forgets to notify about data arrival. + // TODO: Currently we ping the module periodically, + // but maybe there's a better indicator that we need to poll + if (millis() - prev_check > 500) { + got_data = true; + prev_check = millis(); + } + at->maintain(); + } + return rx.size() + sock_available; + } + + virtual int read(uint8_t *buf, size_t size) { + TINY_GSM_YIELD(); + at->maintain(); + size_t cnt = 0; + while (cnt < size && sock_connected) { + size_t chunk = TinyGsmMin(size-cnt, rx.size()); + if (chunk > 0) { + rx.get(buf, chunk); + buf += chunk; + cnt += chunk; + continue; + } + // TODO: Read directly into user buffer? + at->maintain(); + if (sock_available > 0) { + at->modemRead(rx.free(), mux); + } else { + break; + } + } + return cnt; + } + + virtual int read() { + uint8_t c; + if (read(&c, 1) == 1) { + return c; + } + return -1; + } + + virtual int peek() { return -1; } //TODO + virtual void flush() { at->stream.flush(); } + + virtual uint8_t connected() { + if (available()) { + return true; + } + return sock_connected; + } + virtual operator bool() { return connected(); } + + /* + * Extended API + */ + + String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED; + +private: + TinyGsmSim7000* at; + uint8_t mux; + uint16_t sock_available; + uint32_t prev_check; + bool sock_connected; + bool got_data; + RxFifo rx; +}; + +class GsmClientSecure : public GsmClient +{ +public: + GsmClientSecure() {} + + GsmClientSecure(TinyGsmSim7000& modem, uint8_t mux = 1) + : GsmClient(modem, mux) + {} + +public: + virtual int connect(const char *host, uint16_t port) { + stop(); + TINY_GSM_YIELD(); + rx.clear(); + sock_connected = at->modemConnect(host, port, mux, true); + return sock_connected; + } +}; + +public: + + TinyGsmSim7000(Stream& stream) + : stream(stream) + { + memset(sockets, 0, sizeof(sockets)); + } + + /* + * Basic functions + */ + bool begin() { + return init(); + } + + bool init() { + if (!testAT()) { + return false; + } + sendAT(GF("&FZ")); // Factory + Reset + waitResponse(); + sendAT(GF("E0")); // Echo Off + if (waitResponse() != 1) { + return false; + } + getSimStatus(); + return true; + } + + void setBaud(unsigned long baud) { + sendAT(GF("+IPR="), baud); + } + + bool testAT(unsigned long timeout = 10000L) { + //streamWrite(GF("AAAAA" GSM_NL)); // TODO: extra A's to help detect the baud rate + for (unsigned long start = millis(); millis() - start < timeout; ) { + sendAT(GF("")); + if (waitResponse(200) == 1) { + delay(100); + return true; + } + delay(100); + } + return false; + } + + void maintain() { + for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) { + GsmClient* sock = sockets[mux]; + if (sock && sock->got_data) { + sock->got_data = false; + sock->sock_available = modemGetAvailable(mux); + } + } + while (stream.available()) { + waitResponse(10, NULL, NULL); + } + } + + bool factoryDefault() { + sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write + waitResponse(); + sendAT(GF("+IPR=0")); // Auto-baud + waitResponse(); + sendAT(GF("+IFC=0,0")); // No Flow Control + waitResponse(); + sendAT(GF("+ICF=3,3")); // 8 data 0 parity 1 stop + waitResponse(); + sendAT(GF("+CSCLK=0")); // Disable Slow Clock + waitResponse(); + sendAT(GF("&W")); // Write configuration + return waitResponse() == 1; + } + + String getModemInfo() { + sendAT(GF("I")); + String res; + if (waitResponse(1000L, res) != 1) { + return ""; + } + res.replace(GSM_NL "OK" GSM_NL, ""); + res.replace(GSM_NL, " "); + res.trim(); + return res; + } + + bool hasSSL() { + sendAT(GF("+CIPSSL=?")); + if (waitResponse(GF(GSM_NL "+CIPSSL:")) != 1) { + return false; + } + return waitResponse() == 1; + } + + /* + * Power functions + */ + + bool restart() { + if (!testAT()) { + return false; + } + //Enable Local Time Stamp for getting network time + // TODO: Find a better place for this + sendAT(GF("+CLTS=1")); + if (waitResponse(10000L) != 1) { + return false; + } + sendAT(GF("&W")); + waitResponse(); + sendAT(GF("+CFUN=0")); + if (waitResponse(10000L) != 1) { + return false; + } + sendAT(GF("+CFUN=1,1")); + if (waitResponse(10000L) != 1) { + return false; + } + delay(3000); + return init(); + } + + bool poweroff() { + sendAT(GF("+CPOWD=1")); + return waitResponse(GF("NORMAL POWER DOWN")) == 1; + } + + bool radioOff() { + sendAT(GF("+CFUN=0")); + if (waitResponse(10000L) != 1) { + return false; + } + delay(3000); + return true; + } + + /* + During sleep, the SIM7000 module has its serial communication disabled. In order to reestablish communication + pull the DRT-pin of the SIM7000 module LOW for at least 50ms. Then use this function to disable sleep mode. + The DTR-pin can then be released again. + */ + bool sleepEnable(bool enable = true) { + sendAT(GF("+CSCLK="), enable); + return waitResponse() == 1; + } + + /* + * SIM card functions + */ + + bool simUnlock(const char *pin) { + sendAT(GF("+CPIN=\""), pin, GF("\"")); + return waitResponse() == 1; + } + + String getSimCCID() { + sendAT(GF("+ICCID")); + if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) { + return ""; + } + String res = stream.readStringUntil('\n'); + waitResponse(); + res.trim(); + return res; + } + + String getIMEI() { + sendAT(GF("+GSN")); + if (waitResponse(GF(GSM_NL)) != 1) { + return ""; + } + String res = stream.readStringUntil('\n'); + waitResponse(); + res.trim(); + return res; + } + + SimStatus getSimStatus(unsigned long timeout = 10000L) { + for (unsigned long start = millis(); millis() - start < timeout; ) { + sendAT(GF("+CPIN?")); + if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) { + delay(1000); + continue; + } + int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"), GF("NOT INSERTED")); + waitResponse(); + switch (status) { + case 2: + case 3: return SIM_LOCKED; + case 1: return SIM_READY; + default: return SIM_ERROR; + } + } + return SIM_ERROR; + } + + RegStatus getRegistrationStatus() { + sendAT(GF("+CREG?")); + if (waitResponse(GF(GSM_NL "+CREG:")) != 1) { + return REG_UNKNOWN; + } + streamSkipUntil(','); // Skip format (0) + int status = stream.readStringUntil('\n').toInt(); + waitResponse(); + return (RegStatus)status; + } + + String getOperator() { + sendAT(GF("+COPS?")); + if (waitResponse(GF(GSM_NL "+COPS:")) != 1) { + return ""; + } + streamSkipUntil('"'); // Skip mode and format + String res = stream.readStringUntil('"'); + waitResponse(); + return res; + } + + /* + * Generic network functions + */ + + int getSignalQuality() { + sendAT(GF("+CSQ")); + if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { + return 99; + } + int res = stream.readStringUntil(',').toInt(); + waitResponse(); + return res; + } + + bool isNetworkConnected() { + RegStatus s = getRegistrationStatus(); + return (s == REG_OK_HOME || s == REG_OK_ROAMING); + } + + bool waitForNetwork(unsigned long timeout = 60000L) { + for (unsigned long start = millis(); millis() - start < timeout; ) { + if (isNetworkConnected()) { + return true; + } + delay(250); + } + return false; + } + + /* + * GPRS functions + */ + bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) { + gprsDisconnect(); + + // Set the Bearer for the IP + sendAT(GF("+SAPBR=3,1,\"Contype\",\"GPRS\"")); // Set the connection type to GPRS + waitResponse(); + + sendAT(GF("+SAPBR=3,1,\"APN\",\""), apn, '"'); // Set the APN + waitResponse(); + + if (user && strlen(user) > 0) { + sendAT(GF("+SAPBR=3,1,\"USER\",\""), user, '"'); // Set the user name + waitResponse(); + } + if (pwd && strlen(pwd) > 0) { + sendAT(GF("+SAPBR=3,1,\"PWD\",\""), pwd, '"'); // Set the password + waitResponse(); + } + + // Define the PDP context + sendAT(GF("+CGDCONT=1,\"IP\",\""), apn, '"'); + waitResponse(); + + // Activate the PDP context + sendAT(GF("+CGACT=1,1")); + waitResponse(60000L); + + // Open the definied GPRS bearer context + sendAT(GF("+SAPBR=1,1")); + waitResponse(85000L); + // Query the GPRS bearer context status + sendAT(GF("+SAPBR=2,1")); + if (waitResponse(30000L) != 1) + return false; + + // Attach to GPRS + sendAT(GF("+CGATT=1")); + if (waitResponse(60000L) != 1) + return false; + + // TODO: wait AT+CGATT? + + // Set to multi-IP + sendAT(GF("+CIPMUX=1")); + if (waitResponse() != 1) { + return false; + } + + // Put in "quick send" mode (thus no extra "Send OK") + sendAT(GF("+CIPQSEND=1")); + if (waitResponse() != 1) { + return false; + } + + // Set to get data manually + sendAT(GF("+CIPRXGET=1")); + if (waitResponse() != 1) { + return false; + } + + // Start Task and Set APN, USER NAME, PASSWORD + sendAT(GF("+CSTT=\""), apn, GF("\",\""), user, GF("\",\""), pwd, GF("\"")); + if (waitResponse(60000L) != 1) { + return false; + } + + // Bring Up Wireless Connection with GPRS or CSD + sendAT(GF("+CIICR")); + if (waitResponse(60000L) != 1) { + return false; + } + + // Get Local IP Address, only assigned after connection + sendAT(GF("+CIFSR;E0")); + if (waitResponse(10000L) != 1) { + return false; + } + + // Configure Domain Name Server (DNS) + sendAT(GF("+CDNSCFG=\"8.8.8.8\",\"8.8.4.4\"")); + if (waitResponse() != 1) { + return false; + } + + return true; + } + + bool gprsDisconnect() { + // Shut the TCP/IP connection + sendAT(GF("+CIPSHUT")); + if (waitResponse(60000L) != 1) + return false; + + sendAT(GF("+CGATT=0")); // Deactivate the bearer context + if (waitResponse(60000L) != 1) + return false; + + return true; + } + + bool isGprsConnected() { + sendAT(GF("+CGATT?")); + if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) { + return false; + } + int res = stream.readStringUntil('\n').toInt(); + waitResponse(); + if (res != 1) + return false; + + sendAT(GF("+CIFSR;E0")); // Another option is to use AT+CGPADDR=1 + if (waitResponse() != 1) + return false; + + return true; + } + + String getLocalIP() { + sendAT(GF("+CIFSR;E0")); + String res; + if (waitResponse(10000L, res) != 1) { + return ""; + } + res.replace(GSM_NL "OK" GSM_NL, ""); + res.replace(GSM_NL, ""); + res.trim(); + return res; + } + + IPAddress localIP() { + return TinyGsmIpFromString(getLocalIP()); + } + + /* + * Phone Call functions + */ + + bool setGsmBusy(bool busy = true) { + sendAT(GF("+GSMBUSY="), busy ? 1 : 0); + return waitResponse() == 1; + } + + bool callAnswer() { + sendAT(GF("A")); + return waitResponse() == 1; + } + + // Returns true on pick-up, false on error/busy + bool callNumber(const String& number) { + if (number == GF("last")) { + sendAT(GF("DL")); + } else { + sendAT(GF("D"), number, ";"); + } + int status = waitResponse(60000L, + GFP(GSM_OK), + GF("BUSY" GSM_NL), + GF("NO ANSWER" GSM_NL), + GF("NO CARRIER" GSM_NL)); + switch (status) { + case 1: return true; + case 2: + case 3: return false; + default: return false; + } + } + + bool callHangup() { + sendAT(GF("H")); + return waitResponse() == 1; + } + + // 0-9,*,#,A,B,C,D + bool dtmfSend(char cmd, int duration_ms = 100) { + duration_ms = constrain(duration_ms, 100, 1000); + + sendAT(GF("+VTD="), duration_ms / 100); // VTD accepts in 1/10 of a second + waitResponse(); + + sendAT(GF("+VTS="), cmd); + return waitResponse(10000L) == 1; + } + + /* + * Messaging functions + */ + + String sendUSSD(const String& code) { + sendAT(GF("+CMGF=1")); + waitResponse(); + sendAT(GF("+CSCS=\"HEX\"")); + waitResponse(); + sendAT(GF("+CUSD=1,\""), code, GF("\"")); + if (waitResponse() != 1) { + return ""; + } + if (waitResponse(10000L, GF(GSM_NL "+CUSD:")) != 1) { + return ""; + } + stream.readStringUntil('"'); + String hex = stream.readStringUntil('"'); + stream.readStringUntil(','); + int dcs = stream.readStringUntil('\n').toInt(); + + if (dcs == 15) { + return TinyGsmDecodeHex8bit(hex); + } else if (dcs == 72) { + return TinyGsmDecodeHex16bit(hex); + } else { + return hex; + } + } + + 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("\"")); + if (waitResponse(GF(">")) != 1) { + return false; + } + stream.print(text); + stream.write((char)0x1A); + stream.flush(); + return waitResponse(60000L) == 1; + } + + bool sendSMS_UTF16(const String& number, const void* text, size_t len) { + sendAT(GF("+CMGF=1")); + waitResponse(); + sendAT(GF("+CSCS=\"HEX\"")); + waitResponse(); + sendAT(GF("+CSMP=17,167,0,8")); + waitResponse(); + + sendAT(GF("+CMGS=\""), number, GF("\"")); + if (waitResponse(GF(">")) != 1) { + return false; + } + + uint16_t* t = (uint16_t*)text; + for (size_t i=0; i> 8; + if (c < 0x10) { stream.print('0'); } + stream.print(c, HEX); + c = t[i] & 0xFF; + if (c < 0x10) { stream.print('0'); } + stream.print(c, HEX); + } + stream.write((char)0x1A); + stream.flush(); + return waitResponse(60000L) == 1; + } + + + /* + * Location functions + */ + + String getGsmLocation() { + sendAT(GF("+CIPGSMLOC=1,1")); + if (waitResponse(10000L, GF(GSM_NL "+CIPGSMLOC:")) != 1) { + return ""; + } + String res = stream.readStringUntil('\n'); + waitResponse(); + res.trim(); + return res; + } + + /* + * Time functions + */ + String getGSMDateTime(TinyGSMDateTimeFormat format) { + sendAT(GF("+CCLK?")); + if (waitResponse(2000L, GF(GSM_NL "+CCLK: \"")) != 1) { + return ""; + } + + String res; + + switch(format) { + case DATE_FULL: + res = stream.readStringUntil('"'); + break; + case DATE_TIME: + streamSkipUntil(','); + res = stream.readStringUntil('"'); + break; + case DATE_DATE: + res = stream.readStringUntil(','); + break; + } + return res; + } + + /* + * Battery functions + */ + // Use: float vBatt = modem.getBattVoltage() / 1000.0; + uint16_t getBattVoltage() { + sendAT(GF("+CBC")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return 0; + } + streamSkipUntil(','); // Skip + streamSkipUntil(','); // Skip + + uint16_t res = stream.readStringUntil(',').toInt(); + waitResponse(); + return res; + } + + int getBattPercent() { + sendAT(GF("+CBC")); + if (waitResponse(GF(GSM_NL "+CBC:")) != 1) { + return false; + } + stream.readStringUntil(','); + int res = stream.readStringUntil(',').toInt(); + waitResponse(); + return res; + } + +protected: + + bool modemConnect(const char* host, uint16_t port, uint8_t mux, bool ssl = false) { + int rsp; + 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("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 0; + } + stream.write((uint8_t*)buff, len); + stream.flush(); + if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) { + return 0; + } + streamSkipUntil(','); // Skip mux + return stream.readStringUntil('\n').toInt(); + } + + size_t modemRead(size_t size, uint8_t mux) { +#ifdef TINY_GSM_USE_HEX + sendAT(GF("+CIPRXGET=3,"), mux, ',', size); + if (waitResponse(GF("+CIPRXGET:")) != 1) { + return 0; + } +#else + sendAT(GF("+CIPRXGET=2,"), mux, ',', size); + if (waitResponse(GF("+CIPRXGET:")) != 1) { + return 0; + } +#endif + streamSkipUntil(','); // Skip mode 2/3 + streamSkipUntil(','); // Skip mux + size_t len = stream.readStringUntil(',').toInt(); + sockets[mux]->sock_available = stream.readStringUntil('\n').toInt(); + + for (size_t i=0; irx.put(c); + } + waitResponse(); + return len; + } + + size_t modemGetAvailable(uint8_t mux) { + sendAT(GF("+CIPRXGET=4,"), mux); + size_t result = 0; + if (waitResponse(GF("+CIPRXGET:")) == 1) { + streamSkipUntil(','); // Skip mode 4 + streamSkipUntil(','); // Skip mux + result = stream.readStringUntil('\n').toInt(); + waitResponse(); + } + if (!result) { + sockets[mux]->sock_connected = modemGetConnected(mux); + } + return result; + } + + bool modemGetConnected(uint8_t mux) { + sendAT(GF("+CIPSTATUS="), mux); + int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\"")); + waitResponse(); + return 1 == res; + } + +public: + + /* Utilities */ + + template + void streamWrite(T last) { + stream.print(last); + } + + template + void streamWrite(T head, Args... tail) { + stream.print(head); + streamWrite(tail...); + } + + bool streamSkipUntil(const char c, const unsigned long timeout = 3000L) { + unsigned long startMillis = millis(); + while (millis() - startMillis < timeout) { + while (millis() - startMillis < timeout && !stream.available()) { + TINY_GSM_YIELD(); + } + if (stream.read() == c) + return true; + } + return false; + } + + template + void sendAT(Args... cmd) { + streamWrite("AT", cmd..., GSM_NL); + stream.flush(); + TINY_GSM_YIELD(); + //DBG("### AT:", cmd...); + } + + // TODO: Optimize this! + uint8_t waitResponse(uint32_t timeout, String& data, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + /*String r1s(r1); r1s.trim(); + String r2s(r2); r2s.trim(); + String r3s(r3); r3s.trim(); + String r4s(r4); r4s.trim(); + String r5s(r5); r5s.trim(); + DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ + data.reserve(64); + int index = 0; + unsigned long startMillis = millis(); + do { + TINY_GSM_YIELD(); + while (stream.available() > 0) { + int a = stream.read(); + if (a <= 0) continue; // Skip 0x00 bytes, just in case + data += (char)a; + if (r1 && data.endsWith(r1)) { + index = 1; + goto finish; + } else if (r2 && data.endsWith(r2)) { + index = 2; + goto finish; + } else if (r3 && data.endsWith(r3)) { + index = 3; + goto finish; + } else if (r4 && data.endsWith(r4)) { + index = 4; + goto finish; + } else if (r5 && data.endsWith(r5)) { + index = 5; + goto finish; + } else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) { + String mode = stream.readStringUntil(','); + if (mode.toInt() == 1) { + int mux = stream.readStringUntil('\n').toInt(); + if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) { + sockets[mux]->got_data = true; + } + data = ""; + } else { + data += mode; + } + } else if (data.endsWith(GF("CLOSED" GSM_NL))) { + int nl = data.lastIndexOf(GSM_NL, data.length()-8); + int coma = data.indexOf(',', nl+2); + int mux = data.substring(nl+2, coma).toInt(); + if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) { + sockets[mux]->sock_connected = false; + } + data = ""; + DBG("### Closed: ", mux); + } + } + } while (millis() - startMillis < timeout); +finish: + if (!index) { + data.trim(); + if (data.length()) { + DBG("### Unhandled:", data); + } + data = ""; + } + return index; + } + + uint8_t waitResponse(uint32_t timeout, + GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + String data; + return waitResponse(timeout, data, r1, r2, r3, r4, r5); + } + + uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR), + GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL) + { + return waitResponse(1000, r1, r2, r3, r4, r5); + } + +public: + Stream& stream; + +protected: + GsmClient* sockets[TINY_GSM_MUX_COUNT]; +}; + +#endif From c3cecabefd7d62048f6051573cddafa95c8d74d4 Mon Sep 17 00:00:00 2001 From: MrTarantl Date: Wed, 13 Feb 2019 09:32:30 +0100 Subject: [PATCH 5/6] SIM7000 removed calling functions --- src/TinyGsmClientSIM7000.h | 173 +++++++++++++++++++++++++++---------- 1 file changed, 128 insertions(+), 45 deletions(-) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index ae370db..53d9e09 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -458,6 +458,126 @@ public: return false; } + /* + * GPS location functions + */ + + // enable GPS + bool enableGPS() { + uint16_t state; + + sendAT(GF("+CGNSPWR=1")); + if (waitResponse() != 1) { + return false; + } + + return true; + } + + bool disableGPS() { + uint16_t state; + + sendAT(GF("+CGNSPWR=0")); + if (waitResponse() != 1) { + return false; + } + + return true; + } + + // get the RAW GPS output + String getGPSraw() { + sendAT(GF("+CGNSINF")); + if (waitResponse(GF(GSM_NL "+CGNSINF:")) != 1) { + return ""; + } + String res = stream.readStringUntil('\n'); + waitResponse(); + res.trim(); + return res; + } + + // get GPS informations + bool getGPS(float *lat, float *lon, float *speed=0, int *alt=0, int *vsat=0, int *usat=0) { + //String buffer = ""; + char chr_buffer[12]; + bool fix = false; + + sendAT(GF("+CGNSINF")); + if (waitResponse(GF(GSM_NL "+CGNSINF:")) != 1) { + return false; + } + + stream.readStringUntil(','); // mode + if ( stream.readStringUntil(',').toInt() == 1 ) fix = true; + stream.readStringUntil(','); //utctime + *lat = stream.readStringUntil(',').toFloat(); //lat + *lon = stream.readStringUntil(',').toFloat(); //lon + if (alt != NULL) *alt = stream.readStringUntil(',').toFloat(); //lon + if (speed != NULL) *speed = stream.readStringUntil(',').toFloat(); //speed + stream.readStringUntil(','); + stream.readStringUntil(','); + stream.readStringUntil(','); + stream.readStringUntil(','); + stream.readStringUntil(','); + stream.readStringUntil(','); + stream.readStringUntil(','); + if (vsat != NULL) *vsat = stream.readStringUntil(',').toInt(); //viewed satelites + if (usat != NULL) *usat = stream.readStringUntil(',').toInt(); //used satelites + stream.readStringUntil('\n'); + + waitResponse(); + + return fix; + } + + // get GPS time + bool getGPSTime(int *year, int *month, int *day, int *hour, int *minute, int *second) { + bool fix = false; + char chr_buffer[12]; + sendAT(GF("+CGNSINF")); + if (waitResponse(GF(GSM_NL "+CGNSINF:")) != 1) { + return false; + } + + for (int i = 0; i < 3; i++) { + String buffer = stream.readStringUntil(','); + buffer.toCharArray(chr_buffer, sizeof(chr_buffer)); + switch (i) { + case 0: + //mode + break; + case 1: + //fixstatus + if ( buffer.toInt() == 1 ) { + fix = buffer.toInt(); + } + break; + case 2: + *year = buffer.substring(0,4).toInt(); + *month = buffer.substring(4,6).toInt(); + *day = buffer.substring(6,8).toInt(); + *hour = buffer.substring(8,10).toInt(); + *minute = buffer.substring(10,12).toInt(); + *second = buffer.substring(12,14).toInt(); + break; + + default: + // if nothing else matches, do the default + // default is optional + break; + } + } + String res = stream.readStringUntil('\n'); + waitResponse(); + + if (fix) { + return true; + } else { + return false; + } + } + /* * GPRS functions */ @@ -598,51 +718,11 @@ public: * Phone Call functions */ - bool setGsmBusy(bool busy = true) { - sendAT(GF("+GSMBUSY="), busy ? 1 : 0); - return waitResponse() == 1; - } - - bool callAnswer() { - sendAT(GF("A")); - return waitResponse() == 1; - } - - // Returns true on pick-up, false on error/busy - bool callNumber(const String& number) { - if (number == GF("last")) { - sendAT(GF("DL")); - } else { - sendAT(GF("D"), number, ";"); - } - int status = waitResponse(60000L, - GFP(GSM_OK), - GF("BUSY" GSM_NL), - GF("NO ANSWER" GSM_NL), - GF("NO CARRIER" GSM_NL)); - switch (status) { - case 1: return true; - case 2: - case 3: return false; - default: return false; - } - } - - bool callHangup() { - sendAT(GF("H")); - return waitResponse() == 1; - } - - // 0-9,*,#,A,B,C,D - bool dtmfSend(char cmd, int duration_ms = 100) { - duration_ms = constrain(duration_ms, 100, 1000); - - sendAT(GF("+VTD="), duration_ms / 100); // VTD accepts in 1/10 of a second - waitResponse(); - - sendAT(GF("+VTS="), cmd); - return waitResponse(10000L) == 1; - } + bool setGsmBusy() TINY_GSM_ATTR_NOT_IMPLEMENTED; + bool callAnswer() TINY_GSM_ATTR_NOT_IMPLEMENTED; + bool callNumber() TINY_GSM_ATTR_NOT_IMPLEMENTED; + bool callHangup() TINY_GSM_ATTR_NOT_IMPLEMENTED; + bool dtmfSend() TINY_GSM_ATTR_NOT_IMPLEMENTED; /* * Messaging functions @@ -675,6 +755,9 @@ public: } bool sendSMS(const String& number, const String& text) { + + sendAT(GF("+AT+CSCA?")); + waitResponse(); sendAT(GF("+CMGF=1")); waitResponse(); //Set GSM 7 bit default alphabet (3GPP TS 23.038) From e4c4b3ebc634368e699dce534bff27843c45789f Mon Sep 17 00:00:00 2001 From: MrTarantl Date: Wed, 13 Feb 2019 11:53:14 +0100 Subject: [PATCH 6/6] SIM7000 - setNetworkMode - setPreferredMode Added Network and Preferred Mode selection functions --- .../AllFunctions/AllFunctions.ino | 199 ++++++++++++++++++ src/TinyGsmClientSIM7000.h | 57 +++++ 2 files changed, 256 insertions(+) create mode 100644 examples/SIM7000-ESP32/AllFunctions/AllFunctions.ino diff --git a/examples/SIM7000-ESP32/AllFunctions/AllFunctions.ino b/examples/SIM7000-ESP32/AllFunctions/AllFunctions.ino new file mode 100644 index 0000000..281a0f8 --- /dev/null +++ b/examples/SIM7000-ESP32/AllFunctions/AllFunctions.ino @@ -0,0 +1,199 @@ +/************************************************************** + * + * TinyGSM Getting Started guide: + * https://tiny.cc/tinygsm-readme + * + * NOTE: + * Some of the functions may be unavailable for your modem. + * Just comment them out. + * + **************************************************************/ + +// Select your modem: +//#define TINY_GSM_MODEM_SIM800 +// #define TINY_GSM_MODEM_SIM808 +// #define TINY_GSM_MODEM_SIM900 +#define TINY_GSM_MODEM_SIM7000 +// #define TINY_GSM_MODEM_UBLOX +// #define TINY_GSM_MODEM_BG96 +// #define TINY_GSM_MODEM_A6 +// #define TINY_GSM_MODEM_A7 +// #define TINY_GSM_MODEM_M590 + +// Set serial for debug console (to the Serial Monitor, speed 115200) +#define SerialMon Serial + +// Set serial for AT commands (to the module) +// Use Hardware Serial on Mega, Leonardo, Micro, ESP32 +#include "HardwareSerial.h" +#define SerialAT Serial2 + +// or Software Serial on Uno, Nano +//#include +//SoftwareSerial SerialAT(2, 3); // RX, TX + +#define MODEM_PWRKEY 18 +//#define DUMP_AT_COMMANDS +#define TINY_GSM_DEBUG SerialMon + +// Set phone numbers, if you want to test SMS and Calls +//#define SMS_TARGET "+4366066033403" +//#define CALL_TARGET "+43xxxxxx" + +// Your GPRS credentials +// Leave empty, if missing user or pass +const char apn[] = "drei.at"; +const char user[] = ""; +const char pass[] = ""; + +#include + +#ifdef DUMP_AT_COMMANDS + #include + StreamDebugger debugger(SerialAT, SerialMon); + TinyGsm modem(debugger); +#else + TinyGsm modem(SerialAT); + TinyGsmClient client(modem); +#endif + +void setup() { + // Set pwr pin 18 (ESP32) --> shield's PWRKEY + pinMode(MODEM_PWRKEY, OUTPUT); + powerOn(); //function for powering on SIM7000 + + // Set console baud rate + SerialMon.begin(115200); + delay(1000); + + SerialAT.begin(9600); + delay(1000); + // Set GSM module baud rate + //TinyGsmAutoBaud(SerialAT); +} + +void loop() { + + // Restart takes quite some time + // To skip it, call init() instead of restart() + DBG("Initializing modem..."); + if (!modem.restart()) { + delay(15000); + return; + } + + //String modemInfo = modem.getModemInfo(); + //DBG("Modem:", modemInfo); + + // Unlock your SIM card with a PIN + //modem.simUnlock("1234"); + + + // Network modes for SIM7000 (2-Automatic),(13-GSM Only),(38-LTE Only),(51-GSM And LTE Only) + String NetworkModes = modem.getNetworkModes(); + DBG("Network Modes:", NetworkModes); + + String NetworkMode = modem.setNetworkMode(0); + DBG("Changed Network Mode:", NetworkMode); + + // Preferred LTE mode selection (1-Cat-M),(2-NB-IoT),(3-Cat-M And NB-IoT) + String PreferredModes = modem.getPreferredModes(); + DBG("Preferred Modes:", PreferredModes); + + String PreferredMode = modem.setPreferredMode(3); + DBG("Changed Preferred Mode:", PreferredMode); + + +/* DBG("Waiting for network..."); + if (!modem.waitForNetwork()) { + delay(10000); + return; + } + + if (modem.isNetworkConnected()) { + DBG("Network connected"); + }*/ + +/* + DBG("Connecting to", apn); + if (!modem.gprsConnect(apn, user, pass)) { + delay(10000); + return; + } + + bool res = modem.isGprsConnected(); + DBG("GPRS status:", res ? "connected" : "not connected"); + + String ccid = modem.getSimCCID(); + DBG("CCID:", ccid); + + String imei = modem.getIMEI(); + DBG("IMEI:", imei); + + String cop = modem.getOperator(); + DBG("Operator:", cop); + + IPAddress local = modem.localIP(); + DBG("Local IP:", local); + + int csq = modem.getSignalQuality(); + DBG("Signal quality:", csq); + + // This is NOT supported on M590 + int battLevel = modem.getBattPercent(); + DBG("Battery lavel:", battLevel); + + // This is only supported on SIMxxx series + float battVoltage = modem.getBattVoltage() / 1000.0F; + DBG("Battery voltage:", battVoltage); + + // This is only supported on SIMxxx series + String gsmLoc = modem.getGsmLocation(); + DBG("GSM location:", gsmLoc); + + // This is only supported on SIMxxx series + String gsmTime = modem.getGSMDateTime(DATE_TIME); + DBG("GSM Time:", gsmTime); + String gsmDate = modem.getGSMDateTime(DATE_DATE); + DBG("GSM Date:", gsmDate); + + + modem.enableGPS(); + String gps_raw = modem.getGPSraw(); + modem.disableGPS(); + DBG("GPS raw data:", gps_raw); + + +#if defined(SMS_TARGET) + res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei); + DBG("SMS:", res ? "OK" : "fail"); + + // This is only supported on SIMxxx series + res = modem.sendSMS_UTF16(SMS_TARGET, u"Привіііт!", 9); + DBG("UTF16 SMS:", res ? "OK" : "fail"); +#endif + + modem.gprsDisconnect(); + if (!modem.isGprsConnected()) { + DBG("GPRS disconnected"); + } else { + DBG("GPRS disconnect: Failed."); + } +*/ + + // Try to power-off (modem may decide to restart automatically) + // To turn off modem completely, please use Reset/Enable pins + modem.poweroff(); + DBG("Poweroff."); + + // Do nothing forevermore + while (true) { + modem.maintain(); + } +} + +void powerOn() { + digitalWrite(MODEM_PWRKEY, LOW); + delay(100); + digitalWrite(MODEM_PWRKEY, HIGH); +} diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 53d9e09..91b901e 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -458,6 +458,62 @@ public: return false; } + + + + + + + + + + + String getNetworkModes() { + sendAT(GF("+CNMP=?")); + if (waitResponse(GF(GSM_NL "+CNMP:")) != 1) { + return ""; + } + String res = stream.readStringUntil('\n'); + waitResponse(); + return res; + } + + String setNetworkMode(uint8_t mode) { + sendAT(GF("+CNMP="), mode); + if (waitResponse(GF(GSM_NL "+CNMP:")) != 1) { + return "OK"; + } + String res = stream.readStringUntil('\n'); + waitResponse(); + return res; + } + + String getPreferredModes() { + sendAT(GF("+CMNB=?")); + if (waitResponse(GF(GSM_NL "+CMNB:")) != 1) { + return ""; + } + String res = stream.readStringUntil('\n'); + waitResponse(); + return res; + } + + String setPreferredMode(uint8_t mode) { + sendAT(GF("+CMNB="), mode); + if (waitResponse(GF(GSM_NL "+CMNB:")) != 1) { + return "OK"; + } + String res = stream.readStringUntil('\n'); + waitResponse(); + return res; + } + + + + + + + /* * GPS location functions */ @@ -698,6 +754,7 @@ public: return true; } + String getLocalIP() { sendAT(GF("+CIFSR;E0")); String res;