Skip to content

Commit

Permalink
Add explicit stop() at the end of the client loop
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelstoer committed Jan 12, 2020
1 parent dcf8a19 commit 8fc8058
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 61 deletions.
7 changes: 3 additions & 4 deletions examples/PlaneSpotterDemo/AdsbExchangeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ void AdsbExchangeClient::updateVisibleAircraft(String searchQuery) {
int pos = 0;
boolean isBody = false;
char c;

int size = 0;
client.setNoDelay(false);
while(client.available() || client.connected()) {
while((size = client.available()) > 0) {
while (client.available() || client.connected()) {
while (client.available()) {
c = client.read();
if (c == '{' || c == '[') {
isBody = true;
Expand All @@ -55,6 +53,7 @@ void AdsbExchangeClient::updateVisibleAircraft(String searchQuery) {
parser.parse(c);
}
}
client.stop();
}
endDocument();
}
Expand Down
16 changes: 8 additions & 8 deletions src/AerisForecasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ void AerisForecasts::doUpdate(AerisForecastData *forecasts, String url, uint8_t
http.begin(url);
bool isBody = false;
char c;
int size;
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
Expand All @@ -58,13 +57,13 @@ void AerisForecasts::doUpdate(AerisForecastData *forecasts, String url, uint8_t

WiFiClient * client = http.getStreamPtr();

while(client->available() || client->connected()) {
while((size = client->available()) > 0) {
if ((millis() - lost_do) > lostTest) {
Serial.println ("lost in client with a timeout");
client->stop();
ESP.restart();
}
while (client->available() || client->connected()) {
while (client->available()) {
if ((millis() - lost_do) > lostTest) {
Serial.println("lost in client with a timeout");
client->stop();
ESP.restart();
}
c = client->read();
if (c == '{' || c == '[') {

Expand All @@ -74,6 +73,7 @@ void AerisForecasts::doUpdate(AerisForecastData *forecasts, String url, uint8_t
parser.parse(c);
}
}
client->stop();
}
}
this->forecasts = nullptr;
Expand Down
15 changes: 8 additions & 7 deletions src/AerisObservations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ void AerisObservations::doUpdate(AerisObservationsData *observations, String url

WiFiClient * client = http.getStreamPtr();

while(client->available() || client->connected()) {
while((size = client->available()) > 0) {
if ((millis() - lost_do) > lostTest) {
Serial.println ("lost in client with a timeout");
client->stop();
ESP.restart();
}
while (client->available() || client->connected()) {
while (client->available()) {
if ((millis() - lost_do) > lostTest) {
Serial.println("lost in client with a timeout");
client->stop();
ESP.restart();
}
c = client->read();
if (c == '{' || c == '[') {

Expand All @@ -72,6 +72,7 @@ void AerisObservations::doUpdate(AerisObservationsData *observations, String url
parser.parse(c);
}
}
client->stop();
}
}
this->observations = nullptr;
Expand Down
16 changes: 8 additions & 8 deletions src/AerisSunMoon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String url) {
http.begin(url);
bool isBody = false;
char c;
int size;
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
Expand All @@ -57,13 +56,13 @@ void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String url) {

WiFiClient * client = http.getStreamPtr();

while(client->available() || client->connected()) {
while((size = client->available()) > 0) {
if ((millis() - lost_do) > lostTest) {
Serial.println ("lost in client with a timeout");
client->stop();
ESP.restart();
}
while (client->available() || client->connected()) {
while (client->available()) {
if ((millis() - lost_do) > lostTest) {
Serial.println("lost in client with a timeout");
client->stop();
ESP.restart();
}
c = client->read();
if (c == '{' || c == '[') {

Expand All @@ -73,6 +72,7 @@ void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String url) {
parser.parse(c);
}
}
client->stop();
}
}
this->sunMoonData = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/MetOfficeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ void MetOfficeClient::doUpdate(String url) {
boolean isBody = false;
char c;

int size = 0;
client.setNoDelay(false);
while(client.connected()) {
while((size = client.available()) > 0) {
while (client.connected()) {
while (client.available()) {
c = client.read();
if (c == '{' || c == '[') {
isBody = true;
Expand All @@ -108,6 +107,7 @@ void MetOfficeClient::doUpdate(String url) {
parser.parse(c);
}
}
client.stop();
}
}

Expand Down
17 changes: 8 additions & 9 deletions src/OpenWeatherMapCurrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url
http.begin(url);
bool isBody = false;
char c;
int size;
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
Expand All @@ -65,16 +64,15 @@ void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url

WiFiClient * client = http.getStreamPtr();

while(client->connected() || client->available()) {
while((size = client->available()) > 0) {
if ((millis() - lost_do) > lostTest) {
Serial.println ("lost in client with a timeout");
client->stop();
ESP.restart();
}
while (client->connected() || client->available()) {
while (client->available()) {
if ((millis() - lost_do) > lostTest) {
Serial.println("lost in client with a timeout");
client->stop();
ESP.restart();
}
c = client->read();
if (c == '{' || c == '[') {

isBody = true;
}
if (isBody) {
Expand All @@ -83,6 +81,7 @@ void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url
// give WiFi and TCP/IP libraries a chance to handle pending events
yield();
}
client->stop();
}
}
this->data = nullptr;
Expand Down
17 changes: 8 additions & 9 deletions src/OpenWeatherMapForecast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, Strin
http.begin(url);
bool isBody = false;
char c;
int size;
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
Expand All @@ -68,16 +67,15 @@ uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, Strin

WiFiClient * client = http.getStreamPtr();

while(client->connected() || client->available()) {
while((size = client->available()) > 0) {
if ((millis() - lost_do) > lostTest) {
Serial.println ("lost in client with a timeout");
client->stop();
ESP.restart();
}
while (client->connected() || client->available()) {
while (client->available()) {
if ((millis() - lost_do) > lostTest) {
Serial.println("lost in client with a timeout");
client->stop();
ESP.restart();
}
c = client->read();
if (c == '{' || c == '[') {

isBody = true;
}
if (isBody) {
Expand All @@ -86,6 +84,7 @@ uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, Strin
// give WiFi and TCP/IP libraries a chance to handle pending events
yield();
}
client->stop();
}
}
this->data = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/ThingspeakClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ void ThingspeakClient::getLastChannelItem(String channelId, String readApiKey) {
boolean isBody = false;
char c;

int size = 0;
client.setNoDelay(false);
while(client.available() || client.connected()) {
while((size = client.available()) > 0) {
while (client.available() || client.connected()) {
while (client.available()) {
c = client.read();
if (c == '{' || c == '[') {
isBody = true;
Expand All @@ -55,6 +54,7 @@ void ThingspeakClient::getLastChannelItem(String channelId, String readApiKey) {
parser.parse(c);
}
}
client.stop();
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/TimeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,15 @@ void TimeClient::updateTime() {
}

String line;

int size = 0;
client.setNoDelay(false);
while(client.available() || client.connected()) {
while((size = client.available()) > 0) {
while (client.available() || client.connected()) {
while (client.available()) {
line = client.readStringUntil('\n');
line.toUpperCase();
// example:
// date: Thu, 19 Nov 2015 20:25:40 GMT
if (line.startsWith("DATE: ")) {
Serial.println(line.substring(23, 25) + ":" + line.substring(26, 28) + ":" +line.substring(29, 31));
Serial.println(line.substring(23, 25) + ":" + line.substring(26, 28) + ":" + line.substring(29, 31));
int parsedHours = line.substring(23, 25).toInt();
int parsedMinutes = line.substring(26, 28).toInt();
int parsedSeconds = line.substring(29, 31).toInt();
Expand All @@ -74,8 +72,8 @@ void TimeClient::updateTime() {
localMillisAtUpdate = millis();
}
}
client.stop();
}

}

String TimeClient::getHours() {
Expand Down
7 changes: 3 additions & 4 deletions src/WorldClockClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ void WorldClockClient::updateTime() {
int pos = 0;
boolean isBody = false;
char c;

int size = 0;
client.setNoDelay(false);
while(client.available() || client.connected()) {
while((size = client.available()) > 0) {
while (client.available() || client.connected()) {
while (client.available()) {
c = client.read();
if (c == '{' || c == '[') {
isBody = true;
Expand All @@ -103,6 +101,7 @@ void WorldClockClient::updateTime() {
parser.parse(c);
}
}
client.stop();
}
}

Expand Down

0 comments on commit 8fc8058

Please sign in to comment.