From 8bc626f4159f83b1eb2de2246aaab08d9bb2c007 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Thu, 4 May 2017 15:22:21 -0400 Subject: [PATCH 1/3] Totally different attempt --- TinyGsmClientA6.h | 8 +++--- TinyGsmClientESP8266.h | 5 ++-- TinyGsmClientM590.h | 58 +++++++++++++++++++++++++++--------------- TinyGsmClientSIM800.h | 5 ++-- TinyGsmClientXBee.h | 23 ++++++++--------- 5 files changed, 57 insertions(+), 42 deletions(-) diff --git a/TinyGsmClientA6.h b/TinyGsmClientA6.h index 4b9e80e..e41113b 100644 --- a/TinyGsmClientA6.h +++ b/TinyGsmClientA6.h @@ -476,13 +476,11 @@ public: while (!stream.available()) {} sockets[mux]->rx.put(stream.read()); } - data = ""; return index; } else if (data.endsWith(GF("+TCPCLOSED:"))) { int mux = streamReadUntil(',').toInt(); streamReadUntil('\n'); sockets[mux]->sock_connected = false; - data = ""; } } } while (millis() - startMillis < timeout); @@ -492,7 +490,6 @@ public: if (data.length()) { DBG("### Unhandled:", data); } - data = ""; } else { data.trim(); @@ -501,7 +498,6 @@ public: if (data.length()) { DBG(GSM_NL, "<<< ", data); } - data = ""; } return index; } @@ -521,8 +517,10 @@ public: } private: - int modemConnect(const char* host, uint16_t port, uint8_t* mux) { + int modemConnect(const char* host, uint16_t port, uint8_t* mux, bool isUDP=false) { sendAT(GF("+CIPSTART="), GF("\"TCP"), GF("\",\""), host, GF("\","), port); + if (isUDP) sendAT(GF("+CIPSTART="), GF("\"UDP"), GF("\",\""), host, GF("\","), port); + else sendAT(GF("+CIPSTART="), GF("\"TCP"), GF("\",\""), host, GF("\","), port); if (waitResponse(75000L, GF(GSM_NL "+CIPNUM:")) != 1) { return -1; diff --git a/TinyGsmClientESP8266.h b/TinyGsmClientESP8266.h index a911f91..989d8ca 100644 --- a/TinyGsmClientESP8266.h +++ b/TinyGsmClientESP8266.h @@ -370,8 +370,9 @@ 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")); + int modemConnect(const char* host, uint16_t port, uint8_t mux, bool isUDP=false) { + if (isUDP) sendAT(GF("+CIPSTART="), mux, ',', GF("\"UDP"), GF("\",\""), host, GF("\","), port, GF(",120")); + else sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port, GF(",120")); int rsp = waitResponse(75000L, GFP(GSM_OK), GFP(GSM_ERROR), diff --git a/TinyGsmClientM590.h b/TinyGsmClientM590.h index 7cee3fe..5e7490c 100644 --- a/TinyGsmClientM590.h +++ b/TinyGsmClientM590.h @@ -398,8 +398,8 @@ public: /* * Messaging functions */ - - void sendUSSD() { + bool gprsConnect(const char* apn, const char* user, const char* pwd) { + return false; } void sendSMS() { @@ -448,6 +448,9 @@ public: String r5s(r5); r5s.trim(); DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ data.reserve(64); + bool gotData = false; + int mux = -1; + int len = 0; int index = 0; unsigned long startMillis = millis(); do { @@ -472,24 +475,24 @@ public: index = 5; goto finish; } else if (data.endsWith(GF("+TCPRECV:"))) { - int mux = streamReadUntil(',').toInt(); - int len = streamReadUntil(',').toInt(); - if (len > sockets[mux]->rx.free()) { - DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); - } else { - DBG("### Got: ", len, "->", sockets[mux]->rx.free()); - } - while (len--) { - while (!stream.available()) {} - sockets[mux]->rx.put(stream.read()); - } - data = ""; - return index; + mux = stream.readStringUntil(',').toInt(); + data += mux; + data += (','); + len = stream.readStringUntil(',').toInt(); + data += len; + data += (','); + gotData = true; + index = 6; + goto finish; } else if (data.endsWith(GF("+TCPCLOSE:"))) { - int mux = streamReadUntil(',').toInt(); - streamReadUntil('\n'); + mux = stream.readStringUntil(',').toInt(); + data += mux; + data += (','); + String concl = stream.readStringUntil('\n'); + data += concl; sockets[mux]->sock_connected = false; - data = ""; + index = 7; + goto finish; } } } while (millis() - startMillis < timeout); @@ -499,7 +502,6 @@ public: if (data.length()) { DBG("### Unhandled:", data); } - data = ""; } else { data.trim(); @@ -508,7 +510,23 @@ public: if (data.length()) { DBG(GSM_NL, "<<< ", data); } - 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()); + } } return index; } diff --git a/TinyGsmClientSIM800.h b/TinyGsmClientSIM800.h index 62c0bf9..96276cb 100644 --- a/TinyGsmClientSIM800.h +++ b/TinyGsmClientSIM800.h @@ -636,8 +636,9 @@ public: } private: - int modemConnect(const char* host, uint16_t port, uint8_t mux) { - sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port); + int modemConnect(const char* host, uint16_t port, uint8_t mux, bool isUDP=false) { + if (isUDP) sendAT(GF("+CIPSTART="), mux, ',', GF("\"UDP"), GF("\",\""), host, GF("\","), port); + else sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port); int rsp = waitResponse(75000L, GF("CONNECT OK" GSM_NL), GF("CONNECT FAIL" GSM_NL), diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index e88f43d..0f89478 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -312,10 +312,6 @@ public: commandMode(); - sendAT(GF("AP"), 0); // Put in transparent mode - waitResponse(); - sendAT(GF("IP"), 1); // Put in TCP mode - waitResponse(); sendAT(GF("EE"), 2); // Set security to WPA2 waitResponse(); @@ -348,10 +344,6 @@ public: */ bool gprsConnect(const char* apn, const char* user = "", const char* pw = "") { commandMode(); - sendAT(GF("AP"), 0); // Put in transparent mode - waitResponse(); - sendAT(GF("IP"), 1); // Put in TCP mode - waitResponse(); sendAT(GF("AN"), apn); // Set the APN waitResponse(); writeChanges(); @@ -375,8 +367,6 @@ public: bool sendSMS(const String& number, const String& text) { commandMode(); - sendAT(GF("AP"), 0); // Put in transparent mode - waitResponse(); sendAT(GF("IP"), 2); // Put in text messaging mode waitResponse(); sendAT(GF("PH"), number); // Set the phone number @@ -475,16 +465,16 @@ public: } private: - int modemConnect(const char* host, uint16_t port, uint8_t mux = 1) { + int modemConnect(const char* host, uint16_t port, uint8_t mux = 1, bool isUDP=false) { sendAT(GF("LA"), host); String ipadd; ipadd.reserve(16); ipadd = streamReadUntil('\r'); IPAddress ip; ip.fromString(ipadd); - return modemConnect(ip, port); + return modemConnect(ip, port, mux, isUDP); } - int modemConnect(IPAddress ip, uint16_t port, uint8_t mux = 1) { + int modemConnect(IPAddress ip, uint16_t port, uint8_t mux = 1, bool isUDP=false) { String host; host.reserve(16); host += ip[0]; host += "."; @@ -493,6 +483,13 @@ private: host += ip[2]; host += "."; host += ip[3]; + if (isUDP) { + sendAT(GF("IP"), 0); // Put in UDP mode + waitResponse(); + } else { + sendAT(GF("IP"), 1); // Put in TCP mode + waitResponse(); + } sendAT(GF("DL"), host); waitResponse(); sendAT(GF("DE"), String(port, HEX)); From 7b0c0217d55cac81b212db4d2472282d70f8fee2 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Thu, 4 May 2017 15:27:38 -0400 Subject: [PATCH 2/3] Just some debugger changes --- TinyGsmClientA6.h | 50 ++++++++++++++++++++++++++++++++------------- TinyGsmClientM590.h | 4 ++-- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/TinyGsmClientA6.h b/TinyGsmClientA6.h index e41113b..79e3680 100644 --- a/TinyGsmClientA6.h +++ b/TinyGsmClientA6.h @@ -441,6 +441,9 @@ public: String r5s(r5); r5s.trim(); DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ data.reserve(64); + bool gotData = false; + int mux = -1; + int len = 0; int index = 0; unsigned long startMillis = millis(); do { @@ -465,22 +468,24 @@ public: index = 5; goto finish; } else if (data.endsWith(GF("+CIPRCV:"))) { - int mux = streamReadUntil(',').toInt(); - int len = streamReadUntil(',').toInt(); - if (len > sockets[mux]->rx.free()) { - DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); - } else { - DBG("### Got: ", len, "->", sockets[mux]->rx.free()); - } - while (len--) { - while (!stream.available()) {} - sockets[mux]->rx.put(stream.read()); - } - return index; + mux = stream.readStringUntil(',').toInt(); + data += mux; + data += (','); + len = stream.readStringUntil(',').toInt(); + data += len; + data += (','); + gotData = true; + index = 6; + goto finish; } else if (data.endsWith(GF("+TCPCLOSED:"))) { - int mux = streamReadUntil(',').toInt(); - streamReadUntil('\n'); + mux = stream.readStringUntil(',').toInt(); + data += mux; + data += (','); + String concl = stream.readStringUntil('\n'); + data += concl; sockets[mux]->sock_connected = false; + index = 7; + goto finish; } } } while (millis() - startMillis < timeout); @@ -499,6 +504,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()); + } + } return index; } diff --git a/TinyGsmClientM590.h b/TinyGsmClientM590.h index 5e7490c..776091a 100644 --- a/TinyGsmClientM590.h +++ b/TinyGsmClientM590.h @@ -398,8 +398,8 @@ public: /* * Messaging functions */ - bool gprsConnect(const char* apn, const char* user, const char* pwd) { - return false; + + void sendUSSD() { } void sendSMS() { From d06031a89c6a57da888e16c260cd1baa663bef60 Mon Sep 17 00:00:00 2001 From: SRGDamia1 Date: Thu, 4 May 2017 15:54:15 -0400 Subject: [PATCH 3/3] Removed everything but cleaning up the debugging --- TinyGsmClientA6.h | 4 +--- TinyGsmClientESP8266.h | 5 ++--- TinyGsmClientSIM800.h | 5 ++--- TinyGsmClientXBee.h | 19 +++++++------------ 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/TinyGsmClientA6.h b/TinyGsmClientA6.h index 79e3680..e7549bf 100644 --- a/TinyGsmClientA6.h +++ b/TinyGsmClientA6.h @@ -539,10 +539,8 @@ public: } private: - int modemConnect(const char* host, uint16_t port, uint8_t* mux, bool isUDP=false) { + int modemConnect(const char* host, uint16_t port, uint8_t* mux) { sendAT(GF("+CIPSTART="), GF("\"TCP"), GF("\",\""), host, GF("\","), port); - if (isUDP) sendAT(GF("+CIPSTART="), GF("\"UDP"), GF("\",\""), host, GF("\","), port); - else sendAT(GF("+CIPSTART="), GF("\"TCP"), GF("\",\""), host, GF("\","), port); if (waitResponse(75000L, GF(GSM_NL "+CIPNUM:")) != 1) { return -1; diff --git a/TinyGsmClientESP8266.h b/TinyGsmClientESP8266.h index 989d8ca..a911f91 100644 --- a/TinyGsmClientESP8266.h +++ b/TinyGsmClientESP8266.h @@ -370,9 +370,8 @@ public: } private: - int modemConnect(const char* host, uint16_t port, uint8_t mux, bool isUDP=false) { - if (isUDP) sendAT(GF("+CIPSTART="), mux, ',', GF("\"UDP"), GF("\",\""), host, GF("\","), port, GF(",120")); - else sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port, GF(",120")); + int modemConnect(const char* host, uint16_t port, uint8_t mux) { + sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port, GF(",120")); int rsp = waitResponse(75000L, GFP(GSM_OK), GFP(GSM_ERROR), diff --git a/TinyGsmClientSIM800.h b/TinyGsmClientSIM800.h index 96276cb..62c0bf9 100644 --- a/TinyGsmClientSIM800.h +++ b/TinyGsmClientSIM800.h @@ -636,9 +636,8 @@ public: } private: - int modemConnect(const char* host, uint16_t port, uint8_t mux, bool isUDP=false) { - if (isUDP) sendAT(GF("+CIPSTART="), mux, ',', GF("\"UDP"), GF("\",\""), host, GF("\","), port); - else sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port); + int modemConnect(const char* host, uint16_t port, uint8_t mux) { + sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port); int rsp = waitResponse(75000L, GF("CONNECT OK" GSM_NL), GF("CONNECT FAIL" GSM_NL), diff --git a/TinyGsmClientXBee.h b/TinyGsmClientXBee.h index 0f89478..5ca7a8b 100644 --- a/TinyGsmClientXBee.h +++ b/TinyGsmClientXBee.h @@ -465,16 +465,16 @@ public: } private: - int modemConnect(const char* host, uint16_t port, uint8_t mux = 1, bool isUDP=false) { + int modemConnect(const char* host, uint16_t port, uint8_t mux = 1) { sendAT(GF("LA"), host); String ipadd; ipadd.reserve(16); ipadd = streamReadUntil('\r'); IPAddress ip; ip.fromString(ipadd); - return modemConnect(ip, port, mux, isUDP); + return modemConnect(ip, port); } - int modemConnect(IPAddress ip, uint16_t port, uint8_t mux = 1, bool isUDP=false) { + int modemConnect(IPAddress ip, uint16_t port, uint8_t mux = 1) { String host; host.reserve(16); host += ip[0]; host += "."; @@ -483,16 +483,11 @@ private: host += ip[2]; host += "."; host += ip[3]; - if (isUDP) { - sendAT(GF("IP"), 0); // Put in UDP mode - waitResponse(); - } else { - sendAT(GF("IP"), 1); // Put in TCP mode - waitResponse(); - } - sendAT(GF("DL"), host); + sendAT(GF("IP"), 1); // Put in TCP mode + waitResponse(); + sendAT(GF("DL"), host); // Set the "Destination Address Low" waitResponse(); - sendAT(GF("DE"), String(port, HEX)); + sendAT(GF("DE"), String(port, HEX)); // Set the destination port int rsp = waitResponse(); return rsp; }