added time drift check command

This commit is contained in:
Emanuele Trabattoni
2025-07-30 10:15:13 +02:00
parent 1d1eb6fbfa
commit 581eca124e
8 changed files with 137 additions and 41 deletions

View File

@@ -48,7 +48,7 @@ void loop()
LOG_INFO("Temperature sensors connected ->", sensors);
// Create device structure to pass all devices in the callbacks as needed
devices_t devices(rtc, tmp, seneca, buzzer, led, io);
devices_t devices(eth, rtc, tmp, seneca, buzzer, led, io);
//////////////// DEVICES ////////////////
//////////////// MQTT ////////////////
@@ -72,11 +72,11 @@ void loop()
return;
}
const std::string cmd = doc["cmd"].as<std::string>();
ArduinoJson::JsonDocument params = doc["params"];
const ArduinoJson::JsonDocument params = doc["params"];
if (commands::commandMap.contains(cmd))
{ // call command from command map in this same thread (the MQTT thread)
LOG_INFO("Executing command", cmd.c_str());
auto answer = std::move(commands::commandMap.at(cmd)(devices, params)); // here the magic happens
const auto answer = std::move(commands::commandMap.at(cmd)(devices, params)); // here the magic happens
if (answer.isNull())
return;
mqtt.publish(conf.m_mqttPublish["answers"], answer);
@@ -104,12 +104,13 @@ void loop()
uint8_t mqttRetries(0);
while (timeRetries++ < conf.m_ntpRetries)
{
if (eth.getNtpTime(ntpTime) && rtc.setDatetime(drivers::PCF85063::fromEpoch(ntpTime)))
{
if (eth.getNtpTime(ntpTime))
{ // skip NTP update for drift testing
buzzer.beep(250, NOTE_A);
led.setColor(led.COLOR_ORANGE);
// rtc.setDatetime(drivers::PCF85063::fromEpoch(ntpTime));
const drivers::PCF85063::datetime_t dt(drivers::PCF85063::fromEpoch(ntpTime));
LOG_INFO("NTP Time Update: ", drivers::PCF85063::datetime2str(dt).c_str());
LOG_INFO("NTP Time: ", drivers::PCF85063::datetime2str(dt).c_str());
break;
}
delay(100);
@@ -118,6 +119,7 @@ void loop()
{
if (mqtt.connect())
{
buzzer.beep(250, NOTE_B);
led.setColor(led.COLOR_GREEN);
mqtt.subscribe(conf.m_mqttSubscribe["commands"], commandsCallback);
break;
@@ -133,8 +135,10 @@ void loop()
while (true)
{
const uint32_t start(millis());
const std::string timeStr(rtc.getTimeStr());
LOG_INFO("[", k++, "] Loop - Current Datetime", timeStr.c_str());
drivers::PCF85063::datetime_t datetime;
rtc.readDatetime(datetime);
const std::string timeStr(drivers::PCF85063::datetime2str(datetime));
LOG_INFO("[", k++, "] Loop - Current Datetime UTC", timeStr.c_str());
{
ArduinoJson::JsonDocument poll;