Skip to content

Commit

Permalink
Fixed ESP socket status
Browse files Browse the repository at this point in the history
  • Loading branch information
SRGDamia1 committed Aug 27, 2019
1 parent dac86a2 commit be6304d
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions src/TinyGsmClientESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,25 +278,26 @@ TINY_GSM_MODEM_MAINTAIN_LISTEN()

bool isNetworkConnected() {
RegStatus s = getRegistrationStatus();
return (s == REG_OK_IP || s == REG_OK_TCP);
}

bool waitForNetwork(unsigned long timeout_ms = 60000L) {
for (unsigned long start = millis(); millis() - start < timeout_ms; ) {
sendAT(GF("+CIPSTATUS"));
int res1 = waitResponse(3000, GF("busy p..."), GF("STATUS:"));
if (res1 == 2) {
int res2 = waitResponse(GFP(GSM_ERROR), GF("2"), GF("3"), GF("4"), GF("5"));
if (res2 == 2 || res2 == 3) {
waitResponse();
return true;
}
}
delay(250);
if (s == REG_OK_IP || s == REG_OK_TCP) {
// with these, we're definitely connected
return true;
}
else if (s == REG_OK_NO_TCP) {
// with this, we may or may not be connected
if (getLocalIP() == "") {
return false;
}
else {
return true;
}
}
else {
return false;
}
return false;
}

TINY_GSM_MODEM_WAIT_FOR_NETWORK()

/*
* WiFi functions
*/
Expand Down Expand Up @@ -386,8 +387,37 @@ TINY_GSM_MODEM_MAINTAIN_LISTEN()
}

bool modemGetConnected(uint8_t mux) {
RegStatus s = getRegistrationStatus();
return (s == REG_OK_IP || s == REG_OK_TCP);
sendAT(GF("+CIPSTATUS"));
if (waitResponse(3000, GF("STATUS:")) != 1) return REG_UNKNOWN;
int status =
waitResponse(GFP(GSM_ERROR), GF("2"), GF("3"), GF("4"), GF("5"));
if (status != 3) {
// if the status is anything but 3, there are no connections open
waitResponse(); // Returns an OK after the status
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
sockets[muxNo]->sock_connected = false;
}
return false;
}
bool verified_connections[TINY_GSM_MUX_COUNT] = {0, 0, 0, 0, 0};
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
uint8_t has_status = waitResponse(GF("+CIPSTATUS:"), GFP(GSM_OK), GFP(GSM_ERROR));
if (has_status == 1) {
size_t returned_mux = stream.readStringUntil(',').toInt();
streamSkipUntil(','); // Skip mux
streamSkipUntil(','); // Skip type
streamSkipUntil(','); // Skip remote IP
streamSkipUntil(','); // Skip remote port
streamSkipUntil(','); // Skip local port
streamSkipUntil('\n'); // Skip client/server type
verified_connections[returned_mux] = 1;
}
if (has_status == 2) break; // once we get to the ok, stop
}
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
sockets[muxNo]->sock_connected = verified_connections[muxNo];
}
return verified_connections[mux];
}

public:
Expand Down

0 comments on commit be6304d

Please sign in to comment.