|
| 1 | +#include <Arduino.h> |
| 2 | +#include "settings.h" |
| 3 | + |
| 4 | +#include "Rtc.h" |
| 5 | + |
| 6 | +#include "Log.h" |
| 7 | +#include "Mqtt.h" |
| 8 | +#include "Wlan.h" |
| 9 | + |
| 10 | +#ifdef RTC_ENABLE |
| 11 | + #include <RTClib.h> |
| 12 | + #include <Wire.h> |
| 13 | + |
| 14 | + #include <time.h> |
| 15 | + |
| 16 | +extern TwoWire i2cBusTwo; |
| 17 | + |
| 18 | +static RTC_DS3231 rtc; |
| 19 | +static bool rtcAvailable = false; |
| 20 | + |
| 21 | +// How often the (NTP-disciplined) system time is written back to the RTC |
| 22 | +static constexpr uint32_t rtcResyncInterval = 3600UL * 1000UL; // 1h |
| 23 | +// How often the current time is published via MQTT |
| 24 | +static constexpr uint32_t rtcPublishInterval = 60UL * 1000UL; // 1min |
| 25 | + |
| 26 | +static uint32_t lastResyncTimestamp = 0; |
| 27 | +static uint32_t lastPublishTimestamp = 0; |
| 28 | + |
| 29 | +// Returns true if the system clock holds a plausible (NTP-set) time |
| 30 | +static bool systemTimeIsValid(struct tm *timeinfo) { |
| 31 | + if (!getLocalTime(timeinfo, 5)) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + return (timeinfo->tm_year + 1900) >= 2024; |
| 35 | +} |
| 36 | + |
| 37 | +void Rtc_Init(void) { |
| 38 | + if (!rtc.begin(&i2cBusTwo)) { |
| 39 | + Log_Println("RTC> DS3231 not found on i2cBusTwo", LOGLEVEL_NOTICE); |
| 40 | + return; |
| 41 | + } |
| 42 | + rtcAvailable = true; |
| 43 | + |
| 44 | + if (rtc.lostPower()) { |
| 45 | + Log_Println("RTC> DS3231 lost power - waiting for NTP to set the time", LOGLEVEL_NOTICE); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // RTC stores UTC; seed the system clock from it so the time is correct |
| 50 | + // immediately, even before WiFi/NTP are up. |
| 51 | + const DateTime now = rtc.now(); |
| 52 | + if (now.year() < 2024) { |
| 53 | + Log_Println("RTC> stored time implausible - waiting for NTP", LOGLEVEL_NOTICE); |
| 54 | + return; |
| 55 | + } |
| 56 | + struct timeval tv = {.tv_sec = (time_t) now.unixtime(), .tv_usec = 0}; |
| 57 | + settimeofday(&tv, nullptr); |
| 58 | + Log_Printf(LOGLEVEL_NOTICE, "RTC> system time set from DS3231: %04d-%02d-%02d %02d:%02d:%02d UTC", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second()); |
| 59 | +} |
| 60 | + |
| 61 | +void Rtc_SetFromSystemTime(void) { |
| 62 | + if (!rtcAvailable) { |
| 63 | + return; |
| 64 | + } |
| 65 | + struct tm timeinfo; |
| 66 | + if (!systemTimeIsValid(&timeinfo)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + // store UTC in the RTC (DateTime(uint32_t) interprets the value as unix-time) |
| 70 | + rtc.adjust(DateTime((uint32_t) time(nullptr))); |
| 71 | + Log_Println("RTC> DS3231 disciplined from system time", LOGLEVEL_NOTICE); |
| 72 | +} |
| 73 | + |
| 74 | +bool Rtc_IsAvailable(void) { |
| 75 | + return rtcAvailable; |
| 76 | +} |
| 77 | + |
| 78 | +bool Rtc_LostPower(void) { |
| 79 | + return rtcAvailable && rtc.lostPower(); |
| 80 | +} |
| 81 | + |
| 82 | +float Rtc_GetTemperature(void) { |
| 83 | + if (!rtcAvailable) { |
| 84 | + return NAN; |
| 85 | + } |
| 86 | + return rtc.getTemperature(); |
| 87 | +} |
| 88 | + |
| 89 | +void Rtc_Cyclic(void) { |
| 90 | + if (!rtcAvailable) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const uint32_t millisNow = millis(); |
| 95 | + |
| 96 | + // Periodically write the (NTP-disciplined) system time back to the RTC to |
| 97 | + // compensate for drift. Only when WiFi is up so we trust the system clock. |
| 98 | + if (millisNow - lastResyncTimestamp >= rtcResyncInterval) { |
| 99 | + lastResyncTimestamp = millisNow; |
| 100 | + if (Wlan_IsConnected()) { |
| 101 | + Rtc_SetFromSystemTime(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Publish the current time via MQTT |
| 106 | + #ifdef MQTT_ENABLE |
| 107 | + if (millisNow - lastPublishTimestamp >= rtcPublishInterval) { |
| 108 | + lastPublishTimestamp = millisNow; |
| 109 | + struct tm timeinfo; |
| 110 | + if (systemTimeIsValid(&timeinfo)) { |
| 111 | + char timeStringBuff[32]; |
| 112 | + strftime(timeStringBuff, sizeof(timeStringBuff), "%Y-%m-%d %H:%M:%S", &timeinfo); |
| 113 | + publishMqtt(topicRtc, timeStringBuff, false); |
| 114 | + } |
| 115 | + } |
| 116 | + #endif |
| 117 | +} |
| 118 | + |
| 119 | +#else // RTC_ENABLE not set: provide empty stubs |
| 120 | + |
| 121 | +void Rtc_Init(void) { |
| 122 | +} |
| 123 | +void Rtc_Cyclic(void) { |
| 124 | +} |
| 125 | +void Rtc_SetFromSystemTime(void) { |
| 126 | +} |
| 127 | + |
| 128 | +bool Rtc_IsAvailable(void) { |
| 129 | + return false; |
| 130 | +} |
| 131 | + |
| 132 | +bool Rtc_LostPower(void) { |
| 133 | + return false; |
| 134 | +} |
| 135 | + |
| 136 | +float Rtc_GetTemperature(void) { |
| 137 | + return NAN; |
| 138 | +} |
| 139 | + |
| 140 | +#endif |
0 commit comments