From 712c58c18e5399416650a356c39d4ee6f8dfd9db Mon Sep 17 00:00:00 2001 From: bitsy <36607587+bitsy@users.noreply.github.com> Date: Sun, 16 Sep 2018 21:11:26 -0700 Subject: [PATCH 01/10] Update NTPClient.cpp The NTPClient method update() has been changed to only return "TRUE" after a successful update. The following conditions return "FALSE": - Interval duration requirement not met - forceUpdate() times out --- NTPClient.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 87b7a53..79924c7 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -89,7 +89,7 @@ bool NTPClient::forceUpdate() { this->_currentEpoc = secsSince1900 - SEVENZYYEARS; - return true; + return true; // return true after successful update } bool NTPClient::update() { @@ -98,7 +98,7 @@ bool NTPClient::update() { if (!this->_udpSetup) this->begin(); // setup the UDP client if needed return this->forceUpdate(); } - return true; + return false; // return false if update does not occur } unsigned long NTPClient::getEpochTime() const { From cfbe88a3e2df5b916d4a7146fa0a83dacf88bd39 Mon Sep 17 00:00:00 2001 From: James Brown Date: Sat, 12 Jan 2019 13:32:05 -0700 Subject: [PATCH 02/10] Added packet flush before request in forceUpdate(). Fixes issue #49 in the upstream repo --- NTPClient.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NTPClient.cpp b/NTPClient.cpp index 87b7a53..668d72b 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -65,6 +65,10 @@ bool NTPClient::forceUpdate() { Serial.println("Update from NTP Server"); #endif + // flush any existing packets + while(this->_udp->parsePacket() != 0) + this->_udp->flush(); + this->sendNTPPacket(); // Wait till data is there or timeout... From 151bbc5484265cbeeb0273a3752a95aedda22f2f Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 18 Sep 2019 10:01:20 +0200 Subject: [PATCH 03/10] Releasing v3.2.0 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index d4908ca..309b75d 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=NTPClient -version=3.1.0 +version=3.2.0 author=Fabrice Weinberg maintainer=Fabrice Weinberg sentence=An NTPClient to connect to a time server From 9c16cb728bbda2eb5e60781d9cbb8985e6f58207 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 19 Sep 2019 07:29:51 +0200 Subject: [PATCH 04/10] Adding documentation of function 'getEpochTime' to README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6c8c07a..46f9eb5 100644 --- a/README.md +++ b/README.md @@ -45,3 +45,6 @@ void loop() { delay(1000); } ``` + +## Function documentation +`getEpochTime` returns the unix epoch, which are the seconds elapsed since 00:00:00 UTC on 1 January 1970 (leap seconds are ignored, every day is treated as having 86400 seconds). **Attention**: If you have set a time offset this time offset will be added to your epoch timestamp. From 931c47169776418cc499e5195f60f6a8f18bee64 Mon Sep 17 00:00:00 2001 From: sheffieldnick Date: Wed, 26 Apr 2017 15:14:37 +0100 Subject: [PATCH 05/10] Added support for server IPAddress argument to NTPClient --- NTPClient.cpp | 8 +++++++- NTPClient.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) mode change 100644 => 100755 NTPClient.cpp mode change 100644 => 100755 NTPClient.h diff --git a/NTPClient.cpp b/NTPClient.cpp old mode 100644 new mode 100755 index 6133508..f2d484f --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -35,7 +35,13 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName) { this->_poolServerName = poolServerName; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) { +NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP) { + this->_udp = &udp; + this->_poolServerIP = poolServerIP; + this->_poolServerName = NULL; +} + +NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) { this->_udp = &udp; this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; diff --git a/NTPClient.h b/NTPClient.h old mode 100644 new mode 100755 index 02d8752..08c6ee1 --- a/NTPClient.h +++ b/NTPClient.h @@ -30,6 +30,7 @@ class NTPClient { NTPClient(UDP& udp); NTPClient(UDP& udp, long timeOffset); NTPClient(UDP& udp, const char* poolServerName); + NTPClient(UDP& udp, IPAddress poolServerIP); NTPClient(UDP& udp, const char* poolServerName, long timeOffset); NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval); From a80086968a0dfcf0f7d9f336facd1fc09cfa9b58 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 19 Sep 2019 10:10:00 +0200 Subject: [PATCH 06/10] Fixing function signature error of 'NTPClient(UDP& udp, const char* poolServerName, long timeOffset)' (long timeOffset instead of int timeOffset) and adding the overloaded ctors also for an IPAddress --- NTPClient.cpp | 25 ++++++++++++++++++++++--- NTPClient.h | 5 ++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index f2d484f..fffe105 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -37,16 +37,23 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName) { NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP) { this->_udp = &udp; - this->_poolServerIP = poolServerIP; + this->_poolServerIP = poolServerIP; this->_poolServerName = NULL; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) { +NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) { this->_udp = &udp; this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; } +NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset){ + this->_udp = &udp; + this->_timeOffset = timeOffset; + this->_poolServerIP = poolServerIP; + this->_poolServerName = NULL; +} + NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval) { this->_udp = &udp; this->_timeOffset = timeOffset; @@ -54,6 +61,14 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsi this->_updateInterval = updateInterval; } +NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval) { + this->_udp = &udp; + this->_timeOffset = timeOffset; + this->_poolServerIP = poolServerIP; + this->_poolServerName = NULL; + this->_updateInterval = updateInterval; +} + void NTPClient::begin() { this->begin(NTP_DEFAULT_LOCAL_PORT); } @@ -179,7 +194,11 @@ void NTPClient::sendNTPPacket() { // all NTP fields have been given values, now // you can send a packet requesting a timestamp: - this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123 + if (this->_poolServerName) { + this->_udp->beginPacket(this->_poolServerName, 123); + } else { + this->_udp->beginPacket(this->_poolServerIP, 123); + } this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE); this->_udp->endPacket(); } diff --git a/NTPClient.h b/NTPClient.h index 08c6ee1..20ec43b 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -14,6 +14,7 @@ class NTPClient { bool _udpSetup = false; const char* _poolServerName = "pool.ntp.org"; // Default time server + IPAddress _poolServerIP; int _port = NTP_DEFAULT_LOCAL_PORT; long _timeOffset = 0; @@ -30,9 +31,11 @@ class NTPClient { NTPClient(UDP& udp); NTPClient(UDP& udp, long timeOffset); NTPClient(UDP& udp, const char* poolServerName); - NTPClient(UDP& udp, IPAddress poolServerIP); NTPClient(UDP& udp, const char* poolServerName, long timeOffset); NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval); + NTPClient(UDP& udp, IPAddress poolServerIP); + NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset); + NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval); /** * Set time server name From a8bdae7ca42cdd210f50abacf1aea255204a10f5 Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Mon, 31 Aug 2020 02:25:05 +0200 Subject: [PATCH 07/10] Added method to set random local port Added method to set a random local port, so that the board needs not to use always the same embedded local port for receiving NTP packets. --- NTPClient.cpp | 12 +++++++++--- NTPClient.h | 9 +++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index fffe105..ddb76fb 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -73,7 +73,7 @@ void NTPClient::begin() { this->begin(NTP_DEFAULT_LOCAL_PORT); } -void NTPClient::begin(int port) { +void NTPClient::begin(long port) { this->_port = port; this->_udp->begin(this->_port); @@ -120,7 +120,7 @@ bool NTPClient::forceUpdate() { bool NTPClient::update() { if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval || this->_lastUpdate == 0) { // Update if there was no update yet. - if (!this->_udpSetup) this->begin(); // setup the UDP client if needed + 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 @@ -181,7 +181,8 @@ void NTPClient::sendNTPPacket() { // set all bytes in the buffer to 0 memset(this->_packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request - // (see URL above for details on the packets) + // (see URL above for details on the packets) Serial.println(this->_port); + this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode this->_packetBuffer[1] = 0; // Stratum, or type of clock this->_packetBuffer[2] = 6; // Polling Interval @@ -202,3 +203,8 @@ void NTPClient::sendNTPPacket() { this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE); this->_udp->endPacket(); } + +void NTPClient::setRandomPort() { + randomSeed(analogRead(0)); + this->_port = random(1, 65534); +} \ No newline at end of file diff --git a/NTPClient.h b/NTPClient.h index 20ec43b..6498e82 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -15,7 +15,7 @@ class NTPClient { const char* _poolServerName = "pool.ntp.org"; // Default time server IPAddress _poolServerIP; - int _port = NTP_DEFAULT_LOCAL_PORT; + long _port = NTP_DEFAULT_LOCAL_PORT; long _timeOffset = 0; unsigned long _updateInterval = 60000; // In ms @@ -44,6 +44,11 @@ class NTPClient { */ void setPoolServerName(const char* poolServerName); + /** + * Set random local port + */ + void setRandomPort(); + /** * Starts the underlying UDP client with the default local port */ @@ -52,7 +57,7 @@ class NTPClient { /** * Starts the underlying UDP client with the specified local port */ - void begin(int port); + void begin(long port); /** * This should be called in the main loop of your application. By default an update from the NTP Server is only From 9e4323bcefadc5f87b3689b57d3667815e87caae Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Mon, 31 Aug 2020 15:31:55 +0200 Subject: [PATCH 08/10] Adding range choice for setRandomPort() --- NTPClient.cpp | 4 ++-- NTPClient.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index ddb76fb..caa30d1 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -204,7 +204,7 @@ void NTPClient::sendNTPPacket() { this->_udp->endPacket(); } -void NTPClient::setRandomPort() { +void NTPClient::setRandomPort(long minValue, long maxValue) { randomSeed(analogRead(0)); - this->_port = random(1, 65534); + this->_port = random(minValue, maxValue); } \ No newline at end of file diff --git a/NTPClient.h b/NTPClient.h index 6498e82..226f54a 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -47,7 +47,7 @@ class NTPClient { /** * Set random local port */ - void setRandomPort(); + void setRandomPort(long minValue, long maxValue); /** * Starts the underlying UDP client with the default local port From f5673290fc8d90f92e7841123c4411cdb99865fe Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Thu, 3 Sep 2020 14:52:08 +0200 Subject: [PATCH 09/10] Changing long into unsigned int --- NTPClient.cpp | 6 +++--- NTPClient.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index caa30d1..760e142 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -73,7 +73,7 @@ void NTPClient::begin() { this->begin(NTP_DEFAULT_LOCAL_PORT); } -void NTPClient::begin(long port) { +void NTPClient::begin(unsigned int port) { this->_port = port; this->_udp->begin(this->_port); @@ -181,7 +181,7 @@ void NTPClient::sendNTPPacket() { // set all bytes in the buffer to 0 memset(this->_packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request - // (see URL above for details on the packets) Serial.println(this->_port); + // (see URL above for details on the packets) this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode this->_packetBuffer[1] = 0; // Stratum, or type of clock @@ -204,7 +204,7 @@ void NTPClient::sendNTPPacket() { this->_udp->endPacket(); } -void NTPClient::setRandomPort(long minValue, long maxValue) { +void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) { randomSeed(analogRead(0)); this->_port = random(minValue, maxValue); } \ No newline at end of file diff --git a/NTPClient.h b/NTPClient.h index 226f54a..7defb1c 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -15,7 +15,7 @@ class NTPClient { const char* _poolServerName = "pool.ntp.org"; // Default time server IPAddress _poolServerIP; - long _port = NTP_DEFAULT_LOCAL_PORT; + unsigned int _port = NTP_DEFAULT_LOCAL_PORT; long _timeOffset = 0; unsigned long _updateInterval = 60000; // In ms @@ -47,7 +47,7 @@ class NTPClient { /** * Set random local port */ - void setRandomPort(long minValue, long maxValue); + void setRandomPort(unsigned int minValue, unsigned int maxValue); /** * Starts the underlying UDP client with the default local port @@ -57,7 +57,7 @@ class NTPClient { /** * Starts the underlying UDP client with the specified local port */ - void begin(long port); + void begin(unsigned int port); /** * This should be called in the main loop of your application. By default an update from the NTP Server is only From d7d8b4512b01f62a4bb1f3a57d82ab5d02ce806c Mon Sep 17 00:00:00 2001 From: Luigi <27034386+luigigubello@users.noreply.github.com> Date: Mon, 7 Sep 2020 17:14:47 +0200 Subject: [PATCH 10/10] Adding default port range (#117) --- NTPClient.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NTPClient.h b/NTPClient.h index 7defb1c..b518c28 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -47,7 +47,7 @@ class NTPClient { /** * Set random local port */ - void setRandomPort(unsigned int minValue, unsigned int maxValue); + void setRandomPort(unsigned int minValue = 49152, unsigned int maxValue = 65535); /** * Starts the underlying UDP client with the default local port