Skip to content

Commit 479f290

Browse files
committed
Efficiency tweaks
- Reset _fracOffset only when we disable fractional tracking - When accounting for the packet reception time, take half the interval. Ideally we'd look at the flight time of the request/response, but for now let's assume it's half of however long we took. - Rework the timeout semantics to be slightly more efficient. - Add a mechanism to disambiguate between "NTP time was not received" vs "It was not time to ask"
1 parent 3fb7658 commit 479f290

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

NTPClient.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,18 @@ bool NTPClient::forceUpdate() {
9191
this->_udp->flush();
9292

9393
this->sendNTPPacket();
94+
this->_packetSent = true;
9495

9596
// Wait till data is there or timeout...
9697
byte timeout = 0;
9798
int cb = 0;
9899
do {
100+
if (timeout++ > 100) return false; // timeout after 1000 ms
99101
delay ( 10 );
100102
cb = this->_udp->parsePacket();
101-
if (timeout > 100) return false; // timeout after 1000 ms
102-
timeout++;
103103
} while (cb == 0);
104104

105-
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
105+
this->_lastUpdate = millis() - (5 * timeout); // Account for delay in reading the time
106106

107107
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
108108

@@ -112,6 +112,8 @@ bool NTPClient::forceUpdate() {
112112
// this is NTP time (seconds since Jan 1 1900):
113113
unsigned long secsSince1900 = highWord << 16 | lowWord;
114114

115+
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
116+
115117
if (this->_fractionalTime) {
116118
highWord = word(this->_packetBuffer[44], this->_packetBuffer[45]);
117119
lowWord = word(this->_packetBuffer[46], this->_packetBuffer[47]);
@@ -120,12 +122,8 @@ bool NTPClient::forceUpdate() {
120122

121123
// Convert the fractional part into ms
122124
this->_fracOffset = (unsigned long)(((double)fracSecs / 4294967295.0) * 1000.0);
123-
} else {
124-
this->_fracOffset = 0;
125125
}
126126

127-
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
128-
129127
return true; // return true after successful update
130128
}
131129

@@ -135,6 +133,7 @@ bool NTPClient::update() {
135133
if (!this->_udpSetup) this->begin(); // setup the UDP client if needed
136134
return this->forceUpdate();
137135
}
136+
this->_packetSent = false;
138137
return false; // return false if update does not occur
139138
}
140139

@@ -171,6 +170,14 @@ String NTPClient::getFormattedTime() const {
171170
return hoursStr + ":" + minuteStr + ":" + secondStr;
172171
}
173172

173+
unsigned long NTPClient::getFractionalOffset() const {
174+
return this->_fracOffset;
175+
}
176+
177+
bool NTPClient::getPacketSent() const {
178+
return this->_packetSent;
179+
}
180+
174181
void NTPClient::end() {
175182
this->_udp->stop();
176183

@@ -187,6 +194,9 @@ void NTPClient::setUpdateInterval(unsigned long updateInterval) {
187194

188195
void NTPClient::setFractionalTime(bool enable) {
189196
this->_fractionalTime = enable;
197+
if (!enable) {
198+
this->_fracOffset = 0;
199+
}
190200
}
191201

192202
void NTPClient::setPoolServerName(const char* poolServerName) {

NTPClient.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class NTPClient {
2525
unsigned long _lastUpdate = 0; // In ms
2626
unsigned long _fracOffset = 0; // In ms
2727

28+
bool _packetSent = false;
29+
2830
byte _packetBuffer[NTP_PACKET_SIZE];
2931

3032
void sendNTPPacket();
@@ -103,6 +105,17 @@ class NTPClient {
103105
*/
104106
unsigned long getEpochTime() const;
105107

108+
/** If we track the fractional part of the NTP resopnse, return the offset
109+
* in milliseconds.
110+
*/
111+
unsigned long getFractionalOffset() const;
112+
113+
/** Whether an NTP packet was send since the last time @c update was called.
114+
* Can be used to disambiguate whether the @c false return from @c update was
115+
* because it was not yet time, or because no response was received.
116+
*/
117+
bool getPacketSent() const;
118+
106119
/**
107120
* Stops the underlying UDP client
108121
*/

0 commit comments

Comments
 (0)