From 2055c4568fe698f240dbfaec863f10a699492f82 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Fri, 5 May 2017 17:01:03 -0400 Subject: [PATCH] Working to stop ESP from hanging --- TinyGsmClientA6.h | 12 ++++++------ TinyGsmClientESP8266.h | 41 ++++++++++++++++++++++------------------- TinyGsmClientM590.h | 12 ++++++------ TinyGsmClientSIM800.h | 2 +- TinyGsmClientXBee.h | 9 ++++----- 5 files changed, 39 insertions(+), 37 deletions(-) diff --git a/TinyGsmClientA6.h b/TinyGsmClientA6.h index e7549bf..1e16bc5 100644 --- a/TinyGsmClientA6.h +++ b/TinyGsmClientA6.h @@ -493,7 +493,7 @@ public: if (!index) { data.trim(); if (data.length()) { - DBG("### Unhandled:", data); + DBG(GSM_NL, "### Unhandled:", data); } } else { @@ -512,13 +512,13 @@ public: DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free()); } while (len--) { - char c[2] = {0}; - stream.readBytes(c, 1); // readBytes includes a timeout - if(c[0]) sockets[mux]->rx.put(c[0]); - // DBG(GSM_NL, c[0], " ", len, " ", stream.available(), " ", sockets[mux]->available()); + TINY_GSM_YIELD(); + int r = stream.read(); + if (r <= 0) continue; // Skip 0x00 bytes, just in case + sockets[mux]->rx.put((char)r); } if (len_orig > sockets[mux]->available()) { - DBG(GSM_NL, "### Fewer characters received than expected: ", len_orig, "->", sockets[mux]->available()); + DBG(GSM_NL, "### Fewer characters received than expected: ", sockets[mux]->available(), " vs ", len_orig); } } return index; diff --git a/TinyGsmClientESP8266.h b/TinyGsmClientESP8266.h index a911f91..0013eea 100644 --- a/TinyGsmClientESP8266.h +++ b/TinyGsmClientESP8266.h @@ -12,7 +12,7 @@ //#define TINY_GSM_DEBUG Serial #if !defined(TINY_GSM_RX_BUFFER) - #define TINY_GSM_RX_BUFFER 512 + #define TINY_GSM_RX_BUFFER 256 #endif #include @@ -20,6 +20,7 @@ #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; +static unsigned int TCP_KEEP_ALIVE = 120; class TinyGsm { @@ -78,6 +79,7 @@ public: at->sendAT(GF("+CIPCLOSE="), mux); sock_connected = false; at->waitResponse(); + at->waitResponse(); } virtual size_t write(const uint8_t *buf, size_t size) { @@ -324,7 +326,7 @@ public: if (!index) { data.trim(); if (data.length()) { - DBG("### Unhandled:", data); + DBG(GSM_NL, "### Unhandled:", data); } } else { @@ -335,23 +337,23 @@ public: DBG(GSM_NL, "<<< ", data); } } - if (gotData) { - int len_orig = len; - if (len > sockets[mux]->rx.free()) { - DBG(GSM_NL, "### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); - } else { - DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free()); - } - while (len--) { - char c[2] = {0}; - stream.readBytes(c, 1); // readBytes includes a timeout - if(c[0]) sockets[mux]->rx.put(c[0]); - // DBG(GSM_NL, c[0], " ", len, " ", stream.available(), " ", sockets[mux]->available()); - } - if (len_orig > sockets[mux]->available()) { - DBG(GSM_NL, "### Fewer characters received than expected: ", len_orig, "->", sockets[mux]->available()); + if (gotData) { + int len_orig = len; + if (len > sockets[mux]->rx.free()) { + DBG(GSM_NL, "### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); + } else { + DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free()); + } + while (len--) { + TINY_GSM_YIELD(); + int r = stream.read(); + if (r <= 0) continue; // Skip 0x00 bytes, just in case + sockets[mux]->rx.put((char)r); + } + if (len_orig > sockets[mux]->available()) { + DBG(GSM_NL, "### Fewer characters received than expected: ", sockets[mux]->available(), " vs ", len_orig); + } } - } return index; } @@ -371,11 +373,12 @@ public: private: int modemConnect(const char* host, uint16_t port, uint8_t mux) { - sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port, GF(",120")); + sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port, GF(","), TCP_KEEP_ALIVE); int rsp = waitResponse(75000L, GFP(GSM_OK), GFP(GSM_ERROR), GF(GSM_NL "ALREADY CONNECT" GSM_NL)); + waitResponse(100, GF("1,CONNECT")); return (1 == rsp); } diff --git a/TinyGsmClientM590.h b/TinyGsmClientM590.h index 776091a..b83a7a2 100644 --- a/TinyGsmClientM590.h +++ b/TinyGsmClientM590.h @@ -500,7 +500,7 @@ public: if (!index) { data.trim(); if (data.length()) { - DBG("### Unhandled:", data); + DBG(GSM_NL, "### Unhandled:", data); } } else { @@ -519,13 +519,13 @@ public: DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free()); } while (len--) { - char c[2] = {0}; - stream.readBytes(c, 1); // readBytes includes a timeout - if(c[0]) sockets[mux]->rx.put(c[0]); - // DBG(GSM_NL, c[0], " ", len, " ", stream.available(), " ", sockets[mux]->available()); + TINY_GSM_YIELD(); + int r = stream.read(); + if (r <= 0) continue; // Skip 0x00 bytes, just in case + sockets[mux]->rx.put((char)r); } if (len_orig > sockets[mux]->available()) { - DBG(GSM_NL, "### Fewer characters received than expected: ", len_orig, "->", sockets[mux]->available()); + DBG(GSM_NL, "### Fewer characters received than expected: ", sockets[mux]->available(), " vs ", len_orig); } } return index; diff --git a/TinyGsmClientSIM800.h b/TinyGsmClientSIM800.h index 62c0bf9..3abbf96 100644 --- a/TinyGsmClientSIM800.h +++ b/TinyGsmClientSIM800.h @@ -604,7 +604,7 @@ public: if (!index) { data.trim(); if (data.length()) { - DBG("### Unhandled:", data); + DBG(GSM_NL, "### Unhandled:", data); } } else { diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 5ca7a8b..6a991ff 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -433,8 +433,8 @@ public: data.replace(GSM_NL GSM_NL, GSM_NL); data.replace(GSM_NL, "\r\n" " "); if (data.length()) { - DBG("### Unhandled:", data); - } + DBG("### Unhandled:", data, "\r\n"); + } else DBG(GSM_NL, "### NO RESPONSE!"); } else { data.trim(); @@ -444,9 +444,6 @@ public: DBG("<<< ", data, "\r\n"); } } - // if (gotData) { - // sockets[mux]->sock_available = modemGetAvailable(mux); - // } return index; } @@ -518,12 +515,14 @@ private: } int streamRead() { + TINY_GSM_YIELD(); int c = stream.read(); DBG((char)c); return c; } String streamReadUntil(char c) { + TINY_GSM_YIELD(); String return_string = stream.readStringUntil(c); return_string.trim(); if (String(c) == GSM_NL){