diff --git a/src/TinyGsmClientA6.h b/src/TinyGsmClientA6.h index ff829ce..e4db461 100644 --- a/src/TinyGsmClientA6.h +++ b/src/TinyGsmClientA6.h @@ -218,7 +218,7 @@ public: void maintain() { //while (stream.available()) { - waitResponse(50, NULL, NULL); + waitResponse(10, NULL, NULL); //} } diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index a479bc5..057a410 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -210,7 +210,7 @@ public: } void maintain() { - waitResponse(50, NULL, NULL); + waitResponse(10, NULL, NULL); } bool factoryDefault() { diff --git a/src/TinyGsmClientM590.h b/src/TinyGsmClientM590.h index 9ccd90e..08c9e80 100644 --- a/src/TinyGsmClientM590.h +++ b/src/TinyGsmClientM590.h @@ -214,7 +214,7 @@ public: void maintain() { //while (stream.available()) { - waitResponse(50, NULL, NULL); + waitResponse(10, NULL, NULL); //} } diff --git a/src/TinyGsmClientSIM800.h b/src/TinyGsmClientSIM800.h index c246b00..8124cda 100644 --- a/src/TinyGsmClientSIM800.h +++ b/src/TinyGsmClientSIM800.h @@ -253,7 +253,7 @@ public: } } while (stream.available()) { - waitResponse(50, NULL, NULL); + waitResponse(10, NULL, NULL); } } diff --git a/src/TinyGsmClientU201.h b/src/TinyGsmClientU201.h index e0fcd5a..ddd2f99 100644 --- a/src/TinyGsmClientU201.h +++ b/src/TinyGsmClientU201.h @@ -247,7 +247,7 @@ public: } } while (stream.available()) { - waitResponse(50, NULL, NULL); + waitResponse(10, NULL, NULL); } } diff --git a/src/TinyGsmClientXBee.h b/src/TinyGsmClientXBee.h index fa4637e..28d875a 100644 --- a/src/TinyGsmClientXBee.h +++ b/src/TinyGsmClientXBee.h @@ -73,19 +73,25 @@ public: public: virtual int connect(const char *host, uint16_t port) { at->streamClear(); // Empty anything remaining in the buffer; - at->commandMode(); - sock_connected = at->modemConnect(host, port, mux, false); - at->writeChanges(); - at->exitCommand(); + bool sock_connected = false; + if (at->commandMode()) { // Don't try if we didn't successfully get into command mode + sock_connected = at->modemConnect(host, port, mux, false); + at->writeChanges(); + at->exitCommand(); + } + at->streamClear(); // Empty anything remaining in the buffer; return sock_connected; } virtual int connect(IPAddress ip, uint16_t port) { at->streamClear(); // Empty anything remaining in the buffer; - at->commandMode(); - sock_connected = at->modemConnect(ip, port, mux, false); - at->writeChanges(); - at->exitCommand(); + bool sock_connected = false; + if (at->commandMode()) { // Don't try if we didn't successfully get into command mode + sock_connected = at->modemConnect(ip, port, mux, false); + at->writeChanges(); + at->exitCommand(); + } + at->streamClear(); // Empty anything remaining in the buffer; return sock_connected; } @@ -168,19 +174,25 @@ public: public: virtual int connect(const char *host, uint16_t port) { at->streamClear(); // Empty anything remaining in the buffer; - at->commandMode(); - sock_connected = at->modemConnect(host, port, mux, true); - at->writeChanges(); - at->exitCommand(); + bool sock_connected = false; + if (at->commandMode()) { // Don't try if we didn't successfully get into command mode + sock_connected = at->modemConnect(host, port, mux, true); + at->writeChanges(); + at->exitCommand(); + } + at->streamClear(); // Empty anything remaining in the buffer; return sock_connected; } virtual int connect(IPAddress ip, uint16_t port) { at->streamClear(); // Empty anything remaining in the buffer; - at->commandMode(); - sock_connected = at->modemConnect(ip, port, mux, true); - at->writeChanges(); - at->exitCommand(); + bool sock_connected = false; + if (at->commandMode()) { // Don't try if we didn't successfully get into command mode + sock_connected = at->modemConnect(ip, port, mux, true); + at->writeChanges(); + at->exitCommand(); + } + at->streamClear(); // Empty anything remaining in the buffer; return sock_connected; } }; @@ -484,12 +496,17 @@ fail: private: int modemConnect(const char* host, uint16_t port, uint8_t mux = 0, bool ssl = false) { - sendAT(GF("LA"), host); String strIP; strIP.reserve(16); - // wait for the response unsigned long startMillis = millis(); - while (stream.available() < 8 && millis() - startMillis < 30000) {}; - strIP = streamReadUntil('\r'); // read result + bool gotIP = false; + while (!gotIP && millis() - startMillis < 30000) + { + sendAT(GF("LA"), host); + while (stream.available() < 4) {};// wait for the response + strIP = streamReadUntil('\r'); // read result + if (!strIP.endsWith(GF("ERROR"))) gotIP = true; + delay(100); // short wait before trying again + } IPAddress ip = TinyGsmIpFromString(strIP); return modemConnect(ip, port, mux, ssl); } @@ -563,7 +580,7 @@ public: bool commandMode(void) { delay(guardTime); // cannot send anything for 1 second before entering command mode streamWrite(GF("+++")); // enter command mode - // DBG("\r\n+++\r\n"); + // DBG("\r\n+++"); return 1 == waitResponse(guardTime*2); } @@ -584,7 +601,7 @@ public: streamWrite("AT", cmd..., GSM_NL); stream.flush(); TINY_GSM_YIELD(); - //DBG("### AT:", cmd...); + // DBG("### AT:", cmd...); } // TODO: Optimize this! @@ -657,7 +674,7 @@ finish: 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 waitResponse(5000, r1, r2, r3, r4, r5); } private: