From a9803d784c218acce59e640ec94f8f0bb4c035e4 Mon Sep 17 00:00:00 2001 From: Dmitriy Logachov Date: Tue, 3 Dec 2024 20:45:15 +0500 Subject: [PATCH 1/2] Added several methods useful for dynamically changing the NTP server. --- NTPClient.cpp | 21 +++++++++++++++++++++ NTPClient.h | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) mode change 100755 => 100644 NTPClient.cpp mode change 100755 => 100644 NTPClient.h diff --git a/NTPClient.cpp b/NTPClient.cpp old mode 100755 new mode 100644 index b435855..b17b960 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -173,14 +173,35 @@ void NTPClient::setTimeOffset(int timeOffset) { this->_timeOffset = timeOffset; } +long NTPClient::getTimeOffset() { + return this->_timeOffset; +} + void NTPClient::setUpdateInterval(unsigned long updateInterval) { this->_updateInterval = updateInterval; } +unsigned long NTPClient::getUpdateInterval() { + return this->_updateInterval; +} + void NTPClient::setPoolServerName(const char* poolServerName) { this->_poolServerName = poolServerName; } +const char *NTPClient::getPoolServerName() { + return this->_poolServerName; +} + +void NTPClient::setPoolServerIP(IPAddress poolServerIP) { + this->_poolServerIP = poolServerIP; + this->_poolServerName = NULL; +} + +IPAddress NTPClient::getPoolServerIP() { + return this->_poolServerIP; +} + void NTPClient::sendNTPPacket() { // set all bytes in the buffer to 0 memset(this->_packetBuffer, 0, NTP_PACKET_SIZE); diff --git a/NTPClient.h b/NTPClient.h old mode 100755 new mode 100644 index a31d32f..bb14018 --- a/NTPClient.h +++ b/NTPClient.h @@ -44,6 +44,23 @@ class NTPClient { */ void setPoolServerName(const char* poolServerName); + /** + * Get time server name + */ + const char *getPoolServerName(); + + /** + * Set time server IP + * + * @param poolServerIP + */ + void setPoolServerIP(IPAddress poolServerIP); + + /* + * Get time server IP + */ + IPAddress getPoolServerIP(); + /** * Set random local port */ @@ -91,12 +108,22 @@ class NTPClient { */ void setTimeOffset(int timeOffset); + /** + * Get the time offset + */ + long getTimeOffset(); + /** * Set the update interval to another frequency. E.g. useful when the * timeOffset should not be set in the constructor */ void setUpdateInterval(unsigned long updateInterval); + /** + * Get the update interval + */ + unsigned long getUpdateInterval(); + /** * @return time formatted like `hh:mm:ss` */ From 1723a89616ae7e3948e57c0b5d0f951a3665cc9f Mon Sep 17 00:00:00 2001 From: Dmitriy Logachov Date: Tue, 3 Dec 2024 21:29:50 +0500 Subject: [PATCH 2/2] Useful method to update time after dynamic NTP server change. --- NTPClient.cpp | 4 ++++ NTPClient.h | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/NTPClient.cpp b/NTPClient.cpp index b17b960..5cb257e 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -231,3 +231,7 @@ void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) { randomSeed(analogRead(0)); this->_port = random(minValue, maxValue); } + +void NTPClient::setLastUpdate(unsigned long sec) { + this->_lastUpdate = sec; +} \ No newline at end of file diff --git a/NTPClient.h b/NTPClient.h index bb14018..76451b9 100644 --- a/NTPClient.h +++ b/NTPClient.h @@ -138,4 +138,9 @@ class NTPClient { * Stops the underlying UDP client */ void end(); + + /** + * Set the last update time + */ + void setLastUpdate(unsigned long sec); };