Skip to content

Commit 237442a

Browse files
authored
Change logic of reading HTTP response stream (#180)
Fixes #178
1 parent 8fc8058 commit 237442a

16 files changed

+153
-144
lines changed

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
"espressif8266",
1616
"espressif32"
1717
],
18-
"version": "2.0.0"
18+
"version": "2.0.1"
1919
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP8266 Weather Station
2-
version=2.0.0
2+
version=2.0.1
33
author=ThingPulse
44
maintainer=ThingPulse <[email protected]>
55
sentence=ESP8266 based internet connected Weather Station

src/AerisForecasts.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323

2424
#include <ESPWiFi.h>
2525
#include <WiFiClient.h>
26-
#include <ESPHTTPClient.h>
2726
#include "AerisForecasts.h"
2827

2928
AerisForecasts::AerisForecasts() {
3029

3130
}
3231

3332
void AerisForecasts::updateForecasts(AerisForecastData *forecasts, String clientId, String clientSecret, String location, uint8_t maxForecasts) {
34-
doUpdate(forecasts, "http://api.aerisapi.com/forecasts/closest?p=" + location + "&client_id=" + clientId + "&client_secret=" + clientSecret, maxForecasts);
33+
doUpdate(forecasts, "/forecasts/closest?p=" + location + "&client_id=" + clientId + "&client_secret=" + clientSecret, maxForecasts);
3534
}
3635

37-
void AerisForecasts::doUpdate(AerisForecastData *forecasts, String url, uint8_t maxForecasts) {
36+
void AerisForecasts::doUpdate(AerisForecastData *forecasts, String path, uint8_t maxForecasts) {
3837
this->maxForecasts = maxForecasts;
3938
this->currentForecast = 0;
4039
unsigned long lostTest = 10000UL;
@@ -43,38 +42,38 @@ void AerisForecasts::doUpdate(AerisForecastData *forecasts, String url, uint8_t
4342
this->forecasts = forecasts;
4443
JsonStreamingParser parser;
4544
parser.setListener(this);
46-
Serial.printf("Getting url: %s\n", url.c_str());
47-
HTTPClient http;
45+
Serial.printf("[HTTP] Requesting resource at http://%s:%u%s\n", host.c_str(), port, path.c_str());
4846

49-
http.begin(url);
50-
bool isBody = false;
51-
char c;
52-
Serial.print("[HTTP] GET...\n");
53-
// start connection and send HTTP header
54-
int httpCode = http.GET();
55-
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
56-
if(httpCode > 0) {
47+
WiFiClient client;
48+
if(client.connect(host, port)) {
49+
bool isBody = false;
50+
char c;
51+
Serial.println("[HTTP] connected, now GETting data");
52+
client.print("GET " + path + " HTTP/1.1\r\n"
53+
"Host: " + host + "\r\n"
54+
"Connection: close\r\n\r\n");
5755

58-
WiFiClient * client = http.getStreamPtr();
59-
60-
while (client->available() || client->connected()) {
61-
while (client->available()) {
56+
while (client.connected() || client.available()) {
57+
if (client.available()) {
6258
if ((millis() - lost_do) > lostTest) {
63-
Serial.println("lost in client with a timeout");
64-
client->stop();
59+
Serial.println("[HTTP] lost in client with a timeout");
60+
client.stop();
6561
ESP.restart();
6662
}
67-
c = client->read();
63+
c = client.read();
6864
if (c == '{' || c == '[') {
69-
7065
isBody = true;
7166
}
7267
if (isBody) {
7368
parser.parse(c);
7469
}
7570
}
76-
client->stop();
71+
// give WiFi and TCP/IP libraries a chance to handle pending events
72+
yield();
7773
}
74+
client.stop();
75+
} else {
76+
Serial.println("[HTTP] failed to connect to host");
7877
}
7978
this->forecasts = nullptr;
8079
}

src/AerisForecasts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ typedef struct AerisForecastData {
120120

121121
class AerisForecasts: public JsonListener {
122122
private:
123+
const String host = "api.aerisapi.com";
124+
const uint8_t port = 80;
123125
boolean isMetric = true;
124126
String currentKey;
125127
String currentParent;

src/AerisObservations.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,55 @@
2323

2424
#include <ESPWiFi.h>
2525
#include <WiFiClient.h>
26-
#include <ESPHTTPClient.h>
2726
#include "AerisObservations.h"
2827

2928
AerisObservations::AerisObservations() {
3029

3130
}
3231

3332
void AerisObservations::updateObservations(AerisObservationsData *observations, String clientId, String clientSecret, String location) {
34-
doUpdate(observations, "http://api.aerisapi.com/observations/closest?p=" + location + "&client_id=" + clientId + "&client_secret=" + clientSecret);
33+
doUpdate(observations, "/observations/closest?p=" + location + "&client_id=" + clientId + "&client_secret=" + clientSecret);
3534
}
3635

37-
void AerisObservations::doUpdate(AerisObservationsData *observations, String url) {
36+
void AerisObservations::doUpdate(AerisObservationsData *observations, String path) {
3837
unsigned long lostTest = 10000UL;
3938
unsigned long lost_do = millis();
4039

4140
this->observations = observations;
4241
JsonStreamingParser parser;
4342
parser.setListener(this);
44-
Serial.printf("Getting url: %s\n", url.c_str());
45-
HTTPClient http;
43+
Serial.printf("[HTTP] Requesting resource at http://%s:%u%s\n", host.c_str(), port, path.c_str());
4644

47-
http.begin(url);
48-
bool isBody = false;
49-
char c;
50-
int size;
51-
Serial.print("[HTTP] GET...\n");
52-
// start connection and send HTTP header
53-
int httpCode = http.GET();
54-
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
55-
if(httpCode > 0) {
45+
WiFiClient client;
46+
if(client.connect(host, port)) {
47+
bool isBody = false;
48+
char c;
49+
Serial.println("[HTTP] connected, now GETting data");
50+
client.print("GET " + path + " HTTP/1.1\r\n"
51+
"Host: " + host + "\r\n"
52+
"Connection: close\r\n\r\n");
5653

57-
WiFiClient * client = http.getStreamPtr();
58-
59-
while (client->available() || client->connected()) {
60-
while (client->available()) {
54+
while (client.connected() || client.available()) {
55+
if (client.available()) {
6156
if ((millis() - lost_do) > lostTest) {
62-
Serial.println("lost in client with a timeout");
63-
client->stop();
57+
Serial.println("[HTTP] lost in client with a timeout");
58+
client.stop();
6459
ESP.restart();
6560
}
66-
c = client->read();
61+
c = client.read();
6762
if (c == '{' || c == '[') {
68-
6963
isBody = true;
7064
}
7165
if (isBody) {
7266
parser.parse(c);
7367
}
7468
}
75-
client->stop();
69+
// give WiFi and TCP/IP libraries a chance to handle pending events
70+
yield();
7671
}
72+
client.stop();
73+
} else {
74+
Serial.println("[HTTP] failed to connect to host");
7775
}
7876
this->observations = nullptr;
7977
}

src/AerisObservations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ typedef struct AerisObservationsData {
8383

8484
class AerisObservations: public JsonListener {
8585
private:
86+
const String host = "api.aerisapi.com";
87+
const uint8_t port = 80;
8688
boolean isMetric = true;
8789
String currentKey;
8890
String currentParent;

src/AerisSunMoon.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,56 @@
2323

2424
#include <ESPWiFi.h>
2525
#include <WiFiClient.h>
26-
#include <ESPHTTPClient.h>
2726
#include "AerisSunMoon.h"
2827

2928
AerisSunMoon::AerisSunMoon() {
3029

3130
}
3231

3332
void AerisSunMoon::updateSunMoon(AerisSunMoonData *sunMoonData, String clientId, String clientSecret, String location) {
34-
doUpdate(sunMoonData, "http://api.aerisapi.com/sunmoon/" + location + "?client_id=" + clientId + "&client_secret=" + clientSecret);
33+
doUpdate(sunMoonData, "/sunmoon/" + location + "?client_id=" + clientId + "&client_secret=" + clientSecret);
3534
}
3635

37-
void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String url) {
36+
void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String path) {
3837
this->sunMoonData = sunMoonData;
3938

4039
unsigned long lostTest = 10000UL;
4140
unsigned long lost_do = millis();
4241

4342
JsonStreamingParser parser;
4443
parser.setListener(this);
45-
Serial.printf("Getting url: %s\n", url.c_str());
46-
HTTPClient http;
47-
48-
http.begin(url);
49-
bool isBody = false;
50-
char c;
51-
Serial.print("[HTTP] GET...\n");
52-
// start connection and send HTTP header
53-
int httpCode = http.GET();
54-
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
55-
if(httpCode > 0) {
56-
57-
WiFiClient * client = http.getStreamPtr();
58-
59-
while (client->available() || client->connected()) {
60-
while (client->available()) {
44+
Serial.printf("[HTTP] Requesting resource at http://%s:%u%s\n", host.c_str(), port, path.c_str());
45+
46+
WiFiClient client;
47+
if(client.connect(host, port)) {
48+
bool isBody = false;
49+
char c;
50+
Serial.println("[HTTP] connected, now GETting data");
51+
client.print("GET " + path + " HTTP/1.1\r\n"
52+
"Host: " + host + "\r\n"
53+
"Connection: close\r\n\r\n");
54+
55+
while (client.connected() || client.available()) {
56+
if (client.available()) {
6157
if ((millis() - lost_do) > lostTest) {
62-
Serial.println("lost in client with a timeout");
63-
client->stop();
58+
Serial.println("[HTTP] lost in client with a timeout");
59+
client.stop();
6460
ESP.restart();
6561
}
66-
c = client->read();
62+
c = client.read();
6763
if (c == '{' || c == '[') {
68-
6964
isBody = true;
7065
}
7166
if (isBody) {
7267
parser.parse(c);
7368
}
7469
}
75-
client->stop();
70+
// give WiFi and TCP/IP libraries a chance to handle pending events
71+
yield();
7672
}
73+
client.stop();
74+
} else {
75+
Serial.println("[HTTP] failed to connect to host");
7776
}
7877
this->sunMoonData = nullptr;
7978
}

src/AerisSunMoon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ typedef struct AerisSunMoonData {
5252

5353
class AerisSunMoon: public JsonListener {
5454
private:
55+
const String host = "api.aerisapi.com";
56+
const uint8_t port = 80;
5557
boolean isMetric = true;
5658
String currentKey;
5759
String currentParent;

src/MetOfficeClient.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ void MetOfficeClient::doUpdate(String url) {
9797
char c;
9898

9999
client.setNoDelay(false);
100-
while (client.connected()) {
101-
while (client.available()) {
100+
while (client.connected() || client.available()) {
101+
if (client.available()) {
102102
c = client.read();
103103
if (c == '{' || c == '[') {
104104
isBody = true;
@@ -107,8 +107,10 @@ void MetOfficeClient::doUpdate(String url) {
107107
parser.parse(c);
108108
}
109109
}
110-
client.stop();
110+
// give WiFi and TCP/IP libraries a chance to handle pending events
111+
yield();
111112
}
113+
client.stop();
112114
}
113115

114116
void MetOfficeClient::whitespace(char c) {

src/OpenWeatherMapCurrent.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,66 +23,64 @@
2323

2424
#include <ESPWiFi.h>
2525
#include <WiFiClient.h>
26-
#include <ESPHTTPClient.h>
2726
#include "OpenWeatherMapCurrent.h"
2827

2928
OpenWeatherMapCurrent::OpenWeatherMapCurrent() {
3029

3130
}
3231

3332
void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location) {
34-
doUpdate(data, buildUrl(appId, "q=" + location));
33+
doUpdate(data, buildPath(appId, "q=" + location));
3534
}
3635

3736
void OpenWeatherMapCurrent::updateCurrentById(OpenWeatherMapCurrentData *data, String appId, String locationId) {
38-
doUpdate(data, buildUrl(appId, "id=" + locationId));
37+
doUpdate(data, buildPath(appId, "id=" + locationId));
3938
}
4039

41-
String OpenWeatherMapCurrent::buildUrl(String appId, String locationParameter) {
40+
String OpenWeatherMapCurrent::buildPath(String appId, String locationParameter) {
4241
String units = metric ? "metric" : "imperial";
43-
return "http://api.openweathermap.org/data/2.5/weather?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language;
42+
return "/data/2.5/weather?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language;
4443
}
4544

46-
void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url) {
45+
void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String path) {
4746
unsigned long lostTest = 10000UL;
4847
unsigned long lost_do = millis();
4948
this->weatherItemCounter = 0;
5049
this->data = data;
5150
JsonStreamingParser parser;
5251
parser.setListener(this);
53-
Serial.printf("Getting url: %s\n", url.c_str());
54-
HTTPClient http;
52+
Serial.printf("[HTTP] Requesting resource at http://%s:%u%s\n", host.c_str(), port, path.c_str());
5553

56-
http.begin(url);
57-
bool isBody = false;
58-
char c;
59-
Serial.print("[HTTP] GET...\n");
60-
// start connection and send HTTP header
61-
int httpCode = http.GET();
62-
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
63-
if(httpCode > 0) {
54+
WiFiClient client;
55+
if(client.connect(host, port)) {
56+
bool isBody = false;
57+
char c;
58+
Serial.println("[HTTP] connected, now GETting data");
59+
client.print("GET " + path + " HTTP/1.1\r\n"
60+
"Host: " + host + "\r\n"
61+
"Connection: close\r\n\r\n");
6462

65-
WiFiClient * client = http.getStreamPtr();
66-
67-
while (client->connected() || client->available()) {
68-
while (client->available()) {
63+
while (client.connected() || client.available()) {
64+
if (client.available()) {
6965
if ((millis() - lost_do) > lostTest) {
70-
Serial.println("lost in client with a timeout");
71-
client->stop();
66+
Serial.println("[HTTP] lost in client with a timeout");
67+
client.stop();
7268
ESP.restart();
7369
}
74-
c = client->read();
70+
c = client.read();
7571
if (c == '{' || c == '[') {
7672
isBody = true;
7773
}
7874
if (isBody) {
7975
parser.parse(c);
8076
}
81-
// give WiFi and TCP/IP libraries a chance to handle pending events
82-
yield();
8377
}
84-
client->stop();
78+
// give WiFi and TCP/IP libraries a chance to handle pending events
79+
yield();
8580
}
81+
client.stop();
82+
} else {
83+
Serial.println("[HTTP] failed to connect to host");
8684
}
8785
this->data = nullptr;
8886
}

0 commit comments

Comments
 (0)