Skip to content

Commit 1d5edf8

Browse files
committed
Dump use of HTTPClient
Use plain TCP communication to fetch & process remote resources Fixes #178
1 parent 8fc8058 commit 1d5edf8

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/OpenWeatherMapForecast.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

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

2928
OpenWeatherMapForecast::OpenWeatherMapForecast() {
@@ -32,49 +31,46 @@ OpenWeatherMapForecast::OpenWeatherMapForecast() {
3231

3332
uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts) {
3433
this->maxForecasts = maxForecasts;
35-
return doUpdate(data, buildUrl(appId, "q=" + location));
34+
return doUpdate(data, buildPath(appId, "q=" + location));
3635
}
3736

3837
uint8_t OpenWeatherMapForecast::updateForecastsById(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts) {
3938
this->maxForecasts = maxForecasts;
40-
return doUpdate(data, buildUrl(appId, "id=" + locationId));
39+
return doUpdate(data, buildPath(appId, "id=" + locationId));
4140
}
4241

43-
String OpenWeatherMapForecast::buildUrl(String appId, String locationParameter) {
42+
String OpenWeatherMapForecast::buildPath(String appId, String locationParameter) {
4443
String units = metric ? "metric" : "imperial";
45-
return "http://api.openweathermap.org/data/2.5/forecast?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language;
44+
return "/data/2.5/forecast?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language;
4645
}
4746

48-
uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, String url) {
47+
uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, String path) {
4948
unsigned long lostTest = 10000UL;
5049
unsigned long lost_do = millis();
5150
this->weatherItemCounter = 0;
5251
this->currentForecast = 0;
5352
this->data = data;
5453
JsonStreamingParser parser;
5554
parser.setListener(this);
56-
Serial.printf("Getting url: %s\n", url.c_str());
57-
HTTPClient http;
55+
Serial.printf("[HTTP] Requesting resource at http://%s:%u%s\n", host.c_str(), port, path.c_str());
5856

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

68-
WiFiClient * client = http.getStreamPtr();
69-
70-
while (client->connected() || client->available()) {
71-
while (client->available()) {
66+
while (client.connected() || client.available()) {
67+
if (client.available()) {
7268
if ((millis() - lost_do) > lostTest) {
73-
Serial.println("lost in client with a timeout");
74-
client->stop();
69+
Serial.println("[HTTP] lost in client with a timeout");
70+
client.stop();
7571
ESP.restart();
7672
}
77-
c = client->read();
73+
c = client.read();
7874
if (c == '{' || c == '[') {
7975
isBody = true;
8076
}
@@ -84,8 +80,8 @@ uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, Strin
8480
// give WiFi and TCP/IP libraries a chance to handle pending events
8581
yield();
8682
}
87-
client->stop();
8883
}
84+
client.stop();
8985
}
9086
this->data = nullptr;
9187
return currentForecast;

src/OpenWeatherMapForecast.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ typedef struct OpenWeatherMapForecastData {
7272

7373
class OpenWeatherMapForecast: public JsonListener {
7474
private:
75+
const String host = "api.openweathermap.org";
76+
const uint8_t port = 80;
7577
String currentKey;
7678
String currentParent;
7779
OpenWeatherMapForecastData *data;
@@ -84,8 +86,8 @@ class OpenWeatherMapForecast: public JsonListener {
8486
uint8_t allowedHoursCount = 0;
8587
boolean isCurrentForecastAllowed = true;
8688

87-
uint8_t doUpdate(OpenWeatherMapForecastData *data, String url);
88-
String buildUrl(String appId, String locationParameter);
89+
uint8_t doUpdate(OpenWeatherMapForecastData *data, String path);
90+
String buildPath(String appId, String locationParameter);
8991

9092
public:
9193
OpenWeatherMapForecast();

0 commit comments

Comments
 (0)