ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ds3231_time.cpp
Go to the documentation of this file.
1#include "ds3231_time.h"
2#include "esphome/core/log.h"
3
4namespace esphome::ds3231 {
5
6ESPHOME_LOG_TAG(TAG, "ds3231.time");
7
8void DS3231Time::update() { this->read_time(); }
9
11 ESP_LOGCONFIG(TAG, "DS3231 Time:");
12 RealTimeClock::dump_config();
13}
14
17 if (!this->status_comm_result(this->parent_->read_calendar(raw), LOG_STR("No valid time from the RTC")))
18 return;
19 const ESPTime rtc_time = time::decode_bcd_calendar<time::DS_BCD_CALENDAR>(raw);
20 ESP_LOGD(TAG, "Read UTC time: %04u-%02u-%02u %02u:%02u:%02u (day of week %u)", rtc_time.year, rtc_time.month,
21 rtc_time.day_of_month, rtc_time.hour, rtc_time.minute, rtc_time.second, rtc_time.day_of_week);
22 // The day of week is a free-running counter on the DS3231 and may have been set with a different
23 // numbering by other software, so it is not used to validate the time.
24 if (!this->status_comm_result(rtc_time.is_valid(/*check_day_of_week=*/false, /*check_day_of_year=*/false),
25 LOG_STR("Invalid RTC time")))
26 return;
27 this->synchronize_epoch_(rtc_time.timestamp);
28}
29
31 auto now = this->utcnow();
32 if (!now.is_valid()) {
33 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
34 return;
35 }
36 if (now.year > 2199) {
37 ESP_LOGE(TAG, "Year %u is out of the DS3231 range (2000-2199).", now.year);
38 return;
39 }
41 time::encode_bcd_calendar<time::DS_BCD_CALENDAR>(now, raw);
42 if (!this->status_comm_result(this->parent_->write_calendar(raw)))
43 return;
44 ESP_LOGD(TAG, "Wrote UTC time: %04u-%02u-%02u %02u:%02u:%02u", now.year, now.month, now.day_of_month, now.hour,
46}
47
48} // namespace esphome::ds3231
uint8_t raw[35]
Definition bl0939.h:0
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
static constexpr size_t CALENDAR_LEN
Seconds through year, the 7-byte BCD calendar the time platform decodes with time::DS_BCD_CALENDAR.
Definition ds3231.h:38
void write_time()
Store the system clock in the RTC.
void read_time()
Set the system clock from the RTC.
ESPTime now()
Get the time in the currently defined timezone.
ESPTime utcnow()
Get the time without any time zone or DST corrections.
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
ESPHOME_LOG_TAG(TAG, "ds3231")
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:48
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
Definition time.h:82
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:36