From a8bdae7ca42cdd210f50abacf1aea255204a10f5 Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Mon, 31 Aug 2020 02:25:05 +0200 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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