Skip to content

Commit 758f05e

Browse files
authored
Switch to OWM location id instead of city,country query (#122)
1 parent 2421d5a commit 758f05e

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ SOFTWARE.
3232
// initiate the client
3333
OpenWeatherMapCurrent client;
3434

35+
// See https://docs.thingpulse.com/how-tos/openweathermap-key/
3536
String OPEN_WEATHER_MAP_APP_ID = "XXX";
36-
String OPEN_WEATHER_MAP_LOCATION = "Zurich,CH";
37+
/*
38+
Go to https://openweathermap.org/find?q= and search for a location. Go through the
39+
result set and select the entry closest to the actual location you want to display
40+
data for. It'll be a URL like https://openweathermap.org/city/2657896. The number
41+
at the end is what you assign to the constant below.
42+
*/
43+
String OPEN_WEATHER_MAP_LOCATION_ID = "2657896";
3744
/*
3845
Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el,
3946
English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl,
@@ -90,7 +97,7 @@ void setup() {
9097
OpenWeatherMapCurrentData data;
9198
client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
9299
client.setMetric(IS_METRIC);
93-
client.updateCurrent(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION);
100+
client.updateCurrentById(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID);
94101

95102
Serial.println("------------------------------------");
96103

examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ SOFTWARE.
3232
// initiate the client
3333
OpenWeatherMapForecast client;
3434

35-
String OPEN_WEATHER_MAP_APP_ID = "";
36-
String OPEN_WEATHER_MAP_LOCATION = "Zurich,CH";
35+
// See https://docs.thingpulse.com/how-tos/openweathermap-key/
36+
String OPEN_WEATHER_MAP_APP_ID = "XXX";
37+
/*
38+
Go to https://openweathermap.org/find?q= and search for a location. Go through the
39+
result set and select the entry closest to the actual location you want to display
40+
data for. It'll be a URL like https://openweathermap.org/city/2657896. The number
41+
at the end is what you assign to the constant below.
42+
*/
43+
String OPEN_WEATHER_MAP_LOCATION_ID = "2657896";
3744
/*
3845
Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el,
3946
English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl,
@@ -93,7 +100,7 @@ void setup() {
93100
client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
94101
uint8_t allowedHours[] = {0,12};
95102
client.setAllowedHours(allowedHours, 2);
96-
uint8_t foundForecasts = client.updateForecasts(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION, MAX_FORECASTS);
103+
uint8_t foundForecasts = client.updateForecastsById(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID, MAX_FORECASTS);
97104
Serial.printf("Found %d forecasts in this call\n", foundForecasts);
98105
Serial.println("------------------------------------");
99106
time_t time;

src/OpenWeatherMapCurrent.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ OpenWeatherMapCurrent::OpenWeatherMapCurrent() {
3131
}
3232

3333
void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location) {
34+
doUpdate(data, buildUrl(appId, "q=" + location));
35+
}
36+
37+
void OpenWeatherMapCurrent::updateCurrentById(OpenWeatherMapCurrentData *data, String appId, String locationId) {
38+
doUpdate(data, buildUrl(appId, "id=" + locationId));
39+
}
40+
41+
String OpenWeatherMapCurrent::buildUrl(String appId, String locationParameter) {
3442
String units = metric ? "metric" : "imperial";
35-
doUpdate(data, "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + appId + "&units=" + units + "&lang=" + language);
43+
return "http://api.openweathermap.org/data/2.5/weather?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language;
3644
}
3745

3846
void OpenWeatherMapCurrent::doUpdate(OpenWeatherMapCurrentData *data, String url) {

src/OpenWeatherMapCurrent.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ class OpenWeatherMapCurrent: public JsonListener {
7878
boolean metric = true;
7979
String language;
8080

81-
void doUpdate(OpenWeatherMapCurrentData *data, String url);
81+
void doUpdate(OpenWeatherMapCurrentData *data, String url);
82+
String buildUrl(String appId, String locationParameter);
8283

8384
public:
8485
OpenWeatherMapCurrent();
8586
void updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location);
87+
void updateCurrentById(OpenWeatherMapCurrentData *data, String appId, String locationId);
8688

8789
void setMetric(boolean metric) {this->metric = metric;}
8890
boolean isMetric() { return metric; }

src/OpenWeatherMapForecast.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,18 @@ OpenWeatherMapForecast::OpenWeatherMapForecast() {
3131
}
3232

3333
uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts) {
34-
String units = metric ? "metric" : "imperial";
3534
this->maxForecasts = maxForecasts;
36-
return doUpdate(data, "http://api.openweathermap.org/data/2.5/forecast?q=" + location + "&appid=" + appId + "&units=" + units + "&lang=" + language);
35+
return doUpdate(data, buildUrl(appId, "q=" + location));
36+
}
37+
38+
uint8_t OpenWeatherMapForecast::updateForecastsById(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts) {
39+
this->maxForecasts = maxForecasts;
40+
return doUpdate(data, buildUrl(appId, "id=" + locationId));
41+
}
42+
43+
String OpenWeatherMapForecast::buildUrl(String appId, String locationParameter) {
44+
String units = metric ? "metric" : "imperial";
45+
return "http://api.openweathermap.org/data/2.5/forecast?" + locationParameter + "&appid=" + appId + "&units=" + units + "&lang=" + language;
3746
}
3847

3948
uint8_t OpenWeatherMapForecast::doUpdate(OpenWeatherMapForecastData *data, String url) {

src/OpenWeatherMapForecast.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ class OpenWeatherMapForecast: public JsonListener {
8484
uint8_t allowedHoursCount = 0;
8585
boolean isCurrentForecastAllowed = true;
8686

87-
uint8_t doUpdate(OpenWeatherMapForecastData *data, String url);
87+
uint8_t doUpdate(OpenWeatherMapForecastData *data, String url);
88+
String buildUrl(String appId, String locationParameter);
8889

8990
public:
9091
OpenWeatherMapForecast();
9192
uint8_t updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts);
93+
uint8_t updateForecastsById(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts);
9294

9395
void setMetric(boolean metric) { this->metric = metric; }
9496
boolean isMetric() { return this->metric; }

0 commit comments

Comments
 (0)