Skip to content

Commit ec4c989

Browse files
committed
Using http client for new refurbished wonderground clients
1 parent 57aca1d commit ec4c989

File tree

4 files changed

+110
-143
lines changed

4 files changed

+110
-143
lines changed

src/WundergroundAlerts.cpp

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ See more at http://blog.squix.ch
2525

2626
#include <ESP8266WiFi.h>
2727
#include <WiFiClient.h>
28+
#include <ESP8266HTTPClient.h>
2829
#include "WundergroundAlerts.h"
2930

3031
WundergroundAlerts::WundergroundAlerts() {
@@ -41,7 +42,7 @@ void WundergroundAlerts::updateAlerts(WGAlert *alerts, uint8_t maxAlerts, String
4142
isAlertUS = false;
4243
isAlertEU = true;
4344
}
44-
doUpdate(alerts, maxAlerts, "/api/" + apiKey + "/alerts/lang:" + language + "/q/" + country + "/" + city + ".json");
45+
doUpdate(alerts, maxAlerts, "http://api.wunderground.com/api/" + apiKey + "/alerts/lang:" + language + "/q/" + country + "/" + city + ".json");
4546
}
4647
// end fowlerk add
4748

@@ -56,51 +57,41 @@ void WundergroundAlerts::updateAlertsPWS(WGAlert *alert, uint8_t maxAlerts, Stri
5657
isAlertUS = false;
5758
isAlertEU = true;
5859
}
59-
doUpdate(alerts, maxAlerts, "/api/" + apiKey + "/alerts/lang:" + language + "/q/pws:" + pws + ".json");
60+
doUpdate(alerts, maxAlerts, "http://api.wunderground.com/api/" + apiKey + "/alerts/lang:" + language + "/q/pws:" + pws + ".json");
6061
}
6162

6263
void WundergroundAlerts::doUpdate(WGAlert *alerts, uint8_t maxAlerts, String url) {
6364
this->alerts = alerts;
6465
this->maxAlerts = maxAlerts;
6566
JsonStreamingParser parser;
6667
parser.setListener(this);
67-
WiFiClient client;
68-
const int httpPort = 80;
69-
if (!client.connect("api.wunderground.com", httpPort)) {
70-
Serial.println("connection failed");
71-
return;
72-
}
7368

74-
Serial.print("Requesting URL: ");
75-
Serial.println(url);
76-
77-
// This will send the request to the server
78-
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
79-
"Host: api.wunderground.com\r\n" +
80-
"Connection: close\r\n\r\n");
81-
int retryCounter = 0;
82-
while(!client.available()) {
83-
delay(1000);
84-
retryCounter++;
85-
if (retryCounter > 10) {
86-
return;
87-
}
88-
}
69+
HTTPClient http;
8970

90-
int pos = 0;
91-
boolean isBody = false;
71+
http.begin(url);
72+
bool isBody = false;
9273
char c;
74+
int size;
75+
Serial.print("[HTTP] GET...\n");
76+
// start connection and send HTTP header
77+
int httpCode = http.GET();
78+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
79+
if(httpCode > 0) {
9380

94-
int size = 0;
95-
client.setNoDelay(false);
96-
while(client.connected()) {
97-
while((size = client.available()) > 0) {
98-
c = client.read();
99-
if (c == '{' || c == '[') {
100-
isBody = true;
101-
}
102-
if (isBody) {
103-
parser.parse(c);
81+
82+
83+
WiFiClient * client = http.getStreamPtr();
84+
85+
while(client->connected()) {
86+
while((size = client->available()) > 0) {
87+
c = client->read();
88+
if (c == '{' || c == '[') {
89+
90+
isBody = true;
91+
}
92+
if (isBody) {
93+
parser.parse(c);
94+
}
10495
}
10596
}
10697
}

src/WundergroundAstronomy.cpp

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,64 +25,56 @@ See more at http://blog.squix.ch
2525

2626
#include <ESP8266WiFi.h>
2727
#include <WiFiClient.h>
28+
#include <ESP8266HTTPClient.h>
2829
#include "WundergroundAstronomy.h"
2930

3031

3132
WundergroundAstronomy::WundergroundAstronomy(boolean _usePM) {
3233
usePM = _usePM;
3334
}
3435
void WundergroundAstronomy::updateAstronomy(WGAstronomy *astronomy, String apiKey, String language, String country, String city) {
35-
doUpdate(astronomy, "/api/" + apiKey + "/astronomy/lang:" + language + "/q/" + country + "/" + city + ".json");
36+
doUpdate(astronomy, "http://api.wunderground.com/api/" + apiKey + "/astronomy/lang:" + language + "/q/" + country + "/" + city + ".json");
3637
}
3738

3839
void WundergroundAstronomy::updateAstronomyPWS(WGAstronomy *astronomy, String apiKey, String language, String pws) {
39-
doUpdate(astronomy, "/api/" + apiKey + "/astronomy/lang:" + language + "/q/pws:" + pws + ".json");
40+
doUpdate(astronomy, "http://api.wunderground.com/api/" + apiKey + "/astronomy/lang:" + language + "/q/pws:" + pws + ".json");
4041
}
4142

4243
void WundergroundAstronomy::doUpdate(WGAstronomy *astronomy, String url) {
4344
this->astronomy = astronomy;
4445
JsonStreamingParser parser;
4546
parser.setListener(this);
46-
WiFiClient client;
47-
const int httpPort = 80;
48-
if (!client.connect("api.wunderground.com", httpPort)) {
49-
Serial.println("connection failed");
50-
return;
51-
}
5247

53-
Serial.print("Requesting URL: ");
54-
Serial.println(url);
55-
56-
// This will send the request to the server
57-
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
58-
"Host: api.wunderground.com\r\n" +
59-
"Connection: close\r\n\r\n");
60-
int retryCounter = 0;
61-
while(!client.available()) {
62-
delay(1000);
63-
retryCounter++;
64-
if (retryCounter > 10) {
65-
return;
66-
}
67-
}
48+
HTTPClient http;
6849

69-
int pos = 0;
70-
boolean isBody = false;
50+
http.begin(url);
51+
bool isBody = false;
7152
char c;
53+
int size;
54+
Serial.print("[HTTP] GET...\n");
55+
// start connection and send HTTP header
56+
int httpCode = http.GET();
57+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
58+
if(httpCode > 0) {
7259

73-
int size = 0;
74-
client.setNoDelay(false);
75-
while(client.connected()) {
76-
while((size = client.available()) > 0) {
77-
c = client.read();
78-
if (c == '{' || c == '[') {
79-
isBody = true;
80-
}
81-
if (isBody) {
82-
parser.parse(c);
60+
61+
62+
WiFiClient * client = http.getStreamPtr();
63+
64+
while(client->connected()) {
65+
while((size = client->available()) > 0) {
66+
c = client->read();
67+
if (c == '{' || c == '[') {
68+
69+
isBody = true;
70+
}
71+
if (isBody) {
72+
parser.parse(c);
73+
}
8374
}
8475
}
8576
}
77+
this->astronomy = nullptr;
8678
}
8779

8880
void WundergroundAstronomy::whitespace(char c) {

src/WundergroundConditions.cpp

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ See more at http://blog.squix.ch
2525

2626
#include <ESP8266WiFi.h>
2727
#include <WiFiClient.h>
28+
#include <ESP8266HTTPClient.h>
2829
#include "WundergroundConditions.h"
2930

3031
WundergroundConditions::WundergroundConditions(boolean _isMetric) {
@@ -33,63 +34,54 @@ WundergroundConditions::WundergroundConditions(boolean _isMetric) {
3334

3435

3536
void WundergroundConditions::updateConditions(WGConditions *conditions, String apiKey, String language, String country, String city) {
36-
doUpdate(conditions, "/api/" + apiKey + "/conditions/lang:" + language + "/q/" + country + "/" + city + ".json");
37+
doUpdate(conditions, "http://api.wunderground.com/api/" + apiKey + "/conditions/lang:" + language + "/q/" + country + "/" + city + ".json");
3738
}
3839

3940
// wunderground change the API URL scheme:
4041
// http://api.wunderground.com/api/<API-KEY>/conditions/lang:de/q/zmw:00000.215.10348.json
4142
void WundergroundConditions::updateConditions(WGConditions *conditions, String apiKey, String language, String zmwCode) {
42-
doUpdate(conditions, "/api/" + apiKey + "/conditions/lang:" + language + "/q/zmw:" + zmwCode + ".json");
43+
doUpdate(conditions, "http://api.wunderground.com/api/" + apiKey + "/conditions/lang:" + language + "/q/zmw:" + zmwCode + ".json");
4344
}
4445

4546
void WundergroundConditions::updateConditionsPWS(WGConditions *conditions, String apiKey, String language, String pws) {
46-
doUpdate(conditions, "/api/" + apiKey + "/conditions/lang:" + language + "/q/pws:" + pws + ".json");
47+
doUpdate(conditions, "http://api.wunderground.com/api/" + apiKey + "/conditions/lang:" + language + "/q/pws:" + pws + ".json");
4748
}
4849

4950
void WundergroundConditions::doUpdate(WGConditions *conditions, String url) {
5051
this->conditions = conditions;
5152
JsonStreamingParser parser;
5253
parser.setListener(this);
53-
WiFiClient client;
54-
const int httpPort = 80;
55-
if (!client.connect("api.wunderground.com", httpPort)) {
56-
Serial.println("connection failed");
57-
return;
58-
}
59-
60-
Serial.print("Requesting URL: ");
61-
Serial.println(url);
62-
63-
// This will send the request to the server
64-
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
65-
"Host: api.wunderground.com\r\n" +
66-
"Connection: close\r\n\r\n");
67-
int retryCounter = 0;
68-
while(!client.available()) {
69-
delay(1000);
70-
retryCounter++;
71-
if (retryCounter > 10) {
72-
return;
73-
}
74-
}
7554

76-
int pos = 0;
77-
boolean isBody = false;
55+
HTTPClient http;
56+
57+
http.begin(url);
58+
bool isBody = false;
7859
char c;
60+
int size;
61+
Serial.print("[HTTP] GET...\n");
62+
// start connection and send HTTP header
63+
int httpCode = http.GET();
64+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
65+
if(httpCode > 0) {
7966

80-
int size = 0;
81-
client.setNoDelay(false);
82-
while(client.connected()) {
83-
while((size = client.available()) > 0) {
84-
c = client.read();
85-
if (c == '{' || c == '[') {
86-
isBody = true;
87-
}
88-
if (isBody) {
89-
parser.parse(c);
67+
68+
69+
WiFiClient * client = http.getStreamPtr();
70+
71+
while(client->connected()) {
72+
while((size = client->available()) > 0) {
73+
c = client->read();
74+
if (c == '{' || c == '[') {
75+
76+
isBody = true;
77+
}
78+
if (isBody) {
79+
parser.parse(c);
80+
}
9081
}
9182
}
9283
}
84+
this->conditions = nullptr;
9385
}
9486

9587
void WundergroundConditions::whitespace(char c) {

src/WundergroundForecast.cpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,70 +25,62 @@ See more at http://blog.squix.ch
2525

2626
#include <ESP8266WiFi.h>
2727
#include <WiFiClient.h>
28+
#include <ESP8266HTTPClient.h>
2829
#include "WundergroundForecast.h"
2930

3031
WundergroundForecast::WundergroundForecast(boolean _isMetric) {
3132
isMetric = _isMetric;
3233
}
3334

3435
void WundergroundForecast::updateForecast(WGForecast *forecasts, uint8_t maxForecasts, String apiKey, String language, String country, String city) {
35-
doUpdate(forecasts, maxForecasts, "/api/" + apiKey + "/forecast10day/lang:" + language + "/q/" + country + "/" + city + ".json");
36+
doUpdate(forecasts, maxForecasts, "http://api.wunderground.com/api/" + apiKey + "/forecast10day/lang:" + language + "/q/" + country + "/" + city + ".json");
3637
}
3738

3839

3940
void WundergroundForecast::updateForecastPWS(WGForecast *forecasts, uint8_t maxForecasts, String apiKey, String language, String pws) {
40-
doUpdate(forecasts, maxForecasts, "/api/" + apiKey + "/forecast10day/lang:" + language + "/q/pws:" + pws + ".json");
41+
doUpdate(forecasts, maxForecasts, "http://api.wunderground.com/api/" + apiKey + "/forecast10day/lang:" + language + "/q/pws:" + pws + ".json");
4142
}
4243

4344
void WundergroundForecast::updateForecastZMW(WGForecast *forecasts, uint8_t maxForecasts, String apiKey, String language, String zmwCode) {
44-
doUpdate(forecasts, maxForecasts, "/api/" + apiKey + "/forecast10day/lang:" + language + "/q/zmw:" + zmwCode + ".json");
45+
doUpdate(forecasts, maxForecasts, "http://api.wunderground.com/api/" + apiKey + "/forecast10day/lang:" + language + "/q/zmw:" + zmwCode + ".json");
4546
}
4647

4748
void WundergroundForecast::doUpdate(WGForecast *forecasts, uint8_t maxForecasts, String url) {
4849
this->forecasts = forecasts;
4950
this->maxForecasts = maxForecasts;
5051
JsonStreamingParser parser;
5152
parser.setListener(this);
52-
WiFiClient client;
53-
const int httpPort = 80;
54-
if (!client.connect("api.wunderground.com", httpPort)) {
55-
Serial.println("connection failed");
56-
return;
57-
}
5853

59-
Serial.print("Requesting URL: ");
60-
Serial.println(url);
61-
62-
// This will send the request to the server
63-
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
64-
"Host: api.wunderground.com\r\n" +
65-
"Connection: close\r\n\r\n");
66-
int retryCounter = 0;
67-
while(!client.available()) {
68-
delay(1000);
69-
retryCounter++;
70-
if (retryCounter > 10) {
71-
return;
72-
}
73-
}
54+
HTTPClient http;
7455

75-
int pos = 0;
76-
boolean isBody = false;
56+
http.begin(url);
57+
bool isBody = false;
7758
char c;
59+
int size;
60+
Serial.print("[HTTP] GET...\n");
61+
// start connection and send HTTP header
62+
int httpCode = http.GET();
63+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
64+
if(httpCode > 0) {
7865

79-
int size = 0;
80-
client.setNoDelay(false);
81-
while(client.connected()) {
82-
while((size = client.available()) > 0) {
83-
c = client.read();
84-
if (c == '{' || c == '[') {
85-
isBody = true;
86-
}
87-
if (isBody) {
88-
parser.parse(c);
66+
67+
68+
WiFiClient * client = http.getStreamPtr();
69+
70+
while(client->connected()) {
71+
while((size = client->available()) > 0) {
72+
c = client->read();
73+
if (c == '{' || c == '[') {
74+
75+
isBody = true;
76+
}
77+
if (isBody) {
78+
parser.parse(c);
79+
}
8980
}
9081
}
9182
}
83+
this->forecasts = nullptr;
9284
}
9385

9486
void WundergroundForecast::whitespace(char c) {

0 commit comments

Comments
 (0)