This commit is contained in:
Dean Blackketter 2019-07-04 12:19:42 -07:00
commit 1809d67746
4 changed files with 38 additions and 24 deletions

View File

@ -25,7 +25,7 @@ NTPClient::NTPClient(UDP& udp) {
this->_udp = &udp; this->_udp = &udp;
} }
NTPClient::NTPClient(UDP& udp, int timeOffset) { NTPClient::NTPClient(UDP& udp, long timeOffset) {
this->_udp = &udp; this->_udp = &udp;
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
} }
@ -35,13 +35,13 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
} }
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) { NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) {
this->_udp = &udp; this->_udp = &udp;
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
} }
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval) { NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval) {
this->_udp = &udp; this->_udp = &udp;
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
@ -135,7 +135,7 @@ bool NTPClient::updated() {
return (_currentEpoc != 0); return (_currentEpoc != 0);
} }
unsigned long NTPClient::getEpochTime() { unsigned long NTPClient::getEpochTime() const {
return this->_timeOffset + // User offset return this->_timeOffset + // User offset
this->_currentEpoc + // Epoc returned by the NTP server this->_currentEpoc + // Epoc returned by the NTP server
((millis() - this->_lastUpdate + (_currentFraction / FRACTIONSPERMILLI)) / 1000); // Time since last update ((millis() - this->_lastUpdate + (_currentFraction / FRACTIONSPERMILLI)) / 1000); // Time since last update
@ -153,20 +153,20 @@ unsigned long long NTPClient::getEpochMillis() {
return epoch; return epoch;
} }
int NTPClient::getDay() { int NTPClient::getDay() const {
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
} }
int NTPClient::getHours() { int NTPClient::getHours() const {
return ((this->getEpochTime() % 86400L) / 3600); return ((this->getEpochTime() % 86400L) / 3600);
} }
int NTPClient::getMinutes() { int NTPClient::getMinutes() const {
return ((this->getEpochTime() % 3600) / 60); return ((this->getEpochTime() % 3600) / 60);
} }
int NTPClient::getSeconds() { int NTPClient::getSeconds() const {
return (this->getEpochTime() % 60); return (this->getEpochTime() % 60);
} }
String NTPClient::getFormattedTime() { String NTPClient::getFormattedTime() const {
unsigned long rawTime = this->getEpochTime(); unsigned long rawTime = this->getEpochTime();
unsigned long hours = (rawTime % 86400L) / 3600; unsigned long hours = (rawTime % 86400L) / 3600;
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
@ -190,7 +190,7 @@ void NTPClient::setTimeOffset(int timeOffset) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
} }
void NTPClient::setUpdateInterval(int updateInterval) { void NTPClient::setUpdateInterval(unsigned long updateInterval) {
this->_updateInterval = updateInterval; this->_updateInterval = updateInterval;
} }
@ -202,6 +202,10 @@ void NTPClient::setUpdateCallback(NTPUpdateCallbackFunction f) {
_updateCallback = f; _updateCallback = f;
} }
void NTPClient::setPoolServerName(const char* poolServerName) {
this->_poolServerName = poolServerName;
}
void NTPClient::sendNTPPacket() { 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);

View File

@ -18,12 +18,12 @@ class NTPClient {
UDP* _udp; UDP* _udp;
bool _udpSetup = false; bool _udpSetup = false;
const char* _poolServerName = "time.nist.gov"; // Default time server const char* _poolServerName = "pool.ntp.org"; // Default time server
int _port = NTP_DEFAULT_LOCAL_PORT; int _port = NTP_DEFAULT_LOCAL_PORT;
int _timeOffset = 0; long _timeOffset = 0;
unsigned int _updateInterval = 60000; // In ms
unsigned int _retryInterval = 1000; // In ms unsigned int _retryInterval = 1000; // In ms
unsigned long _updateInterval = 60000; // In ms
unsigned long _currentEpoc = 0; // In s unsigned long _currentEpoc = 0; // In s
unsigned long _currentFraction = 0; // In 1/(2^32) s unsigned long _currentFraction = 0; // In 1/(2^32) s
@ -39,10 +39,17 @@ class NTPClient {
public: public:
NTPClient(UDP& udp); NTPClient(UDP& udp);
NTPClient(UDP& udp, int timeOffset); NTPClient(UDP& udp, long timeOffset);
NTPClient(UDP& udp, const char* poolServerName); NTPClient(UDP& udp, const char* poolServerName);
NTPClient(UDP& udp, const char* poolServerName, int timeOffset); NTPClient(UDP& udp, const char* poolServerName, long timeOffset);
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval); NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);
/**
* Set time server name
*
* @param poolServerName
*/
void setPoolServerName(const char* poolServerName);
/** /**
* Starts the underlying UDP client with the default local port * Starts the underlying UDP client with the default local port
@ -81,10 +88,10 @@ class NTPClient {
*/ */
bool forceUpdate(); bool forceUpdate();
int getDay(); int getDay() const;
int getHours(); int getHours() const;
int getMinutes(); int getMinutes() const;
int getSeconds(); int getSeconds() const;
/** /**
* Changes the time offset. Useful for changing timezones dynamically * Changes the time offset. Useful for changing timezones dynamically
@ -95,7 +102,7 @@ class NTPClient {
* Set the update interval to another frequency. E.g. useful when the * Set the update interval to another frequency. E.g. useful when the
* timeOffset should not be set in the constructor * timeOffset should not be set in the constructor
*/ */
void setUpdateInterval(int updateInterval); void setUpdateInterval(unsigned long updateInterval);
/** /**
* Set the retry interval to another frequency in ms * Set the retry interval to another frequency in ms
@ -105,12 +112,12 @@ class NTPClient {
/** /**
* @return time formatted like `hh:mm:ss` * @return time formatted like `hh:mm:ss`
*/ */
String getFormattedTime(); String getFormattedTime() const;
/** /**
* @return time in seconds since Jan. 1, 1970 * @return time in seconds since Jan. 1, 1970
*/ */
unsigned long getEpochTime(); unsigned long getEpochTime() const;
/** /**
* @return time in milliseconds since Jan. 1, 1970 * @return time in milliseconds since Jan. 1, 1970

View File

@ -17,7 +17,7 @@ const char *password = "<PASSWORD>";
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
// By default 'time.nist.gov' is used with 60 seconds update interval and // By default 'pool.ntp.org' is used with 60 seconds update interval and
// no offset // no offset
NTPClient timeClient(ntpUDP); NTPClient timeClient(ntpUDP);

View File

@ -18,3 +18,6 @@ getMinutes KEYWORD2
getSeconds KEYWORD2 getSeconds KEYWORD2
getFormattedTime KEYWORD2 getFormattedTime KEYWORD2
getEpochTime KEYWORD2 getEpochTime KEYWORD2
setTimeOffset KEYWORD2
setUpdateInterval KEYWORD2
setPoolServerName KEYWORD2