From 155b5f2b01a08a8864f46e29b10b0a05811755d5 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Wed, 27 Sep 2017 23:45:10 +0300 Subject: [PATCH] Add testAT, setBaud, sleepEnable, isGprsConnected, isNetworkConnected --- TinyGsmClientM590.h | 87 +++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/TinyGsmClientM590.h b/TinyGsmClientM590.h index bb8faca..0ced31b 100644 --- a/TinyGsmClientM590.h +++ b/TinyGsmClientM590.h @@ -179,7 +179,7 @@ public: } bool init() { - if (!autoBaud()) { + if (!testAT()) { return false; } sendAT(GF("&FZE0")); // Factory + Reset + Echo Off @@ -195,9 +195,13 @@ public: return true; } - bool autoBaud(unsigned long timeout = 10000L) { + 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("E0")); + sendAT(GF("")); if (waitResponse(200) == 1) { delay(100); return true; @@ -218,7 +222,7 @@ public: waitResponse(); sendAT(GF("+ICF=3,1")); // 8 data 0 parity 1 stop waitResponse(); - sendAT(GF("+enpwrsave=0")); // Disable PWR save + sendAT(GF("+ENPWRSAVE=0")); // Disable PWR save waitResponse(); sendAT(GF("+XISP=0")); // Use internal stack waitResponse(); @@ -243,7 +247,7 @@ public: */ bool restart() { - if (!autoBaud()) { + if (!testAT()) { return false; } sendAT(GF("+CFUN=15")); @@ -260,6 +264,13 @@ public: return waitResponse(3000L) == 1; } + bool radioOff() TINY_GSM_ATTR_NOT_IMPLEMENTED; + + bool sleepEnable(bool enable = true) { + sendAT(GF("+ENPWRSAVE="), enable); + return waitResponse() == 1; + } + /* * SIM card functions */ @@ -346,13 +357,17 @@ public: 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; ) { - RegStatus s = getRegistrationStatus(); - if (s == REG_OK_HOME || s == REG_OK_ROAMING) { + if (isNetworkConnected()) { return true; } - delay(1000); + delay(500); } return false; } @@ -396,8 +411,19 @@ public: } bool gprsDisconnect() { - sendAT(GF("+XIIC=0")); - return waitResponse(60000L) == 1; + // TODO: There is no command in AT command set + // XIIC=0 does not work + return true; + } + + bool isGprsConnected() { + sendAT(GF("+XIIC?")); + if (waitResponse(GF(GSM_NL "+XIIC:")) != 1) { + return false; + } + int res = stream.readStringUntil(',').toInt(); + waitResponse(); + return res == 1; } String getLocalIP() { @@ -453,9 +479,9 @@ public: } if (dcs == 15) { - return decodeHex8bit(hex); + return TinyGsmDecodeHex8bit(hex); } else if (dcs == 72) { - return decodeHex16bit(hex); + return TinyGsmDecodeHex16bit(hex); } else { return hex; } @@ -493,7 +519,7 @@ public: int getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE; -private: +protected: bool modemConnect(const char* host, uint16_t port, uint8_t mux) { for (int i=0; i<3; i++) { // TODO: no need for loop? @@ -548,37 +574,6 @@ private: return res; } - static String decodeHex8bit(String &instr) { - String result; - for (unsigned i=0; irx.put(stream.read()); } - if (len_orig > sockets[mux]->available()) { + if (len_orig > sockets[mux]->available()) { // TODO DBG(GSM_NL, "### Fewer characters received than expected: ", sockets[mux]->available(), " vs ", len_orig); } data = ""; @@ -699,7 +694,7 @@ finish: return waitResponse(1000, r1, r2, r3, r4, r5); } -private: +protected: Stream& stream; GsmClient* sockets[TINY_GSM_MUX_COUNT]; };