diff --git a/NTPClient.cpp b/NTPClient.cpp old mode 100644 new mode 100755 index 07adce7..7be57b7 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -35,12 +35,25 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName) { this->_poolServerName = poolServerName; } +NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP) { + this->_udp = &udp; + this->_poolServerIP = poolServerIP; + this->_poolServerName = NULL; +} + 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; @@ -48,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); } @@ -93,6 +114,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... @@ -223,7 +248,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 old mode 100644 new mode 100755 index 94470cc..df8c626 --- a/NTPClient.h +++ b/NTPClient.h @@ -19,6 +19,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; @@ -43,6 +44,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 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. 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