diff --git a/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino b/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino index ab1f9b6..b19a85f 100644 --- a/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino +++ b/examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino @@ -32,8 +32,15 @@ SOFTWARE. // initiate the client OpenWeatherMapCurrent client; +// See https://docs.thingpulse.com/how-tos/openweathermap-key/ String OPEN_WEATHER_MAP_APP_ID = "XXX"; -String OPEN_WEATHER_MAP_LOCATION = "Zurich,CH"; +/* +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, English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl, @@ -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); + 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 2952887..f76f49f 100644 --- a/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino +++ b/examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino @@ -32,8 +32,15 @@ SOFTWARE. // initiate the client OpenWeatherMapForecast client; -String OPEN_WEATHER_MAP_APP_ID = ""; -String OPEN_WEATHER_MAP_LOCATION = "Zurich,CH"; +// 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, English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl, @@ -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, 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 f783735..151911c 100644 --- a/src/OpenWeatherMapCurrent.cpp +++ b/src/OpenWeatherMapCurrent.cpp @@ -31,8 +31,16 @@ OpenWeatherMapCurrent::OpenWeatherMapCurrent() { } 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 OpenWeatherMapCurrent::buildUrl(String appId, String locationParameter) { String units = metric ? "metric" : "imperial"; - doUpdate(data, "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&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 fc907aa..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 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 485c963..9afd338 100644 --- a/src/OpenWeatherMapForecast.cpp +++ b/src/OpenWeatherMapForecast.cpp @@ -31,9 +31,18 @@ OpenWeatherMapForecast::OpenWeatherMapForecast() { } uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, 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, buildUrl(appId, "q=" + location)); +} + +uint8_t OpenWeatherMapForecast::updateForecastsById(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts) { + this->maxForecasts = maxForecasts; + 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 ec008d9..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 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; }