@@ -82,48 +82,112 @@ void NTPClient::begin(unsigned int port) {
82
82
}
83
83
84
84
bool NTPClient::forceUpdate () {
85
- #ifdef DEBUG_NTPClient
86
- Serial.println (" Update from NTP Server" );
87
- #endif
88
-
89
85
// flush any existing packets
90
86
while (this ->_udp ->parsePacket () != 0 )
91
87
this ->_udp ->flush ();
92
88
89
+ uint32_t tik,tok; // tik,tok to record wait time, replace timeout
93
90
this ->sendNTPPacket ();
91
+ tik=millis ();
92
+ #ifdef DEBUG_NTPClient
93
+ Serial.println (" sent ntp packet" );
94
+ #endif
94
95
95
96
// Wait till data is there or timeout...
96
- byte timeout = 0 ;
97
- int cb = 0 ;
97
+ uint16_t cb = 0 ;
98
98
do {
99
- delay ( 10 );
99
+ delay (1 ); // poll more frequently to get high accuarcy
100
100
cb = this ->_udp ->parsePacket ();
101
- if (timeout > 100 ) return false ; // timeout after 1000 ms
102
- timeout++;
101
+ if ((millis ()-tik)>this ->_ntp_timeout ){
102
+ this ->_last_fail =millis ();
103
+ return false ;
104
+ }
103
105
} while (cb == 0 );
104
-
105
- this ->_lastUpdate = millis () - (10 * (timeout + 1 )); // Account for delay in reading the time
106
+ tok=millis ();
107
+ #ifdef DEBUG_NTPClient
108
+ Serial.println (" got ntp packet." );
109
+ Serial.print (" tik, tok, (tok-tik)/2: " );
110
+ Serial.print (tik);Serial.print (" , " );
111
+ Serial.print (tok);Serial.print (" , " );
112
+ Serial.println ((tok-tik)/2.0 );
113
+ #endif
106
114
107
115
this ->_udp ->read (this ->_packetBuffer , NTP_PACKET_SIZE);
108
-
109
- unsigned long highWord = word (this ->_packetBuffer [40 ], this ->_packetBuffer [41 ]);
110
- unsigned long lowWord = word (this ->_packetBuffer [42 ], this ->_packetBuffer [43 ]);
116
+ uint32_t high_word,low_word;
117
+ uint32_t receive_int,transmit_int; // integer part
111
118
// combine the four bytes (two words) into a long integer
112
119
// this is NTP time (seconds since Jan 1 1900):
113
- unsigned long secsSince1900 = highWord << 16 | lowWord;
120
+ high_word=word (this ->_packetBuffer [32 ],this ->_packetBuffer [33 ]);
121
+ low_word=word (this ->_packetBuffer [34 ],this ->_packetBuffer [35 ]);
122
+ receive_int=(high_word<<16 ) | low_word;
123
+ high_word=word (this ->_packetBuffer [40 ],this ->_packetBuffer [41 ]);
124
+ low_word=word (this ->_packetBuffer [42 ],this ->_packetBuffer [43 ]);
125
+ transmit_int=(high_word<<16 ) | low_word;
126
+ #ifdef DEBUG_NTPClient
127
+ Serial.print (" receive_int, transmit_int: " );
128
+ Serial.print (receive_int);Serial.print (" , " );
129
+ Serial.println (transmit_int);
130
+ #endif
131
+
132
+ float receive_dec=0 ,transmit_dec=0 ; // decimal part
133
+ high_word=word (this ->_packetBuffer [36 ],this ->_packetBuffer [37 ]);
134
+ receive_dec=high_word/65536.0 ;
135
+ #ifdef DEBUG_NTPClient
136
+ Serial.print (" receive_dec, transmit_dec: " );
137
+ Serial.print (high_word,HEX);Serial.print (" , " );
138
+ Serial.print (receive_dec,6 );Serial.print (" , " );
139
+ #endif
140
+ high_word=word (this ->_packetBuffer [44 ],this ->_packetBuffer [45 ]);
141
+ transmit_dec=high_word/65536.0 ;
142
+ #ifdef DEBUG_NTPClient
143
+ Serial.print (high_word,HEX);Serial.print (" , " );
144
+ Serial.println (transmit_dec,6 );
145
+ #endif
146
+
147
+ float ping_delay;
148
+ ping_delay=(tok-tik)/1000.0 -(transmit_int-receive_int)-(transmit_dec-receive_dec);
149
+ ping_delay/=2.0 ;
150
+ if (ping_delay<=0 ){
151
+ Serial.println (" ERROR: ping_delay < 0.0!" );
152
+ }
153
+
154
+ this ->_lastUpdate =tok;
155
+ this ->_currentEpoc =transmit_int - SEVENZYYEARS ;
156
+ this ->_current_epoc_dec =ping_delay+transmit_dec;
157
+ if (this ->_current_epoc_dec >1 ){
158
+ this ->_currentEpoc +=(int )this ->_current_epoc_dec ;
159
+ this ->_current_epoc_dec -=(int )this ->_current_epoc_dec ;
160
+ }
114
161
115
- this ->_currentEpoc = secsSince1900 - SEVENZYYEARS;
162
+ #ifdef DEBUG_NTPClient
163
+ Serial.print (" current Epoc: " );
164
+ Serial.print (this ->_currentEpoc );Serial.print (" " );
165
+ Serial.println (this ->_current_epoc_dec ,6 );
166
+ #endif
116
167
117
168
return true ; // return true after successful update
118
169
}
119
170
120
- bool NTPClient::update () {
121
- if ((millis () - this ->_lastUpdate >= this ->_updateInterval ) // Update after _updateInterval
122
- || this ->_lastUpdate == 0 ) { // Update if there was no update yet.
123
- if (!this ->_udpSetup || this ->_port != NTP_DEFAULT_LOCAL_PORT) this ->begin (this ->_port ); // setup the UDP client if needed
124
- return this ->forceUpdate ();
171
+ int8_t NTPClient::update () {
172
+ uint32_t now=millis ();
173
+ if (now>=this ->_lastUpdate ){ // if not overflow
174
+ if (now-this ->_lastUpdate >=this ->_updateInterval ){
175
+ if (now-this ->_last_fail >= 1500 ){
176
+ return this ->forceUpdate ();
177
+ }else {
178
+ return 3 ; // return 3 if last failed was just happen
179
+ }
180
+ }
181
+ }else { // if overflowed
182
+ if (now+0xffffffff -this ->_lastUpdate >= this ->_updateInterval ){
183
+ if (now+0xffffffff -this ->_last_fail >= 1500 ){
184
+ return this ->forceUpdate ();
185
+ }else {
186
+ return 3 ;
187
+ }
188
+ }
125
189
}
126
- return false ; // return false if update does not occur
190
+ return 2 ; // return 2 if update does not occur
127
191
}
128
192
129
193
bool NTPClient::isTimeSet () const {
@@ -133,7 +197,13 @@ bool NTPClient::isTimeSet() const {
133
197
unsigned long NTPClient::getEpochTime () const {
134
198
return this ->_timeOffset + // User offset
135
199
this ->_currentEpoc + // Epoch returned by the NTP server
136
- ((millis () - this ->_lastUpdate ) / 1000 ); // Time since last update
200
+ ((millis () - this ->_lastUpdate + (int )(this ->_current_epoc_dec *1000 )) / 1000 ); // Time since last update
201
+ }
202
+
203
+ float NTPClient::get_millis () const {
204
+ float ms = millis () - this ->_lastUpdate + this ->_current_epoc_dec *1000.0 ;
205
+ ms-=(int )(ms/1000 )*1000 ;
206
+ return ms;
137
207
}
138
208
139
209
int NTPClient::getDay () const {
@@ -178,7 +248,16 @@ void NTPClient::setUpdateInterval(unsigned long updateInterval) {
178
248
}
179
249
180
250
void NTPClient::setPoolServerName (const char * poolServerName) {
181
- this ->_poolServerName = poolServerName;
251
+ this ->_poolServerName = poolServerName;
252
+ }
253
+
254
+ void NTPClient::setPoolServerIP (IPAddress server_ip){
255
+ this ->_poolServerIP = server_ip;
256
+ this ->_poolServerName = NULL ;
257
+ }
258
+
259
+ void NTPClient::setTimeout (uint16_t t_ms){
260
+ this ->_ntp_timeout =t_ms;
182
261
}
183
262
184
263
void NTPClient::sendNTPPacket () {
0 commit comments