Skip to content

Commit e4d04dd

Browse files
committed
Adds support for fractional seconds
* Rebases WhymustIhaveaname:master to arduino-libraries:master. * Fixes issues with Spell Check action. * Fixes HiRes example compilation. * Refactors HiRes example to be more idiomatic as compared to the existing examples. * Fixes several line ending and white space inconsistencies.
1 parent 1d64f94 commit e4d04dd

File tree

4 files changed

+85
-100
lines changed

4 files changed

+85
-100
lines changed

Diff for: NTPClient.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@ 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+
8589
// flush any existing packets
8690
while(this->_udp->parsePacket() != 0)
8791
this->_udp->flush();
8892

89-
uint32_t tik,tok; //tik,tok to record wait time, replace timeout
93+
uint32_t tik,tok; //tik,tok to record wait time
9094
this->sendNTPPacket();
9195
tik=millis();
9296
#ifdef DEBUG_NTPClient
93-
Serial.println("sent ntp packet");
97+
Serial.println("Sent ntp packet");
9498
#endif
9599

96100
// Wait till data is there or timeout...
@@ -143,14 +147,14 @@ bool NTPClient::forceUpdate() {
143147
Serial.print(high_word,HEX);Serial.print(", ");
144148
Serial.println(transmit_dec,6);
145149
#endif
146-
150+
147151
float ping_delay;
148152
ping_delay=(tok-tik)/1000.0-(transmit_int-receive_int)-(transmit_dec-receive_dec);
149153
ping_delay/=2.0;
150154
if(ping_delay<=0){
151155
Serial.println("ERROR: ping_delay < 0.0!");
152156
}
153-
157+
154158
this->_lastUpdate=tok;
155159
this->_currentEpoc=transmit_int - SEVENZYYEARS ;
156160
this->_current_epoc_dec=ping_delay+transmit_dec;
@@ -179,7 +183,7 @@ int8_t NTPClient::update() {
179183
}
180184
}
181185
}else{ //if overflowed
182-
if(now+0xffffffff-this->_lastUpdate >= this->_updateInterval){
186+
if(now+0xffffffff-this->_lastUpdate >= this->_updateInterval){
183187
if(now+0xffffffff-this->_last_fail >= 1500){
184188
return this->forceUpdate();
185189
}else{

Diff for: NTPClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class NTPClient {
6262
void setPoolServerIP(IPAddress server_ip);
6363

6464
/**
65-
* Set ntp timeout, recommand not above 1000ms
65+
* Set ntp timeout (recommendation < 1000ms)
6666
*
6767
* @param t_ms
6868
*/

Diff for: examples/HiRes/HiRes.ino

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <NTPClient.h>
2+
// change next line to use with another board/shield
3+
#include <ESP8266WiFi.h>
4+
//#include <WiFi.h>
5+
//#include <WiFi101.h>
6+
#include <WiFiUdp.h>
7+
8+
const char *ssid = "<SSID>";
9+
const char *password = "<PASSWORD>";
10+
11+
WiFiUDP ntpUDP;
12+
13+
// You can specify the time server pool and the offset (in seconds, can be
14+
// changed later with setTimeOffset() ). Additionally you can specify the
15+
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
16+
NTPClient my_time_client(ntpUDP, "europe.pool.ntp.org", 28800, 20000);
17+
18+
inline void sync_time(){
19+
my_time_client.begin();
20+
//smaller timeout will give you more accuracy
21+
//but also larger possibility to fail
22+
my_time_client.setTimeout(800);
23+
Serial.println("syncing...");
24+
25+
while(my_time_client.update()!=1){
26+
delay(2000);
27+
my_time_client.forceUpdate();
28+
}
29+
30+
Serial.print("success: ");
31+
Serial.println(my_time_client.getFormattedTime());
32+
}
33+
34+
void setup(){
35+
Serial.begin(115200);
36+
37+
WiFi.begin(ssid, password);
38+
while ( WiFi.status() != WL_CONNECTED ) {
39+
delay ( 500 );
40+
Serial.print ( "." );
41+
}
42+
43+
sync_time();
44+
}
45+
46+
String s_last_time="s_last_time";
47+
48+
void loop(){
49+
String s_time=my_time_client.getFormattedTime();
50+
if(s_time!=s_last_time){
51+
Serial.print("a second passed ");
52+
Serial.print(s_time);Serial.print(" ");
53+
Serial.print(my_time_client.get_millis(),3);
54+
Serial.println("ms");
55+
s_last_time=s_time;
56+
57+
//please do not update too frequently
58+
int8_t re=my_time_client.update();
59+
if(re==0){
60+
Serial.println("0: sync but failed");
61+
delay(500);
62+
}else if(re==1){
63+
Serial.println("1: sync and suc");
64+
}else if(re==2){
65+
;//Serial.println("2: not time to sync");
66+
}else if(re==3){
67+
Serial.println("3: last failed was just happen");
68+
}else{
69+
Serial.print("return value error: ");
70+
Serial.println(re);
71+
}
72+
}
73+
74+
delay(1);
75+
}

Diff for: examples/ntp_demo.ino

-94
This file was deleted.

0 commit comments

Comments
 (0)