-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepdWeatherClockV1.ino
1388 lines (1240 loc) · 46.5 KB
/
epdWeatherClockV1.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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
epdWeatherClockV1.ino
Copyright (C) 2024 desiFish
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//=============== HEADER SECTION ===============
// E-paper weather clock v1 - Main code
// Uses GxEPD2 library for e-paper display control
// Using Huge App partition (3MB NO OTA/1MB SPIFFS)
//=============== CONFIGURATION ===============
// Enable/disable GxEPD2_GFX base class - uses ~1.2k more code
#define ENABLE_GxEPD2_GFX 0
#include <GxEPD2_3C.h> // 3-color e-paper display
#include <Fonts/FreeMonoBold9pt7b.h>
#include <U8g2_for_Adafruit_GFX.h> // Include U8g2 fonts
#include <Wire.h> // Used to establish serial communication on the I2C bus
#include <SparkFun_TMP117.h> // TMP117 temperature sensor library
#include <Adafruit_Sensor.h> // Adafruit sensor library
#include "Adafruit_BME680.h" // BME680 environmental sensor library
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "RTClib.h" // RTC library
#include "image.h" //for sleep icon
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <BH1750.h> // Light sensor library
#include <TimeLib.h> // for time functions
#include "icons.h" // for weather icons
#include <Arduino.h>
#include <ESPAsyncWebServer.h> // for web server
#include <AsyncTCP.h> // for tcp connection
#include <Preferences.h> // for storing data in flash memory
#include <esp_wifi.h> // for wifi functions
//=============== GLOBAL OBJECTS =================
Preferences pref;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// your wifi name and password
String ssid;
String password;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 19800); // 19800 is offset of India, asia.pool.ntp.org is close to India
// save number of boots
RTC_DATA_ATTR int bootCount = 0; // Persistent boot counter stored in RTC memory
const byte ghostProtek = 5; // ghost protection, 5 means for every 5 boots, 1 boot will be in dark mode
// openWeatherMap Api Key from your profile in account section
String openWeatherMapApiKey = ""; // add your profile key here when running for the first time
// personal custom Api Key from your server
String customApiKey = ""; // add your api key here when running for the first time
// Replace with your lat and lon
String lat = "22.5895515";
String lon = "88.2876455";
RTC_DS3231 rtc; // Initalize rtc
TMP117 sensor; // Initalize temperature sensor
Adafruit_BME680 bme; // Initalize environmental sensor
BH1750 lightMeter(0x23); // Initalize light sensor
// Initalize display for 400x300, UC8276
GxEPD2_3C<GxEPD2_420c_Z21, GxEPD2_420c_Z21::HEIGHT> display(GxEPD2_420c_Z21(/*CS=5*/ /* SS*/ D7, /*DC=*/D1, /*RST=*/D2, /*BUSY=*/D3));
#define BATPIN A0 // Battery voltage divider pin (1M Ohm with 104 Capacitor)
#define DEBUG_PIN D6 // Debug mode toggle pin
/*
GxEPD2_3C<GxEPD2_420c_Z21, GxEPD2_420c_Z21::HEIGHT> display(GxEPD2_420c_Z21(20, 3, 4, 5)); //for XIAO_ESP32C3 in case the above declaration gives error
#define BATPIN 2 // Battery voltage divider pin (1M Ohm with 104 Capacitor)
#define DEBUG_PIN 21 // Debug mode toggle pin
*/
/*
GxEPD2_3C<GxEPD2_420c_Z21, GxEPD2_420c_Z21::HEIGHT> display(GxEPD2_420c_Z21(17, 1, 2, 21)); //for XIAO_ESP32C6
#define BATPIN 0 // Battery voltage divider pin (1M Ohm with 104 Capacitor)
#define DEBUG_PIN 16 // Debug mode toggle pin
*/
U8G2_FOR_ADAFRUIT_GFX u8g2Fonts; // u8g2 fonts
//=============== GLOBAL CONSTANTS ===============
#define BATTERY_LEVEL_SAMPLING 4 // BATTERY_LEVEL_SAMPLING: Number of samples to average for battery reading
#define battType 3.6 // battType: Battery nominal voltage (ICR: 4.2V, LFP: 3.6V) (Change accordingly)
#define battChangeThreshold 0.15 // battChangeThreshold: Minimum voltage change to update battery level
#define battUpperLim 3.3 // battUpperLim: Maximum expected battery voltage (Change accordingly)
#define battHigh 3.4 // battHigh: Healthy battery threshold voltage (Change accordingly)
#define battLow 2.9 // battLow: Low battery warning threshold (Change accordingly)
#define critBattPercent 30 // critBattPercent: Critical battery percentage threshold
/**
* @brief Sleep configuration
* uS_TO_S_FACTOR: Microseconds to seconds conversion
* TIME_TO_SLEEP: Sleep duration in seconds (default 15 mins)
*/
#define uS_TO_S_FACTOR 1000000
int TIME_TO_SLEEP = 900; // 15 minutes
//=============== GLOBAL VARIABLES ===============
// State variables
int nightFlag = 0; // Night mode state preserved across sleep
float battLevel; // Current battery level
bool DEBUG_MODE = false; // Debug mode state
bool BATTERY_CRITICAL = false; // Critical battery state
String jsonBuffer; // for storing json data from api
// for storing highest temp and lowest temp of the day
float hTemp, lTemp;
char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char monthName[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int httpResponseCode; // for storing http response code
// DEBUG_MODE update frequency
unsigned long lastTime1 = 0; // Last light sensor update
const long timerDelay1 = 60000; // Light sensor update interval (60 seconds)
// Define base URLs as const char arrays
const char OPEN_WEATHER_BASE_URL[] = "http://api.openweathermap.org/data/3.0/onecall?lat=";
const char OPEN_WEATHER_PARAMS[] = "&exclude=hourly,minutely&units=metric&appid=";
const char CUSTOM_WEATHER_BASE_URL[] = "http://iotthings.pythonanywhere.com/api/weatherStation/serve?api_key=";
//=============== HTML CODE =================
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html><html><head><title>WiFi Setup</title><meta name="viewport" content="width=device-width,initial-scale=1"><style>body{font-family:Arial;text-align:center;margin:20px}input{margin:10px;padding:5px}form{background:#f0f0f0;padding:20px;display:inline-block}</style></head><body><h1>Weather Station Setup</h1><form action="/" method="POST"><label for="ssid">SSID</label><br><input type="text" id="ssid" name="ssid"><br><label for="pass">Password</label><br><input type="text" id="pass" name="pass"><br><input type="submit" value="Connect"></form></body></html>
)rawliteral";
// Search for parameter in HTTP POST request
const char *PARAM_INPUT_1 = "ssid";
const char *PARAM_INPUT_2 = "pass";
//=============== HELPER FUNCTIONS ===============
/**
* @brief Measures battery voltage with averaging
* @return float Averaged battery voltage in volts
* @note Uses voltage divider with 1MΩ resistor and 104 capacitor
*/
float batteryLevel()
{
uint32_t Vbatt = 0;
for (int i = 0; i < BATTERY_LEVEL_SAMPLING; i++)
{
Vbatt = Vbatt + analogReadMilliVolts(BATPIN); // ADC with correction
delay(10);
}
float Vbattf = 2 * Vbatt / BATTERY_LEVEL_SAMPLING / 1000.0; // attenuation ratio 1/2, mV --> V
// if (DEBUG_MODE) Serial.println(Vbattf);
return (Vbattf);
}
/**
* @brief Disables WiFi and enters power saving mode
* @param extreme If true, reduces CPU frequency to 10MHz
* @note Reduces CPU frequency and disables unused peripherals
*/
void turnOffWifi(bool extreme = false)
{
// Disable WiFi
WiFi.disconnect(true); // Disconnect and clear credentials
WiFi.mode(WIFI_OFF); // Set WiFi mode to off
esp_wifi_stop(); // Stop WiFi
// Additional power savings
btStop(); // Disable Bluetooth - more compatible than esp_bt_controller_disable()
// Reduce CPU frequency last
if (extreme)
setCpuFrequencyMhz(10); // Set CPU to 10MHz
else
setCpuFrequencyMhz(20); // Set CPU to 20MHz
delay(5); // wait for 5ms
if (DEBUG_MODE)
{
Serial.println("Power saving mode enabled");
Serial.println(getCpuFrequencyMhz());
}
}
/**
* @brief Updates RTC time from NTP server if necessary
*
* This function checks if an update is needed based on the following criteria:
* 1. If it's the first run (lastUpdateDay is 0)
* 2. If 20 days have passed since the last update
* 3. If a force update is requested
*
* It handles month rollovers when calculating days passed.
* If an update is needed and WiFi is connected, it fetches the current time
* from an NTP server and updates the RTC.
*
* @param forceUpdate If true, bypasses the normal update interval check
* @return bool Returns true if the time was successfully updated, false otherwise
* @note Requires an active WiFi connection to function
* @note Uses Preferences to store the last update day
*/
bool autoTimeUpdate(bool forceUpdate = false)
{
if (!pref.isKey("lastUpdateDay"))
pref.putUChar("lastUpdateDay", 0);
byte lastUpdateDay = pref.getUChar("lastUpdateDay", 0);
DateTime now = rtc.now();
byte currentDay = now.day();
// Calculate days passed, handling month rollover
int daysPassed = (currentDay - lastUpdateDay + 31) % 31; // Handle month rollover
// Check if 20 days have passed since last update
if (lastUpdateDay == 0 || daysPassed >= 20 || forceUpdate)
{
if (WiFi.status() == WL_CONNECTED)
{
timeClient.begin();
if (timeClient.update() && timeClient.isTimeSet())
{
time_t rawtime = timeClient.getEpochTime();
struct tm *ti = localtime(&rawtime);
uint16_t year = ti->tm_year + 1900;
uint8_t month = ti->tm_mon + 1;
uint8_t day = ti->tm_mday;
rtc.adjust(DateTime(year, month, day,
timeClient.getHours(),
timeClient.getMinutes(),
timeClient.getSeconds()));
// Update last update day
pref.putUChar("lastUpdateDay", currentDay);
if (DEBUG_MODE)
{
Serial.println("RTC updated: " + String(year) + "-" + String(month) + "-" + String(day));
}
return true;
}
}
}
return false;
}
/**
* @brief Prints temperature and environmental data
* @param offset Vertical offset for display positioning (default: 0)
* @param invert Inverts colors for ghost protection (default: false)
*/
void tempPrint(byte offset = 0, bool invert = false);
/**
* @brief Fetches and displays weather data
* @param invert Inverts display colors for ghost protection
* @note Requires active WiFi connection and valid API keys
*/
void weatherPrint(bool invert = false);
//=============== MAIN SETUP AND LOOP ===============
/**
* @brief Initialize and configure all hardware and software components
*
* This function performs the following initializations:
* 1. CPU and Debug Configuration
* - Sets CPU frequency to power-saving mode (20MHz)
* - Initializes serial communication if in debug mode
* - Configures debug pin and mode
*
* 2. Power Management
* - Initializes battery monitoring
* - Manages critical battery state
* - Configures WiFi power state based on battery level
*
* 3. Hardware Initialization
* - Configures I2C communication
* - Initializes e-paper display
* - Sets up environmental sensors (TMP117, BME680)
* - Configures light sensor (BH1750)
*
* 4. State Management
* - Handles night mode transitions
* - Manages data persistence with preferences
* - Updates high/low temperature records
*
* 5. Network Configuration
* - Handles WiFi setup and connection
* - Configures NTP time synchronization
* - Sets up weather API access
*
* 6. Display Functions
* - Updates screen based on current state
* - Handles ghost protection display rotation
* - Shows status information and sensor data
*
* @note Enters deep sleep mode after completion unless in debug mode
* @note Some features are disabled when battery is critical
*/
void setup()
{
setCpuFrequencyMhz(20); // Set CPU to 20MHz
pinMode(BATPIN, INPUT);
pinMode(DEBUG_PIN, INPUT);
if (digitalRead(DEBUG_PIN) == 1) // Check if debug mode is enabled
DEBUG_MODE = true;
if (DEBUG_MODE)
{
Serial.begin(115200);
Serial.println("Setup");
Serial.println(getCpuFrequencyMhz());
}
pref.begin("database", false); // Open the preferences "database"
if (!pref.isKey("battCrit"))
pref.putBool("battCrit", false);
BATTERY_CRITICAL = pref.getBool("battCrit", false);
bool tempBATTERY_CRITICAL = BATTERY_CRITICAL;
if (BATTERY_CRITICAL)
turnOffWifi(true); // turn off wifi to save power when battery is critical
Wire.begin(); // Start the I2C communication
Wire.setClock(400000); // Set clock speed to be the fastest for better communication (fast mode)
analogReadResolution(12); // Set ADC resolution to 12-bit
display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
u8g2Fonts.begin(display); // connect u8g2 procedures to Adafruit GFX
if (!pref.isKey("nightFlag"))
{ // create key:value pair
pref.putBool("nightFlag", false);
}
nightFlag = pref.getBool("nightFlag", false);
if (lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE))
{
if (DEBUG_MODE)
Serial.println(F("BH1750 Advanced begin"));
}
else
{
if (DEBUG_MODE)
Serial.println(F("Error initialising BH1750"));
errMsg("Error BH1750");
while (1)
yield(); // Runs forever
}
float lux = 0; // Light level in lux
while (!lightMeter.measurementReady(true))
{
yield(); // Wait for the measurement to be ready
}
lux = lightMeter.readLightLevel(); // Get Lux value from sensor
if (DEBUG_MODE)
{
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
}
// if battery is critical, then no need to check wifi and weather api
if ((!BATTERY_CRITICAL && lux != 0) || DEBUG_MODE == true)
{
if (!pref.isKey("ssid"))
{ // create key:value pairs
pref.putString("ssid", "");
pref.putString("password", "");
}
ssid = pref.getString("ssid", "");
password = pref.getString("password", "");
if (ssid == "" || password == "")
{
setCpuFrequencyMhz(80); // Set CPU to 80MHz for wifi manager
// if no ssid or password saved, then start the wifi manager
if (DEBUG_MODE)
Serial.println("No values saved for ssid or password");
// Connect to Wi-Fi network with SSID and password
if (DEBUG_MODE)
Serial.println("Setting AP (Access Point)");
// NULL sets an open Access Point
WiFi.softAP("WCLOCK-WIFI-MANAGER", NULL);
IPAddress IP = WiFi.softAPIP();
if (DEBUG_MODE)
{
Serial.print("AP IP address: ");
Serial.println(IP);
}
debugPrinter("Connect to 'WCLOCK-WIFI-MANAGER' \nfrom your phone or computer (Wifi).\n\nThen go to " + IP.toString() + "\nfrom your browser.");
// Web Server Root URL
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(200, "text/html", index_html); });
server.on("/", HTTP_POST, [](AsyncWebServerRequest *request)
{
int params = request->params();
for (int i = 0; i < params; i++) {
const AsyncWebParameter *p = request->getParam(i);
if (p->isPost()) {
// HTTP POST ssid value
if (p->name() == PARAM_INPUT_1) {
ssid = p->value();
if (DEBUG_MODE) {
Serial.print("SSID set to: ");
Serial.println(ssid);
}
ssid.trim();
pref.putString("ssid", ssid);
}
// HTTP POST pass value
if (p->name() == PARAM_INPUT_2) {
password = p->value();
if (DEBUG_MODE) {
Serial.print("Password set to: ");
Serial.println(password);
}
password.trim(); // remove leading and trailing spaces
pref.putString("password", password);
}
//if (DEBUG_MODE) Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
request->send(200, "text/html", "<h2>Done. Weather Station will now restart</h2>");
delay(3000);
ESP.restart(); });
server.begin();
while (true)
yield(); // Runs forever
}
}
float hTempHold, lTempHold, tempBattLevel;
// if lux is 0, then the device is in dark mode and no need to initialize sensors
if (lux != 0 || DEBUG_MODE == true)
{
if (!rtc.begin())
{
if (DEBUG_MODE)
Serial.println("Couldn't find RTC");
errMsg("Error RTC");
while (1)
; // Runs forever
}
if (DEBUG_MODE)
Serial.println("RTC Ready");
DateTime now = rtc.now();
if ((now.hour() == 0) && (now.minute() >= 0 && now.minute() < 15))
{ // reset high low at midnight
pref.putFloat("hTemp", 0.0);
pref.putFloat("lTemp", 60.0);
}
if (sensor.begin() == true) // Function to check if the TMP117 will correctly self-identify with the proper Device ID/Address
{
if (DEBUG_MODE)
Serial.println("TMP117 Begin");
}
else
{
if (DEBUG_MODE)
Serial.println("Device failed to setup- Freezing code.");
errMsg("Error TMP117");
while (1)
; // Runs forever
}
if (!bme.begin())
{
if (DEBUG_MODE)
Serial.println(F("Could not find a valid BME680 sensor, check wiring!"));
errMsg("Error BME680");
while (1)
; // Runs forever
}
if (DEBUG_MODE)
Serial.println("BME Ready");
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_2X);
bme.setHumidityOversampling(BME680_OS_16X);
bme.setPressureOversampling(BME680_OS_16X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_7);
bme.setGasHeater(0, 0); // 0*C for 0 ms
if (!BATTERY_CRITICAL) // Connect to Wi-Fi network with SSID and password if battery is not critical
{
setCpuFrequencyMhz(80); // Set CPU to 80MHz for wifi
delay(5);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
if (DEBUG_MODE)
Serial.println("Connection Failed");
break;
}
if (WiFi.status() == WL_CONNECTED) // if wifi is connected
{
if (DEBUG_MODE)
{
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
}
// Get the current day
if (!pref.isKey("timeNeedsUpdate")) // create key:value pairs
pref.putBool("timeNeedsUpdate", true);
bool timeNeedsUpdate = pref.getBool("timeNeedsUpdate", false);
DateTime now = rtc.now();
if ((now.year() == 1970) || rtc.lostPower())
timeNeedsUpdate = true;
byte currentDay = now.day();
// Check if we need to update time (once per day)
if (!pref.isKey("lastCheckedDay")) // create key:value pairs
pref.putUChar("lastCheckedDay", 0);
byte lastCheckedDay = pref.getUChar("lastCheckedDay", 0);
if ((lastCheckedDay != currentDay) || timeNeedsUpdate)
{
if (DEBUG_MODE)
Serial.println("Updating time from NTP server");
if (autoTimeUpdate(timeNeedsUpdate)) // Update time from NTP server
if (DEBUG_MODE)
Serial.println("Time Updated");
else if (DEBUG_MODE)
Serial.println("Time Not updated");
timeNeedsUpdate = false;
pref.putBool("timeNeedsUpdate", false);
lastCheckedDay = currentDay;
pref.putUChar("lastCheckedDay", lastCheckedDay);
}
else if (DEBUG_MODE)
Serial.println("Time already updated");
// Check if the API keys are saved in the preferences
if (!pref.isKey("api")) // create key:value pairs
pref.putString("api", openWeatherMapApiKey);
openWeatherMapApiKey = pref.getString("api", "");
if (!pref.isKey("apiCustom")) // create key:value pairs
pref.putString("apiCustom", customApiKey);
customApiKey = pref.getString("apiCustom", "");
}
else
turnOffWifi(); // turn off wifi to save power when wifi is not connected
}
hTemp = pref.getFloat("hTemp", -1.0);
lTemp = pref.getFloat("lTemp", -1.0);
battLevel = pref.getFloat("battLevel", -1.0);
if (hTemp == -1.0 || lTemp == -1.0 || battLevel == -1.0)
{
if (DEBUG_MODE)
Serial.println("No values saved for hTemp, lTemp or battLevel");
pref.putFloat("hTemp", 0.0);
pref.putFloat("lTemp", 60.0);
pref.putFloat("battLevel", battType);
}
hTempHold = hTemp, lTempHold = lTemp, tempBattLevel = battLevel;
}
bool tempNightFlag = nightFlag;
if (DEBUG_MODE)
Serial.println("Setup done");
if (lux == 0)
{
TIME_TO_SLEEP = 300; // 5 min sleep time in dark mode
if (nightFlag == 0) // prevents unnecessary redrawing of same thing in dark mode
{
nightFlag = 1;
display.setRotation(0);
display.setFullWindow();
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
display.drawInvertedBitmap(0, 0, nightMode, 400, 300, GxEPD_BLACK); // display sleep icon
} while (display.nextPage());
}
display.hibernate();
display.powerOff();
}
else // if lux is not 0, then the device is in normal mode
{
nightFlag = 0;
display.setRotation(0);
display.setFullWindow();
display.firstPage();
do
{
if (WiFi.status() == WL_CONNECTED) // if wifi is connected, then fetch weather data
{
++bootCount; // increment the boot counter
if (DEBUG_MODE)
Serial.println("Time And Weather");
if (bootCount == ghostProtek)
{
display.fillScreen(GxEPD_BLACK);
tempPrint(0, true); // prints temperature and battery level
weatherPrint(true); // prints weather data
}
else // if not ghost protection, then normal display
{
display.fillScreen(GxEPD_WHITE);
tempPrint(); // prints temperature and battery level
weatherPrint(); // prints weather data
}
if (bootCount == ghostProtek) // reset boot counter after ghost protection
bootCount = 0;
if (DEBUG_MODE)
Serial.println("Time And Weather Done");
}
else // if wifi is not connected, then only display time
{
display.fillScreen(GxEPD_WHITE);
if (DEBUG_MODE)
Serial.println("Time Only");
display.drawBitmap(270, 0, wifiOff, 12, 12, GxEPD_BLACK); // wifi off icon
tempPrint(40); // offset for wifi off which shifts the temperature display to the middle
if (DEBUG_MODE)
Serial.println("Time Done");
}
} while (display.nextPage());
display.hibernate();
display.powerOff();
}
if (DEBUG_MODE)
Serial.println("Data Write");
if (lux != 0) // if lux is 0, then the device is in sleep mode and no need to save data
{
if (hTempHold != hTemp)
pref.putFloat("hTemp", hTemp);
if (lTempHold != lTemp)
pref.putFloat("lTemp", lTemp);
if (tempBattLevel != battLevel)
pref.putFloat("battLevel", battLevel);
if (tempBATTERY_CRITICAL != BATTERY_CRITICAL)
pref.putBool("battCrit", BATTERY_CRITICAL);
}
if (tempNightFlag != nightFlag) // if night mode changes, then save the new state
pref.putBool("nightFlag", nightFlag);
pref.end(); // Close the preferences
Wire.end(); // End I2C communication
if (DEBUG_MODE)
{
Serial.println("Data Write Done");
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP / 60) + " Mins");
Serial.println("Going to sleep now");
Serial.flush(); // Flush the serial buffer
delay(5);
}
if (!DEBUG_MODE) // if debug mode is off, then go to deep sleep
{
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); // Set the sleep time
esp_deep_sleep_start(); // Enter deep sleep
}
else
Serial.println("Going to loop");
}
/**
* @brief Main loop function that runs continuously in debug mode
*
* This function only executes when DEBUG_MODE is true. It provides
* continuous monitoring and debugging capabilities by:
* 1. Checking timing conditions every timerDelay1 interval
* 2. Displaying debug messages on the e-paper display
* 3. Allowing for interactive testing and monitoring
*
* Future debug functionality can be added within the timer check.
* The function uses non-blocking delays via millis() to maintain
* responsiveness.
*
* @note This loop is skipped during normal operation (DEBUG_MODE = false)
* @note Uses timerDelay1 (60s) to prevent excessive display updates
*/
void loop()
{
if ((millis() - lastTime1) > timerDelay1)
{
Serial.println("In LOOP");
errMsg("DEBUG MODE"); // Display debug message
// Additional debug functions can be added here
lastTime1 = millis();
}
yield();
}
//=============== WEATHER AND DISPLAY FUNCTIONS ===============
/**
* @brief Fetches weather data from API endpoint
* @param serverName URL of the weather API endpoint
* @return String JSON response from server
*/
String weatherDataAPI(const char *serverName)
{
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0)
{
if (DEBUG_MODE)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
payload = http.getString();
}
else
{
if (DEBUG_MODE)
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
}
// Free resources
http.end();
return payload;
}
/**
* @brief Prints temperature and environmental data
* @param offset Vertical offset for display positioning (default: 0)
* @param invert Inverts colors for ghost protection (default: false)
*/
void tempPrint(byte offset, bool invert)
{
// Configure fonts and colors once at the start
uint16_t bg = invert ? GxEPD_BLACK : GxEPD_WHITE;
uint16_t fg = invert ? GxEPD_WHITE : GxEPD_BLACK;
uint16_t lineColor = (BATTERY_CRITICAL || invert) ? GxEPD_WHITE : GxEPD_RED;
u8g2Fonts.setFontMode(1);
u8g2Fonts.setFontDirection(0);
u8g2Fonts.setForegroundColor(fg);
u8g2Fonts.setBackgroundColor(bg);
// Temperature reading
float tempC = 0;
if (sensor.dataReady())
{
tempC = sensor.readTempC();
hTemp = max(hTemp, tempC);
lTemp = min(lTemp, tempC);
}
// Battery level handling
float newBattLevel = batteryLevel();
battLevel = (newBattLevel < battLevel) ? newBattLevel : ((newBattLevel - battLevel) >= battChangeThreshold || newBattLevel > battUpperLim) ? newBattLevel
: battLevel;
// Battery display section
u8g2Fonts.setFont(u8g2_font_luRS08_tf);
u8g2Fonts.setCursor(28, 11);
u8g2Fonts.print(battLevel, 2);
u8g2Fonts.print("V");
int percent = constrain(((battLevel - battLow) / (battHigh - battLow)) * 100, 0, 100);
BATTERY_CRITICAL = percent < critBattPercent;
u8g2Fonts.setCursor(63, 11);
if (!BATTERY_CRITICAL)
{
u8g2Fonts.print(percent, 1);
u8g2Fonts.print("%");
if (invert)
{
u8g2Fonts.setCursor(123, 11);
u8g2Fonts.print(" GHOSTING PROTECTION");
}
}
else
{
u8g2Fonts.print("BATTERY CRITICAL, WIFI TURNED OFF");
}
iconBattery(display, percent, invert);
// Time and date display
DateTime now = rtc.now();
char timeStr[6];
sprintf(timeStr, "%02d:%02d", now.hour(), now.minute());
u8g2Fonts.setCursor(295, 11);
u8g2Fonts.print("Last Update: ");
u8g2Fonts.print(timeStr);
u8g2Fonts.setFont(u8g2_font_logisoso20_tf);
u8g2Fonts.setCursor(10, 75 + offset);
u8g2Fonts.print(now.day() < 10 ? "0" : "");
u8g2Fonts.print(now.day());
u8g2Fonts.print(", ");
u8g2Fonts.print(monthName[now.month() - 1]);
u8g2Fonts.setCursor(10, 105 + offset);
u8g2Fonts.print(daysOfTheWeek[now.dayOfTheWeek()]);
// Main temperature display
u8g2Fonts.setFont(u8g2_font_inb19_mf);
u8g2Fonts.setCursor(320, 60 + offset);
u8g2Fonts.print("o");
u8g2Fonts.setFont(u8g2_font_logisoso58_tf);
u8g2Fonts.setCursor(150, 110 + offset);
u8g2Fonts.print(String(tempC));
u8g2Fonts.setCursor(330, 110 + offset);
u8g2Fonts.print("C");
// Draw separator lines
for (int i = 0; i < 2; i++)
{
display.fillRect(0, 121 + offset + (i * 33), 400, 2, lineColor);
}
// Environmental readings
if (!bme.beginReading() || !bme.endReading())
{
if (DEBUG_MODE)
Serial.println("BME READING ERROR");
return;
}
// Display environmental data
u8g2Fonts.setFont(u8g2_font_logisoso20_tf);
u8g2Fonts.setCursor(2, 150 + offset);
u8g2Fonts.print(bme.humidity);
u8g2Fonts.print("%");
u8g2Fonts.setCursor(264, 150 + offset);
u8g2Fonts.print(bme.pressure / 100.0);
u8g2Fonts.print("hPa");
// High/Low temperature display
u8g2Fonts.setFont(u8g2_font_logisoso16_tf);
const char *labels[] = {"H:", "L:"};
float temps[] = {hTemp, lTemp};
int positions[] = {85, 180};
for (int i = 0; i < 2; i++)
{
u8g2Fonts.setCursor(positions[i], 148 + offset);
u8g2Fonts.print(labels[i]);
u8g2Fonts.print(temps[i]);
u8g2Fonts.setFont(u8g2_font_fub11_tf);
u8g2Fonts.setCursor(positions[i] + 63, 138 + offset);
u8g2Fonts.print("o");
u8g2Fonts.setFont(u8g2_font_logisoso16_tf);
u8g2Fonts.setCursor(positions[i] + 73, 148 + offset);
u8g2Fonts.print("C");
}
}
/**
* @brief Fetches and displays weather data
* @param invert Inverts display colors for ghost protection
* @note Requires active WiFi connection and valid API keys
*/
void weatherPrint(bool invert)
{
uint16_t bg = invert ? GxEPD_BLACK : GxEPD_WHITE;
uint16_t fg = invert ? GxEPD_WHITE : GxEPD_BLACK;
uint16_t red = invert ? GxEPD_WHITE : GxEPD_RED;
char serverPath[256]; // Buffer for API URL
strcpy(serverPath, OPEN_WEATHER_BASE_URL);
strcat(serverPath, lat.c_str());
strcat(serverPath, "&lon=");
strcat(serverPath, lon.c_str());
strcat(serverPath, OPEN_WEATHER_PARAMS);
strcat(serverPath, openWeatherMapApiKey.c_str());
jsonBuffer = weatherDataAPI(serverPath);
if (httpResponseCode == -1 || httpResponseCode == -11)
ESP.restart();
if (DEBUG_MODE)
Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined")
{
if (DEBUG_MODE)
Serial.println("Parsing input failed!");
ESP.restart();
return;
}
// Override with custom weather URL
strcpy(serverPath, CUSTOM_WEATHER_BASE_URL);
strcat(serverPath, customApiKey.c_str());
jsonBuffer = weatherDataAPI(serverPath);
if (httpResponseCode == -1 || httpResponseCode == -11)
ESP.restart();
if (DEBUG_MODE)
Serial.println(jsonBuffer);
JSONVar customObject = JSON.parse(jsonBuffer);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(customObject) == "undefined")
{
if (DEBUG_MODE)
Serial.println("Parsing input failed!");
ESP.restart();
return;
}
if (myObject["current"]["temp"] == null)
{
networkInfo();
turnOffWifi(); // Turn off WiFi as soon as possible after data fetch
}
else
{
wifiStatus(invert);
turnOffWifi(); // Turn off WiFi as soon as possible after data fetch
u8g2Fonts.setFontMode(1);
u8g2Fonts.setFontDirection(0);
u8g2Fonts.setForegroundColor(fg);
u8g2Fonts.setBackgroundColor(bg);
u8g2Fonts.setFont(u8g2_font_helvB10_tf);
u8g2Fonts.setCursor(29, 170);
u8g2Fonts.print("OUTDOOR");
u8g2Fonts.setFont(u8g2_font_fub20_tf); // u8g2_font_fub30_tf
uint16_t width;
width = u8g2Fonts.getUTF8Width(JSON.stringify(customObject["data"]["temp"]).c_str());
u8g2Fonts.setCursor(20, 200); // start writing at this position
u8g2Fonts.print(customObject["data"]["temp"]);
u8g2Fonts.setCursor(30 + width, 200);
u8g2Fonts.print("C");
u8g2Fonts.setFont(u8g2_font_fub11_tf);
u8g2Fonts.setCursor(22 + width, 185); // start writing at this position
u8g2Fonts.print("o");
u8g2Fonts.setFont(u8g2_font_fur11_tf); // u8g2_font_fur14_tf
width = u8g2Fonts.getUTF8Width(("Real Feel:" + JSON.stringify(myObject["current"]["feels_like"])).c_str());
u8g2Fonts.setCursor(5, 220); // start writing at this position
u8g2Fonts.print("Real Feel:");
u8g2Fonts.setCursor(75, 220);
u8g2Fonts.print(myObject["current"]["feels_like"]);
u8g2Fonts.setCursor(width + 16, 220);
u8g2Fonts.print(String("C"));
u8g2Fonts.setFont(u8g2_font_baby_tf); // u8g2_font_robot_de_niro_tf
u8g2Fonts.setCursor(13 + width, 211); // start writing at this position
u8g2Fonts.print("o");
u8g2Fonts.setFont(u8g2_font_fur14_tf);
u8g2Fonts.setCursor(5, 245); // start writing at this position
u8g2Fonts.print(customObject["data"]["humidity"]);
u8g2Fonts.print(String("%"));
u8g2Fonts.setCursor(5, 270); // start writing at this position
u8g2Fonts.print(customObject["data"]["pressure"]);
u8g2Fonts.print(String("hPa"));
u8g2Fonts.setFont(u8g2_font_helvB10_tf);
u8g2Fonts.setCursor(5, 294); // start writing at this position
u8g2Fonts.print("UVI: ");
u8g2Fonts.print(myObject["current"]["uvi"]);
u8g2Fonts.setFont(u8g2_font_fur11_tf);