Skip to content

Commit

Permalink
Dump use of HTTPClient
Browse files Browse the repository at this point in the history
Use plain TCP communication to fetch & process remote resources

Fixes #178
  • Loading branch information
marcelstoer committed Mar 22, 2020
1 parent 8fc8058 commit b94feaa
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 147 deletions.
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
5 changes: 0 additions & 5 deletions src/ESPHTTPClient.h

This file was deleted.

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

0 comments on commit b94feaa

Please sign in to comment.