From 7204ca9201891f6aa20e4ca878bd6def86b83d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Tue, 10 Jul 2018 22:55:56 +0200 Subject: [PATCH 1/3] Switch to OWM location id instead of city,country query --- .../OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino | 4 ++-- .../OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino | 4 ++-- src/OpenWeatherMapCurrent.cpp | 4 ++-- src/OpenWeatherMapCurrent.h | 2 +- src/OpenWeatherMapForecast.cpp | 4 ++-- src/OpenWeatherMapForecast.h | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino b/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino index ab1f9b6..7072495 100644 --- a/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino +++ b/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino @@ -33,7 +33,7 @@ SOFTWARE. OpenWeatherMapCurrent client; String OPEN_WEATHER_MAP_APP_ID = "XXX"; -String OPEN_WEATHER_MAP_LOCATION = "Zurich,CH"; +String OPEN_WEATHER_MAP_LOCATION_ID = "2657896"; /* Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el, English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl, @@ -90,7 +90,7 @@ void setup() { OpenWeatherMapCurrentData data; client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE); client.setMetric(IS_METRIC); - client.updateCurrent(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION); + client.updateCurrent(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID); Serial.println("------------------------------------"); diff --git a/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino b/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino index 2952887..532eeb5 100644 --- a/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino +++ b/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino @@ -33,7 +33,7 @@ SOFTWARE. OpenWeatherMapForecast client; String OPEN_WEATHER_MAP_APP_ID = ""; -String OPEN_WEATHER_MAP_LOCATION = "Zurich,CH"; +String OPEN_WEATHER_MAP_LOCATION_ID = "2657896"; /* Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el, English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl, @@ -93,7 +93,7 @@ void setup() { client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE); uint8_t allowedHours[] = {0,12}; client.setAllowedHours(allowedHours, 2); - uint8_t foundForecasts = client.updateForecasts(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION, MAX_FORECASTS); + uint8_t foundForecasts = client.updateForecasts(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID, MAX_FORECASTS); Serial.printf("Found %d forecasts in this call\n", foundForecasts); Serial.println("------------------------------------"); time_t time; diff --git a/src/OpenWeatherMapCurrent.cpp b/src/OpenWeatherMapCurrent.cpp index f783735..84c7140 100644 --- a/src/OpenWeatherMapCurrent.cpp +++ b/src/OpenWeatherMapCurrent.cpp @@ -30,9 +30,9 @@ OpenWeatherMapCurrent::OpenWeatherMapCurrent() { } -void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location) { +void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, String locationId) { String units = metric ? "metric" : "imperial"; - doUpdate(data, "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + appId + "&units=" + units + "&lang=" + language); + doUpdate(data, "http://api.openweathermap.org/data/2.5/weather?id=" + locationId + "&appid=" + appId + "&units=" + units + "&lang=" + language); } void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url) { diff --git a/src/OpenWeatherMapCurrent.h b/src/OpenWeatherMapCurrent.h index fc907aa..27b5325 100644 --- a/src/OpenWeatherMapCurrent.h +++ b/src/OpenWeatherMapCurrent.h @@ -82,7 +82,7 @@ class OpenWeatherMapCurrent: public JsonListener { public: OpenWeatherMapCurrent(); - void updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location); + void updateCurrent(OpenWeatherMapCurrentData *data, String appId, String locationId); void setMetric(boolean metric) {this->metric = metric;} boolean isMetric() { return metric; } diff --git a/src/OpenWeatherMapForecast.cpp b/src/OpenWeatherMapForecast.cpp index 485c963..e665612 100644 --- a/src/OpenWeatherMapForecast.cpp +++ b/src/OpenWeatherMapForecast.cpp @@ -30,10 +30,10 @@ OpenWeatherMapForecast::OpenWeatherMapForecast() { } -uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts) { +uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts) { String units = metric ? "metric" : "imperial"; this->maxForecasts = maxForecasts; - return doUpdate(data, "http://api.openweathermap.org/data/2.5/forecast?q=" + location + "&appid=" + appId + "&units=" + units + "&lang=" + language); + return doUpdate(data, "http://api.openweathermap.org/data/2.5/forecast?id=" + locationId + "&appid=" + appId + "&units=" + units + "&lang=" + language); } uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, String url) { diff --git a/src/OpenWeatherMapForecast.h b/src/OpenWeatherMapForecast.h index ec008d9..541d983 100644 --- a/src/OpenWeatherMapForecast.h +++ b/src/OpenWeatherMapForecast.h @@ -88,7 +88,7 @@ class OpenWeatherMapForecast: public JsonListener { public: OpenWeatherMapForecast(); - uint8_t updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts); + uint8_t updateForecasts(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts); void setMetric(boolean metric) { this->metric = metric; } boolean isMetric() { return this->metric; } From fdede5ac7651321e32d1e3708dd6c6098ae6b9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Sun, 22 Jul 2018 08:26:58 +0200 Subject: [PATCH 2/3] Introduce updateForecastsById and updateCurrentById Use them as the new defaults --- .../OpenWeatherMapCurrentDemo.ino | 9 ++++++++- .../OpenWeatherMapForecastDemo.ino | 11 +++++++++-- src/OpenWeatherMapCurrent.cpp | 12 ++++++++++-- src/OpenWeatherMapCurrent.h | 6 ++++-- src/OpenWeatherMapForecast.cpp | 15 ++++++++++++--- src/OpenWeatherMapForecast.h | 6 ++++-- 6 files changed, 47 insertions(+), 12 deletions(-) diff --git a/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino b/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino index 7072495..b19a85f 100644 --- a/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino +++ b/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino @@ -32,7 +32,14 @@ SOFTWARE. // initiate the client OpenWeatherMapCurrent client; +// See https://docs.thingpulse.com/how-tos/openweathermap-key/ String OPEN_WEATHER_MAP_APP_ID = "XXX"; +/* +Go to https://openweathermap.org/find?q= and search for a location. Go through the +result set and select the entry closest to the actual location you want to display +data for. It'll be a URL like https://openweathermap.org/city/2657896. The number +at the end is what you assign to the constant below. + */ String OPEN_WEATHER_MAP_LOCATION_ID = "2657896"; /* Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el, @@ -90,7 +97,7 @@ void setup() { OpenWeatherMapCurrentData data; client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE); client.setMetric(IS_METRIC); - client.updateCurrent(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID); + client.updateCurrentById(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID); Serial.println("------------------------------------"); diff --git a/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino b/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino index 532eeb5..f76f49f 100644 --- a/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino +++ b/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino @@ -32,7 +32,14 @@ SOFTWARE. // initiate the client OpenWeatherMapForecast client; -String OPEN_WEATHER_MAP_APP_ID = ""; +// See https://docs.thingpulse.com/how-tos/openweathermap-key/ +String OPEN_WEATHER_MAP_APP_ID = "XXX"; +/* +Go to https://openweathermap.org/find?q= and search for a location. Go through the +result set and select the entry closest to the actual location you want to display +data for. It'll be a URL like https://openweathermap.org/city/2657896. The number +at the end is what you assign to the constant below. + */ String OPEN_WEATHER_MAP_LOCATION_ID = "2657896"; /* Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el, @@ -93,7 +100,7 @@ void setup() { client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE); uint8_t allowedHours[] = {0,12}; client.setAllowedHours(allowedHours, 2); - uint8_t foundForecasts = client.updateForecasts(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID, MAX_FORECASTS); + uint8_t foundForecasts = client.updateForecastsById(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID, MAX_FORECASTS); Serial.printf("Found %d forecasts in this call\n", foundForecasts); Serial.println("------------------------------------"); time_t time; diff --git a/src/OpenWeatherMapCurrent.cpp b/src/OpenWeatherMapCurrent.cpp index 84c7140..ba1849b 100644 --- a/src/OpenWeatherMapCurrent.cpp +++ b/src/OpenWeatherMapCurrent.cpp @@ -30,9 +30,17 @@ OpenWeatherMapCurrent::OpenWeatherMapCurrent() { } -void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, String locationId) { +void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location) { + doUpdate(data, buildUrl(appId, "q=" + location)); +} + +void OpenWeatherMapCurrent::updateCurrentById(OpenWeatherMapCurrentData *data, String appId, String locationId) { + doUpdate(data, buildUrl(appId, "id=" + locationId)); +} + +String OpenWeatherMapForecast::buildUrl(String appId, String locationParameter) { String units = metric ? "metric" : "imperial"; - doUpdate(data, "http://api.openweathermap.org/data/2.5/weather?id=" + locationId + "&appid=" + appId + "&units=" + units + "&lang=" + language); + return "http://api.openweathermap.org/data/2.5/weather?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language; } void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url) { diff --git a/src/OpenWeatherMapCurrent.h b/src/OpenWeatherMapCurrent.h index 27b5325..fa4d0eb 100644 --- a/src/OpenWeatherMapCurrent.h +++ b/src/OpenWeatherMapCurrent.h @@ -78,11 +78,13 @@ class OpenWeatherMapCurrent: public JsonListener { boolean metric = true; String language; - void doUpdate(OpenWeatherMapCurrentData *data, String url); + void doUpdate(OpenWeatherMapCurrentData *data, String url); + String buildUrl(String appId, String locationParameter); public: OpenWeatherMapCurrent(); - void updateCurrent(OpenWeatherMapCurrentData *data, String appId, String locationId); + void updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location); + void updateCurrentById(OpenWeatherMapCurrentData *data, String appId, String locationId); void setMetric(boolean metric) {this->metric = metric;} boolean isMetric() { return metric; } diff --git a/src/OpenWeatherMapForecast.cpp b/src/OpenWeatherMapForecast.cpp index e665612..9afd338 100644 --- a/src/OpenWeatherMapForecast.cpp +++ b/src/OpenWeatherMapForecast.cpp @@ -30,10 +30,19 @@ OpenWeatherMapForecast::OpenWeatherMapForecast() { } -uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts) { - String units = metric ? "metric" : "imperial"; +uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts) { + this->maxForecasts = maxForecasts; + return doUpdate(data, buildUrl(appId, "q=" + location)); +} + +uint8_t OpenWeatherMapForecast::updateForecastsById(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts) { this->maxForecasts = maxForecasts; - return doUpdate(data, "http://api.openweathermap.org/data/2.5/forecast?id=" + locationId + "&appid=" + appId + "&units=" + units + "&lang=" + language); + return doUpdate(data, buildUrl(appId, "id=" + locationId)); +} + +String OpenWeatherMapForecast::buildUrl(String appId, String locationParameter) { + String units = metric ? "metric" : "imperial"; + return "http://api.openweathermap.org/data/2.5/forecast?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language; } uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, String url) { diff --git a/src/OpenWeatherMapForecast.h b/src/OpenWeatherMapForecast.h index 541d983..909e7d9 100644 --- a/src/OpenWeatherMapForecast.h +++ b/src/OpenWeatherMapForecast.h @@ -84,11 +84,13 @@ class OpenWeatherMapForecast: public JsonListener { uint8_t allowedHoursCount = 0; boolean isCurrentForecastAllowed = true; - uint8_t doUpdate(OpenWeatherMapForecastData *data, String url); + uint8_t doUpdate(OpenWeatherMapForecastData *data, String url); + String buildUrl(String appId, String locationParameter); public: OpenWeatherMapForecast(); - uint8_t updateForecasts(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts); + uint8_t updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts); + uint8_t updateForecastsById(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts); void setMetric(boolean metric) { this->metric = metric; } boolean isMetric() { return this->metric; } From 165f2ebe1c45c343ffa5297b0bb492ff2267ec4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Thu, 26 Jul 2018 22:29:03 +0200 Subject: [PATCH 3/3] Fix copy-paste bug --- src/OpenWeatherMapCurrent.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenWeatherMapCurrent.cpp b/src/OpenWeatherMapCurrent.cpp index ba1849b..151911c 100644 --- a/src/OpenWeatherMapCurrent.cpp +++ b/src/OpenWeatherMapCurrent.cpp @@ -38,7 +38,7 @@ void OpenWeatherMapCurrent::updateCurrentById(OpenWeatherMapCurrentData *data, S doUpdate(data, buildUrl(appId, "id=" + locationId)); } -String OpenWeatherMapForecast::buildUrl(String appId, String locationParameter) { +String OpenWeatherMapCurrent::buildUrl(String appId, String locationParameter) { String units = metric ? "metric" : "imperial"; return "http://api.openweathermap.org/data/2.5/weather?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language; }