Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/OpenWeatherMapCurrentDemo/OpenWeatherMapCurrentDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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("------------------------------------");

Expand Down
13 changes: 10 additions & 3 deletions examples/OpenWeatherMapForecastDemo/OpenWeatherMapForecastDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/OpenWeatherMapCurrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/OpenWeatherMapCurrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
13 changes: 11 additions & 2 deletions src/OpenWeatherMapForecast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/OpenWeatherMapForecast.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down