From ac19e0d7db619787f88e0f5a2ae5d7859a4c7c88 Mon Sep 17 00:00:00 2001 From: Uli Date: Sat, 14 May 2016 12:04:18 +0200 Subject: [PATCH 1/2] 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. --- NTPClient.cpp | 9 ++++++++- NTPClient.h | 11 +++++++++++ examples/Advanced/Advanced.ino | 5 +++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 9f25d55..7b1a4e2 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -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(); } - diff --git a/NTPClient.h b/NTPClient.h index 9b856de..4d5630d 100644 --- a/NTPClient.h +++ b/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` */ diff --git a/examples/Advanced/Advanced.ino b/examples/Advanced/Advanced.ino index 6169940..2559508 100644 --- a/examples/Advanced/Advanced.ino +++ b/examples/Advanced/Advanced.ino @@ -10,8 +10,9 @@ const char *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(){ From 85c4caca9456f3e6d0f73be5a4672172562840b0 Mon Sep 17 00:00:00 2001 From: Uli Date: Sat, 14 May 2016 12:08:45 +0200 Subject: [PATCH 2/2] Fixed Readme Initialization with a different timeserver had a missing ntpUDP Added the default values for interval and offset to the comment of the default constructor in the ReadMe --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 685cc50..29462dc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# NTPClient +# NTPClient [![Build Status](https://travis-ci.org/arduino-libraries/NTPClient.svg?branch=master)](https://travis-ci.org/arduino-libraries/NTPClient) @@ -17,12 +17,13 @@ const char *password = ""; WiFiUDP ntpUDP; -// By default 'time.nist.gov' is used. +// By default 'time.nist.gov' is used with 60 seconds update interval and +// no offset NTPClient timeClient(ntpUDP); // You can specify the time server pool and the offset, (in seconds) // additionaly you can specify the update interval (in milliseconds). -// NTPClient timeClient("europe.pool.ntp.org", 3600, 60000); +// NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000); void setup(){ Serial.begin(11520); @@ -38,9 +39,9 @@ void setup(){ void loop() { timeClient.update(); - + Serial.println(timeClient.getFormattedTime()); - + delay(1000); } ```