From b5a611a270fffef98d796b213fd7fec86c952ea6 Mon Sep 17 00:00:00 2001 From: masterx1981 <76602320+masterx1981@users.noreply.github.com> Date: Mon, 28 Dec 2020 20:32:49 +0100 Subject: [PATCH 1/2] Added setPoolServer IP --- NTPClient.cpp | 7 ++++++- NTPClient.h | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 760e142..82207ba 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -174,7 +174,12 @@ void NTPClient::setUpdateInterval(unsigned long updateInterval) { } void NTPClient::setPoolServerName(const char* poolServerName) { - this->_poolServerName = poolServerName; + this->_poolServerName = poolServerName; +} + +void NTPClient::setPoolServerIP(IPAddress poolServerIP) { + this->_poolServerName = NULL; + this->_poolServerIP = poolServerIP; } void NTPClient::sendNTPPacket() { diff --git a/NTPClient.h b/NTPClient.h index b518c28..bc325d3 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -44,6 +44,13 @@ class NTPClient { */ void setPoolServerName(const char* poolServerName); + /** + * Set time server IP + * + * @param poolServerIP + */ + void setPoolServerIP(IPAddress poolServerIP); + /** * Set random local port */ From be86f1b324e30ce97b85384648cddd6d13120ee9 Mon Sep 17 00:00:00 2001 From: masterx1981 <76602320+masterx1981@users.noreply.github.com> Date: Wed, 30 Dec 2020 11:09:08 +0100 Subject: [PATCH 2/2] update() return last sync state As the update() function return false both if the forceUpdate fail, and also if the timeout isn't expired, we are not able to know if the last ntp update was fine. Wouldn't be better to return true if the last sync was gone ok, and false if wasn't good? --- NTPClient.cpp | 8 ++++++-- NTPClient.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 82207ba..58bc539 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -98,11 +98,15 @@ bool NTPClient::forceUpdate() { do { delay ( 10 ); cb = this->_udp->parsePacket(); - if (timeout > 100) return false; // timeout after 1000 ms + if (timeout > 100) { + this->_lastUpdateOk = false; + return false; // timeout after 1000 ms + } timeout++; } while (cb == 0); this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time + this->_lastUpdateOk = true; this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); @@ -123,7 +127,7 @@ bool NTPClient::update() { if (!this->_udpSetup || this->_port != NTP_DEFAULT_LOCAL_PORT) this->begin(this->_port); // setup the UDP client if needed return this->forceUpdate(); } - return false; // return false if update does not occur + return this->_lastUpdateOk; // return last sync state } unsigned long NTPClient::getEpochTime() const { diff --git a/NTPClient.h b/NTPClient.h index bc325d3..8e8a591 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -22,6 +22,7 @@ class NTPClient { unsigned long _currentEpoc = 0; // In s unsigned long _lastUpdate = 0; // In ms + bool _lastUpdateOk = false; // byte _packetBuffer[NTP_PACKET_SIZE];