Merge branch 'master' into master
This commit is contained in:
commit
b451c022c3
@ -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;
|
||||||
@ -101,26 +101,26 @@ bool NTPClient::update() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) / 1000); // Time since last update
|
((millis() - this->_lastUpdate) / 1000); // Time since last update
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
@ -183,10 +183,14 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
33
NTPClient.h
33
NTPClient.h
@ -13,11 +13,11 @@ 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 long _updateInterval = 60000; // In ms
|
||||||
|
|
||||||
unsigned long _currentEpoc = 0; // In s
|
unsigned long _currentEpoc = 0; // In s
|
||||||
unsigned long _lastUpdate = 0; // In ms
|
unsigned long _lastUpdate = 0; // In ms
|
||||||
@ -28,10 +28,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
|
||||||
@ -62,9 +69,9 @@ class NTPClient {
|
|||||||
int getHours();
|
int getHours();
|
||||||
int getMinutes();
|
int getMinutes();
|
||||||
int getSeconds();
|
int getSeconds();
|
||||||
int getYear();
|
int getYear();
|
||||||
int getMonth();
|
int getMonth();
|
||||||
int getDate();
|
int getDate();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Changes the time offset. Useful for changing timezones dynamically
|
* Changes the time offset. Useful for changing timezones dynamically
|
||||||
@ -75,22 +82,22 @@ 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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return time formatted like `hh:mm:ss`
|
* @return time formatted like `hh:mm:ss`
|
||||||
*/
|
*/
|
||||||
String getFormattedTime();
|
String getFormattedTime();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return date formatted like `dd.MM.yyyy`
|
* @return date formatted like `dd.MM.yyyy`
|
||||||
*/
|
*/
|
||||||
String getFormattedDate();
|
String getFormattedDate();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return time in seconds since Jan. 1, 1970
|
* @return time in seconds since Jan. 1, 1970
|
||||||
*/
|
*/
|
||||||
unsigned long getEpochTime();
|
unsigned long getEpochTime() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops the underlying UDP client
|
* Stops the underlying UDP client
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
@ -18,3 +18,6 @@ getMinutes KEYWORD2
|
|||||||
getSeconds KEYWORD2
|
getSeconds KEYWORD2
|
||||||
getFormattedTime KEYWORD2
|
getFormattedTime KEYWORD2
|
||||||
getEpochTime KEYWORD2
|
getEpochTime KEYWORD2
|
||||||
|
setTimeOffset KEYWORD2
|
||||||
|
setUpdateInterval KEYWORD2
|
||||||
|
setPoolServerName KEYWORD2
|
||||||
|
Loading…
Reference in New Issue
Block a user