set ntp parameters via config file

This commit is contained in:
Emanuele Trabattoni
2025-08-03 12:11:00 +02:00
parent a1a66ebf8e
commit 4a1e944ea2
6 changed files with 39 additions and 20 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<uint8_t> 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()