@@ -60,6 +60,25 @@ void NTPClient::begin(int port) {
60
60
this ->_udpSetup = true ;
61
61
}
62
62
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
+
63
82
bool NTPClient::forceUpdate () {
64
83
#ifdef DEBUG_NTPClient
65
84
Serial.println (" Update from NTP Server" );
@@ -69,36 +88,38 @@ bool NTPClient::forceUpdate() {
69
88
70
89
// Wait till data is there or timeout...
71
90
byte timeout = 0 ;
72
- int cb = 0 ;
91
+ bool cb = 0 ;
73
92
do {
74
93
delay ( 10 );
75
- cb = this ->_udp -> parsePacket ();
94
+ cb = this ->checkResponse ();
76
95
if (timeout > 100 ) return false ; // timeout after 1000 ms
77
96
timeout++;
78
- } while (cb == 0 );
97
+ } while (cb == false );
79
98
80
- this ->_lastUpdate = millis () - (10 * (timeout + 1 )); // Account for delay in reading the time
99
+ return true ;
100
+ }
81
101
82
- this ->_udp ->read (this ->_packetBuffer , NTP_PACKET_SIZE);
102
+ bool NTPClient::update () {
103
+ bool updated = false ;
104
+ unsigned long now = millis ();
83
105
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
89
109
90
- this ->_currentEpoc = secsSince1900 - SEVENZYYEARS;
110
+ // setup the UDP client if needed
111
+ if (!this ->_udpSetup ) {
112
+ this ->begin ();
113
+ }
91
114
92
- return true ;
93
- }
115
+ this -> sendNTPPacket () ;
116
+ }
94
117
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 ();
100
120
}
101
- return true ;
121
+
122
+ return updated;
102
123
}
103
124
104
125
unsigned long NTPClient::getEpochTime () {
@@ -168,4 +189,7 @@ void NTPClient::sendNTPPacket() {
168
189
this ->_udp ->beginPacket (this ->_poolServerName , 123 ); // NTP requests are to port 123
169
190
this ->_udp ->write (this ->_packetBuffer , NTP_PACKET_SIZE);
170
191
this ->_udp ->endPacket ();
192
+
193
+ this ->_lastRequest = millis ();
194
+
171
195
}
0 commit comments