Added functions for changing the timeOffset and updateInterval later
I added two functions: ```cpp setTimeOffset(int timeOffset) ``` Allows you to set the offset at a later stage (e.g. upon timezone changes or change from summertime to standard time) ```cpp setUpdateInterval(int updateInterval) ``` Allows to set the interval at a later point. This was helpful when not setting an offset at initialization and e.g. for other sync times during special occasions.
This commit is contained in:
parent
e0fe10f578
commit
ac19e0d7db
@ -140,6 +140,14 @@ void NTPClient::end() {
|
||||
this->_udpSetup = false;
|
||||
}
|
||||
|
||||
void NTPClient::setTimeOffset(int timeOffset) {
|
||||
this->_timeOffset = timeOffset;
|
||||
}
|
||||
|
||||
void NTPClient::setUpdateInterval(int updateInterval) {
|
||||
this->_updateInterval = updateInterval;
|
||||
}
|
||||
|
||||
void NTPClient::sendNTPPacket() {
|
||||
// set all bytes in the buffer to 0
|
||||
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
|
||||
@ -161,4 +169,3 @@ void NTPClient::sendNTPPacket() {
|
||||
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||
this->_udp->endPacket();
|
||||
}
|
||||
|
||||
|
11
NTPClient.h
11
NTPClient.h
@ -63,6 +63,17 @@ class NTPClient {
|
||||
int getMinutes();
|
||||
int getSeconds();
|
||||
|
||||
/**
|
||||
* Changes the time offset. Useful for changing timezones dynamically
|
||||
*/
|
||||
void setTimeOffset(int timeOffset);
|
||||
|
||||
/**
|
||||
* Set the update interval to another frequency. E.g. useful when the
|
||||
* timeOffset should not be set in the constructor
|
||||
*/
|
||||
void setUpdateInterval(int updateInterval);
|
||||
|
||||
/**
|
||||
* @return time formatted like `hh:mm:ss`
|
||||
*/
|
||||
|
@ -10,8 +10,9 @@ const char *password = "<PASSWORD>";
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
|
||||
// You can specify the time server pool and the offset, (in seconds)
|
||||
// additionaly you can specify the update interval (in milliseconds).
|
||||
// You can specify the time server pool and the offset (in seconds, can be
|
||||
// changed later with setTimeOffset() ). Additionaly you can specify the
|
||||
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
|
||||
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
|
||||
|
||||
void setup(){
|
||||
|
Loading…
Reference in New Issue
Block a user