Skip to content

Commit a225c1c

Browse files
authored
Update NTPClient.cpp
Until now the fraction of the second was not taken into account. Therefore the resulting time offset is up to 1000 ms. Using the provided Information increases the accuracy of the synchronization to 20-30 milliseconds. And it requires just two lines of code. https://imgur.com/a/aVjvHON In this plot both methods are compared. Plotted is the time difference in respect to a PPS signal provided by a GPS receiver. For comparison the time was synchronized over 1000 times and the resulting time difference filled into the respective histogram.
1 parent d7d8b45 commit a225c1c

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

NTPClient.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ bool NTPClient::forceUpdate() {
114114

115115
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
116116

117+
// get also the most significant 16 bits of the fraction of the second
118+
unsigned long fracWord = word(this->_packetBuffer[44], this->_packetBuffer[45]);
119+
120+
// Account for fraction of the second.
121+
this->_lastUpdate -= (fracWord/66); // should be devided by 2^16/1000 = 65.536 but 66 is close enough
122+
117123
return true; // return true after successful update
118124
}
119125

@@ -207,4 +213,4 @@ void NTPClient::sendNTPPacket() {
207213
void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
208214
randomSeed(analogRead(0));
209215
this->_port = random(minValue, maxValue);
210-
}
216+
}

0 commit comments

Comments
 (0)