Merge remote-tracking branch 'upstream/master'

This commit is contained in:
stash-s 2020-10-31 21:33:55 +01:00
commit 6bfd1c29a4
4 changed files with 64 additions and 8 deletions

54
NTPClient.cpp Normal file → Executable file
View File

@ -44,6 +44,15 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName)
this->_poolServerName = poolServerName;
}
NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP)
:_updateStartHandler(NULL),
_updateEndHandler(NULL)
{
this->_udp = &udp;
this->_poolServerIP = poolServerIP;
this->_poolServerName = NULL;
}
NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset)
:_updateStartHandler(NULL),
_updateEndHandler(NULL)
@ -53,6 +62,16 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset)
this->_poolServerName = poolServerName;
}
NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset)
:_updateStartHandler(NULL),
_updateEndHandler(NULL)
{
this->_udp = &udp;
this->_timeOffset = timeOffset;
this->_poolServerIP = poolServerIP;
this->_poolServerName = NULL;
}
NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval)
:_updateStartHandler(NULL),
_updateEndHandler(NULL)
@ -63,11 +82,22 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsi
this->_updateInterval = updateInterval;
}
NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval)
:_updateStartHandler(NULL),
_updateEndHandler(NULL)
{
this->_udp = &udp;
this->_timeOffset = timeOffset;
this->_poolServerIP = poolServerIP;
this->_poolServerName = NULL;
this->_updateInterval = updateInterval;
}
void NTPClient::begin() {
this->begin(NTP_DEFAULT_LOCAL_PORT);
}
void NTPClient::begin(int port) {
void NTPClient::begin(unsigned int port) {
this->_port = port;
this->_udp->begin(this->_port);
@ -84,6 +114,10 @@ bool NTPClient::forceUpdate() {
this->_updateStartHandler();
}
// flush any existing packets
while(this->_udp->parsePacket() != 0)
this->_udp->flush();
this->sendNTPPacket();
// Wait till data is there or timeout...
@ -112,16 +146,16 @@ bool NTPClient::forceUpdate() {
this->_updateEndHandler();
}
return true;
return true; // return true after successful update
}
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 true;
return false; // return false if update does not occur
}
unsigned long NTPClient::getEpochTime() const {
@ -180,6 +214,7 @@ void NTPClient::sendNTPPacket() {
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode
this->_packetBuffer[1] = 0; // Stratum, or type of clock
this->_packetBuffer[2] = 6; // Polling Interval
@ -192,7 +227,16 @@ 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();
}
void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
randomSeed(analogRead(0));
this->_port = random(minValue, maxValue);
}

13
NTPClient.h Normal file → Executable file
View File

@ -20,7 +20,8 @@ class NTPClient {
bool _udpSetup = false;
const char* _poolServerName = "pool.ntp.org"; // Default time server
int _port = NTP_DEFAULT_LOCAL_PORT;
IPAddress _poolServerIP;
unsigned int _port = NTP_DEFAULT_LOCAL_PORT;
long _timeOffset = 0;
unsigned long _updateInterval = 60000; // In ms
@ -41,6 +42,9 @@ class NTPClient {
NTPClient(UDP& udp, const char* poolServerName);
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
@ -49,6 +53,11 @@ class NTPClient {
*/
void setPoolServerName(const char* poolServerName);
/**
* Set random local port
*/
void setRandomPort(unsigned int minValue = 49152, unsigned int maxValue = 65535);
/**
* Starts the underlying UDP client with the default local port
*/
@ -57,7 +66,7 @@ class NTPClient {
/**
* Starts the underlying UDP client with the specified local port
*/
void begin(int 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

View File

@ -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.

View File

@ -1,5 +1,5 @@
name=NTPClient
version=3.1.0
version=3.2.0
author=Fabrice Weinberg
maintainer=Fabrice Weinberg <fabrice@weinberg.me>
sentence=An NTPClient to connect to a time server