23
23
24
24
#include < ESPWiFi.h>
25
25
#include < WiFiClient.h>
26
- #include < ESPHTTPClient.h>
27
26
#include " OpenWeatherMapForecast.h"
28
27
29
28
OpenWeatherMapForecast::OpenWeatherMapForecast () {
@@ -32,49 +31,46 @@ OpenWeatherMapForecast::OpenWeatherMapForecast() {
32
31
33
32
uint8_t OpenWeatherMapForecast::updateForecasts (OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts) {
34
33
this ->maxForecasts = maxForecasts;
35
- return doUpdate (data, buildUrl (appId, " q=" + location));
34
+ return doUpdate (data, buildPath (appId, " q=" + location));
36
35
}
37
36
38
37
uint8_t OpenWeatherMapForecast::updateForecastsById (OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts) {
39
38
this ->maxForecasts = maxForecasts;
40
- return doUpdate (data, buildUrl (appId, " id=" + locationId));
39
+ return doUpdate (data, buildPath (appId, " id=" + locationId));
41
40
}
42
41
43
- String OpenWeatherMapForecast::buildUrl (String appId, String locationParameter) {
42
+ String OpenWeatherMapForecast::buildPath (String appId, String locationParameter) {
44
43
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;
46
45
}
47
46
48
- uint8_t OpenWeatherMapForecast::doUpdate (OpenWeatherMapForecastData *data, String url ) {
47
+ uint8_t OpenWeatherMapForecast::doUpdate (OpenWeatherMapForecastData *data, String path ) {
49
48
unsigned long lostTest = 10000UL ;
50
49
unsigned long lost_do = millis ();
51
50
this ->weatherItemCounter = 0 ;
52
51
this ->currentForecast = 0 ;
53
52
this ->data = data;
54
53
JsonStreamingParser parser;
55
54
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 ());
58
56
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: " + host + " \r\n "
64
+ " Connection: close \r\n\r\n " );
67
65
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 ()) {
72
68
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 ();
75
71
ESP.restart ();
76
72
}
77
- c = client-> read ();
73
+ c = client. read ();
78
74
if (c == ' {' || c == ' [' ) {
79
75
isBody = true ;
80
76
}
@@ -84,8 +80,10 @@ uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, Strin
84
80
// give WiFi and TCP/IP libraries a chance to handle pending events
85
81
yield ();
86
82
}
87
- client->stop ();
88
83
}
84
+ client.stop ();
85
+ } else {
86
+ Serial.println (" [HTTP] failed to connect to host" );
89
87
}
90
88
this ->data = nullptr ;
91
89
return currentForecast;
0 commit comments