Skip to content

Commit 0ccc182

Browse files
committed
feat: async update
1 parent 0d7162f commit 0ccc182

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

NTPClient.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,47 @@ bool NTPClient::update() {
126126
return false; // return false if update does not occur
127127
}
128128

129+
int NTPClient::asyncUpdate() {
130+
if ((millis() - this->_sendTime >= this->_updateInterval) // Update after _updateInterval
131+
|| this->_sendTime == 0) { // Update if there was no update yet.
132+
_sendTime = millis();
133+
if (!this->_udpSetup || this->_port != NTP_DEFAULT_LOCAL_PORT) this->begin(this->_port); // setup the UDP client if needed
134+
this->sendNTPPacket();
135+
136+
int code = 2;
137+
if (_needUpdate) code = -1; // timeout
138+
_needUpdate = true;
139+
return code;
140+
}
141+
142+
if (_needUpdate) {
143+
int cb = this->_udp->parsePacket();
144+
if (cb == 0) return 2;
145+
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
146+
147+
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
148+
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
149+
// combine the four bytes (two words) into a long integer
150+
// this is NTP time (seconds since Jan 1 1900):
151+
unsigned long secsSince1900 = highWord << 16 | lowWord;
152+
153+
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
154+
this->_lastUpdate = millis();
155+
_needUpdate = false;
156+
return 0;
157+
}
158+
159+
return 1; // return false if update does not occur
160+
}
161+
129162
bool NTPClient::isTimeSet() const {
130163
return (this->_lastUpdate != 0); // returns true if the time has been set, else false
131164
}
132165

166+
long long NTPClient::getEpochTimeMillis() const {
167+
return this->_currentEpoc * 1000LL + millis() - this->_lastUpdate;
168+
}
169+
133170
unsigned long NTPClient::getEpochTime() const {
134171
return this->_timeOffset + // User offset
135172
this->_currentEpoc + // Epoch returned by the NTP server

NTPClient.h

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class NTPClient {
2020

2121
unsigned long _updateInterval = 60000; // In ms
2222

23+
bool _needUpdate = true;
24+
unsigned long _sendTime = 0;
2325
unsigned long _currentEpoc = 0; // In s
2426
unsigned long _lastUpdate = 0; // In ms
2527

@@ -74,6 +76,14 @@ class NTPClient {
7476
*/
7577
bool forceUpdate();
7678

79+
/**
80+
* Alternatevly this can be called instead of update() in the main loop of your application.
81+
* AsyncUpdate from the NTP Server is made every _updateInterval milliseconds.
82+
*
83+
* @return 0 on success, -1 on failure, 1 if no update is needed, 2 if update is in progress
84+
*/
85+
int asyncUpdate();
86+
7787
/**
7888
* This allows to check if the NTPClient successfully received a NTP packet and set the time.
7989
*
@@ -107,6 +117,12 @@ class NTPClient {
107117
*/
108118
unsigned long getEpochTime() const;
109119

120+
121+
/**
122+
* @return time in milliseconds since Jan. 1, 1970 (UTC+0)
123+
*/
124+
long long getEpochTimeMillis() const;
125+
110126
/**
111127
* Stops the underlying UDP client
112128
*/

0 commit comments

Comments
 (0)