Skip to content

Commit 8e8278c

Browse files
committed
Add OWM lat/lon API
Contributes to #219
1 parent 14514a5 commit 8e8278c

File tree

7 files changed

+36
-19
lines changed

7 files changed

+36
-19
lines changed

Diff for: examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino

+7-6
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ OpenWeatherMapCurrent client;
3939
// See https://docs.thingpulse.com/how-tos/openweathermap-key/
4040
String OPEN_WEATHER_MAP_APP_ID = "XXX";
4141
/*
42-
Go to https://openweathermap.org/find?q= and search for a location. Go through the
43-
result set and select the entry closest to the actual location you want to display
44-
data for. It'll be a URL like https://openweathermap.org/city/2657896. The number
45-
at the end is what you assign to the constant below.
42+
Use the OWM GeoCoder API to find lat/lon for your city: https://openweathermap.org/api/geocoding-api
43+
Or use any other geocoding service.
44+
Or go to https://openweathermap.org, search for your city and monitor the calls in the browser dev console :)
4645
*/
47-
String OPEN_WEATHER_MAP_LOCATION_ID = "2657896";
46+
// Example: Zurich, Switzerland
47+
float OPEN_WEATHER_MAP_LOCATION_LAT = 47.3667;
48+
float OPEN_WEATHER_MAP_LOCATION_LON = 8.55;
4849
/*
4950
Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el,
5051
English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl,
@@ -105,7 +106,7 @@ void setup() {
105106
OpenWeatherMapCurrentData data;
106107
client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
107108
client.setMetric(IS_METRIC);
108-
client.updateCurrentById(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID);
109+
client.updateCurrent(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_LAT, OPEN_WEATHER_MAP_LOCATION_LON);
109110

110111
Serial.println("------------------------------------");
111112

Diff for: examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino

+7-6
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ OpenWeatherMapForecast client;
3939
// See https://docs.thingpulse.com/how-tos/openweathermap-key/
4040
String OPEN_WEATHER_MAP_APP_ID = "XXX";
4141
/*
42-
Go to https://openweathermap.org/find?q= and search for a location. Go through the
43-
result set and select the entry closest to the actual location you want to display
44-
data for. It'll be a URL like https://openweathermap.org/city/2657896. The number
45-
at the end is what you assign to the constant below.
42+
Use the OWM GeoCoder API to find lat/lon for your city: https://openweathermap.org/api/geocoding-api
43+
Or use any other geocoding service.
44+
Or go to https://openweathermap.org, search for your city and monitor the calls in the browser dev console :)
4645
*/
47-
String OPEN_WEATHER_MAP_LOCATION_ID = "2657896";
46+
// Example: Zurich, Switzerland
47+
float OPEN_WEATHER_MAP_LOCATION_LAT = 47.3667;
48+
float OPEN_WEATHER_MAP_LOCATION_LON = 8.55;
4849
/*
4950
Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el,
5051
English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl,
@@ -108,7 +109,7 @@ void setup() {
108109
client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
109110
uint8_t allowedHours[] = {0,12};
110111
client.setAllowedHours(allowedHours, 2);
111-
uint8_t foundForecasts = client.updateForecastsById(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID, MAX_FORECASTS);
112+
uint8_t foundForecasts = client.updateForecasts(data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_LAT, OPEN_WEATHER_MAP_LOCATION_LON, MAX_FORECASTS);
112113
Serial.printf("Found %d forecasts in this call\n", foundForecasts);
113114
Serial.println("------------------------------------");
114115
time_t time;

Diff for: examples/WeatherStationDemo/WeatherStationDemo.ino

+8-7
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@ const int SDC_PIN = 4; //D4;
7777
// https://docs.thingpulse.com/how-tos/openweathermap-key/
7878
String OPEN_WEATHER_MAP_APP_ID = "XXX";
7979
/*
80-
Go to https://openweathermap.org/find?q= and search for a location. Go through the
81-
result set and select the entry closest to the actual location you want to display
82-
data for. It'll be a URL like https://openweathermap.org/city/2657896. The number
83-
at the end is what you assign to the constant below.
80+
Use the OWM GeoCoder API to find lat/lon for your city: https://openweathermap.org/api/geocoding-api
81+
Or use any other geocoding service.
82+
Or go to https://openweathermap.org, search for your city and monitor the calls in the browser dev console :)
8483
*/
85-
String OPEN_WEATHER_MAP_LOCATION_ID = "2657896";
84+
// Example: Zurich, Switzerland
85+
float OPEN_WEATHER_MAP_LOCATION_LAT = 47.3667;
86+
float OPEN_WEATHER_MAP_LOCATION_LON = 8.55;
8687

8788
// Pick a language code from this list:
8889
// Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el,
@@ -246,13 +247,13 @@ void updateData(OLEDDisplay *display) {
246247
drawProgress(display, 30, "Updating weather...");
247248
currentWeatherClient.setMetric(IS_METRIC);
248249
currentWeatherClient.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
249-
currentWeatherClient.updateCurrentById(&currentWeather, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID);
250+
currentWeatherClient.updateCurrentBy(&currentWeather, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_LAT, OPEN_WEATHER_MAP_LOCATION_LON);
250251
drawProgress(display, 50, "Updating forecasts...");
251252
forecastClient.setMetric(IS_METRIC);
252253
forecastClient.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
253254
uint8_t allowedHours[] = {12};
254255
forecastClient.setAllowedHours(allowedHours, sizeof(allowedHours));
255-
forecastClient.updateForecastsById(forecasts, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID, MAX_FORECASTS);
256+
forecastClient.updateForecasts(forecasts, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_LAT, OPEN_WEATHER_MAP_LOCATION_LON, MAX_FORECASTS);
256257

257258
readyForWeatherUpdate = false;
258259
drawProgress(display, 100, "Done...");

Diff for: src/OpenWeatherMapCurrent.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ OpenWeatherMapCurrent::OpenWeatherMapCurrent() {
3232
void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location) {
3333
doUpdate(data, buildPath(appId, "q=" + location));
3434
}
35+
void OpenWeatherMapCurrent::updateCurrent(OpenWeatherMapCurrentData *data, String appId, float lat, float lon) {
36+
doUpdate(data, buildPath(appId, "lat=" + String(lat) + "&lon=" + String(lon)));
37+
}
3538

3639
void OpenWeatherMapCurrent::updateCurrentById(OpenWeatherMapCurrentData *data, String appId, String locationId) {
3740
doUpdate(data, buildPath(appId, "id=" + locationId));

Diff for: src/OpenWeatherMapCurrent.h

+3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ class OpenWeatherMapCurrent: public JsonListener {
8787

8888
public:
8989
OpenWeatherMapCurrent();
90+
// deprecated as per https://openweathermap.org/current#builtin
9091
void updateCurrent(OpenWeatherMapCurrentData *data, String appId, String location);
92+
void updateCurrent(OpenWeatherMapCurrentData *data, String appId, float lat, float lon);
93+
// deprecated as per https://openweathermap.org/current#builtin
9194
void updateCurrentById(OpenWeatherMapCurrentData *data, String appId, String locationId);
9295

9396
void setMetric(boolean metric) {this->metric = metric;}

Diff for: src/OpenWeatherMapForecast.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data
3434
return doUpdate(data, buildPath(appId, "q=" + location));
3535
}
3636

37+
uint8_t OpenWeatherMapForecast::updateForecasts(OpenWeatherMapForecastData *data, String appId, float lat, float lon, uint8_t maxForecasts) {
38+
this->maxForecasts = maxForecasts;
39+
return doUpdate(data, buildPath(appId, "lat=" + String(lat) + "&lon=" + String(lon)));
40+
}
41+
3742
uint8_t OpenWeatherMapForecast::updateForecastsById(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts) {
3843
this->maxForecasts = maxForecasts;
3944
return doUpdate(data, buildPath(appId, "id=" + locationId));

Diff for: src/OpenWeatherMapForecast.h

+3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ class OpenWeatherMapForecast: public JsonListener {
9393

9494
public:
9595
OpenWeatherMapForecast();
96+
// deprecated as per https://openweathermap.org/current#builtin
9697
uint8_t updateForecasts(OpenWeatherMapForecastData *data, String appId, String location, uint8_t maxForecasts);
98+
uint8_t updateForecasts(OpenWeatherMapForecastData *data, String appId, float lat, float lon, uint8_t maxForecasts);
99+
// deprecated as per https://openweathermap.org/current#builtin
97100
uint8_t updateForecastsById(OpenWeatherMapForecastData *data, String appId, String locationId, uint8_t maxForecasts);
98101

99102
void setMetric(boolean metric) { this->metric = metric; }

0 commit comments

Comments
 (0)