Skip to content

Commit 1c77e16

Browse files
WhymustIhaveanametickelton
authored andcommitted
change a lots
1 parent 367254d commit 1c77e16

File tree

2 files changed

+133
-27
lines changed

2 files changed

+133
-27
lines changed

NTPClient.cpp

+103-24
Original file line numberDiff line numberDiff line change
@@ -82,48 +82,112 @@ void NTPClient::begin(unsigned int port) {
8282
}
8383

8484
bool NTPClient::forceUpdate() {
85-
#ifdef DEBUG_NTPClient
86-
Serial.println("Update from NTP Server");
87-
#endif
88-
8985
// flush any existing packets
9086
while(this->_udp->parsePacket() != 0)
9187
this->_udp->flush();
9288

89+
uint32_t tik,tok; //tik,tok to record wait time, replace timeout
9390
this->sendNTPPacket();
91+
tik=millis();
92+
#ifdef DEBUG_NTPClient
93+
Serial.println("sent ntp packet");
94+
#endif
9495

9596
// Wait till data is there or timeout...
96-
byte timeout = 0;
97-
int cb = 0;
97+
uint16_t cb = 0;
9898
do {
99-
delay ( 10 );
99+
delay (1); //poll more frequently to get high accuarcy
100100
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+
}
103105
} 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
106114

107115
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
111118
// combine the four bytes (two words) into a long integer
112119
// 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+
}
114161

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
116167

117168
return true; // return true after successful update
118169
}
119170

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+
}
125189
}
126-
return false; // return false if update does not occur
190+
return 2; // return 2 if update does not occur
127191
}
128192

129193
bool NTPClient::isTimeSet() const {
@@ -133,7 +197,13 @@ bool NTPClient::isTimeSet() const {
133197
unsigned long NTPClient::getEpochTime() const {
134198
return this->_timeOffset + // User offset
135199
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;
137207
}
138208

139209
int NTPClient::getDay() const {
@@ -178,7 +248,16 @@ void NTPClient::setUpdateInterval(unsigned long updateInterval) {
178248
}
179249

180250
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;
182261
}
183262

184263
void NTPClient::sendNTPPacket() {

NTPClient.h

+30-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define SEVENZYYEARS 2208988800UL
88
#define NTP_PACKET_SIZE 48
99
#define NTP_DEFAULT_LOCAL_PORT 1337
10+
#define DEBUG_NTPClient
1011

1112
class NTPClient {
1213
private:
@@ -21,7 +22,11 @@ class NTPClient {
2122
unsigned long _updateInterval = 60000; // In ms
2223

2324
unsigned long _currentEpoc = 0; // In s
24-
unsigned long _lastUpdate = 0; // In ms
25+
float _current_epoc_dec = 0; // In s, decimal part of current epoc
26+
uint32_t _lastUpdate = 0xff000000;// In ms
27+
28+
uint16_t _ntp_timeout = 1000; // In ms
29+
uint32_t _last_fail = 0xffff0000; // In ms
2530

2631
byte _packetBuffer[NTP_PACKET_SIZE];
2732

@@ -49,6 +54,20 @@ class NTPClient {
4954
*/
5055
void setRandomPort(unsigned int minValue = 49152, unsigned int maxValue = 65535);
5156

57+
/**
58+
* clear time server and set ip
59+
*
60+
* @param ServerIP
61+
*/
62+
void setPoolServerIP(IPAddress server_ip);
63+
64+
/**
65+
* Set ntp timeout, recommand not above 1000ms
66+
*
67+
* @param t_ms
68+
*/
69+
void setTimeout(uint16_t t_ms);
70+
5271
/**
5372
* Starts the underlying UDP client with the default local port
5473
*/
@@ -63,9 +82,12 @@ class NTPClient {
6382
* This should be called in the main loop of your application. By default an update from the NTP Server is only
6483
* made every 60 seconds. This can be configured in the NTPClient constructor.
6584
*
66-
* @return true on success, false on failure
85+
* @return 1(true) on updated and success
86+
* 0(false) on updated and failure
87+
* 2 on not time to update
88+
* 3 on it's time to update but last failed was just happen, so it decided to wait more
6789
*/
68-
bool update();
90+
int8_t update();
6991

7092
/**
7193
* This will force the update from the NTP Server.
@@ -107,6 +129,11 @@ class NTPClient {
107129
*/
108130
unsigned long getEpochTime() const;
109131

132+
/**
133+
* @return ms of this second, in ms
134+
*/
135+
float get_millis() const;
136+
110137
/**
111138
* Stops the underlying UDP client
112139
*/

0 commit comments

Comments
 (0)