-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepdNtpClockV1.ino
466 lines (401 loc) · 13.8 KB
/
epdNtpClockV1.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
/*
epdNtpClockV1.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/>.
*/
#include <SPI.h>
#include "src/EPD_3in52.h"
#include "src/imagedata.h"
#include "src/epdpaint.h"
#include <Wire.h>
#include "RTClib.h"
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Preferences.h>
#include <BH1750.h>
// powersave wifi off
#include "esp_wifi.h"
Preferences pref; // preference library object
RTC_DS3231 rtc; // ds3231 object
BH1750 lightMeter(0x23); // Initalize light sensor
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
int TIME_TO_SLEEP = 60; // in seconds
#define BATPIN A0 // battery voltage divider connection pin (1M Ohm with 104 Capacitor)
#define BATTERY_LEVEL_SAMPLING 4 // number of times to average the reading
// System configuration constants
#define WIFI_CONNECT_TIMEOUT 10000 // Timeout for WiFi connection attempts (ms)
#define CPU_FREQ_NORMAL 80 // CPU frequency during active operations (MHz)
#define CPU_FREQ_SLEEP 20 // CPU frequency during sleep mode (MHz)
#define BATTERY_LEVEL_SAMPLING 4 // Number of ADC samples for battery voltage averaging
// Battery monitoring thresholds (Volts)
#define battChangeThreshold 0.15 // Minimum voltage change to update reading
#define battUpperLim 3.3 // Upper limit for normal battery operation
#define battHigh 3.4 // Full battery threshold
#define battLow 2.9 // Low battery threshold
const char *ssid = "SonyBraviaX400";
const char *password = "79756622761";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 19800); // 19800 is offset of India, asia.pool.ntp.org is close to India
#define COLORED 0
#define UNCOLORED 1
UBYTE image[68000];
Epd epd;
bool errFlag = false;
/**
* @brief Measures battery voltage with averaging
* @return float Actual battery voltage in volts
* @note Uses multiple samples to reduce noise
*/
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
// Serial.println(Vbattf);
return (Vbattf);
}
#define WIFI_CONNECT_TIMEOUT 10000 // 10 seconds timeout
#define CPU_FREQ_NORMAL 80
#define CPU_FREQ_SLEEP 20
static const char daysOfTheWeek[7][10] PROGMEM = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
/**
* @brief Enables WiFi with power-optimized settings
* @note Includes timeout and CPU frequency management
*/
void enableWiFi()
{
setCpuFrequencyMhz(CPU_FREQ_NORMAL);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED &&
millis() - startAttemptTime < WIFI_CONNECT_TIMEOUT)
{
delay(100);
}
if (WiFi.status() != WL_CONNECTED)
{
disableWiFi();
}
}
/**
* @brief Disables WiFi and other wireless interfaces to save power
* @note Also configures CPU for low-power operation
*/
void disableWiFi()
{
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
esp_wifi_stop();
btStop();
setCpuFrequencyMhz(CPU_FREQ_SLEEP);
}
/**
* @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)
{
enableWiFi();
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);
Serial.println("RTC updated: " + String(year) + "-" +
String(month) + "-" + String(day));
return true;
}
}
}
return false;
}
/**
* @brief Pads single digit numbers with leading zero
* @param num Number to pad
* @return String Padded number as string
*/
String padNum(int num)
{
return (num < 10 ? "0" : "") + String(num);
}
/**
* @brief Main setup function - runs once at startup/wake
* @note Device enters deep sleep after completing operations
*/
void setup()
{
disableWiFi(); // Initialize peripherals with power-optimized settings
Serial.begin(115200);
Wire.begin();
analogReadResolution(12);
Serial.println(getCpuFrequencyMhz());
pinMode(BATPIN, INPUT);
pref.begin("database", false);
if (!rtc.begin())
{
Serial.println("Couldn't find RTC");
// showMsg("RTC Error");
errFlag = true;
}
if (lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE))
{
Serial.println(F("BH1750 Advanced begin"));
}
else
{
Serial.println(F("Error initialising BH1750"));
errFlag = true;
}
float lux = 0;
while (!lightMeter.measurementReady(true))
{
yield();
}
lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
if (!pref.isKey("nightFlag"))
{ // create key:value pair
pref.putBool("nightFlag", false);
}
bool nightFlag = pref.getBool("nightFlag", false); // remembers last state of clock i.e. sleeping or not
bool tempNightFlag = nightFlag;
if (lux == 0)
{
if (!nightFlag)
{ // prevents unnecessary redrawing of same thing
nightFlag = true;
if (epd.Init() != 0)
{
Serial.println("e-Paper init failed");
return;
}
epd.Clear();
if (errFlag)
showMsg("Error");
else
showMsg("SLEEPING");
}
}
else
{
nightFlag = false;
float battLevel;
if (!pref.isKey("battLevel")) // create key:value pairs
pref.putFloat("battLevel", 0.0);
battLevel = pref.getFloat("battLevel", 0.0);
float tempBattLevel = battLevel;
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()) // if RTC lost power or not set
timeNeedsUpdate = true;
// Get the current day
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)
{
Serial.println("Updating time from NTP server");
if (autoTimeUpdate(timeNeedsUpdate)) // Update time from NTP server
Serial.println("Time updated");
else
Serial.println("Time Not updated");
disableWiFi(); // Turn off WiFi to save power
timeNeedsUpdate = false;
pref.putBool("timeNeedsUpdate", false);
lastCheckedDay = currentDay; // Update last checked day
pref.putUChar("lastCheckedDay", lastCheckedDay);
}
else
Serial.println("Time already updated");
// Battery level handling
float newBattLevel = batteryLevel();
battLevel = (newBattLevel < battLevel) ? newBattLevel : ((newBattLevel - battLevel) >= battChangeThreshold || newBattLevel > battUpperLim) ? newBattLevel
: battLevel;
int percent = constrain(((battLevel - battLow) / (battHigh - battLow)) * 100, 0, 100);
if (tempBattLevel != battLevel)
pref.putFloat("battLevel", battLevel);
String percentStr;
if (battLevel >= 4.0)
percentStr = "USB";
else
percentStr = String(percent) + "%";
// battery level end
bool timeFlag;
byte tempHour = now.twelveHour();
byte temp = int(rtc.getTemperature());
String dateString = padNum(now.day()) + "/" + padNum(now.month()) + "/" + String(now.year());
String timeString = padNum(tempHour) + ":" + padNum(now.minute()) + (now.isPM() ? " PM" : " AM");
if (epd.Init() != 0)
{
Serial.println("e-Paper init failed");
return;
}
epd.Clear();
Serial.println("e-Paper Clear");
if (timeNeedsUpdate)
showMsg("Time Resync Await");
else if (errFlag)
showMsg("Error");
else
showTime(daysOfTheWeek[now.dayOfTheWeek()], timeString, dateString, String(battLevel) + "V", percentStr, percent /*for battery icon*/, String(temp) + "C");
}
if (tempNightFlag != nightFlag)
pref.putBool("nightFlag", nightFlag);
pref.end(); // Close the preferences
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP / 60) + " Mins");
// Go to sleep now
Serial.println("Going to sleep now");
Serial.flush();
delay(5);
esp_deep_sleep_start();
}
void loop()
{
// This will never run
}
// various error msg display function
/**
* @brief Displays error or status messages
* @param msg Message to display
* @note Optimized for minimum display updates
*/
void showMsg(String msg)
{
epd.display_NUM(EPD_3IN52_WHITE);
epd.lut_GC();
epd.refresh();
epd.SendCommand(0x50);
epd.SendData(0x17);
delay(100);
Paint paint(image, 240, 360); // width should be the multiple of 8
paint.SetRotate(3); // Top right (0,0)
paint.Clear(COLORED);
paint.DrawStringAt(10, 100, msg.c_str(), &Font48, UNCOLORED);
epd.display_part(paint.GetImage(), 0, 0, paint.GetWidth(), paint.GetHeight());
epd.lut_GC();
epd.refresh();
Serial.println("sleep......");
delay(100);
epd.sleep();
Serial.println("end");
}
// Displays time, battery info. First para is week in const char, then time in hh:mm am/pm, then date in dd/mm/yyyy, then battlevel in X.YZV, percent in XY%
/**
* @brief Updates display with time and status information
* @param w Weekday string (const)
* @param timeString Formatted time string
* @param dateString Formatted date string
* @param battLevel Battery voltage string
* @param percentStr Battery percentage string
* @param percent Battery percentage value for icon
* @param temp Temperature string
* @note Implements power-efficient display update strategy
*/
void showTime(const char *w, String timeString, String dateString,
String battLevel, String percentStr, byte percent, String temp)
{
epd.display_NUM(EPD_3IN52_WHITE);
epd.lut_GC();
epd.refresh();
epd.SendCommand(0x50);
epd.SendData(0x17);
delay(100);
Paint paint(image, 240, 360); // width should be the multiple of 8
paint.SetRotate(3); // Top right (0,0)
paint.Clear(UNCOLORED);
// Battery icon outline
paint.DrawRectangle(10, 4, 26, 12, COLORED);
paint.DrawRectangle(8, 6, 10, 10, COLORED);
// Battery fill level
byte fillX = 25; // Default to empty (rightmost position)
if (percent > 0)
{
if (percent >= 95)
fillX = 11; // Full
else if (percent >= 85)
fillX = 13; // Full-Med
else if (percent >= 70)
fillX = 15; // Med
else if (percent > 50)
fillX = 17; // Med-half
else if (percent > 30)
fillX = 19; // Half
else if (percent > 10)
fillX = 21; // Low-half
else if (percent > 5)
fillX = 23; // Low
else if (percent > 2)
fillX = 25; // Critical
paint.DrawFilledRectangle(fillX, 4, 25, 11, COLORED);
}
paint.DrawStringAt(325, 5, percentStr.c_str(), &Font12, COLORED);
paint.DrawStringAt(280, 5, battLevel.c_str(), &Font12, COLORED);
paint.DrawStringAt(60, 30, w, &Font48, COLORED);
paint.DrawStringAt(295, 50, temp.c_str(), &Font24, COLORED);
paint.DrawStringAt(327, 47, "o", &Font8, COLORED);
paint.DrawStringAt(60, 100, timeString.c_str(), &Font48, COLORED);
paint.DrawStringAt(60, 170, dateString.c_str(), &Font48, COLORED);
epd.display_part(paint.GetImage(), 0, 0, paint.GetWidth(), paint.GetHeight());
epd.lut_GC();
epd.refresh();
Serial.println("sleep......");
delay(100);
epd.sleep();
Serial.println("end");
}