Added WaitForNetwork and too much debugging to ESP8266

This commit is contained in:
SRGDamia1
2017-05-02 18:16:06 -04:00
parent 0b4e1317c8
commit f84789a49b

View File

@@ -12,7 +12,7 @@
//#define TINY_GSM_DEBUG Serial //#define TINY_GSM_DEBUG Serial
#if !defined(TINY_GSM_RX_BUFFER) #if !defined(TINY_GSM_RX_BUFFER)
#define TINY_GSM_RX_BUFFER 256 #define TINY_GSM_RX_BUFFER 512
#endif #endif
#include <TinyGsmCommon.h> #include <TinyGsmCommon.h>
@@ -202,8 +202,20 @@ public:
} }
bool waitForNetwork(unsigned long timeout = 60000L) { bool waitForNetwork(unsigned long timeout = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF("+CIPSTATUS"));
String res1 = stream.readStringUntil(':');
DBG(GSM_NL, res1, ':');
String res2 = stream.readStringUntil(*GSM_NL);
DBG(res2);
waitResponse();
if (res2 == GF("2") || res2 == GF("3") || res2 == GF("4")) {
return true; return true;
} }
delay(1000);
}
return false;
}
/* /*
* WiFi functions * WiFi functions
@@ -265,6 +277,9 @@ public:
String r5s(r5); r5s.trim(); String r5s(r5); r5s.trim();
DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/ DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
data.reserve(64); data.reserve(64);
bool gotData = false;
int mux = -1;
int len = 0;
int index = 0; int index = 0;
unsigned long startMillis = millis(); unsigned long startMillis = millis();
do { do {
@@ -289,22 +304,19 @@ public:
index = 5; index = 5;
goto finish; goto finish;
} else if (data.endsWith(GF(GSM_NL "+IPD,"))) { } else if (data.endsWith(GF(GSM_NL "+IPD,"))) {
int mux = stream.readStringUntil(',').toInt(); mux = stream.readStringUntil(',').toInt();
int len = stream.readStringUntil(':').toInt(); data += mux;
if (len > sockets[mux]->rx.free()) { data += (',');
DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); len = stream.readStringUntil(':').toInt();
} else { data += len;
DBG("### Got: ", len, "->", sockets[mux]->rx.free()); data += (':');
} gotData = true;
while (len--) { index = 6;
while (!stream.available()) {} goto finish;
sockets[mux]->rx.put(stream.read()); } else if (data.endsWith(GF("1,CLOSED" GSM_NL))) { //TODO: use mux
}
data = "";
return index;
} else if (data.endsWith(GF(GSM_NL "1,CLOSED" GSM_NL))) { //TODO: use mux
sockets[1]->sock_connected = false; sockets[1]->sock_connected = false;
data = ""; index = 7;
goto finish;
} }
} }
} while (millis() - startMillis < timeout); } while (millis() - startMillis < timeout);
@@ -314,7 +326,31 @@ public:
if (data.length()) { if (data.length()) {
DBG("### Unhandled:", data); DBG("### Unhandled:", data);
} }
data = ""; }
else {
data.trim();
data.replace(GSM_NL GSM_NL, GSM_NL);
data.replace(GSM_NL, GSM_NL " ");
if (data.length()) {
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; return index;
} }