Skip to content

Commit 839497c

Browse files
committed
Add hour filter for forecast loading
1 parent 558b714 commit 839497c

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ See more at https://blog.squix.org
3434
// initiate the client
3535
OpenWeatherMapForecast client;
3636

37-
String OPEN_WEATHER_MAP_APP_ID = "XXX";
37+
String OPEN_WEATHER_MAP_APP_ID = "";
3838
String OPEN_WEATHER_MAP_LOCATION = "Zurich,CH";
3939
/*
4040
Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el,
@@ -47,7 +47,7 @@ Chinese Simplified - zh_cn, Chinese Traditional - zh_tw.
4747
*/
4848
String OPEN_WEATHER_MAP_LANGUAGE = "de";
4949
boolean IS_METRIC = false;
50-
uint8_t MAX_FORECASTS = 20;
50+
uint8_t MAX_FORECASTS = 15;
5151

5252
/**
5353
* WiFi Settings
@@ -93,11 +93,13 @@ void setup() {
9393
OpenWeatherMapForecastData data[MAX_FORECASTS];
9494
client.setMetric(IS_METRIC);
9595
client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
96-
client.updateForecasts(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION, MAX_FORECASTS);
97-
96+
uint8_t allowedHours[] = {0,12};
97+
client.setAllowedHours(allowedHours, 2);
98+
uint8_t foundForecasts = client.updateForecasts(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION, MAX_FORECASTS);
99+
Serial.printf("Found %d forecasts in this call\n", foundForecasts);
98100
Serial.println("------------------------------------");
99101
time_t time;
100-
for (uint8_t i = 0; i < MAX_FORECASTS; i++) {
102+
for (uint8_t i = 0; i < foundForecasts; i++) {
101103
Serial.printf("---\nForecast number: %d\n", i);
102104
// {"dt":1527066000, uint32_t observationTime;
103105
time = data[i].observationTime;
@@ -135,6 +137,8 @@ void setup() {
135137
Serial.printf("windSpeed: %f\n", data[i].windSpeed);
136138
// "deg":207.501 float windDeg;
137139
Serial.printf("windDeg: %f\n", data[i].windDeg);
140+
// rain: {3h: 0.055}, float rain;
141+
Serial.printf("rain: %f\n", data[i].rain);
138142
// },"sys":{"pod":"d"}
139143
// dt_txt: "2018-05-23 09:00:00" String observationTimeText;
140144
Serial.printf("observationTimeText: %s\n", data[i].observationTimeText.c_str());

src/OpenWeatherMapForecast.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ OpenWeatherMapForecast::OpenWeatherMapForecast() {
3232

3333
}
3434

35-
void OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts) {
35+
uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts) {
3636
String units = metric ? "metric" : "imperial";
3737
this->maxForecasts = maxForecasts;
38-
doUpdate(data, "http://api.openweathermap.org/data/2.5/forecast?q=" + location + "&appid=" + appId + "&units=" + units + "&lang=" + language);
38+
return doUpdate(data, "http://api.openweathermap.org/data/2.5/forecast?q=" + location + "&appid=" + appId + "&units=" + units + "&lang=" + language);
3939
}
4040

41-
void OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, String url) {
41+
uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, String url) {
4242
unsigned long lostTest = 10000UL;
4343
unsigned long lost_do = millis();
4444
this->weatherItemCounter = 0;
@@ -80,6 +80,7 @@ void OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, String u
8080
}
8181
}
8282
this->data = nullptr;
83+
return currentForecast;
8384
}
8485

8586
void OpenWeatherMapForecast::whitespace(char c) {
@@ -101,11 +102,31 @@ void OpenWeatherMapForecast::value(String value) {
101102
// {"dt":1527066000, uint32_t observationTime;
102103
if (currentKey == "dt") {
103104
data[currentForecast].observationTime = value.toInt();
105+
106+
if (allowedHoursCount > 0) {
107+
time_t time = data[currentForecast].observationTime;
108+
struct tm* timeInfo;
109+
timeInfo = localtime(&time);
110+
uint8_t currentHour = timeInfo->tm_hour;
111+
for (uint8_t i = 0; i < allowedHoursCount; i++) {
112+
if (currentHour == allowedHours[i]) {
113+
isCurrentForecastAllowed = true;
114+
return;
115+
}
116+
}
117+
isCurrentForecastAllowed = false;
118+
return;
119+
}
120+
}
121+
if (!isCurrentForecastAllowed) {
122+
return;
104123
}
105124
// "main":{
106125
// "temp":17.35, float temp;
107126
if (currentKey == "temp") {
108127
data[currentForecast].temp = value.toFloat();
128+
// initialize potentially empty values:
129+
data[currentForecast].rain = 0;;
109130
}
110131
// "temp_min":16.89, float tempMin;
111132
if (currentKey == "temp_min") {
@@ -167,6 +188,10 @@ void OpenWeatherMapForecast::value(String value) {
167188
if (currentKey == "deg") {
168189
data[currentForecast].windDeg = value.toFloat();
169190
}
191+
// rain: {3h: 0.055}, float rain;
192+
if (currentKey == "3h") {
193+
data[currentForecast].rain = value.toFloat();
194+
}
170195
// },"sys":{"pod":"d"}
171196
// dt_txt: "2018-05-23 09:00:00" String observationTimeText;
172197
if (currentKey == "dt_txt") {

src/OpenWeatherMapForecast.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ See more at https://thingpulse.com
2626
#pragma once
2727
#include <JsonListener.h>
2828
#include <JsonStreamingParser.h>
29+
#include <time.h>
2930

3031
typedef struct OpenWeatherMapForecastData {
3132
// {"dt":1527066000,
@@ -63,6 +64,8 @@ typedef struct OpenWeatherMapForecastData {
6364
float windSpeed;
6465
// "deg":207.501
6566
float windDeg;
67+
// rain: {3h: 0.055},
68+
float rain;
6669
// },"sys":{"pod":"d"}
6770
// dt_txt: "2018-05-23 09:00:00"
6871
String observationTimeText;
@@ -79,18 +82,24 @@ class OpenWeatherMapForecast: public JsonListener {
7982
uint8_t currentForecast;
8083
boolean metric = true;
8184
String language = "en";
85+
uint8_t *allowedHours;
86+
uint8_t allowedHoursCount = 0;
87+
boolean isCurrentForecastAllowed = true;
8288

83-
84-
void doUpdate(OpenWeatherMapForecastData *data, String url);
89+
uint8_t doUpdate(OpenWeatherMapForecastData *data, String url);
8590

8691
public:
8792
OpenWeatherMapForecast();
88-
void updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts);
93+
uint8_t updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts);
8994

9095
void setMetric(boolean metric) { this->metric = metric; }
9196
boolean isMetric() { return this->metric; }
9297
void setLanguage(String language) { this->language = language; }
9398
String getLanguage() { return this->language; }
99+
void setAllowedHours(uint8_t *allowedHours, uint8_t allowedHoursCount) {
100+
this->allowedHours = allowedHours;
101+
this->allowedHoursCount = allowedHoursCount;
102+
}
94103

95104

96105
String getMeteoconIcon(String icon);

0 commit comments

Comments
 (0)