Skip to content

Commit fe4f7bf

Browse files
committed
Update WeatherStationDemo with OpenWeatherMap client
1 parent 839497c commit fe4f7bf

File tree

4 files changed

+349
-464
lines changed

4 files changed

+349
-464
lines changed

examples/WeatherStationDemo/WeatherStationDemo.ino

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ See more at https://thingpulse.com
3535
#include "SSD1306Wire.h"
3636
#include "OLEDDisplayUi.h"
3737
#include "Wire.h"
38-
#include "AerisObservations.h"
39-
#include "AerisForecasts.h"
38+
#include "OpenWeatherMapCurrent.h"
39+
#include "OpenWeatherMapForecast.h"
4040
#include "WeatherStationFonts.h"
4141
#include "WeatherStationImages.h"
4242

@@ -67,32 +67,43 @@ const int SDA_PIN = 5; //D3;
6767
const int SDC_PIN = 4; //D4;
6868
#endif
6969

70-
// TimeClient settings
71-
const float UTC_OFFSET = 2;
7270

73-
// Aeris Settings
71+
// OpenWeatherMap Settings
72+
// Sign up here to get an API key:
73+
// https://home.openweathermap.org/users/sign_up
7474
const boolean IS_METRIC = true;
75-
const String AERIS_CLIENT_ID = "tWOmsRUXe4EFTHQKmUKOK";
76-
const String AERIS_CLIENT_SECRET = "gRoMoapOyg46HwB7dRmoVPaJ0vUgAiud1CFWuLfF";
77-
const String AERIS_LOCATION = "Zurich,CH";
75+
String OPEN_WEATHER_MAP_APP_ID = "6bdd4d9d45a97d690103477a4c67c38f";
76+
String OPEN_WEATHER_MAP_LOCATION = "Zurich,CH";
77+
78+
// Pick a language code from this list:
79+
// Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el,
80+
// English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl,
81+
// Croatian - hr, Hungarian - hu, Italian - it, Japanese - ja, Korean - kr,
82+
// Latvian - la, Lithuanian - lt, Macedonian - mk, Dutch - nl, Polish - pl,
83+
// Portuguese - pt, Romanian - ro, Russian - ru, Swedish - se, Slovak - sk,
84+
// Slovenian - sl, Spanish - es, Turkish - tr, Ukrainian - ua, Vietnamese - vi,
85+
// Chinese Simplified - zh_cn, Chinese Traditional - zh_tw.
86+
87+
String OPEN_WEATHER_MAP_LANGUAGE = "de";
7888
const uint8_t MAX_FORECASTS = 4;
7989

80-
// Initialize the oled display for address 0x3c
81-
// sda-pin=14 and sdc-pin=12
82-
SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);
83-
OLEDDisplayUi ui( &display );
84-
90+
// Adjust according to your language
8591
const String WDAY_NAMES[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
8692
const String MONTH_NAMES[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
8793

8894
/***************************
8995
* End Settings
9096
**************************/
91-
AerisObservationsData aerisData;
92-
AerisObservations aerisClient;
97+
// Initialize the oled display for address 0x3c
98+
// sda-pin=14 and sdc-pin=12
99+
SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);
100+
OLEDDisplayUi ui( &display );
101+
102+
OpenWeatherMapCurrentData currentWeather;
103+
OpenWeatherMapCurrent currentWeatherClient;
93104

94-
AerisForecastData forecasts[MAX_FORECASTS];
95-
AerisForecasts forecastClient;
105+
OpenWeatherMapForecastData forecasts[MAX_FORECASTS];
106+
OpenWeatherMapForecast forecastClient;
96107

97108
#define TZ_MN ((TZ)*60)
98109
#define TZ_SEC ((TZ)*3600)
@@ -112,17 +123,17 @@ void updateData(OLEDDisplay *display);
112123
void drawDateTime(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
113124
void drawCurrentWeather(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
114125
void drawForecast(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
115-
void drawThingspeak(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
116126
void drawForecastDetails(OLEDDisplay *display, int x, int y, int dayIndex);
127+
void drawDHTData(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
117128
void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state);
118129
void setReadyForWeatherUpdate();
119130

120131

121132
// Add frames
122133
// this array keeps function pointers to all frames
123134
// frames are the single views that slide from right to left
124-
FrameCallback frames[] = { drawDateTime, drawCurrentWeather, drawForecast };
125-
int numberOfFrames = 3;
135+
FrameCallback frames[] = { drawDateTime, drawCurrentWeather, drawForecast, drawDHTData };
136+
int numberOfFrames = 4;
126137

127138
OverlayCallback overlays[] = { drawHeaderOverlay };
128139
int numberOfOverlays = 1;
@@ -223,10 +234,16 @@ void drawProgress(OLEDDisplay *display, int percentage, String label) {
223234

224235
void updateData(OLEDDisplay *display) {
225236
drawProgress(display, 10, "Updating time...");
226-
drawProgress(display, 30, "Updating observations...");
227-
aerisClient.updateObservations(&aerisData, AERIS_CLIENT_ID, AERIS_CLIENT_SECRET, AERIS_LOCATION);
237+
drawProgress(display, 30, "Updating weather...");
238+
currentWeatherClient.setMetric(IS_METRIC);
239+
currentWeatherClient.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
240+
currentWeatherClient.updateCurrent(&currentWeather, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION);
228241
drawProgress(display, 50, "Updating forecasts...");
229-
forecastClient.updateForecasts(forecasts, AERIS_CLIENT_ID, AERIS_CLIENT_SECRET, AERIS_LOCATION, MAX_FORECASTS);
242+
forecastClient.setMetric(IS_METRIC);
243+
forecastClient.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
244+
uint8_t allowedHours[] = {12};
245+
forecastClient.setAllowedHours(allowedHours, sizeof(allowedHours));
246+
forecastClient.updateForecasts(forecasts, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION, MAX_FORECASTS);
230247

231248
readyForWeatherUpdate = false;
232249
drawProgress(display, 100, "Done...");
@@ -257,27 +274,28 @@ void drawDateTime(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, in
257274

258275
void drawCurrentWeather(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
259276
display->setFont(ArialMT_Plain_10);
260-
display->setTextAlignment(TEXT_ALIGN_LEFT);
261-
display->drawString(60 + x, 5 + y, aerisData.weather);
277+
display->setTextAlignment(TEXT_ALIGN_CENTER);
278+
display->drawString(64 + x, 38 + y, currentWeather.description);
262279

263280
display->setFont(ArialMT_Plain_24);
264-
String temp = IS_METRIC ? String(aerisData.tempC) + "°C" : String(aerisData.tempF) + "°F";
265-
display->drawString(60 + x, 15 + y, temp);
281+
display->setTextAlignment(TEXT_ALIGN_LEFT);
282+
String temp = String(currentWeather.temp, 1) + (IS_METRIC ? "°C" : "°F");
283+
display->drawString(60 + x, 5 + y, temp);
266284

267-
display->setFont(Meteocons_Plain_42);
285+
display->setFont(Meteocons_Plain_36);
268286
display->setTextAlignment(TEXT_ALIGN_CENTER);
269-
display->drawString(32 + x, 05 + y, aerisData.iconMeteoCon);
287+
display->drawString(32 + x, 0 + y, currentWeather.iconMeteoCon);
270288
}
271289

272290

273291
void drawForecast(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
274-
drawForecastDetails(display, x, y, 1);
275-
drawForecastDetails(display, x + 44, y, 2);
276-
drawForecastDetails(display, x + 88, y, 3);
292+
drawForecastDetails(display, x, y, 0);
293+
drawForecastDetails(display, x + 44, y, 1);
294+
drawForecastDetails(display, x + 88, y, 2);
277295
}
278296

279297
void drawForecastDetails(OLEDDisplay *display, int x, int y, int dayIndex) {
280-
time_t observationTimestamp = forecasts[dayIndex].timestamp;
298+
time_t observationTimestamp = forecasts[dayIndex].observationTime;
281299
struct tm* timeInfo;
282300
timeInfo = localtime(&observationTimestamp);
283301
display->setTextAlignment(TEXT_ALIGN_CENTER);
@@ -286,15 +304,18 @@ void drawForecastDetails(OLEDDisplay *display, int x, int y, int dayIndex) {
286304

287305
display->setFont(Meteocons_Plain_21);
288306
display->drawString(x + 20, y + 12, forecasts[dayIndex].iconMeteoCon);
289-
String temp = String(forecasts[dayIndex].minTempC) + "|" + String(forecasts[dayIndex].maxTempC);
290-
if (!IS_METRIC) {
291-
String temp = String(forecasts[dayIndex].minTempF) + "|" + String(forecasts[dayIndex].maxTempF);
292-
}
307+
String temp = String(forecasts[dayIndex].temp, 0) + (IS_METRIC ? "°C" : "°F");
293308
display->setFont(ArialMT_Plain_10);
294309
display->drawString(x + 20, y + 34, temp);
295310
display->setTextAlignment(TEXT_ALIGN_LEFT);
296311
}
297312

313+
void drawDHTData(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
314+
display->setTextAlignment(TEXT_ALIGN_CENTER);
315+
display->setFont(ArialMT_Plain_10);
316+
display->drawString(x + 64, y, "Sensor Data");
317+
}
318+
298319
void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) {
299320
now = time(nullptr);
300321
struct tm* timeInfo;
@@ -307,7 +328,7 @@ void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) {
307328
display->setTextAlignment(TEXT_ALIGN_LEFT);
308329
display->drawString(0, 54, String(buff));
309330
display->setTextAlignment(TEXT_ALIGN_RIGHT);
310-
String temp = IS_METRIC ? String(aerisData.tempC) + "°C" : String(aerisData.tempF) + "°F";
331+
String temp = String(currentWeather.temp, 1) + (IS_METRIC ? "°C" : "°F");
311332
display->drawString(128, 54, temp);
312333
display->drawHorizontalLine(0, 52, 128);
313334
}

0 commit comments

Comments
 (0)