From 4aa643c22a578b29308858a8f55a27ad1fc5d838 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Wed, 27 Sep 2017 23:43:30 +0300 Subject: [PATCH] Add testAT, setBaud, isGprsConnected, isNetworkConnected --- TinyGsmClientA6.h | 104 +++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/TinyGsmClientA6.h b/TinyGsmClientA6.h index f3895de..d24b76d 100644 --- a/TinyGsmClientA6.h +++ b/TinyGsmClientA6.h @@ -182,7 +182,7 @@ public: } bool init() { - if (!autoBaud()) { + if (!testAT()) { return false; } sendAT(GF("&FZE0")); // Factory + Reset + Echo Off @@ -196,9 +196,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; @@ -238,7 +242,7 @@ public: */ bool restart() { - if (!autoBaud()) { + if (!testAT()) { return false; } sendAT(GF("+RST=1")); @@ -251,6 +255,10 @@ public: return waitResponse() == 1; } + bool radioOff() TINY_GSM_ATTR_NOT_IMPLEMENTED; + + bool sleepEnable(bool enable = true) TINY_GSM_ATTR_NOT_IMPLEMENTED; + /* * SIM card functions */ @@ -337,13 +345,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; } @@ -383,7 +395,31 @@ public: bool gprsDisconnect() { sendAT(GF("+CIPSHUT")); - return waitResponse(60000L) == 1; + if (waitResponse(60000L) != 1) + return false; + + sendAT(GF("+CGATT=0")); + 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")); // TODO: check this + if (waitResponse() != 1) + return false; + + return true; } String getLocalIP() { @@ -461,9 +497,9 @@ public: int dcs = stream.readStringUntil('\n').toInt(); if (dcs == 15) { - return decodeHex7bit(hex); + return TinyGsmDecodeHex7bit(hex); } else if (dcs == 72) { - return decodeHex16bit(hex); + return TinyGsmDecodeHex16bit(hex); } else { return hex; } @@ -506,7 +542,7 @@ public: return res; } -private: +protected: bool modemConnect(const char* host, uint16_t port, uint8_t* mux) { sendAT(GF("+CIPSTART="), GF("\"TCP"), GF("\",\""), host, GF("\","), port); @@ -548,50 +584,6 @@ private: return 1 == res; } - static String decodeHex7bit(String &instr) { - String result; - byte reminder = 0; - int bitstate = 7; - for (unsigned i=0; i> bitstate; - bitstate--; - if (bitstate == 0) { - char c = reminder; - result += c; - reminder = 0; - bitstate = 7; - } - } - return result; - } - - static String decodeHex16bit(String &instr) { - String result; - for (unsigned i=0; i