Skip to content

Commit b68746a

Browse files
committed
Make update() non-blocking.
1 parent 9dc9ad1 commit b68746a

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

NTPClient.cpp

+43-19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ void NTPClient::begin(int port) {
6060
this->_udpSetup = true;
6161
}
6262

63+
bool NTPClient::checkResponse() {
64+
65+
if (this->_udp->parsePacket()) {
66+
this->_lastUpdate = millis();
67+
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
68+
69+
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
70+
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
71+
// combine the four bytes (two words) into a long integer
72+
// this is NTP time (seconds since Jan 1 1900):
73+
unsigned long secsSince1900 = highWord << 16 | lowWord;
74+
75+
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
76+
return true;
77+
} else {
78+
return false;
79+
}
80+
}
81+
6382
bool NTPClient::forceUpdate() {
6483
#ifdef DEBUG_NTPClient
6584
Serial.println("Update from NTP Server");
@@ -69,36 +88,38 @@ bool NTPClient::forceUpdate() {
6988

7089
// Wait till data is there or timeout...
7190
byte timeout = 0;
72-
int cb = 0;
91+
bool cb = 0;
7392
do {
7493
delay ( 10 );
75-
cb = this->_udp->parsePacket();
94+
cb = this->checkResponse();
7695
if (timeout > 100) return false; // timeout after 1000 ms
7796
timeout++;
78-
} while (cb == 0);
97+
} while (cb == false);
7998

80-
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
99+
return true;
100+
}
81101

82-
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
102+
bool NTPClient::update() {
103+
bool updated = false;
104+
unsigned long now = millis();
83105

84-
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
85-
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
86-
// combine the four bytes (two words) into a long integer
87-
// this is NTP time (seconds since Jan 1 1900):
88-
unsigned long secsSince1900 = highWord << 16 | lowWord;
106+
if ((now - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
107+
|| this->_lastUpdate == 0
108+
|| now - _lastRequest > _retryInterval) { // Update if there was no response to the request
89109

90-
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
110+
// setup the UDP client if needed
111+
if (!this->_udpSetup) {
112+
this->begin();
113+
}
91114

92-
return true;
93-
}
115+
this->sendNTPPacket();
116+
}
94117

95-
bool NTPClient::update() {
96-
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
97-
|| this->_lastUpdate == 0) { // Update if there was no update yet.
98-
if (!this->_udpSetup) this->begin(); // setup the UDP client if needed
99-
return this->forceUpdate();
118+
if (_lastRequest > _lastUpdate) {
119+
updated = checkResponse();
100120
}
101-
return true;
121+
122+
return updated;
102123
}
103124

104125
unsigned long NTPClient::getEpochTime() {
@@ -168,4 +189,7 @@ void NTPClient::sendNTPPacket() {
168189
this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
169190
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
170191
this->_udp->endPacket();
192+
193+
this->_lastRequest = millis();
194+
171195
}

NTPClient.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ class NTPClient {
1818
int _timeOffset = 0;
1919

2020
unsigned int _updateInterval = 60000; // In ms
21+
unsigned int _retryInterval = 1000; // In ms
2122

2223
unsigned long _currentEpoc = 0; // In s
2324
unsigned long _lastUpdate = 0; // In ms
25+
unsigned long _lastRequest = 0; // IN ms
2426

2527
byte _packetBuffer[NTP_PACKET_SIZE];
2628

2729
void sendNTPPacket();
30+
bool checkResponse();
2831

2932
public:
3033
NTPClient(UDP& udp);
@@ -53,7 +56,7 @@ class NTPClient {
5356

5457
/**
5558
* This will force the update from the NTP Server.
56-
*
59+
* This can block for a full second
5760
* @return true on success, false on failure
5861
*/
5962
bool forceUpdate();

0 commit comments

Comments
 (0)