Skip to content

Commit 8fc8058

Browse files
committed
Add explicit stop() at the end of the client loop
Fixes #174 As per https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/client-examples.html this is now required.
1 parent dcf8a19 commit 8fc8058

10 files changed

+56
-61
lines changed

examples/PlaneSpotterDemo/AdsbExchangeClient.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ void AdsbExchangeClient::updateVisibleAircraft(String searchQuery) {
4242
int pos = 0;
4343
boolean isBody = false;
4444
char c;
45-
46-
int size = 0;
4745
client.setNoDelay(false);
48-
while(client.available() || client.connected()) {
49-
while((size = client.available()) > 0) {
46+
while (client.available() || client.connected()) {
47+
while (client.available()) {
5048
c = client.read();
5149
if (c == '{' || c == '[') {
5250
isBody = true;
@@ -55,6 +53,7 @@ void AdsbExchangeClient::updateVisibleAircraft(String searchQuery) {
5553
parser.parse(c);
5654
}
5755
}
56+
client.stop();
5857
}
5958
endDocument();
6059
}

src/AerisForecasts.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ void AerisForecasts::doUpdate(AerisForecastData *forecasts, String url, uint8_t
4949
http.begin(url);
5050
bool isBody = false;
5151
char c;
52-
int size;
5352
Serial.print("[HTTP] GET...\n");
5453
// start connection and send HTTP header
5554
int httpCode = http.GET();
@@ -58,13 +57,13 @@ void AerisForecasts::doUpdate(AerisForecastData *forecasts, String url, uint8_t
5857

5958
WiFiClient * client = http.getStreamPtr();
6059

61-
while(client->available() || client->connected()) {
62-
while((size = client->available()) > 0) {
63-
if ((millis() - lost_do) > lostTest) {
64-
Serial.println ("lost in client with a timeout");
65-
client->stop();
66-
ESP.restart();
67-
}
60+
while (client->available() || client->connected()) {
61+
while (client->available()) {
62+
if ((millis() - lost_do) > lostTest) {
63+
Serial.println("lost in client with a timeout");
64+
client->stop();
65+
ESP.restart();
66+
}
6867
c = client->read();
6968
if (c == '{' || c == '[') {
7069

@@ -74,6 +73,7 @@ void AerisForecasts::doUpdate(AerisForecastData *forecasts, String url, uint8_t
7473
parser.parse(c);
7574
}
7675
}
76+
client->stop();
7777
}
7878
}
7979
this->forecasts = nullptr;

src/AerisObservations.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ void AerisObservations::doUpdate(AerisObservationsData *observations, String url
5656

5757
WiFiClient * client = http.getStreamPtr();
5858

59-
while(client->available() || client->connected()) {
60-
while((size = client->available()) > 0) {
61-
if ((millis() - lost_do) > lostTest) {
62-
Serial.println ("lost in client with a timeout");
63-
client->stop();
64-
ESP.restart();
65-
}
59+
while (client->available() || client->connected()) {
60+
while (client->available()) {
61+
if ((millis() - lost_do) > lostTest) {
62+
Serial.println("lost in client with a timeout");
63+
client->stop();
64+
ESP.restart();
65+
}
6666
c = client->read();
6767
if (c == '{' || c == '[') {
6868

@@ -72,6 +72,7 @@ void AerisObservations::doUpdate(AerisObservationsData *observations, String url
7272
parser.parse(c);
7373
}
7474
}
75+
client->stop();
7576
}
7677
}
7778
this->observations = nullptr;

src/AerisSunMoon.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String url) {
4848
http.begin(url);
4949
bool isBody = false;
5050
char c;
51-
int size;
5251
Serial.print("[HTTP] GET...\n");
5352
// start connection and send HTTP header
5453
int httpCode = http.GET();
@@ -57,13 +56,13 @@ void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String url) {
5756

5857
WiFiClient * client = http.getStreamPtr();
5958

60-
while(client->available() || client->connected()) {
61-
while((size = client->available()) > 0) {
62-
if ((millis() - lost_do) > lostTest) {
63-
Serial.println ("lost in client with a timeout");
64-
client->stop();
65-
ESP.restart();
66-
}
59+
while (client->available() || client->connected()) {
60+
while (client->available()) {
61+
if ((millis() - lost_do) > lostTest) {
62+
Serial.println("lost in client with a timeout");
63+
client->stop();
64+
ESP.restart();
65+
}
6766
c = client->read();
6867
if (c == '{' || c == '[') {
6968

@@ -73,6 +72,7 @@ void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String url) {
7372
parser.parse(c);
7473
}
7574
}
75+
client->stop();
7676
}
7777
}
7878
this->sunMoonData = nullptr;

src/MetOfficeClient.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ void MetOfficeClient::doUpdate(String url) {
9696
boolean isBody = false;
9797
char c;
9898

99-
int size = 0;
10099
client.setNoDelay(false);
101-
while(client.connected()) {
102-
while((size = client.available()) > 0) {
100+
while (client.connected()) {
101+
while (client.available()) {
103102
c = client.read();
104103
if (c == '{' || c == '[') {
105104
isBody = true;
@@ -108,6 +107,7 @@ void MetOfficeClient::doUpdate(String url) {
108107
parser.parse(c);
109108
}
110109
}
110+
client.stop();
111111
}
112112
}
113113

src/OpenWeatherMapCurrent.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url
5656
http.begin(url);
5757
bool isBody = false;
5858
char c;
59-
int size;
6059
Serial.print("[HTTP] GET...\n");
6160
// start connection and send HTTP header
6261
int httpCode = http.GET();
@@ -65,16 +64,15 @@ void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url
6564

6665
WiFiClient * client = http.getStreamPtr();
6766

68-
while(client->connected() || client->available()) {
69-
while((size = client->available()) > 0) {
70-
if ((millis() - lost_do) > lostTest) {
71-
Serial.println ("lost in client with a timeout");
72-
client->stop();
73-
ESP.restart();
74-
}
67+
while (client->connected() || client->available()) {
68+
while (client->available()) {
69+
if ((millis() - lost_do) > lostTest) {
70+
Serial.println("lost in client with a timeout");
71+
client->stop();
72+
ESP.restart();
73+
}
7574
c = client->read();
7675
if (c == '{' || c == '[') {
77-
7876
isBody = true;
7977
}
8078
if (isBody) {
@@ -83,6 +81,7 @@ void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url
8381
// give WiFi and TCP/IP libraries a chance to handle pending events
8482
yield();
8583
}
84+
client->stop();
8685
}
8786
}
8887
this->data = nullptr;

src/OpenWeatherMapForecast.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, Strin
5959
http.begin(url);
6060
bool isBody = false;
6161
char c;
62-
int size;
6362
Serial.print("[HTTP] GET...\n");
6463
// start connection and send HTTP header
6564
int httpCode = http.GET();
@@ -68,16 +67,15 @@ uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, Strin
6867

6968
WiFiClient * client = http.getStreamPtr();
7069

71-
while(client->connected() || client->available()) {
72-
while((size = client->available()) > 0) {
73-
if ((millis() - lost_do) > lostTest) {
74-
Serial.println ("lost in client with a timeout");
75-
client->stop();
76-
ESP.restart();
77-
}
70+
while (client->connected() || client->available()) {
71+
while (client->available()) {
72+
if ((millis() - lost_do) > lostTest) {
73+
Serial.println("lost in client with a timeout");
74+
client->stop();
75+
ESP.restart();
76+
}
7877
c = client->read();
7978
if (c == '{' || c == '[') {
80-
8179
isBody = true;
8280
}
8381
if (isBody) {
@@ -86,6 +84,7 @@ uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, Strin
8684
// give WiFi and TCP/IP libraries a chance to handle pending events
8785
yield();
8886
}
87+
client->stop();
8988
}
9089
}
9190
this->data = nullptr;

src/ThingspeakClient.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ void ThingspeakClient::getLastChannelItem(String channelId, String readApiKey) {
4343
boolean isBody = false;
4444
char c;
4545

46-
int size = 0;
4746
client.setNoDelay(false);
48-
while(client.available() || client.connected()) {
49-
while((size = client.available()) > 0) {
47+
while (client.available() || client.connected()) {
48+
while (client.available()) {
5049
c = client.read();
5150
if (c == '{' || c == '[') {
5251
isBody = true;
@@ -55,6 +54,7 @@ void ThingspeakClient::getLastChannelItem(String channelId, String readApiKey) {
5554
parser.parse(c);
5655
}
5756
}
57+
client.stop();
5858
}
5959
}
6060

src/TimeClient.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,15 @@ void TimeClient::updateTime() {
5353
}
5454

5555
String line;
56-
57-
int size = 0;
5856
client.setNoDelay(false);
59-
while(client.available() || client.connected()) {
60-
while((size = client.available()) > 0) {
57+
while (client.available() || client.connected()) {
58+
while (client.available()) {
6159
line = client.readStringUntil('\n');
6260
line.toUpperCase();
6361
// example:
6462
// date: Thu, 19 Nov 2015 20:25:40 GMT
6563
if (line.startsWith("DATE: ")) {
66-
Serial.println(line.substring(23, 25) + ":" + line.substring(26, 28) + ":" +line.substring(29, 31));
64+
Serial.println(line.substring(23, 25) + ":" + line.substring(26, 28) + ":" + line.substring(29, 31));
6765
int parsedHours = line.substring(23, 25).toInt();
6866
int parsedMinutes = line.substring(26, 28).toInt();
6967
int parsedSeconds = line.substring(29, 31).toInt();
@@ -74,8 +72,8 @@ void TimeClient::updateTime() {
7472
localMillisAtUpdate = millis();
7573
}
7674
}
75+
client.stop();
7776
}
78-
7977
}
8078

8179
String TimeClient::getHours() {

src/WorldClockClient.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,9 @@ void WorldClockClient::updateTime() {
9090
int pos = 0;
9191
boolean isBody = false;
9292
char c;
93-
94-
int size = 0;
9593
client.setNoDelay(false);
96-
while(client.available() || client.connected()) {
97-
while((size = client.available()) > 0) {
94+
while (client.available() || client.connected()) {
95+
while (client.available()) {
9896
c = client.read();
9997
if (c == '{' || c == '[') {
10098
isBody = true;
@@ -103,6 +101,7 @@ void WorldClockClient::updateTime() {
103101
parser.parse(c);
104102
}
105103
}
104+
client.stop();
106105
}
107106
}
108107

0 commit comments

Comments
 (0)