forked from wuspy/portal_calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportal_calendar.ino
387 lines (341 loc) · 12.1 KB
/
portal_calendar.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <Arduino.h>
#include <WiFi.h>
#include <math.h>
#include "global.h"
#include "Configuration.h"
#include "time_util.h"
#include "Display.h"
#include "driver/rtc_io.h"
#include "weather.h"
const uint8_t ACTION_NTP_SYNC = 0b001;
const uint8_t ACTION_TZ_SYNC = 0b010;
const uint8_t ACTION_WEATHER_SYNC = 0b100;
/**
* A bitmask of the actions above. Controls what is scheduled to be performed on scheduledWakeup.
*/
RTC_DATA_ATTR uint8_t scheduledActions = 0;
/**
* The next scheduled wakeup time.
*/
RTC_DATA_ATTR time_t scheduledWakeup = 0;
/**
* Time the current deep sleep cycle was started. Needed for RTC drift correction.
*/
RTC_DATA_ATTR time_t sleepStartTime = 0;
/**
* Stores the current day of the year displayed
*/
RTC_DATA_ATTR int displayedYDay = 0;
RTC_DATA_ATTR bool showWeather = false;
RTC_DATA_ATTR time_t displayedWeatherTime = 0;
[[noreturn]] void deepSleep(time_t seconds)
{
time(&sleepStartTime);
scheduledWakeup = sleepStartTime + seconds;
correctSleepDuration(&seconds);
log_i("Sleeping for %lus, scheduled wakeup is %s\n\n", seconds, printTime(scheduledWakeup));
if (Config.getWeatherEnabled()) {
// Enable wakeup on boot button
#ifdef ENABLE_GPIO0_PULLUP
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
rtc_gpio_pullup_en(GPIO_NUM_0);
rtc_gpio_pulldown_dis(GPIO_NUM_0);
#endif // ENABLE_GPIO0_PULLUP
esp_sleep_enable_ext1_wakeup(0x1 << GPIO_NUM_0, ESP_EXT1_WAKEUP_ALL_LOW);
}
// Enable timer wakeup
esp_sleep_enable_timer_wakeup((uint64_t)seconds * uS_PER_S);
esp_deep_sleep_start();
}
void stopWifi()
{
if (WiFi.getMode() != WIFI_OFF) {
log_i("Stopping Wi-Fi");
unsigned long start = millis();
WiFi.disconnect(true, true);
WiFi.mode(WIFI_OFF);
log_i("Wi-Fi shutdown took %lums", millis() - start);
}
}
[[noreturn]] void error(String message)
{
log_i("Sleeping with error");
stopWifi(); // Power down wifi before updating display to limit current draw from battery
Display.error(message, true);
deepSleep(SECONDS_PER_HOUR);
}
[[noreturn]] void errorNoWifi()
{
error(
"NO WI-FI CONNECTION\n\n"
"Your Wi-Fi network is either down, out of range, "
"or you entered the wrong password.\n\n"
"Wi-Fi Name:\n"
+ Config.getWifiSsid()
);
}
[[noreturn]] void errorNtpFailed()
{
error(
"NO INTERNET CONNECTION\n\n"
"Your Wi-Fi network works, but the NTP servers didn't "
"respond. This probably means your Wi-Fi has no internet "
"connection. Or, you configured the NTP servers yourself, "
"in which case you might have messed something up.\n\n"
"NTP servers:\n"
+ Config.getPrimaryNtpServer() + "\n"
+ Config.getSecondaryNtpServer()
);
}
[[noreturn]] void errorTzLookupFailed()
{
error(
"TIMEZONE LOOKUP FAILED\n\n"
"Your timezone is either invalid, or the timezone servers "
"are down. If you configured the timezone servers "
"yourself, you might have messed something up.\n\n"
"Your timezone:\n"
+ Config.getTimezoneName() + "\n\n"
"Timezone servers:\n"
+ Config.getPrimaryTimezonedServer() + "\n"
+ Config.getSecondaryTimezonedServer()
);
}
[[noreturn]] void errorInvalidOwmApiKey()
{
error(
"INVALID OPENWEATHERMAP API KEY\n\n"
"OpenWeatherMap.org says your API key is invalid. "
"You probably have an issue with your configuration. "
"Go to your account -> My API Keys and make sure "
"the one there matches the one you entered. Or, just "
"disable the weather feature entirely."
);
}
[[noreturn]] void errorBrownout()
{
// Brownout was likely caused by the wifi radio, so hopefully there's still
// enough power to refresh the display
log_w("Brownout detected");
Display.error(
"REPLACE BATTERIES\n\n"
"If the device does not restart automatically "
"after new batteries have been inserted, "
"press the RESET button on the back.",
false
);
// Sleep forever
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
esp_deep_sleep_start();
}
void startConfigServer()
{
Config.startConfigServer();
if (Config.wasSaved()) {
// Reset state
scheduledWakeup = 0;
scheduledActions = 0;
savedTimezone[0] = '\0';
showWeather = Config.getWeatherEnabled();
displayedYDay = 0;
displayedWeatherTime = 0;
lastWeatherSync = 0;
}
}
void showWelcomeScreen()
{
Display.showWelcomeScreen();
// Sleep forever
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
esp_deep_sleep_start();
}
void runScheduledActions()
{
// Sync timezone
if ((scheduledActions & ACTION_TZ_SYNC) || !isSystemTimeValid()) {
if (Config.connectToSavedWifi()) {
String tz;
TimezonedResult result = getPosixTz(
{ Config.getPrimaryTimezonedServer(), Config.getSecondaryTimezonedServer() },
Config.getTimezoneName(),
tz
);
if (result == TimezonedResult::Ok) {
setTimezone(tz.c_str());
scheduledActions &= ~ACTION_TZ_SYNC;
} else if (!savedTimezone[0]) {
errorTzLookupFailed();
}
} else if (!savedTimezone[0]) {
// WiFi didn't connect and we have no idea what timezone we're in
errorNoWifi();
}
}
// Sync time
if ((scheduledActions & ACTION_NTP_SYNC) || !lastNtpSync) {
if (Config.connectToSavedWifi()) {
if (syncNtp({ Config.getPrimaryNtpServer(), Config.getSecondaryNtpServer() })) {
scheduledActions &= ~ACTION_NTP_SYNC;
} else if (!isSystemTimeValid()) {
// Sync unsuccesful and we have no idea what time it is
errorNtpFailed();
}
} else if (!isSystemTimeValid()) {
// WiFi didn't connect and we have no idea what time it is
errorNoWifi();
}
}
// Sync weather
if ((scheduledActions & ACTION_WEATHER_SYNC) || !lastWeatherSync) {
if (Config.getWeatherEnabled()) {
if (Config.connectToSavedWifi()) {
OwmResult result = refreshWeather();
switch (result) {
case OwmResult::SUCCESS:
scheduledActions &= ~ACTION_WEATHER_SYNC;
break;
case OwmResult::INVALID_API_KEY:
errorInvalidOwmApiKey();
default:
// Ignore other OWM errors because weather isn't critical
break;
}
} else if (!lastWeatherSync) {
errorNoWifi();
}
} else {
scheduledActions &= ~ACTION_WEATHER_SYNC;
}
}
}
void setup()
{
time_t t;
time(&t);
#if CORE_DEBUG_LEVEL > 0
Serial.begin(115200);
#endif
setTimezone();
if (sleepStartTime) {
correctSystemClock(t - sleepStartTime);
time(&t);
sleepStartTime = 0;
}
log_i("Waking up at %s", printTime(t));
const esp_reset_reason_t resetReason = esp_reset_reason();
const esp_sleep_wakeup_cause_t wakeupCause = esp_sleep_get_wakeup_cause();
log_i("Wakeup cause: %u, Reset reason: %u", wakeupCause, resetReason);
if (resetReason == ESP_RST_BROWNOUT) {
errorBrownout();
}
Config.begin();
// Check if configuration is required
if (!Config.isConfigured()) {
log_i("Not configured");
while (!Config.isConfigured()) {
if (Config.isOnUsbPower()) {
log_i("On USB power");
startConfigServer();
} else {
log_i("Not on USB power");
showWelcomeScreen();
}
}
}
// Check for wakeup from boot button press
else if (wakeupCause == ESP_SLEEP_WAKEUP_EXT1 && Config.getWeatherEnabled() && isSystemTimeValid()) {
log_i("Toggling showWeather");
showWeather = !showWeather;
}
// Check if config server is manually started by pressing reset while plugged in
else if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && Config.isOnUsbPower()) {
#ifndef DISABLE_MANUAL_CONFIG_SERVER_ACTIVATION
log_i("Config server was manually started");
startConfigServer();
if (!Config.isConfigured()) {
// Just in case they cleared some required settings
showWelcomeScreen();
}
#else
log_i("Config server would have been manually started, but is disabled");
#endif
}
time(&t);
if (t >= scheduledWakeup) {
runScheduledActions();
scheduledWakeup = 0;
}
stopWifi();
WiFi.setHostname(Config.getHostname().c_str());
time(&t);
tm now;
localtime_r(&t, &now);
// Check if display should be updated
bool needsDisplayUpdate = now.tm_yday != displayedYDay;
if (Config.getWeatherEnabled()) {
needsDisplayUpdate = needsDisplayUpdate
|| static_cast<bool>(displayedWeatherTime) != showWeather
|| (showWeather
&& lastWeatherSync != displayedWeatherTime
&& getSecondsToMidnight(&now) > SECONDS_BEFORE_MIDNIGHT_TO_SYNC_1 * 2);
}
if (needsDisplayUpdate) {
log_i("Updating display at %s", printTime(t));
Display.update(&now, getLocale(Config.getLocale()), showWeather);
displayedYDay = now.tm_yday;
displayedWeatherTime = Config.getWeatherEnabled() && showWeather ? lastWeatherSync : 0;
}
time(&t);
if (scheduledWakeup) {
log_i("Wakeup is already scheduled");
// max with 0 is needed in case the display was updated since the first scheduledWakeup check,
// which could make it in the past now despite being in the future before the display update
deepSleep(max((time_t)0, scheduledWakeup - t));
}
// Schedule next wakeup
localtime_r(&t, &now);
int secondsToMidnight = getSecondsToMidnight(&now) + 1; // +1 to make sure it's actually at or past midnight
// failedActions doesn't care about timezone sync because as long as we've got it once it's probably still correct
uint8_t failedActions = scheduledActions & ~ACTION_TZ_SYNC;
if (failedActions) {
log_w("Sync failed, scheduling retry");
if (secondsToMidnight <= SECONDS_BEFORE_MIDNIGHT_TO_SYNC_1) {
if (Config.getTwoNtpSyncsPerDay()) {
if (secondsToMidnight <= SECONDS_BEFORE_MIDNIGHT_TO_SYNC_2) {
// Try to sync 3 more times before midnight
deepSleep(min(secondsToMidnight, SECONDS_BEFORE_MIDNIGHT_TO_SYNC_2 / 3));
}
// Try to sync 3 more times before the 2nd scheduled sync
deepSleep(min(
secondsToMidnight - SECONDS_BEFORE_MIDNIGHT_TO_SYNC_2,
(SECONDS_BEFORE_MIDNIGHT_TO_SYNC_1 - SECONDS_BEFORE_MIDNIGHT_TO_SYNC_2) / 3
));
}
// Try to sync 4 more times before midnight
deepSleep(min(secondsToMidnight, SECONDS_BEFORE_MIDNIGHT_TO_SYNC_1 / 4));
}
// Keep trying to sync every hour
deepSleep(min(secondsToMidnight - SECONDS_BEFORE_MIDNIGHT_TO_SYNC_1, 3600));
}
if (secondsToMidnight <= SECONDS_BEFORE_MIDNIGHT_TO_SYNC_1 * 2) {
if (!Config.getTwoNtpSyncsPerDay() || secondsToMidnight <= SECONDS_BEFORE_MIDNIGHT_TO_SYNC_2 * 2) {
// Sleep until midnight
log_i("Sleeping until midnight");
deepSleep(secondsToMidnight);
}
// Sleep until second NTP sync
scheduledActions = ACTION_NTP_SYNC;
log_i("Sleeping for 2nd NTP sync");
deepSleep(secondsToMidnight - SECONDS_BEFORE_MIDNIGHT_TO_SYNC_2);
}
// Sleep until first NTP sync
scheduledActions =
ACTION_NTP_SYNC
| ACTION_TZ_SYNC
| (Config.getWeatherEnabled() ? ACTION_WEATHER_SYNC : 0);
log_i("Sleeping for 1st sync");
deepSleep(secondsToMidnight - SECONDS_BEFORE_MIDNIGHT_TO_SYNC_1);
}
void loop()
{
}