|
23 | 23 |
|
24 | 24 | #include <ESPWiFi.h> |
25 | 25 | #include <WiFiClient.h> |
26 | | -#include <ESPHTTPClient.h> |
27 | 26 | #include "OpenWeatherMapCurrent.h" |
28 | 27 |
|
29 | 28 | OpenWeatherMapCurrent::OpenWeatherMapCurrent() { |
30 | 29 |
|
31 | 30 | } |
32 | 31 |
|
33 | 32 | void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location) { |
34 | | - doUpdate(data, buildUrl(appId, "q=" + location)); |
| 33 | + doUpdate(data, buildPath(appId, "q=" + location)); |
35 | 34 | } |
36 | 35 |
|
37 | 36 | void OpenWeatherMapCurrent::updateCurrentById(OpenWeatherMapCurrentData *data, String appId, String locationId) { |
38 | | - doUpdate(data, buildUrl(appId, "id=" + locationId)); |
| 37 | + doUpdate(data, buildPath(appId, "id=" + locationId)); |
39 | 38 | } |
40 | 39 |
|
41 | | -String OpenWeatherMapCurrent::buildUrl(String appId, String locationParameter) { |
| 40 | +String OpenWeatherMapCurrent::buildPath(String appId, String locationParameter) { |
42 | 41 | String units = metric ? "metric" : "imperial"; |
43 | | - return "http://api.openweathermap.org/data/2.5/weather?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language; |
| 42 | + return "/data/2.5/weather?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language; |
44 | 43 | } |
45 | 44 |
|
46 | | -void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url) { |
| 45 | +void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String path) { |
47 | 46 | unsigned long lostTest = 10000UL; |
48 | 47 | unsigned long lost_do = millis(); |
49 | 48 | this->weatherItemCounter = 0; |
50 | 49 | this->data = data; |
51 | 50 | JsonStreamingParser parser; |
52 | 51 | parser.setListener(this); |
53 | | - Serial.printf("Getting url: %s\n", url.c_str()); |
54 | | - HTTPClient http; |
| 52 | + Serial.printf("[HTTP] Requesting resource at http://%s:%u%s\n", host.c_str(), port, path.c_str()); |
55 | 53 |
|
56 | | - http.begin(url); |
57 | | - bool isBody = false; |
58 | | - char c; |
59 | | - Serial.print("[HTTP] GET...\n"); |
60 | | - // start connection and send HTTP header |
61 | | - int httpCode = http.GET(); |
62 | | - Serial.printf("[HTTP] GET... code: %d\n", httpCode); |
63 | | - if(httpCode > 0) { |
| 54 | + WiFiClient client; |
| 55 | + if(client.connect(host, port)) { |
| 56 | + bool isBody = false; |
| 57 | + char c; |
| 58 | + Serial.println("[HTTP] connected, now GETting data"); |
| 59 | + client.print("GET " + path + " HTTP/1.1\r\n" |
| 60 | + "Host: " + host + "\r\n" |
| 61 | + "Connection: close\r\n\r\n"); |
64 | 62 |
|
65 | | - WiFiClient * client = http.getStreamPtr(); |
66 | | - |
67 | | - while (client->connected() || client->available()) { |
68 | | - while (client->available()) { |
| 63 | + while (client.connected() || client.available()) { |
| 64 | + if (client.available()) { |
69 | 65 | if ((millis() - lost_do) > lostTest) { |
70 | | - Serial.println("lost in client with a timeout"); |
71 | | - client->stop(); |
| 66 | + Serial.println("[HTTP] lost in client with a timeout"); |
| 67 | + client.stop(); |
72 | 68 | ESP.restart(); |
73 | 69 | } |
74 | | - c = client->read(); |
| 70 | + c = client.read(); |
75 | 71 | if (c == '{' || c == '[') { |
76 | 72 | isBody = true; |
77 | 73 | } |
78 | 74 | if (isBody) { |
79 | 75 | parser.parse(c); |
80 | 76 | } |
81 | | - // give WiFi and TCP/IP libraries a chance to handle pending events |
82 | | - yield(); |
83 | 77 | } |
84 | | - client->stop(); |
| 78 | + // give WiFi and TCP/IP libraries a chance to handle pending events |
| 79 | + yield(); |
85 | 80 | } |
| 81 | + client.stop(); |
| 82 | + } else { |
| 83 | + Serial.println("[HTTP] failed to connect to host"); |
86 | 84 | } |
87 | 85 | this->data = nullptr; |
88 | 86 | } |
|
0 commit comments