Skip to content

Change logic of reading HTTP response stream #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"espressif8266",
"espressif32"
],
"version": "2.0.0"
"version": "2.0.1"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP8266 Weather Station
version=2.0.0
version=2.0.1
author=ThingPulse
maintainer=ThingPulse <[email protected]>
sentence=ESP8266 based internet connected Weather Station
Expand Down
43 changes: 21 additions & 22 deletions src/AerisForecasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@

#include <ESPWiFi.h>
#include <WiFiClient.h>
#include <ESPHTTPClient.h>
#include "AerisForecasts.h"

AerisForecasts::AerisForecasts() {

}

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

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

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

WiFiClient * client = http.getStreamPtr();

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

isBody = true;
}
if (isBody) {
parser.parse(c);
}
}
client->stop();
// give WiFi and TCP/IP libraries a chance to handle pending events
yield();
}
client.stop();
} else {
Serial.println("[HTTP] failed to connect to host");
}
this->forecasts = nullptr;
}
Expand Down
2 changes: 2 additions & 0 deletions src/AerisForecasts.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ typedef struct AerisForecastData {

class AerisForecasts: public JsonListener {
private:
const String host = "api.aerisapi.com";
const uint8_t port = 80;
boolean isMetric = true;
String currentKey;
String currentParent;
Expand Down
44 changes: 21 additions & 23 deletions src/AerisObservations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,55 @@

#include <ESPWiFi.h>
#include <WiFiClient.h>
#include <ESPHTTPClient.h>
#include "AerisObservations.h"

AerisObservations::AerisObservations() {

}

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

void AerisObservations::doUpdate(AerisObservationsData *observations, String url) {
void AerisObservations::doUpdate(AerisObservationsData *observations, String path) {
unsigned long lostTest = 10000UL;
unsigned long lost_do = millis();

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

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();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if(httpCode > 0) {
WiFiClient client;
if(client.connect(host, port)) {
bool isBody = false;
char c;
Serial.println("[HTTP] connected, now GETting data");
client.print("GET " + path + " HTTP/1.1\r\n"
"Host: " + host + "\r\n"
"Connection: close\r\n\r\n");

WiFiClient * client = http.getStreamPtr();

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

isBody = true;
}
if (isBody) {
parser.parse(c);
}
}
client->stop();
// give WiFi and TCP/IP libraries a chance to handle pending events
yield();
}
client.stop();
} else {
Serial.println("[HTTP] failed to connect to host");
}
this->observations = nullptr;
}
Expand Down
2 changes: 2 additions & 0 deletions src/AerisObservations.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ typedef struct AerisObservationsData {

class AerisObservations: public JsonListener {
private:
const String host = "api.aerisapi.com";
const uint8_t port = 80;
boolean isMetric = true;
String currentKey;
String currentParent;
Expand Down
47 changes: 23 additions & 24 deletions src/AerisSunMoon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,56 @@

#include <ESPWiFi.h>
#include <WiFiClient.h>
#include <ESPHTTPClient.h>
#include "AerisSunMoon.h"

AerisSunMoon::AerisSunMoon() {

}

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

void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String url) {
void AerisSunMoon::doUpdate(AerisSunMoonData *sunMoonData, String path) {
this->sunMoonData = sunMoonData;

unsigned long lostTest = 10000UL;
unsigned long lost_do = millis();

JsonStreamingParser parser;
parser.setListener(this);
Serial.printf("Getting url: %s\n", url.c_str());
HTTPClient http;

http.begin(url);
bool isBody = false;
char c;
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if(httpCode > 0) {

WiFiClient * client = http.getStreamPtr();

while (client->available() || client->connected()) {
while (client->available()) {
Serial.printf("[HTTP] Requesting resource at http://%s:%u%s\n", host.c_str(), port, path.c_str());

WiFiClient client;
if(client.connect(host, port)) {
bool isBody = false;
char c;
Serial.println("[HTTP] connected, now GETting data");
client.print("GET " + path + " HTTP/1.1\r\n"
"Host: " + host + "\r\n"
"Connection: close\r\n\r\n");

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

isBody = true;
}
if (isBody) {
parser.parse(c);
}
}
client->stop();
// give WiFi and TCP/IP libraries a chance to handle pending events
yield();
}
client.stop();
} else {
Serial.println("[HTTP] failed to connect to host");
}
this->sunMoonData = nullptr;
}
Expand Down
2 changes: 2 additions & 0 deletions src/AerisSunMoon.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ typedef struct AerisSunMoonData {

class AerisSunMoon: public JsonListener {
private:
const String host = "api.aerisapi.com";
const uint8_t port = 80;
boolean isMetric = true;
String currentKey;
String currentParent;
Expand Down
8 changes: 5 additions & 3 deletions src/MetOfficeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void MetOfficeClient::doUpdate(String url) {
char c;

client.setNoDelay(false);
while (client.connected()) {
while (client.available()) {
while (client.connected() || client.available()) {
if (client.available()) {
c = client.read();
if (c == '{' || c == '[') {
isBody = true;
Expand All @@ -107,8 +107,10 @@ void MetOfficeClient::doUpdate(String url) {
parser.parse(c);
}
}
client.stop();
// give WiFi and TCP/IP libraries a chance to handle pending events
yield();
}
client.stop();
}

void MetOfficeClient::whitespace(char c) {
Expand Down
50 changes: 24 additions & 26 deletions src/OpenWeatherMapCurrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,66 +23,64 @@

#include <ESPWiFi.h>
#include <WiFiClient.h>
#include <ESPHTTPClient.h>
#include "OpenWeatherMapCurrent.h"

OpenWeatherMapCurrent::OpenWeatherMapCurrent() {

}

void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location) {
doUpdate(data, buildUrl(appId, "q=" + location));
doUpdate(data, buildPath(appId, "q=" + location));
}

void OpenWeatherMapCurrent::updateCurrentById(OpenWeatherMapCurrentData *data, String appId, String locationId) {
doUpdate(data, buildUrl(appId, "id=" + locationId));
doUpdate(data, buildPath(appId, "id=" + locationId));
}

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

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

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

WiFiClient * client = http.getStreamPtr();

while (client->connected() || client->available()) {
while (client->available()) {
while (client.connected() || client.available()) {
if (client.available()) {
if ((millis() - lost_do) > lostTest) {
Serial.println("lost in client with a timeout");
client->stop();
Serial.println("[HTTP] lost in client with a timeout");
client.stop();
ESP.restart();
}
c = client->read();
c = client.read();
if (c == '{' || c == '[') {
isBody = true;
}
if (isBody) {
parser.parse(c);
}
// give WiFi and TCP/IP libraries a chance to handle pending events
yield();
}
client->stop();
// give WiFi and TCP/IP libraries a chance to handle pending events
yield();
}
client.stop();
} else {
Serial.println("[HTTP] failed to connect to host");
}
this->data = nullptr;
}
Expand Down
Loading