feat: DS3231 RTC read/set with lost-power detection

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-17 19:55:47 +07:00
co-authored by Claude Opus 4.8
parent d042a94378
commit 2d52fcd851
3 changed files with 43 additions and 3 deletions
+10 -3
View File
@@ -3,6 +3,7 @@
#include "config.h"
#include "display_ui.h"
#include "sensors.h"
#include "rtc_time.h"
static void i2cScan() {
Serial.println(F("I2C scan:"));
@@ -28,12 +29,18 @@ void setup() {
displaySplash("Weather", "Predictor v0.1");
if (!sensorsBegin()) Serial.println(F("BMP180 not found!"));
if (!rtcBegin()) Serial.println(F("DS3231 not found!"));
if (rtcLostPower()) {
Serial.println(F("RTC lost power -> setting to build time"));
rtcSet(RtcTime{2026, 7, 17, 12, 0, 0}); // temporary; NTP will correct later
}
}
void loop() {
RtcTime t = rtcNow();
float abs_ = readAbsPressureHpa();
float msl = toSeaLevelHpa(abs_, DEFAULT_ALTITUDE_M);
Serial.printf("abs=%.1f hPa msl=%.1f hPa t=%.1f C\n",
abs_, msl, readTemperatureC());
Serial.printf("%04u-%02u-%02u %02u:%02u:%02u abs=%.1f msl=%.1f t=%.1f\n",
t.year, t.month, t.day, t.hour, t.minute, t.second,
abs_, toSeaLevelHpa(abs_, DEFAULT_ALTITUDE_M), readTemperatureC());
delay(2000);
}