-
Notifications
You must be signed in to change notification settings - Fork 3
/
EInkAssistant.ino
788 lines (745 loc) · 27.4 KB
/
EInkAssistant.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/**
* E-Ink Assistant 墨水屏智能助理
*
* 基于 ESP8266/ESP32 使用 Arduino 开发的低功耗墨水屏应用, 具有时钟, 日历, 天气等功能, 且提供接口可自行扩展
*
* @author QingChenW
*/
#include "config.h"
#include <functional>
#include <vector>
#include <time.h>
#if defined(ESP8266)
#include <user_interface.h>
#include <coredecls.h>
#include <smartconfig.h>
#elif defined(ESP32)
#include <esp_system.h>
#include <esp_wifi.h>
#include <esp_sntp.h>
#endif
#include <Arduino.h>
#include <Ticker.h>
#if defined(ESP8266)
#include <Updater.h>
#include <ESP_EEPROM.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#define RTC_DATA_ATTR
typedef ESP8266WebServer WebServer;
#elif defined(ESP32)
#include <Update.h>
#include <EEPROM.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WebServer.h>
#include <detail/mimetable.h>
#endif
#include <WiFiUdp.h>
#include <ArduinoJson.h>
#include "font.h"
#include "bitmap.h"
#include "lang.h"
#include "draw.h"
#include "API.hpp"
#include "util.h"
#define MIME_TYPE(t) (mime::mimeTable[mime::type::t].mimeType)
// 参数为是否首次刷新
typedef std::function<void(bool)> DrawPageFunc;
const char *product_name = "einkassistant";
const char *model_name = MODEL;
const char *version = VERSION;
const uint32_t version_code = VERSION_CODE;
Ticker keyDebounce;
EPD_CLASS epd(EPD_DRIVER(EPD_CS, EPD_DC, EPD_RST, EPD_BUSY));
U8G2_FOR_ADAFRUIT_GFX u8g2Fonts;
WebServer server(80);
std::vector<DrawPageFunc> pages;
bool keyPressed;
time_t sleepTimer;
uint8_t pageCustom;
String displayBuffer;
struct Config {
uint8_t version; // 配置版本, 若与程序版本号不同则重置配置
char hostname[25]; // 主机名
uint32_t update_interval; // 首页更新间隔(秒)
int8_t theme; // 图标样式, -1: 跟随时间, 0: 白色描边(白天), 1: 黑色填充(夜间)
uint8_t hour_step; // 逐小时天气预报的显示步长(时)
uint32_t location_id; // 位置id
uint32_t bilibili_uid; // B站uid
char bilibili_cookie[36]; // B站cookie(SESSDATA)
} config;
RTC_DATA_ATTR struct RTCData {
uint32_t crc32; // TODO 是否有必要为 RTC memory 添加 CRC 校验
time_t wakeup_time; // 从睡眠中唤醒时的时间(秒)
int8_t page; // 当前正在显示的页面
time_t next_update; // 下次更新时间(秒)
uint32_t location_id; // 当日天气预报的缓存
char date[11];
char sunrise[6];
char sunset[6];
char moonPhase[10];
uint16_t moonPhaseIcon;
} rtcdata;
int8_t getBatteryLevel() {
#if ENABLE_BATTERY_DISPLAY
#error "请实现 int8_t getBatteryLevel() 函数"
#else
return -1;
#endif
}
bool resetConfig(bool wifi = false) {
if (wifi) {
#if defined(ESP8266)
struct station_config conf;
memset(&conf, 0, sizeof(conf));
ETS_UART_INTR_DISABLE();
wifi_station_set_config(&conf);
ETS_UART_INTR_ENABLE();
#elif defined(ESP32)
wifi_config_t conf;
memset(&conf, 0, sizeof(conf));
esp_wifi_set_config(WIFI_IF_STA, &conf);
#endif
}
config.version = version_code;
strcpy(config.hostname, product_name);
config.update_interval = 3600;
config.theme = -1;
config.hour_step = 1;
config.location_id = 101010100;
config.bilibili_uid = 1;
strcpy(config.bilibili_cookie, "");
EEPROM.put(0, config);
return EEPROM.commit();
}
// unit: second
void gotoSleep(uint32_t s) {
if (Serial) Serial.printf("Sleep for %d seconds\n", s); // 这里不能把字符串存在 Flash 里, 否则墨水屏颜色会变淡
pinMode(KEY_SWITCH, INPUT);
// epd.hibernate();
delay(1000);
// 从深度睡眠中唤醒也会导致 RTC timer 被清零, 所以存在 RTC memory 里
rtcdata.wakeup_time = time(nullptr) + s;
#if defined(ESP8266)
ESP.rtcUserMemoryWrite(0, (uint32_t*) &rtcdata, sizeof(RTCData));
ESP.deepSleep(s * 1000000UL, WAKE_RF_DEFAULT);
#elif defined(ESP32)
if (s > 0)
esp_sleep_enable_timer_wakeup(s * 1000000UL);
esp_deep_sleep_start();
#endif
}
// auto calculate sleep time by update interval
void gotoSleep() {
time_t sleep_time = rtcdata.next_update - time(nullptr);
if (sleep_time <= 60) {
// 要休眠的时间小于一分钟, 可能是还没到点就刷新了
sleep_time = config.update_interval;
}
#if defined(ESP8266)
time_t max_time = ESP.deepSleepMax() / 1000000;
gotoSleep(min(sleep_time, max_time));
#elif defined(ESP32)
gotoSleep(sleep_time);
#endif
}
void checkBattery() {
if ((uint8_t) getBatteryLevel() <= BATTERY_LOW_PERCENTAGE) {
startDraw(epd);
u8g2Fonts.setFont(u8g2_font_wqy12_t);
drawCenteredString(u8g2Fonts, epd.width() / 2, epd.height() * 3 / 4, TEXT_LOW_POWER);
endDraw(epd);
gotoSleep(0);
}
}
void prepareOTA() {
#ifdef ESP8266
WiFiUDP::stopAll();
#endif
startDraw(epd);
epd.fillTriangle(epd.width() / 2, 8, epd.width() / 2 - 24, 32, epd.width() / 2 + 24, 32, GxEPD_BLACK);
epd.fillRect(epd.width() / 2 - 12, 32, 24, 42, GxEPD_BLACK);
u8g2Fonts.setFont(u8g2_font_wqy12_t);
drawCenteredString(u8g2Fonts, epd.width() / 2, epd.height() - 16, TEXT_UPDATING_1);
drawCenteredString(u8g2Fonts, epd.width() / 2, epd.height() - 4, TEXT_UPDATING_2);
endDraw(epd);
}
bool isNight(String time) {
if (config.theme >= 0) return config.theme;
uint8_t hour = time.substring(11, 13).toInt();
return hour < 6 || hour >= 18;
}
void drawTitleBar(const char *title, bool sleeping, int8_t rssi, int8_t battery) {
// Draw title
u8g2Fonts.setFont(u8g2_font_wqy12_t);
u8g2Fonts.drawUTF8(2, 12, title);
// Draw battery level
uint16_t x = epd.width() - 2;
if (battery >= 0) {
epd.drawRect(x - 21, 2, 19, 10, GxEPD_BLACK);
epd.fillRect(x - 2, 4, 2, 6, GxEPD_BLACK);
if (battery > 0)
epd.fillRect(x - 19, 4, 15 * battery / 100, 6, GxEPD_BLACK);
x -= 25;
}
// Draw WiFi RSSI
epd.fillRect(x - 11, 8, 3, 4, GxEPD_BLACK);
if (rssi >= -80)
epd.fillRect(x - 7, 5, 3, 7, GxEPD_BLACK);
if (rssi >= -70)
epd.fillRect(x - 3, 2, 3, 10, GxEPD_BLACK);
if (rssi == 31) { // Failure, invalid value.
int16_t x1 = x - 11, y1 = 2;
int16_t x2 = x, y2 = 12;
epd.drawLine(x1, y1, x2, y2, GxEPD_BLACK);
epd.drawLine(x2, y1, x1, y2, GxEPD_BLACK);
}
x -= 15;
// Draw sleep status
if (sleeping) {
u8g2Fonts.drawUTF8(x - 6, 12, "Z");
}
// Draw separator
epd.drawFastHLine(0, 15, epd.width(), GxEPD_BLACK);
}
void drawWeatherNow(Weather &weather) {
// Draw temperature and humidity
u8g2Fonts.setFont(u8g2_font_helvB14_tf);
u8g2Fonts.setCursor(0, 36);
u8g2Fonts.printf("%d°C/%d%%", weather.temp, weather.humidity);
// Draw weathervane and wind speed
uint16_t x = 0;
if (weather.wind360 >= 0) {
drawArrow(epd, x + 8, 47, -18, weather.wind360, 8, 16);
x += 18;
}
u8g2Fonts.setFont(u8g2_font_wqy12_t);
u8g2Fonts.setCursor(x, 52);
u8g2Fonts.printf("%s %d级", weather.windDir.c_str(), weather.windScale);
// Draw Weather icon and text
u8g2Fonts.setFont(u8g2_font_qweather_icon_16);
drawCenteredString(u8g2Fonts, epd.width() / 2, 40, getWeatherIcon(weather.icon, isNight(weather.time)));
u8g2Fonts.setFont(u8g2_font_wqy12_t);
drawCenteredString(u8g2Fonts, epd.width() / 2, 52, weather.text.c_str());
}
void drawForecastHourly(HourlyForecast &forecast) {
// Draw grid
uint16_t grid_w = epd.width() / forecast.length;
epd.drawFastHLine(0, 57, epd.width(), GxEPD_BLACK);
for (uint8_t i = 1; i < forecast.length; i++) {
epd.drawFastVLine(grid_w * i, 58, epd.height() - 58, GxEPD_BLACK);
}
// Draw hourly forecast
u8g2Fonts.setFont(u8g2_font_wqy12_t);
for (uint8_t i = 0; i < forecast.length; i++) {
Weather &weather = forecast.weather[i];
uint16_t x = grid_w * i + grid_w / 2;
drawCenteredString(u8g2Fonts, x, 69, weather.time.substring(11, 16).c_str());
u8g2Fonts.setFont(u8g2_font_qweather_icon_16);
drawCenteredString(u8g2Fonts, x, epd.height() - 12, getWeatherIcon(weather.icon, isNight(weather.time)));
u8g2Fonts.setFont(u8g2_font_wqy12_t);
String temp = String(weather.temp) + "°C";
drawCenteredString(u8g2Fonts, x, epd.height() - 2, temp.c_str());
}
}
void drawForecastDaily(DailyForecast &forecast) {
DailyWeather &weather = *forecast.weather;
// Draw sunrise, sunset and moon phase
u8g2Fonts.setFont(u8g2_font_wqy12_t);
u8g2Fonts.setCursor(epd.width() - 84, 28);
u8g2Fonts.printf("日出%s", weather.sunrise.c_str());
u8g2Fonts.setCursor(epd.width() - 84, 41);
u8g2Fonts.printf("日落%s", weather.sunset.c_str());
drawCenteredString(u8g2Fonts, epd.width() - 60, 54, weather.moonPhase.c_str());
// Draw moon icon
u8g2Fonts.setFont(u8g2_font_qweather_icon_16);
const char *icon = getWeatherIcon(weather.moonPhaseIcon);
u8g2Fonts.drawUTF8(epd.width() - u8g2Fonts.getUTF8Width(icon) - 4, 48, icon);
}
uint8_t addPage(DrawPageFunc callback) {
pages.push_back(callback);
return pages.size() - 1;
}
void refreshPage() {
if (Update.isRunning()) return;
Serial.print(F("Refresh page "));
Serial.println(rtcdata.page);
pages[rtcdata.page](false);
}
void showPage(int8_t page) {
if (Update.isRunning()) return;
if (page == rtcdata.page) {
refreshPage();
return;
}
Serial.print(F("Show page "));
Serial.println(page);
pages[page](true);
rtcdata.page = page;
}
void lastPage() {
int8_t page = rtcdata.page - 1;
if (page < 0) page = pages.size() - 1;
showPage(page);
}
void nextPage() {
int8_t page = rtcdata.page + 1;
page %= pages.size();
showPage(page);
}
IRAM_ATTR void onKeyPressed() {
keyDebounce.once_ms(20, []() {
if (digitalRead(KEY_SWITCH) == KEY_TRIGGER_LEVEL) {
keyPressed = true;
}
});
}
void initPages() {
// 主页面: 天气
addPage([](bool init) {
time_t timestamp = time(nullptr);
tm *ptime = localtime(×tamp);
#if SLEEP_ON_NIGHT == true
if (ptime->tm_hour >= 0 && ptime->tm_hour < 5) {
goto update_timer;
}
#endif
String title = datetimeToString(FORMAT_DATETIME, ptime);
Weather currentWeather = {};
Weather hourlyWeather[6] = {};
DailyWeather dailyWeather = {};
dailyWeather.date = rtcdata.date;
dailyWeather.sunrise = rtcdata.sunrise;
dailyWeather.sunset = rtcdata.sunset;
dailyWeather.moonPhase = rtcdata.moonPhase;
dailyWeather.moonPhaseIcon = rtcdata.moonPhaseIcon;
HourlyForecast hourlyForecast = {
.weather = hourlyWeather,
.length = ARRAY_LENGTH(hourlyWeather),
.interval = config.hour_step
};
DailyForecast dailyForecast = {
.weather = &dailyWeather,
.length = 1
};
bool success = api.getWeatherNow(currentWeather, config.location_id)
& api.getForecastHourly(hourlyForecast, config.location_id);
if (rtcdata.location_id != config.location_id ||
dailyWeather.date.substring(8, 10).toInt() != ptime->tm_mday) {
success &= api.getForecastDaily(dailyForecast, config.location_id);
strcpy(rtcdata.date, dailyWeather.date.c_str());
strcpy(rtcdata.sunrise, dailyWeather.sunrise.c_str());
strcpy(rtcdata.sunset, dailyWeather.sunset.c_str());
strcpy(rtcdata.moonPhase, dailyWeather.moonPhase.c_str());
rtcdata.moonPhaseIcon = dailyWeather.moonPhaseIcon;
}
rtcdata.location_id = config.location_id;
startDraw(epd);
bool isSleeping = false;
#if SLEEP_TIMEOUT > 0
#if defined(ESP8266)
isSleeping = ESP.getResetInfoPtr()->reason == REASON_DEEP_SLEEP_AWAKE;
#elif defined(ESP32)
isSleeping = esp_reset_reason() == ESP_RST_DEEPSLEEP;
#endif
#endif
drawTitleBar(title.c_str(), isSleeping, WiFi.RSSI(), getBatteryLevel());
drawWeatherNow(currentWeather);
drawForecastHourly(hourlyForecast);
drawForecastDaily(dailyForecast);
if (!success) {
u8g2Fonts.setFont(u8g2_font_wqy12_t);
u8g2Fonts.setForegroundColor(GxEPD_RED);
drawCenteredString(u8g2Fonts, epd.width() / 2, epd.height() / 2 + 6, TEXT_WEATHER_FAILED);
u8g2Fonts.setForegroundColor(GxEPD_BLACK);
}
endDraw(epd);
sleepTimer = millis();
update_timer:
rtcdata.next_update = (time(nullptr) / config.update_interval + 1) * config.update_interval;
// 如果上一次更新失败导致 next_update 没有更新, 从而导致这次更新时 next_update 仍在当前时间之前
if (rtcdata.next_update < time(nullptr)) {
rtcdata.next_update += config.update_interval;
}
});
// Bilibili页面: 显示up主粉丝数, 播放量和点赞量
addPage([](bool init) {
Bilibili bilibili = {};
api.getFollower(bilibili, config.bilibili_uid);
bool success = false;
if (*config.bilibili_cookie != '\0') {
success = api.getLikes(bilibili, config.bilibili_uid, config.bilibili_cookie);
}
startDraw(epd);
// epd.drawInvertedBitmap(8, (epd.height() - 55) / 2, IMG_BILI_LOGO, 52, 55, GxEPD_BLACK);
epd.drawInvertedBitmap(8, (epd.height() - 65) / 2, IMG_BILI_LOGO_2, 68, 65, GxEPD_BLACK);
uint16_t x = 80;
u8g2Fonts.setFont(u8g2_font_fub30_tf);
drawCenteredString(u8g2Fonts, (x + epd.width() - 8) / 2, 56, humanizeNumber(bilibili.follower).c_str());
if (success) {
u8g2Fonts.setFont(u8g2_font_bili_icon_16);
u8g2Fonts.drawUTF8(x + 10, epd.height() - 16, "\uE6E3");
u8g2Fonts.drawUTF8(x + 68, epd.height() - 16, "\uE6E0");
u8g2Fonts.setFont(u8g2_font_wqy12_t);
u8g2Fonts.drawUTF8(x + 34, epd.height() - 20, humanizeNumber(bilibili.view).c_str());
u8g2Fonts.drawUTF8(x + 94, epd.height() - 20, humanizeNumber(bilibili.likes).c_str());
}
endDraw(epd);
});
// 下位机页面: 显示从 HTTP 接口发来的文本
pageCustom = addPage([](bool init) {
startDraw(epd);
u8g2Fonts.setFont(u8g2_font_wqy12_t);
u8g2Fonts.setCursor(0, 12);
if (displayBuffer.length()) {
u8g2Fonts.print(displayBuffer);
} else {
u8g2Fonts.printf(TEXT_CUSTOM_EMPTY, WiFi.localIP().toString().c_str());
}
endDraw(epd);
});
// 关于页面: 显示IP, 版本和版权信息以及必不可少的一言
addPage([](bool init) {
Hitokoto hitokoto = {};
api.getHitokoto(hitokoto);
startDraw(epd);
u8g2Fonts.setFont(u8g2_font_wqy12_t);
u8g2Fonts.setCursor(0, 24);
u8g2Fonts.printf(" 墨水屏智能助理 %s\n", version);
u8g2Fonts.println(" Copyright (C) 2022-2023 WC");
u8g2Fonts.println(" 气象数据由 和风天气 提供");
u8g2Fonts.println("");
u8g2Fonts.println(" IP: " + WiFi.localIP().toString());
u8g2Fonts.println("");
u8g2Fonts.print(" ");
u8g2Fonts.println(hitokoto.sentence);
endDraw(epd);
});
}
// TODO 适配硬件 RTC, 我这块板子上没有, 等换板子再说吧
void setup() {
// BUG 上电的时候有概率墨水屏颜色会变淡, 还不知道是什么原因造成的
/*
#if defined(ESP8266) && EPD_CS == 15 // ESP8266 的 GPIO15 在上电时为低电平, 会导致墨水屏被选中
digitalWrite(EPD_CS, HIGH);
pinMode(EPD_CS, OUTPUT);
delay(500);
#endif
*/
#ifdef ESP32
SPI.begin(EPD_CLK, -1, EPD_MOSI, EPD_CS);
#endif
u8g2Fonts.begin(epd);
u8g2Fonts.setForegroundColor(GxEPD_BLACK);
u8g2Fonts.setBackgroundColor(GxEPD_WHITE);
epd.setRotation(EPD_ROTATION);
checkBattery();
Serial.begin(115200);
Serial.println();
Serial.print(F("E-Ink Assistant, version: "));
Serial.println(version);
Serial.println(F("Made by QingChenW with love"));
#if defined(ESP8266)
#define RST_REASON_DEEP_SLEEP REASON_DEEP_SLEEP_AWAKE
uint32_t resetReason = ESP.getResetInfoPtr()->reason;
#elif defined(ESP32)
#define RST_REASON_DEEP_SLEEP ESP_RST_DEEPSLEEP
uint32_t resetReason = esp_reset_reason();
#endif
// 这里不判断 SLEEP_TIMEOUT 是因为我要的效果是只要从休眠中唤醒就不显示加载界面
if (resetReason != RST_REASON_DEEP_SLEEP) {
#if !ENABLE_LOADING_SCREEN
epd.init(0);
epd.clearScreen();
epd.hibernate();
#else
startDraw(epd);
u8g2Fonts.setFont(u8g2_font_wqy12_t);
drawCenteredString(u8g2Fonts, epd.width() / 2, epd.height() * 3 / 4, TEXT_LOADING);
endDraw(epd, true);
#endif
} else {
#ifdef ESP8266
ESP.rtcUserMemoryRead(0, (uint32_t*) &rtcdata, sizeof(RTCData));
#endif
// 从 RTC memory 里取出唤醒时的时间, 并判断是否需要继续休眠
timeval tv;
tv.tv_sec = rtcdata.wakeup_time;
settimeofday(&tv, nullptr);
if (rtcdata.page == 0 && rtcdata.next_update - rtcdata.wakeup_time > 60) {
gotoSleep();
}
}
EEPROM.begin(sizeof(Config));
EEPROM.get(0, config);
if (config.version != version_code) {
Serial.printf_P(PSTR("Update config from %d to %d\n"), config.version, version_code);
resetConfig();
}
if (resetReason != RST_REASON_DEEP_SLEEP)
delay(3000); // 如果去掉延时, 建议不要显示加载界面
#if defined(ESP8266)
volatile bool has_set_time = false;
settimeofday_cb([&has_set_time](bool sntp) { has_set_time = true; });
configTime(TIMEZONE, NTP_SERVERS);
#elif defined(ESP32)
static volatile bool has_set_time = false;
sntp_set_time_sync_notification_cb([](struct timeval *tv) { has_set_time = true; });
configTzTime(TIMEZONE, NTP_SERVERS);
#endif
Serial.print(F("WiFi connecting"));
WiFi.persistent(true);
WiFi.setAutoReconnect(true);
WiFi.begin();
while (true) {
wl_status_t status = WiFi.status();
uint8_t retry = 0;
while (status != WL_CONNECTED && retry++ < 20) {
delay(1000);
Serial.print('.');
status = WiFi.status();
}
Serial.println();
if (status == WL_CONNECTED) {
Serial.print(F("Connected, IP address: "));
Serial.println(WiFi.localIP());
break;
} else {
if (SLEEP_TIMEOUT && resetReason == RST_REASON_DEEP_SLEEP) {
Serial.println(F("Failed, try connect next time"));
gotoSleep();
}
Serial.println(F("Failed, try SmartConfig"));
startDraw(epd);
drawQRCode(epd, (epd.width() - 58) / 2, 16, 2, WIFI_CONFIG_URL);
u8g2Fonts.setFont(u8g2_font_wqy12_t);
drawCenteredString(u8g2Fonts, epd.width() / 2, epd.height() - 12, TEXT_SMART_CONFIG);
endDraw(epd, true);
#if defined(ESP8266)
smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS);
WiFi.beginSmartConfig();
#elif defined(ESP32)
WiFi.beginSmartConfig(SC_TYPE_ESPTOUCH_AIRKISS);
#endif
while (!WiFi.smartConfigDone()) {
delay(1000);
Serial.print('.');
}
}
}
WiFi.setHostname(config.hostname);
initPages();
if (!SLEEP_TIMEOUT || resetReason != RST_REASON_DEEP_SLEEP) {
server.on("/", HTTP_GET, []() {
server.send(200, MIME_TYPE(html), F("Hello World!"));
});
server.on("/version", HTTP_GET, []() {
StaticJsonDocument<256> doc;
doc["product"] = product_name;
doc["model"] = model_name;
doc["version"] = version;
doc["version_code"] = version_code;
#if defined(ESP8266)
doc["sdk_version"] = ESP.getFullVersion();
#elif defined(ESP32)
doc["sdk_version"] = ESP.getSdkVersion();
#endif
String str;
serializeJson(doc, str);
server.send(200, MIME_TYPE(json), str);
});
server.on("/status", HTTP_GET, []() {
StaticJsonDocument<256> doc;
#if defined(ESP8266)
doc["reset_reason"] = ESP.getResetReason();
doc["free_heap"] = ESP.getFreeHeap();
doc["heap_fragment"] = ESP.getHeapFragmentation();
doc["max_free_block"] = ESP.getMaxFreeBlockSize();
#elif defined(ESP32)
doc["reset_reason"] = esp_reset_reason();
doc["free_heap"] = ESP.getFreeHeap();
doc["heap_max_free_block"] = ESP.getMaxAllocHeap();
doc["free_psram"] = ESP.getFreePsram();
doc["psram_max_free_block"] = ESP.getMaxAllocPsram();
#endif
doc["RSSI"] = WiFi.RSSI();
doc["page"] = rtcdata.page;
doc["next_update"] = rtcdata.next_update;
String str;
serializeJson(doc, str);
server.send(200, MIME_TYPE(json), str);
});
server.on("/config", HTTP_GET, []() {
StaticJsonDocument<256> doc;
doc["ip"] = WiFi.localIP().toString();
doc["mask"] = WiFi.subnetMask().toString();
doc["gateway"] = WiFi.gatewayIP().toString();
doc["hostname"] = config.hostname;
doc["update_itv"] = config.update_interval;
doc["theme"] = config.theme;
doc["hour_step"] = config.hour_step;
doc["locid"] = config.location_id;
doc["uid"] = config.bilibili_uid;
String str;
serializeJson(doc, str);
server.send(200, MIME_TYPE(json), str);
});
server.on("/config", HTTP_POST, []() {
String value = server.arg("hostname");
if (value.length() > 0)
strncpy(config.hostname, value.c_str(), 24);
value = server.arg("update_itv");
if (value.length() > 0)
config.update_interval = max((int) value.toInt(), 300);
value = server.arg("theme");
if (value.length() > 0)
config.theme = min(max((int) value.toInt(), -1), 1);
value = server.arg("hour_step");
if (value.length() > 0)
config.hour_step = max((int) value.toInt(), 1);
value = server.arg("locid");
if (value.length() > 0)
config.location_id = value.toInt();
value = server.arg("uid");
if (value.length() > 0)
config.bilibili_uid = value.toInt();
value = server.arg("bili_cookie");
if (value.length() > 0)
strncpy(config.bilibili_cookie, value.c_str(), 36);
EEPROM.put(0, config);
refreshPage();
server.send(200, MIME_TYPE(txt), EEPROM.commit() ? "OK" : "FAIL");
});
server.on("/reset", HTTP_GET, []() {
bool wifi = server.arg("wifi") == "true";
server.send(200, MIME_TYPE(txt), resetConfig(wifi) ? "OK" : "FAIL");
});
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, MIME_TYPE(txt), !Update.hasError() ? "OK" : "FAIL");
delay(1000); // 不延时的话无法返回响应
Serial.println(F("Rebooting..."));
ESP.restart();
}, []() {
HTTPUpload &upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.print(F("Upload file: "));
Serial.println(upload.filename.c_str());
prepareOTA();
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (Update.begin(maxSketchSpace)) {
Serial.println(F("Start to update"));
} else {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) {
Serial.print(F("Update Success: "));
Serial.println(upload.totalSize);
} else {
Update.printError(Serial);
}
}
yield();
});
server.on("/display", HTTP_POST, []() {
displayBuffer = server.arg("content");
if (rtcdata.page == pageCustom) {
refreshPage();
}
delay(3000);
server.send(200, MIME_TYPE(txt), "OK");
});
server.begin();
MDNS.begin(config.hostname);
MDNS.addService("http", "tcp", 80);
}
Serial.print(F("Waiting for time sync..."));
sleepTimer = millis();
while (!has_set_time) {
delay(500);
if (millis() - sleepTimer > 30000) {
break;
}
}
#if defined(ESP8266)
settimeofday_cb(BoolCB());
#elif defined(ESP32)
sntp_set_time_sync_notification_cb(nullptr);
#endif
if (!has_set_time) {
Serial.println(F("NTP timeout"));
Serial.print(F("Try to use http api..."));
uint64_t timestamp;
if (api.getTimestamp(timestamp)) {
timeval tv = {
.tv_sec = timestamp / 1000LL,
.tv_usec = (timestamp % 1000L) * 1000L
};
settimeofday(&tv, NULL);
} else {
if (!SLEEP_TIMEOUT || resetReason != RST_REASON_DEEP_SLEEP) {
startDraw(epd);
u8g2Fonts.setFont(u8g2_font_wqy12_t);
u8g2Fonts.setForegroundColor(GxEPD_RED);
drawCenteredString(u8g2Fonts, epd.width() / 2, epd.height() * 3 / 4, TEXT_TIME_ERROR);
u8g2Fonts.setForegroundColor(GxEPD_BLACK);
endDraw(epd, true);
}
gotoSleep();
}
}
Serial.println(F("Done"));
if (SLEEP_TIMEOUT && resetReason == RST_REASON_DEEP_SLEEP && rtcdata.page == 0) {
if (time(nullptr) - rtcdata.next_update > -60) {
while (time(nullptr) - rtcdata.next_update < 0)
delay(1000);
refreshPage();
}
gotoSleep();
}
refreshPage();
pinMode(KEY_SWITCH, KEY_PIN_MODE);
attachInterrupt(KEY_SWITCH, onKeyPressed, KEY_TRIGGER_LEVEL == LOW ? FALLING : RISING);
}
void loop() {
checkBattery();
#ifdef ESP8266
MDNS.update();
#endif
server.handleClient();
if (Update.isRunning()) return;
if (keyPressed) {
while (digitalRead(KEY_SWITCH) == KEY_TRIGGER_LEVEL)
delay(10);
nextPage();
keyPressed = false;
}
if (rtcdata.page == 0) {
#if SLEEP_TIMEOUT > 0
if (millis() - sleepTimer >= SLEEP_TIMEOUT * 1000) {
time_t timestamp = time(nullptr);
tm *time = localtime(×tamp);
String title = datetimeToString(FORMAT_DATETIME, time);
startDraw(epd);
drawTitleBar(title.c_str(), true, WiFi.RSSI(), getBatteryLevel());
epd.displayWindow(0, 0, epd.width(), 16);
epd.hibernate();
gotoSleep();
}
#endif
time_t delta = time(nullptr) - rtcdata.next_update;
if (delta > 0 && delta <= 60) {
refreshPage();
}
}
// TODO 在这里延时 100ms 以上可以让 MCU 进入 Modem-sleep 模式
// 可以将待机电流降低到 20mA, 但是 http 响应时间会增加 1 秒
// delay(100);
}