Working to stop ESP from hanging

This commit is contained in:
SRGDamia1
2017-05-05 17:01:03 -04:00
parent 9cab85e167
commit 2055c4568f
5 changed files with 39 additions and 37 deletions

View File

@@ -493,7 +493,7 @@ public:
if (!index) { if (!index) {
data.trim(); data.trim();
if (data.length()) { if (data.length()) {
DBG("### Unhandled:", data); DBG(GSM_NL, "### Unhandled:", data);
} }
} }
else { else {
@@ -512,13 +512,13 @@ public:
DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free()); DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free());
} }
while (len--) { while (len--) {
char c[2] = {0}; TINY_GSM_YIELD();
stream.readBytes(c, 1); // readBytes includes a timeout int r = stream.read();
if(c[0]) sockets[mux]->rx.put(c[0]); if (r <= 0) continue; // Skip 0x00 bytes, just in case
// DBG(GSM_NL, c[0], " ", len, " ", stream.available(), " ", sockets[mux]->available()); sockets[mux]->rx.put((char)r);
} }
if (len_orig > sockets[mux]->available()) { 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; return index;

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 512 #define TINY_GSM_RX_BUFFER 256
#endif #endif
#include <TinyGsmCommon.h> #include <TinyGsmCommon.h>
@@ -20,6 +20,7 @@
#define GSM_NL "\r\n" #define GSM_NL "\r\n"
static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL; static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL;
static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL; static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL;
static unsigned int TCP_KEEP_ALIVE = 120;
class TinyGsm class TinyGsm
{ {
@@ -78,6 +79,7 @@ public:
at->sendAT(GF("+CIPCLOSE="), mux); at->sendAT(GF("+CIPCLOSE="), mux);
sock_connected = false; sock_connected = false;
at->waitResponse(); at->waitResponse();
at->waitResponse();
} }
virtual size_t write(const uint8_t *buf, size_t size) { virtual size_t write(const uint8_t *buf, size_t size) {
@@ -324,7 +326,7 @@ public:
if (!index) { if (!index) {
data.trim(); data.trim();
if (data.length()) { if (data.length()) {
DBG("### Unhandled:", data); DBG(GSM_NL, "### Unhandled:", data);
} }
} }
else { else {
@@ -335,23 +337,23 @@ public:
DBG(GSM_NL, "<<< ", data); DBG(GSM_NL, "<<< ", data);
} }
} }
if (gotData) { if (gotData) {
int len_orig = len; int len_orig = len;
if (len > sockets[mux]->rx.free()) { if (len > sockets[mux]->rx.free()) {
DBG(GSM_NL, "### Buffer overflow: ", len, "->", sockets[mux]->rx.free()); DBG(GSM_NL, "### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
} else { } else {
DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free()); 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);
}
} }
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;
} }
@@ -371,11 +373,12 @@ public:
private: private:
int modemConnect(const char* host, uint16_t port, uint8_t mux) { 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, int rsp = waitResponse(75000L,
GFP(GSM_OK), GFP(GSM_OK),
GFP(GSM_ERROR), GFP(GSM_ERROR),
GF(GSM_NL "ALREADY CONNECT" GSM_NL)); GF(GSM_NL "ALREADY CONNECT" GSM_NL));
waitResponse(100, GF("1,CONNECT"));
return (1 == rsp); return (1 == rsp);
} }

View File

@@ -500,7 +500,7 @@ public:
if (!index) { if (!index) {
data.trim(); data.trim();
if (data.length()) { if (data.length()) {
DBG("### Unhandled:", data); DBG(GSM_NL, "### Unhandled:", data);
} }
} }
else { else {
@@ -519,13 +519,13 @@ public:
DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free()); DBG(GSM_NL, "### Got: ", len, "->", sockets[mux]->rx.free());
} }
while (len--) { while (len--) {
char c[2] = {0}; TINY_GSM_YIELD();
stream.readBytes(c, 1); // readBytes includes a timeout int r = stream.read();
if(c[0]) sockets[mux]->rx.put(c[0]); if (r <= 0) continue; // Skip 0x00 bytes, just in case
// DBG(GSM_NL, c[0], " ", len, " ", stream.available(), " ", sockets[mux]->available()); sockets[mux]->rx.put((char)r);
} }
if (len_orig > sockets[mux]->available()) { 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; return index;

View File

@@ -604,7 +604,7 @@ public:
if (!index) { if (!index) {
data.trim(); data.trim();
if (data.length()) { if (data.length()) {
DBG("### Unhandled:", data); DBG(GSM_NL, "### Unhandled:", data);
} }
} }
else { else {

View File

@@ -433,8 +433,8 @@ public:
data.replace(GSM_NL GSM_NL, GSM_NL); data.replace(GSM_NL GSM_NL, GSM_NL);
data.replace(GSM_NL, "\r\n" " "); data.replace(GSM_NL, "\r\n" " ");
if (data.length()) { if (data.length()) {
DBG("### Unhandled:", data); DBG("### Unhandled:", data, "\r\n");
} } else DBG(GSM_NL, "### NO RESPONSE!");
} }
else { else {
data.trim(); data.trim();
@@ -444,9 +444,6 @@ public:
DBG("<<< ", data, "\r\n"); DBG("<<< ", data, "\r\n");
} }
} }
// if (gotData) {
// sockets[mux]->sock_available = modemGetAvailable(mux);
// }
return index; return index;
} }
@@ -518,12 +515,14 @@ private:
} }
int streamRead() { int streamRead() {
TINY_GSM_YIELD();
int c = stream.read(); int c = stream.read();
DBG((char)c); DBG((char)c);
return c; return c;
} }
String streamReadUntil(char c) { String streamReadUntil(char c) {
TINY_GSM_YIELD();
String return_string = stream.readStringUntil(c); String return_string = stream.readStringUntil(c);
return_string.trim(); return_string.trim();
if (String(c) == GSM_NL){ if (String(c) == GSM_NL){