diff --git a/include/config.h b/include/config.h index 041ce3a..c0315a3 100644 --- a/include/config.h +++ b/include/config.h @@ -56,7 +56,7 @@ public: file.close(); // close config file before unmounting filesystem }; - ArduinoJson::JsonDocument& getConfig() + ArduinoJson::JsonDocument &getConfig() { std::lock_guard lock(m_mutex); serialize(); @@ -152,6 +152,7 @@ private: ntp["timezone"] = m_ntpTimezone; ntp["updateInterval"] = m_ntpUpdateInterval; ntp["retries"] = m_ntpRetries; + ntp["ntpRtcOffsetRegister"] = m_ntpRtcOffsetRegister; }; { @@ -220,9 +221,10 @@ private: { auto ntp = m_configJson["ntp"]; m_ntpPool = ntp["pool"].as(); - m_ntpTimezone = ntp["timezone"].as(); + m_ntpTimezone = ntp["timezone"].as(); m_ntpUpdateInterval = ntp["updateInterval"].as(); m_ntpRetries = ntp["retries"].as(); + m_ntpRtcOffsetRegister = ntp["ntpRtcOffsetRegister"].as(); }; { @@ -272,9 +274,10 @@ public: // NTP std::string m_ntpPool = "pool.ntp.org"; - uint16_t m_ntpTimezone = 3600; // GTM +1 + int8_t m_ntpTimezone = +1; // GMT +1 uint16_t m_ntpUpdateInterval = 3600; // every hour uint8_t m_ntpRetries = 5; + uint8_t m_ntpRtcOffsetRegister = 0xE7; // -25 pulses in fast mode // MQTT std::string m_mqttHost = "10.0.2.249"; diff --git a/lib/ETH/ETH_Driver.cpp b/lib/ETH/ETH_Driver.cpp index 3d8ba96..5da1704 100644 --- a/lib/ETH/ETH_Driver.cpp +++ b/lib/ETH/ETH_Driver.cpp @@ -3,12 +3,12 @@ namespace drivers { - Ethernet::Ethernet(const std::string hostname) : m_hostname(hostname), m_connected(false), m_localIP(IPAddress()), m_udp(NetworkUDP()), m_timeClient(m_udp) + Ethernet::Ethernet(const std::string &hostname, const std::string &ntpPool, const int8_t tz, const uint16_t updateInterval) : m_hostname(hostname), m_ntpPool(ntpPool), m_connected(false), m_localIP(IPAddress()), m_udp(NetworkUDP()), m_timeClient(m_udp) { SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI); ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, SPI); - m_timeClient = std::move(NTPClient(m_udp, "pool.ntp.org", 0, 3600)); // NTP server, time offset in seconds, update interval + m_timeClient = std::move(NTPClient(m_udp, m_ntpPool.c_str(), tz * 3600, updateInterval)); // NTP server, time offset in seconds, update interval m_timeClient.begin(); } @@ -30,6 +30,17 @@ namespace drivers return false; } + const bool Ethernet::setNtpTimeOffset(const int8_t tz) + { + if (m_connected) + { + m_timeClient.setTimeOffset(tz * 3600); + LOG_DEBUG("Time zone UTC ", tz); + return true; + } + return false; + } + const bool Ethernet::isConnected() { return m_connected; diff --git a/lib/ETH/ETH_Driver.h b/lib/ETH/ETH_Driver.h index 514d56c..5dee2e4 100644 --- a/lib/ETH/ETH_Driver.h +++ b/lib/ETH/ETH_Driver.h @@ -30,15 +30,17 @@ namespace drivers { public: - Ethernet(const std::string hostname); + Ethernet(const std::string &hostname, const std::string &ntpPool, const int8_t tz, const uint16_t updateInterval); ~Ethernet(); void onEvent(arduino_event_id_t event, arduino_event_info_t info); const bool isConnected(); const bool getNtpTime(time_t &time); + const bool setNtpTimeOffset(const int8_t tz); private: const std::string m_hostname; + const std::string m_ntpPool; bool m_connected; NetworkUDP m_udp; IPAddress m_localIP; diff --git a/lib/RTC/PCF85063_Driver.cpp b/lib/RTC/PCF85063_Driver.cpp index 9d2b9d7..dc4fbde 100644 --- a/lib/RTC/PCF85063_Driver.cpp +++ b/lib/RTC/PCF85063_Driver.cpp @@ -18,9 +18,6 @@ namespace drivers const uint8_t def_conf2 = RTC_CTRL_2_DEFAULT | RTC_CTRL_2_MI; // enable 1 minute interrupt success &= m_i2c.write(m_address, RTC_CTRL_2_ADDR, {def_conf2}); } - // set clock correction pulses - const uint8_t correction = 0xE7; // fast mode -25 correction pulses, because clock is beyond - success &= m_i2c.write(m_address, RTC_OFFSET_ADDR, {correction}); if (!success) LOG_ERROR("RTC Init Failure"); } @@ -163,18 +160,19 @@ namespace drivers const bool PCF85063::setOffset(const uint8_t ofst) { - LOG_INFO("RTC set offset [", printHex(ofst).c_str(), "]"); + LOG_DEBUG("RTC set offset [", printHex(ofst).c_str(), "]"); return m_i2c.write(m_address, RTC_OFFSET_ADDR, {ofst}); } - + const uint8_t PCF85063::getOffset() { std::vector buf; - if (m_i2c.read(m_address, RTC_OFFSET_ADDR, 1, buf)) { - LOG_INFO("RTC get offset [", printHex(buf.front()).c_str(), "]"); - return buf.front(); + if (m_i2c.read(m_address, RTC_OFFSET_ADDR, 1, buf)) + { + LOG_DEBUG("RTC get offset [", printHex(buf.front()).c_str(), "]"); + return buf.front(); } - return {}; + return UINT8_MAX; } const std::string PCF85063::getTimeStr() diff --git a/src/commands.cpp b/src/commands.cpp index 76a82cd..13c9337 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -108,7 +108,6 @@ namespace commands response["cmd"] = "getCronJob"; auto &cron = Cron::getInstance(dev); auto eventName = params["name"].as(); - response["values"]["name"] = eventName; if (eventName.empty()) { @@ -132,6 +131,7 @@ namespace commands } Cron::CronEvent event; + response["values"]["name"] = eventName; if (!cron.getEvent(eventName, event)) { LOG_ERROR("getCronJob failed to get job [", eventName.c_str(), "]"); diff --git a/src/main.cpp b/src/main.cpp index 7797318..694ebb4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,13 +35,16 @@ void loop() auto i2c = drivers::I2C(); auto bus = drivers::MODBUS(9600, SERIAL_8N1); auto rtc = drivers::PCF85063(i2c); - auto eth = drivers::Ethernet(conf.m_ethHostname); + auto eth = drivers::Ethernet(conf.m_ethHostname, conf.m_ntpPool, conf.m_ntpTimezone, conf.m_ntpUpdateInterval); auto tmp = drivers::R4DCB08(bus, conf.m_modbusTemperatureAddr); auto seneca = drivers::S50140(bus, conf.m_modbusSenecaAddr); auto buzzer = drivers::Buzzer(); auto led = drivers::Led(); delay(500); auto io = digitalIO(i2c, bus, {conf.m_modbusRelayAddr}); + // get RTC time drift offset value + rtc.setOffset(conf.m_ntpRtcOffsetRegister); + LOG_INFO("RTC offset register -> ", printHex(rtc.getOffset()).c_str()); // Initialize temperature sensors sensors = tmp.getNum(); tmp.setCorrection(conf.m_tempCorrectionValues); @@ -83,13 +86,13 @@ void loop() MQTTwrapper::MessageCallback onMessage = [&devices](const MQTTwrapper::Topic &topic, const MQTTwrapper::Message &message) { - LOG_INFO("onMessage callback [", topic.c_str(), "]"); + LOG_DEBUG("onMessage callback [", topic.c_str(), "]"); devices.led.setColor(devices.led.COLOR_MAGENTA); }; - + MQTTwrapper::MessageCallback onPublish = [&devices](const MQTTwrapper::Topic &topic, const MQTTwrapper::Message &message) { - LOG_INFO("onPublish callback [", topic.c_str(), "]"); + LOG_DEBUG("onPublish callback [", topic.c_str(), "]"); devices.led.setColor(devices.led.COLOR_SKYBLUE); }; @@ -126,6 +129,8 @@ void loop() uint8_t mqttRetries(0); while (timeRetries++ < conf.m_ntpRetries) { + eth.setNtpTimeOffset(conf.m_ntpTimezone); + LOG_INFO("NTP Timezone UTC", conf.m_ntpTimezone >= 0 ? "+" : "", conf.m_ntpTimezone); if (eth.getNtpTime(ntpTime)) { // skip NTP update for drift testing buzzer.beep(250, NOTE_A);