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.
This commit is contained in:
Luigi Gubello 2020-08-31 02:25:05 +02:00
parent 894b21de11
commit a8bdae7ca4
2 changed files with 16 additions and 5 deletions

View File

@ -73,7 +73,7 @@ void NTPClient::begin() {
this->begin(NTP_DEFAULT_LOCAL_PORT); this->begin(NTP_DEFAULT_LOCAL_PORT);
} }
void NTPClient::begin(int port) { void NTPClient::begin(long port) {
this->_port = port; this->_port = port;
this->_udp->begin(this->_port); this->_udp->begin(this->_port);
@ -120,7 +120,7 @@ bool NTPClient::forceUpdate() {
bool NTPClient::update() { bool NTPClient::update() {
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|| this->_lastUpdate == 0) { // Update if there was no update yet. || 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 this->forceUpdate();
} }
return false; // return false if update does not occur return false; // return false if update does not occur
@ -181,7 +181,8 @@ void NTPClient::sendNTPPacket() {
// set all bytes in the buffer to 0 // set all bytes in the buffer to 0
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE); memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request // 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[0] = 0b11100011; // LI, Version, Mode
this->_packetBuffer[1] = 0; // Stratum, or type of clock this->_packetBuffer[1] = 0; // Stratum, or type of clock
this->_packetBuffer[2] = 6; // Polling Interval this->_packetBuffer[2] = 6; // Polling Interval
@ -202,3 +203,8 @@ void NTPClient::sendNTPPacket() {
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE); this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
this->_udp->endPacket(); this->_udp->endPacket();
} }
void NTPClient::setRandomPort() {
randomSeed(analogRead(0));
this->_port = random(1, 65534);
}

View File

@ -15,7 +15,7 @@ class NTPClient {
const char* _poolServerName = "pool.ntp.org"; // Default time server const char* _poolServerName = "pool.ntp.org"; // Default time server
IPAddress _poolServerIP; IPAddress _poolServerIP;
int _port = NTP_DEFAULT_LOCAL_PORT; long _port = NTP_DEFAULT_LOCAL_PORT;
long _timeOffset = 0; long _timeOffset = 0;
unsigned long _updateInterval = 60000; // In ms unsigned long _updateInterval = 60000; // In ms
@ -44,6 +44,11 @@ class NTPClient {
*/ */
void setPoolServerName(const char* poolServerName); void setPoolServerName(const char* poolServerName);
/**
* Set random local port
*/
void setRandomPort();
/** /**
* Starts the underlying UDP client with the default local port * 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 * 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 * This should be called in the main loop of your application. By default an update from the NTP Server is only