diff --git a/extras/doc/Quectel_M95_GSM_Specification_V3.1.pdf b/extras/doc/Quectel_M95_GSM_Specification_V3.1.pdf new file mode 100644 index 0000000..b73c228 Binary files /dev/null and b/extras/doc/Quectel_M95_GSM_Specification_V3.1.pdf differ diff --git a/extras/doc/Quectel_M95_at_commands_manual_v1.3.pdf b/extras/doc/Quectel_M95_at_commands_manual_v1.3.pdf new file mode 100644 index 0000000..201fd3e Binary files /dev/null and b/extras/doc/Quectel_M95_at_commands_manual_v1.3.pdf differ diff --git a/src/TinyGsmClient.h b/src/TinyGsmClient.h index 7067294..0952a31 100644 --- a/src/TinyGsmClient.h +++ b/src/TinyGsmClient.h @@ -48,20 +48,20 @@ #define TINY_GSM_MODEM_HAS_GPRS #include typedef TinyGsmA6 TinyGsm; - typedef TinyGsm::GsmClient TinyGsmClient; + typedef TinyGsmA6::GsmClient TinyGsmClient; #elif defined(TINY_GSM_MODEM_M590) #define TINY_GSM_MODEM_HAS_GPRS #include typedef TinyGsmM590 TinyGsm; - typedef TinyGsm::GsmClient TinyGsmClient; + typedef TinyGsmM590::GsmClient TinyGsmClient; #elif defined(TINY_GSM_MODEM_ESP8266) #define TINY_GSM_MODEM_HAS_WIFI #include typedef TinyGsmESP8266 TinyGsm; - typedef TinyGsm::GsmClient TinyGsmClient; - typedef TinyGsm::GsmClientSecure TinyGsmClientSecure; + typedef TinyGsmESP8266::GsmClient TinyGsmClient; + typedef TinyGsmESP8266::GsmClientSecure TinyGsmClientSecure; #elif defined(TINY_GSM_MODEM_XBEE) #define TINY_GSM_MODEM_HAS_GPRS @@ -74,7 +74,8 @@ #elif defined(TINY_GSM_MODEM_M95) #define TINY_GSM_MODEM_HAS_GPRS #include - typedef TinyGsm::GsmClient TinyGsmClient; + typedef TinyGsmM95 TinyGsm; + typedef TinyGsmM95::GsmClient TinyGsmClient; #else #error "Please define GSM modem model" diff --git a/src/TinyGsmClientA6.h b/src/TinyGsmClientA6.h index 3d87d29..42f0a67 100644 --- a/src/TinyGsmClientA6.h +++ b/src/TinyGsmClientA6.h @@ -38,21 +38,15 @@ enum RegStatus { REG_UNKNOWN = 4, }; - //============================================================================// //============================================================================// // Declaration of the TinyGsmA6 Class //============================================================================// //============================================================================// - - class TinyGsmA6 { -public: - - //============================================================================// //============================================================================// // The A6 Client Class @@ -60,6 +54,8 @@ public: //============================================================================// +public: + class GsmClient : public Client { friend class TinyGsmA6; @@ -343,10 +339,10 @@ public: int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK")); waitResponse(); switch (status) { - case 2: - case 3: return SIM_LOCKED; - case 1: return SIM_READY; - default: return SIM_ERROR; + case 2: + case 3: return SIM_LOCKED; + case 1: return SIM_READY; + default: return SIM_ERROR; } } return SIM_ERROR; @@ -370,16 +366,16 @@ public: * Generic network functions */ - 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; - } + 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; + } int getSignalQuality() { sendAT(GF("+CSQ")); @@ -587,7 +583,7 @@ protected: } bool modemGetConnected(uint8_t mux) { - sendAT(GF("+CIPSTATUS")); //TODO mux? + sendAT(GF("+CIPSTATUS")); // TODO mux? int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\"")); waitResponse(); return 1 == res; diff --git a/src/TinyGsmClientBG96.h b/src/TinyGsmClientBG96.h index f65da65..6c4502c 100644 --- a/src/TinyGsmClientBG96.h +++ b/src/TinyGsmClientBG96.h @@ -9,14 +9,14 @@ #ifndef TinyGsmClientBG96_h #define TinyGsmClientBG96_h -//#define TINY_GSM_DEBUG Serial -//#define TINY_GSM_USE_HEX +// #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 +#define TINY_GSM_MUX_COUNT 12 #include @@ -39,10 +39,22 @@ enum RegStatus { REG_UNKNOWN = 4, }; +//============================================================================// +//============================================================================// +// Declaration of the TinyGsmBG96 Class +//============================================================================// +//============================================================================// class TinyGsmBG96 { +//============================================================================// +//============================================================================// +// The BG96 Client Class +//============================================================================// +//============================================================================// + + public: class GsmClient : public Client @@ -166,13 +178,20 @@ public: private: TinyGsmBG96* at; - uint8_t mux; - uint16_t sock_available; - bool sock_connected; - bool got_data; - RxFifo rx; + uint8_t mux; + uint16_t sock_available; + bool sock_connected; + bool got_data; + RxFifo rx; }; +//============================================================================// +//============================================================================// +// The BG96 Secure Client +//============================================================================// +//============================================================================// + + class GsmClientSecure : public GsmClient { public: @@ -192,9 +211,19 @@ public: } }; +//============================================================================// +//============================================================================// +// The BG96 Modem Functions +//============================================================================// +//============================================================================// + public: +#ifdef GSM_DEFAULT_STREAM + TinyGsmBG96(Stream& stream = GSM_DEFAULT_STREAM) +#else TinyGsmBG96(Stream& stream) +#endif : stream(stream) { memset(sockets, 0, sizeof(sockets)); @@ -344,26 +373,15 @@ public: 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; + 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) { @@ -379,6 +397,17 @@ public: * Generic network functions */ + 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; + } + int getSignalQuality() { sendAT(GF("+CSQ")); if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { @@ -404,6 +433,27 @@ public: return false; } + String getLocalIP() { + sendAT(GF("+CGPADDR=1")); + if (waitResponse(10000L, GF(GSM_NL "+CGPADDR:")) != 1) { + return ""; + } + streamSkipUntil(','); + String res = stream.readStringUntil('\n'); + if (waitResponse() != 1) { + return ""; + } + return res; + } + + IPAddress localIP() { + return TinyGsmIpFromString(getLocalIP()); + } + + /* + * WiFi functions + */ + /* * GPRS functions */ @@ -450,53 +500,6 @@ public: return localIP() != 0; } - String getLocalIP() { - sendAT(GF("+CGPADDR=1")); - if (waitResponse(10000L, GF(GSM_NL "+CGPADDR:")) != 1) { - return ""; - } - streamSkipUntil(','); - String res = stream.readStringUntil('\n'); - if (waitResponse() != 1) { - return ""; - } - return res; - } - - IPAddress localIP() { - return TinyGsmIpFromString(getLocalIP()); - } - - /* - * Phone Call functions - */ - - bool setGsmBusy(bool busy = true) TINY_GSM_ATTR_NOT_AVAILABLE; - - bool callAnswer() { - sendAT(GF("A")); - return waitResponse() == 1; - } - - // Returns true on pick-up, false on error/busy - bool callNumber(const String& number) TINY_GSM_ATTR_NOT_IMPLEMENTED; - - bool callHangup() { - sendAT(GF("H")); - return waitResponse() == 1; - } - - // 0-9,*,#,A,B,C,D - bool dtmfSend(char cmd, int duration_ms = 100) { // TODO: check - 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 */ @@ -660,9 +663,13 @@ public: streamWrite(tail...); } - bool streamSkipUntil(char c) { //TODO: timeout - while (true) { - while (!stream.available()) { TINY_GSM_YIELD(); } + bool streamSkipUntil(char c) { + const unsigned long timeout = 1000L; + unsigned long startMillis = millis(); + while (millis() - startMillis < timeout) { + while (millis() - startMillis < timeout && !stream.available()) { + TINY_GSM_YIELD(); + } if (stream.read() == c) return true; } @@ -674,7 +681,7 @@ public: streamWrite("AT", cmd..., GSM_NL); stream.flush(); TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); + DBG("### AT:", cmd...); } // TODO: Optimize this! @@ -682,12 +689,12 @@ public: 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 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);*/ + DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s); data.reserve(64); int index = 0; unsigned long startMillis = millis(); @@ -743,6 +750,7 @@ finish: } data = ""; } + DBG('<', index, '>'); return index; } diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index 3652277..7755428 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -172,10 +172,10 @@ public: String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED; private: - TinyGsmESP8266* at; - uint8_t mux; - bool sock_connected; - RxFifo rx; + TinyGsmESP8266* at; + uint8_t mux; + bool sock_connected; + RxFifo rx; }; //============================================================================// @@ -257,8 +257,8 @@ public: for (unsigned long start = millis(); millis() - start < timeout; ) { sendAT(GF("")); if (waitResponse(200) == 1) { - delay(100); - return true; + delay(100); + return true; } delay(100); } @@ -313,7 +313,6 @@ public: bool sleepEnable(bool enable = true) TINY_GSM_ATTR_NOT_IMPLEMENTED; - /* * SIM card functions */ @@ -323,13 +322,13 @@ public: * Generic network functions */ - RegStatus getRegistrationStatus() { - sendAT(GF("+CIPSTATUS")); + RegStatus getRegistrationStatus() { + sendAT(GF("+CIPSTATUS")); if (waitResponse(3000, GF("STATUS:")) != 1) return REG_UNKNOWN; int status = waitResponse(GFP(GSM_ERROR), GF("2"), GF("3"), GF("4"), GF("5")); - waitResponse(); // Returns an OK after the status + waitResponse(); // Returns an OK after the status return (RegStatus)status; - } + } int getSignalQuality() { sendAT(GF("+CWJAP_CUR?")); diff --git a/src/TinyGsmClientM590.h b/src/TinyGsmClientM590.h index cbe95b6..7a107ca 100644 --- a/src/TinyGsmClientM590.h +++ b/src/TinyGsmClientM590.h @@ -54,6 +54,7 @@ class TinyGsmM590 //============================================================================// //============================================================================// + public: class GsmClient : public Client @@ -172,10 +173,10 @@ public: String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED; private: - TinyGsmM590* at; - uint8_t mux; - bool sock_connected; - RxFifo rx; + TinyGsmM590* at; + uint8_t mux; + bool sock_connected; + RxFifo rx; }; //============================================================================// @@ -236,8 +237,8 @@ public: for (unsigned long start = millis(); millis() - start < timeout; ) { sendAT(GF("")); if (waitResponse(200) == 1) { - delay(100); - return true; + delay(100); + return true; } delay(100); } @@ -349,10 +350,10 @@ public: int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK")); waitResponse(); switch (status) { - case 2: - case 3: return SIM_LOCKED; - case 1: return SIM_READY; - default: return SIM_ERROR; + case 2: + case 3: return SIM_LOCKED; + case 1: return SIM_READY; + default: return SIM_ERROR; } } return SIM_ERROR; @@ -373,16 +374,16 @@ public: * Generic network functions */ - 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; - } + 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; + } int getSignalQuality() { sendAT(GF("+CSQ")); diff --git a/src/TinyGsmClientM95.h b/src/TinyGsmClientM95.h index e7d4308..e3142fc 100644 --- a/src/TinyGsmClientM95.h +++ b/src/TinyGsmClientM95.h @@ -9,13 +9,15 @@ #ifndef TinyGsmClientM95_h #define TinyGsmClientM95_h -#define TINY_GSM_DEBUG Serial -#define TINY_GSM_USE_HEX +// #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 6 + #include #define GSM_NL "\r\n" @@ -37,721 +39,797 @@ enum RegStatus { REG_UNKNOWN = 4, }; +//============================================================================// +//============================================================================// +// Declaration of the TinyGsmM95 Class +//============================================================================// +//============================================================================// -class TinyGsm { - -public: - TinyGsm(Stream& stream) : stream(stream) {} - - class GsmClient : public Client { - friend class TinyGsm; - typedef TinyGsmFifo RxFifo; - - public: - GsmClient() {} +class TinyGsmM95 +{ - GsmClient(TinyGsm& modem, uint8_t mux = 1) { - init(&modem, mux); - } - - bool init(TinyGsm* modem, uint8_t mux = 1) { - this->at = modem; - this->mux = mux; - sock_available = 0; - sock_connected = false; +//============================================================================// +//============================================================================// +// The M95 Client Class +//============================================================================// +//============================================================================// - at->sockets[mux] = this; - return true; - } +public: - virtual int connect(const char *host, uint16_t port) { - TINY_GSM_YIELD(); - rx.clear(); - sock_connected = at->modemConnect(host, port, mux); - return sock_connected; - } +class GsmClient : public Client +{ + friend class TinyGsmM95; + typedef TinyGsmFifo RxFifo; - 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); - } +public: + GsmClient() {} - virtual void stop() { - TINY_GSM_YIELD(); - at->sendAT(GF("+QICLOSE")); - sock_connected = false; - at->waitResponse(60000L, GF("CLOSED"), GF("CLOSE OK"), GF("ERROR")); - } + GsmClient(TinyGsmM95& modem, uint8_t mux = 1) { + init(&modem, mux); + } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - return at->modemSend(buf, size, mux); - } + bool init(TinyGsmM95* modem, uint8_t mux = 1) { + this->at = modem; + this->mux = mux; + sock_available = 0; + sock_connected = false; + got_data = false; - virtual size_t write(uint8_t c) { - return write(&c, 1); - } + at->sockets[mux] = this; - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size()) { - at->maintain(); - } - return rx.size() + sock_available; - } + return true; + } - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - size_t cnt = 0; - while (cnt < size) { - 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; - } +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("+QICLOSE="), mux); + sock_connected = false; + at->waitResponse(60000L, GF("CLOSED"), GF("CLOSE OK"), GF("ERROR")); + 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 int available() { + TINY_GSM_YIELD(); + if (!rx.size()) { + 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) { + 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: + TinyGsmM95* at; + uint8_t mux; + uint16_t sock_available; + bool sock_connected; + bool got_data; + RxFifo rx; +}; - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } +//============================================================================// +//============================================================================// +// The M95 Has No Secure Client +//============================================================================// +//============================================================================// - 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(); } - - private: - TinyGsm* at; - uint8_t mux; - uint16_t sock_available; - bool sock_connected; - RxFifo rx; - }; +//============================================================================// +//============================================================================// +// The M95 Modem Functions +//============================================================================// +//============================================================================// - // Basic functions - bool begin() { - return init(); - } +public: - bool init() { - if (!autoBaud()) { - return false; - } - sendAT(GF("&FZE0")); // Factory + Reset + Echo Off - if (waitResponse() != 1) { - return false; - } - getSimStatus(); +#ifdef GSM_DEFAULT_STREAM + TinyGsmM95(Stream& stream = GSM_DEFAULT_STREAM) +#else + TinyGsmM95(Stream& stream) +#endif + : stream(stream) + { + memset(sockets, 0, sizeof(sockets)); + } + + /* + * Basic functions + */ + bool begin() { + return init(); + } + + bool init() { + if (!testAT()) { + return false; + } + sendAT(GF("&FZE0")); // Factory + Reset + 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) { + for (unsigned long start = millis(); millis() - start < timeout; ) { + sendAT(GF("")); + if (waitResponse(200) == 1) { + delay(100); return true; - } - - bool autoBaud(unsigned long timeout = 10000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - sendAT(GF("")); - if (waitResponse(200) == 1) { - delay(100); - return true; - } - delay(100); - } - return false; - } - - void maintain() { - 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; - } - - // Power functions - - bool restart() { - if (!autoBaud()) { - return false; - } - sendAT(GF("+CFUN=0")); - if (waitResponse(10000L, GF("NORMAL POWER DOWN"), GF("OK"), GF("FAIL")) == 3) { - return false; - } - sendAT(GF("+CFUN=1")); - if (waitResponse(10000L, GF("Call Ready"), GF("OK"), GF("FAIL")) == 3) { - return false; - } - return init(); - } - - // SIM card & Networ Operator 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; - } - - int getSignalQuality() { - sendAT(GF("+CSQ")); - if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) { - return 99; - } - int res = stream.readStringUntil(',').toInt(); - waitResponse(); - return res; - } - - String getClock() { - sendAT(GF("+QLTS")); - if (waitResponse(GF(GSM_NL "+QLTS:")) != 1) { - // Serial.println( F("No response from AT+QLTS") ); - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - 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; - } - - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - RegStatus s = getRegistrationStatus(); - if (s == REG_OK_HOME || s == REG_OK_ROAMING) { - return true; - } else if (s == REG_UNREGISTERED) { - return false; - } - delay(1000); - } - return false; - } - - void setHostFormat( bool useDottedQuad ) { - if ( useDottedQuad ) { - sendAT(GF("+QIDNSIP=0")); - } else { - sendAT(GF("+QIDNSIP=1")); - } - waitResponse(); - } - - // GPRS functions - bool gprsConnect(const char* apn, const char* user, const char* pwd) { - gprsDisconnect(); - - sendAT(GF("+QIFGCNT=0")); - waitResponse(); - - sendAT(GF("+QICSGP=1,\""), apn, GF("\",\""), user, GF("\",\""), pwd, GF("\"")); - waitResponse(); - - sendAT(GF("+QIREGAPP")); - waitResponse(); - - sendAT(GF("+QIACT")); - waitResponse(10000L); - + } + 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() { + return false; // TODO: For now + } + + /* + * Power functions + */ + + bool restart() { + if (!testAT()) { + return false; + } + sendAT(GF("+CFUN=0")); + if (waitResponse(10000L, GF("NORMAL POWER DOWN"), GF("OK"), GF("FAIL")) == 3) { + return false; + } + sendAT(GF("+CFUN=1")); + if (waitResponse(10000L, GF("Call Ready"), GF("OK"), GF("FAIL")) == 3) { + return false; + } + return init(); + } + + bool poweroff() { + sendAT(GF("+QPOWD")); + return waitResponse(GF("POWERED DOWN")) == 1; // TODO + } + + bool radioOff() { + sendAT(GF("+CFUN=0")); + if (waitResponse(10000L) != 1) { + return false; + } + delay(3000); + return true; + } + + /* + * 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; + } + + 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 + */ + + 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; + } + + 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; + } - bool gprsDisconnect() { - sendAT(GF("+QIDEACT")); - return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1; + void setHostFormat( bool useDottedQuad ) { + if ( useDottedQuad ) { + sendAT(GF("+QIDNSIP=0")); + } else { + sendAT(GF("+QIDNSIP=1")); } + waitResponse(); + } - // Phone Call functions + String getLocalIP() { + sendAT(GF("+CGPADDR=1")); + if (waitResponse(10000L, GF(GSM_NL "+CGPADDR:")) != 1) { + return ""; + } + streamSkipUntil(','); + String res = stream.readStringUntil('\n'); + if (waitResponse() != 1) { + return ""; + } + return res; + } + + IPAddress localIP() { + return TinyGsmIpFromString(getLocalIP()); + } - bool setGsmBusy(bool busy = true) { - sendAT(GF("+GSMBUSY="), busy ? 1 : 0); - return waitResponse() == 1; - } + /* + * WiFi functions + */ + + /* + * GPRS functions + */ + bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) { + gprsDisconnect(); - bool callAnswer() { - sendAT(GF("A")); - return waitResponse() == 1; + sendAT(GF("+QIFGCNT=0")); // Set the forground context + if (waitResponse() != 1) { + return false; } - bool callNumber(const String& number) { - sendAT(GF("D"), number); - return waitResponse() == 1; + sendAT(GF("+QICSGP=1,\""), apn, GF("\",\""), user, GF("\",\""), pwd, GF("\"")); + if (waitResponse() != 1) { + return false; } - bool callHangup(const String& number) { - sendAT(GF("H"), number); - return waitResponse() == 1; - } + sendAT(GF("+QIREGAPP")); + if (waitResponse() != 1) { + return false; + } - // Messaging functions + sendAT(GF("+QIACT")); + waitResponse(10000L); + + return true; + } - bool sendSMS(const String& number, const String& text) { - sendAT(GF("+CMGF=1")); - waitResponse(); - sendAT(GF("+CMGS=\""), number, GF("\"")); - if (waitResponse(GF(">")) != 1) { - return false; - } - stream.print(text); - stream.write((char)0x1A); - 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); - return waitResponse(60000L) == 1; + bool gprsDisconnect() { + sendAT(GF("+QIDEACT")); + return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1; + } + + 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; + + return localIP() != 0; + } + + /* + * Messaging functions + */ + + String sendUSSD(const String& code) TINY_GSM_ATTR_NOT_IMPLEMENTED; + + bool sendSMS(const String& number, const String& text) { + sendAT(GF("+CMGF=1")); + 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; } - void sendUSSD() {} + 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; + } + + void sendUSSD() {} + + void sendSMS() {} + + /** Delete all SMS */ + bool deleteAllSMS() { + sendAT(GF("+QMGDA=6")); + if (waitResponse(waitResponse(60000L, GF("OK"), GF("ERROR")) == 1) ) { + return true; + } + return false; + } + - void sendSMS() {} + /* + * Location functions + */ + void getLocation() {} - /** Delete all SMS */ - bool deleteAllSMS() { - sendAT(GF("+QMGDA=6")); - if (waitResponse(waitResponse(60000L, GF("OK"), GF("ERROR")) == 1) ) { - return true; - } - return false; - } - - // Location functions - void getLocation() {} - - String getGsmLocation() { - sendAT(GF("+CIPGSMLOC=1,1")); - if (waitResponse(GF(GSM_NL "+CIPGSMLOC:")) != 1) { - return ""; - } - String res = stream.readStringUntil('\n'); - waitResponse(); - res.trim(); - return res; - } - - // Battery functions - - // HTTP functions - void httpGet( const char *url ) { - int len = strlen( url ); - sendAT( GF("+QHTTPURL="), len); - delay( 500 ); - streamWrite(url, GSM_NL); - stream.flush(); - waitResponse(5000L, GF("OK")); - - sendAT( GF("+QHTTPGET")); - waitResponse(30000L); - - String data; - sendAT( GF("+QHTTPREAD")); - waitResponse(30000L, data); - } + String getGsmLocation() { + sendAT(GF("+CIPGSMLOC=1,1")); + if (waitResponse(GF(GSM_NL "+CIPGSMLOC:")) != 1) { + return ""; + } + String res = stream.readStringUntil('\n'); + waitResponse(); + res.trim(); + 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: - Stream& stream; - GsmClient* sockets[5]; - - int modemConnect(const char* host, uint16_t port, uint8_t mux) { - sendAT(GF("+QIOPEN="), GF("\"TCP"), GF("\",\""), host, GF("\","), port); - int rsp = waitResponse(75000L, - GF("CONNECT OK" GSM_NL), - GF("CONNECT FAIL" GSM_NL), - GF("ALREADY CONNECT" GSM_NL)); - if ( rsp != 1 ) { - return 0; - } - return (1 == rsp); - } - int modemSend(const void* buff, size_t len, uint8_t mux) { - sendAT(GF("+QIHEAD=1")); - waitResponse(5000L); - - sendAT(GF("+QISEND="),len); - if (waitResponse(GF(">")) != 1) { - return -1; - } - stream.write((uint8_t*)buff, len); - if (waitResponse(GF(GSM_NL "SEND OK")) != 1) { - return -1; - } - - bool allAcknowledged = false; - // bool failed = false; - while ( !allAcknowledged ) { - sendAT( GF("+QISACK")); - if (waitResponse(5000L, GF(GSM_NL "+QISACK:")) != 1) { - return -1; - } else { - streamSkipUntil(','); /** Skip total */ - streamSkipUntil(','); /** Skip acknowledged data size */ - if ( stream.readStringUntil('\n').toInt() == 0 ) { - allAcknowledged = true; - } - } - } - waitResponse(5000L); + bool modemConnect(const char* host, uint16_t port, uint8_t mux) { + sendAT(GF("+QIOPEN="), GF("\"TCP"), GF("\",\""), host, GF("\","), port); + int rsp = waitResponse(75000L, + GF("CONNECT OK" GSM_NL), + GF("CONNECT FAIL" GSM_NL), + GF("ALREADY CONNECT" GSM_NL)); + if ( rsp != 1 ) { + return false; + } + return (1 == rsp); + } + + int modemSend(const void* buff, size_t len, uint8_t mux) { + sendAT(GF("+QIHEAD=1")); + waitResponse(5000L); + + sendAT(GF("+QISEND="), mux, ',', len); + if (waitResponse(GF(">")) != 1) { + return 0; + } + stream.write((uint8_t*)buff, len); + stream.flush(); + if (waitResponse(GF(GSM_NL "SEND OK")) != 1) { + return 0; + } + + bool allAcknowledged = false; + // bool failed = false; + while ( !allAcknowledged ) { + sendAT( GF("+QISACK")); + if (waitResponse(5000L, GF(GSM_NL "+QISACK:")) != 1) { + return -1; + } else { + streamSkipUntil(','); /** Skip total */ + streamSkipUntil(','); /** Skip acknowledged data size */ + if ( stream.readStringUntil('\n').toInt() == 0 ) { + allAcknowledged = true; + } + } + } + waitResponse(5000L); + + // streamSkipUntil(','); // Skip mux + // return stream.readStringUntil('\n').toInt(); + return 1; + } + + size_t modemRead(size_t size, uint8_t mux) { + sendAT(GF("+QIRD="), mux, ',', size); + if (waitResponse(GF("+QIRD:")) != 1) { + return 0; + } + size_t len = stream.readStringUntil('\n').toInt(); + + for (size_t i=0; irx.put(c); + } + waitResponse(); + DBG("### READ:", mux, ",", len); + return len; + } + + size_t modemGetAvailable(uint8_t mux) { + sendAT(GF("+QIRD="), mux, GF(",0")); + size_t result = 0; + if (waitResponse(GF("+QIRD:")) == 1) { + streamSkipUntil(','); // Skip total received + streamSkipUntil(','); // Skip have read + result = stream.readStringUntil('\n').toInt(); + DBG("### STILL:", mux, "has", result); + waitResponse(); + } + if (!result) { + sockets[mux]->sock_connected = modemGetConnected(mux); + } + return result; + } + + bool modemGetConnected(uint8_t mux) { + sendAT(GF("+QISTATE=1,"), mux); + //+QISTATE: 0,"TCP","151.139.237.11",80,5087,4,1,0,0,"uart1" + + if (waitResponse(GF("+QISTATE:"))) + return false; + + streamSkipUntil(','); // Skip mux + streamSkipUntil(','); // Skip socket type + streamSkipUntil(','); // Skip remote ip + streamSkipUntil(','); // Skip remote port + streamSkipUntil(','); // Skip local port + int res = stream.readStringUntil(',').toInt(); // socket state + + waitResponse(); + + // 0 Initial, 1 Opening, 2 Connected, 3 Listening, 4 Closing + return 2 == res; + } - // streamSkipUntil(','); // Skip mux - // return stream.readStringUntil('\n').toInt(); - return 1; - } - - 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; - } - - // Utilities - template - void streamWrite(T last) { - stream.print(last); - } +public: - template - void streamWrite(T head, Args... tail) { - stream.print(head); - streamWrite(tail...); - } + /* Utilities */ - int streamRead() { return stream.read(); } + template + void streamWrite(T last) { + stream.print(last); + } - bool streamSkipUntil(char c, int timeoutInMs=1000) { - unsigned long startTime = millis(); - unsigned long t = millis(); - while ( t < (startTime + timeoutInMs) ) { - while (!stream.available() && (t < (startTime + timeoutInMs)) ) { - t = millis(); - delay( 250 ); - } - if ( stream.available() ) { - if (stream.read() == c) { - return true; - } - } - t = millis(); - } - return false; - } + template + void streamWrite(T head, Args... tail) { + stream.print(head); + streamWrite(tail...); + } - template - void sendAT(Args... cmd) { - streamWrite("AT", cmd..., GSM_NL); - stream.flush(); + bool streamSkipUntil(char c) { + const unsigned long timeout = 1000L; + unsigned long startMillis = millis(); + while (millis() - startMillis < timeout) { + while (millis() - startMillis < timeout && !stream.available()) { 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); - bool gotData = false; - int mux = -1; - int index = 0; - unsigned long startMillis = millis(); - do { - TINY_GSM_YIELD(); - while (stream.available() > 0) { - int a = streamRead(); - if (a <= 0) continue; // Skip 0x00 bytes, just in case - Serial.print((char)a); - data += (char)a; - if (r1 && data.endsWith(r1)) { - index = 1; - DBG("<<< Got1 '", String(r1), "'"); - goto finish; - } else if (r2 && data.endsWith(r2)) { - index = 2; - DBG("<<< Got2 '", String(r2), "'"); - goto finish; - } else if (r3 && data.endsWith(r3)) { - index = 3; - DBG("<<< Got3 '", String(r3), "'"); - goto finish; - } else if (r4 && data.endsWith(r4)) { - index = 4; - DBG("<<< Got4 '", String(r4), "'"); - goto finish; - } else if (r5 && data.endsWith(r5)) { - index = 5; - DBG("<<< Got5 '", String(r5), "'"); - goto finish; - } else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) { - String mode = stream.readStringUntil(','); - if (mode.toInt() == 1) { - mux = stream.readStringUntil('\n').toInt(); - gotData = 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); - mux = data.substring(nl+2, coma).toInt(); - if (mux) { - sockets[mux]->sock_connected = false; - data = ""; - } - } - } - } while (millis() - startMillis < timeout); - finish: - if (!index) { - data.trim(); - if (data.length()) { - DBG("### Unhandled:", data); - } - data = ""; - } - if (gotData) { - sockets[mux]->sock_available = modemGetAvailable(mux); - } - 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); + } + if (stream.read() == c) + return true; } - - void printResponse(uint32_t timeout) { - unsigned long startMillis = millis(); - do { - TINY_GSM_YIELD(); - while (stream.available() > 0) { - int a = streamRead(); - if (a <= 0) continue; // Skip 0x00 bytes, just in case - Serial.print((char)a); + 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 "+QIURC:"))) { + stream.readStringUntil('\"'); + String urc = stream.readStringUntil('\"'); + stream.readStringUntil(','); + if (urc == "recv") { + int mux = stream.readStringUntil('\n').toInt(); + DBG("### URC RECV:", mux); + if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) { + sockets[mux]->got_data = true; } - } while (millis() - startMillis < timeout); - } - - uint16_t waitForHTTPResponse(uint32_t timeout, char * buffer, uint8_t bufferSize) { - unsigned long startMillis = millis(); - uint8_t bufferIndex = 0; - uint16_t httpCode = 0; - do { - TINY_GSM_YIELD(); - while (stream.available() > 0) { - int a = streamRead(); - if (a <= 0) continue; // Skip 0x00 bytes, just in case - Serial.print((char)a); - if (bufferIndex < bufferSize-1) { // leave room to null-terminate - buffer[bufferIndex] = (char)a; - bufferIndex++; - } - if ((char)a == '\n') { - // null-terminate buffer! - buffer[bufferIndex] = '\0'; - if (strstr((const char *)buffer, "CLOSED") != NULL) { - return httpCode; - } - if(httpCode == 0 && strstr((const char *)buffer, "HTTP/1.") != NULL) { - strtok((char *)buffer, " "); /** Parse out HTTP/1.x */ - - char * codeString = strtok(NULL, " "); - httpCode = atoi((const char *)codeString); - } - bufferIndex = 0; - } + } else if (urc == "closed") { + int mux = stream.readStringUntil('\n').toInt(); + DBG("### URC CLOSE:", mux); + if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) { + sockets[mux]->sock_connected = false; } - } while (millis() - startMillis < timeout); + } else { + stream.readStringUntil('\n'); + } + data = ""; + } + } + } while (millis() - startMillis < timeout); +finish: + if (!index) { + data.trim(); + if (data.length()) { + DBG("### Unhandled:", data); + } + data = ""; + } + DBG('<', index, '>'); + 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); + } - return httpCode; - } -}; +public: + Stream& stream; -typedef TinyGsm::GsmClient TinyGsmClient; +protected: + GsmClient* sockets[TINY_GSM_MUX_COUNT]; +}; #endif diff --git a/src/TinyGsmClientSIM800.h b/src/TinyGsmClientSIM800.h index 5ba81ed..8132ebf 100644 --- a/src/TinyGsmClientSIM800.h +++ b/src/TinyGsmClientSIM800.h @@ -273,8 +273,8 @@ public: for (unsigned long start = millis(); millis() - start < timeout; ) { sendAT(GF("")); if (waitResponse(200) == 1) { - delay(100); - return true; + delay(100); + return true; } delay(100); } @@ -425,10 +425,10 @@ public: 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; + case 2: + case 3: return SIM_LOCKED; + case 1: return SIM_READY; + default: return SIM_ERROR; } } return SIM_ERROR; @@ -449,16 +449,16 @@ public: * Generic network functions */ - 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; - } + 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; + } int getSignalQuality() { sendAT(GF("+CSQ")); diff --git a/src/TinyGsmClientUBLOX.h b/src/TinyGsmClientUBLOX.h index f034d77..68347e0 100644 --- a/src/TinyGsmClientUBLOX.h +++ b/src/TinyGsmClientUBLOX.h @@ -39,7 +39,6 @@ enum RegStatus { REG_UNKNOWN = 4, }; - //============================================================================// //============================================================================// // Declaration of the TinyGsmU201 Class @@ -177,12 +176,12 @@ public: String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED; private: - TinyGsmU201* at; - uint8_t mux; - uint16_t sock_available; - bool sock_connected; - bool got_data; - RxFifo rx; + TinyGsmU201* at; + uint8_t mux; + uint16_t sock_available; + bool sock_connected; + bool got_data; + RxFifo rx; }; //============================================================================// @@ -212,7 +211,6 @@ public: } }; - //============================================================================// //============================================================================// // The U201 Modem Functions @@ -375,10 +373,10 @@ public: 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; + case 2: + case 3: return SIM_LOCKED; + case 1: return SIM_READY; + default: return SIM_ERROR; } } return SIM_ERROR; @@ -399,16 +397,16 @@ public: * Generic network functions */ - RegStatus getRegistrationStatus() { - sendAT(GF("+CGREG?")); - if (waitResponse(GF(GSM_NL "+CGREG:")) != 1) { - return REG_UNKNOWN; - } - streamSkipUntil(','); // Skip format (0) - int status = stream.readStringUntil('\n').toInt(); - waitResponse(); - return (RegStatus)status; - } + RegStatus getRegistrationStatus() { + sendAT(GF("+CGREG?")); + if (waitResponse(GF(GSM_NL "+CGREG:")) != 1) { + return REG_UNKNOWN; + } + streamSkipUntil(','); // Skip format (0) + int status = stream.readStringUntil('\n').toInt(); + waitResponse(); + return (RegStatus)status; + } int getSignalQuality() { sendAT(GF("+CSQ")); @@ -521,19 +519,6 @@ public: return localIP() != 0; } - - /* - * Phone Call functions - */ - - bool setGsmBusy(bool busy = true) TINY_GSM_ATTR_NOT_AVAILABLE; - - bool callAnswer() TINY_GSM_ATTR_NOT_IMPLEMENTED; - - bool callNumber(const String& number) TINY_GSM_ATTR_NOT_IMPLEMENTED; - - bool callHangup() TINY_GSM_ATTR_NOT_IMPLEMENTED; - /* * Messaging functions */ @@ -721,7 +706,7 @@ public: TINY_GSM_YIELD(); while (stream.available() > 0) { int a = stream.read(); - if (a < 0) continue; + if (a <= 0) continue; // Skip 0x00 bytes, just in case data += (char)a; if (r1 && data.endsWith(r1)) { index = 1; diff --git a/src/TinyGsmClientXBee.h b/src/TinyGsmClientXBee.h index 650b4c4..dbacf7a 100644 --- a/src/TinyGsmClientXBee.h +++ b/src/TinyGsmClientXBee.h @@ -44,7 +44,6 @@ enum XBeeType { XBEE3_LTENB = 3, // Digi XBee3™ Cellular NB-IoT -- HS unknown to SRGD }; - //============================================================================// //============================================================================// // Declaration of the TinyGsmXBee Class @@ -170,9 +169,9 @@ public: String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED; private: - TinyGsmXBee* at; - uint8_t mux; - bool sock_connected; + TinyGsmXBee* at; + uint8_t mux; + bool sock_connected; }; //============================================================================// @@ -665,8 +664,6 @@ fail: protected: -private: - bool modemConnect(const char* host, uint16_t port, uint8_t mux = 0, bool ssl = false) { String strIP; strIP.reserve(16); unsigned long startMillis = millis();