NTPClient now does updates asynchronously. Thanks to https://github.com/arduino-libraries/NTPClient/pull/22
This commit is contained in:
@ -26,7 +26,7 @@ NTPClient::NTPClient(UDP& udp) {
|
||||
this->_udp = &udp;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, long timeOffset) {
|
||||
NTPClient::NTPClient(UDP& udp, int timeOffset) {
|
||||
this->_udp = &udp;
|
||||
this->_timeOffset = timeOffset;
|
||||
}
|
||||
@ -36,13 +36,13 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
|
||||
this->_poolServerName = poolServerName;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) {
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) {
|
||||
this->_udp = &udp;
|
||||
this->_timeOffset = timeOffset;
|
||||
this->_poolServerName = poolServerName;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval) {
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval) {
|
||||
this->_udp = &udp;
|
||||
this->_timeOffset = timeOffset;
|
||||
this->_poolServerName = poolServerName;
|
||||
@ -61,79 +61,128 @@ void NTPClient::begin(int port) {
|
||||
this->_udpSetup = true;
|
||||
}
|
||||
|
||||
bool NTPClient::checkResponse() {
|
||||
|
||||
if (this->_udp->parsePacket()) {
|
||||
this->_lastUpdate = millis();
|
||||
this->_lastRequest = 0; // no outstanding request
|
||||
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||
|
||||
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
|
||||
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
|
||||
// combine the four bytes (two words) into a long integer
|
||||
// this is NTP time (seconds since Jan 1 1900):
|
||||
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
||||
|
||||
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
|
||||
|
||||
highWord = word(this->_packetBuffer[44], this->_packetBuffer[45]);
|
||||
lowWord = word(this->_packetBuffer[46], this->_packetBuffer[47]);
|
||||
this->_currentFraction = highWord << 16 | lowWord;
|
||||
|
||||
random16_add_entropy(this->_packetBuffer[44]<<8 | this->_packetBuffer[45]);
|
||||
random16_add_entropy(this->_packetBuffer[46]<<8 | this->_packetBuffer[47]);
|
||||
|
||||
// if the user has set a callback function for when the time is updated, call it
|
||||
if (_updateCallback) { _updateCallback(this); }
|
||||
|
||||
this->_timeValid = true;
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool NTPClient::forceUpdate() {
|
||||
#ifdef DEBUG_NTPClient
|
||||
Serial.println("Update from NTP Server");
|
||||
#endif
|
||||
|
||||
while(this->_udp->parsePacket() != 0)
|
||||
this->_udp->flush();
|
||||
this->_timeValid = false;
|
||||
|
||||
while(this->_udp->parsePacket() != 0) this->_udp->flush();
|
||||
|
||||
this->sendNTPPacket();
|
||||
|
||||
// Wait till data is there or timeout...
|
||||
byte timeout = 0;
|
||||
int cb = 0;
|
||||
bool cb = 0;
|
||||
do {
|
||||
delay ( 10 );
|
||||
cb = this->_udp->parsePacket();
|
||||
if (timeout > 100) {
|
||||
this->_timeValid = false;
|
||||
return false; // timeout after 1000 ms
|
||||
}
|
||||
cb = this->checkResponse();
|
||||
if (timeout > 100) return false; // timeout after 1000 ms
|
||||
timeout++;
|
||||
} while (cb == 0);
|
||||
this->_timeValid = true;
|
||||
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
|
||||
|
||||
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||
|
||||
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
|
||||
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
|
||||
// combine the four bytes (two words) into a long integer
|
||||
// this is NTP time (seconds since Jan 1 1900):
|
||||
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
||||
|
||||
random16_add_entropy(this->_packetBuffer[44]<<8 | this->_packetBuffer[45]);
|
||||
random16_add_entropy(this->_packetBuffer[46]<<8 | this->_packetBuffer[47]);
|
||||
|
||||
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
|
||||
} while (cb == false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NTPClient::update() {
|
||||
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|
||||
|| this->_lastUpdate == 0) { // Update if there was no update yet.
|
||||
if (!this->_udpSetup) this->begin(); // setup the UDP client if needed
|
||||
return this->forceUpdate();
|
||||
bool updated = false;
|
||||
unsigned long now = millis();
|
||||
|
||||
if ( ((_lastRequest == 0) && (_lastUpdate == 0)) // Never requested or updated
|
||||
|| ((_lastRequest == 0) && ((now - _lastUpdate) >= _updateInterval)) // Update after _updateInterval
|
||||
|| ((_lastRequest != 0) && ((now - _lastRequest) > _retryInterval)) ) { // Update if there was no response to the request
|
||||
|
||||
this->_timeValid = false;
|
||||
|
||||
// setup the UDP client if needed
|
||||
if (!this->_udpSetup) {
|
||||
this->begin();
|
||||
}
|
||||
|
||||
this->sendNTPPacket();
|
||||
}
|
||||
return true;
|
||||
|
||||
if (_lastRequest) {
|
||||
updated = checkResponse();
|
||||
}
|
||||
|
||||
return updated;
|
||||
}
|
||||
|
||||
unsigned long NTPClient::getEpochTime() const {
|
||||
bool NTPClient::updated() {
|
||||
return (_currentEpoc != 0);
|
||||
}
|
||||
|
||||
bool NTPClient::getTimeValid() {
|
||||
return this->_timeValid;
|
||||
}
|
||||
|
||||
unsigned long NTPClient::getEpochTime() {
|
||||
return this->_timeOffset + // User offset
|
||||
this->_currentEpoc + // Epoc returned by the NTP server
|
||||
((millis() - this->_lastUpdate) / 1000); // Time since last update
|
||||
((millis() - this->_lastUpdate + (_currentFraction / FRACTIONSPERMILLI)) / 1000); // Time since last update
|
||||
}
|
||||
|
||||
int NTPClient::getDay() const {
|
||||
unsigned long long NTPClient::getEpochMillis() {
|
||||
unsigned long long epoch;
|
||||
|
||||
epoch = this->_timeOffset; // user offset
|
||||
epoch += _currentEpoc; // last time returned via server
|
||||
epoch *= 1000; // convert to millis
|
||||
epoch += _currentFraction / FRACTIONSPERMILLI; // add the fraction from the server
|
||||
epoch += millis() - this->_lastUpdate; // add the millis that have passed since the last update
|
||||
|
||||
return epoch;
|
||||
}
|
||||
|
||||
int NTPClient::getDay() {
|
||||
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
|
||||
}
|
||||
int NTPClient::getHours() const {
|
||||
int NTPClient::getHours() {
|
||||
return ((this->getEpochTime() % 86400L) / 3600);
|
||||
}
|
||||
int NTPClient::getMinutes() const {
|
||||
int NTPClient::getMinutes() {
|
||||
return ((this->getEpochTime() % 3600) / 60);
|
||||
}
|
||||
int NTPClient::getSeconds() const {
|
||||
int NTPClient::getSeconds() {
|
||||
return (this->getEpochTime() % 60);
|
||||
}
|
||||
boolean NTPClient::getTimeValid() const {
|
||||
return (this->_timeValid);
|
||||
}
|
||||
|
||||
String NTPClient::getFormattedTime() const {
|
||||
String NTPClient::getFormattedTime() {
|
||||
unsigned long rawTime = this->getEpochTime();
|
||||
unsigned long hours = (rawTime % 86400L) / 3600;
|
||||
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
|
||||
@ -157,12 +206,16 @@ void NTPClient::setTimeOffset(int timeOffset) {
|
||||
this->_timeOffset = timeOffset;
|
||||
}
|
||||
|
||||
void NTPClient::setUpdateInterval(unsigned long updateInterval) {
|
||||
void NTPClient::setUpdateInterval(int updateInterval) {
|
||||
this->_updateInterval = updateInterval;
|
||||
}
|
||||
|
||||
void NTPClient::setPoolServerName(const char* poolServerName) {
|
||||
this->_poolServerName = poolServerName;
|
||||
void NTPClient::setRetryInterval(int retryInterval) {
|
||||
_retryInterval = retryInterval;
|
||||
}
|
||||
|
||||
void NTPClient::setUpdateCallback(NTPUpdateCallbackFunction f) {
|
||||
_updateCallback = f;
|
||||
}
|
||||
|
||||
void NTPClient::sendNTPPacket() {
|
||||
@ -185,4 +238,7 @@ void NTPClient::sendNTPPacket() {
|
||||
this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
|
||||
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||
this->_udp->endPacket();
|
||||
|
||||
this->_lastRequest = millis();
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user