From e4d04dd53cb3f07eed06fb785c35d0e398692540 Mon Sep 17 00:00:00 2001 From: tickelton Date: Wed, 26 May 2021 21:07:45 +0200 Subject: [PATCH] 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. --- NTPClient.cpp | 14 +++--- NTPClient.h | 2 +- examples/HiRes/HiRes.ino | 75 ++++++++++++++++++++++++++++++++ examples/ntp_demo.ino | 94 ---------------------------------------- 4 files changed, 85 insertions(+), 100 deletions(-) create mode 100644 examples/HiRes/HiRes.ino delete mode 100644 examples/ntp_demo.ino diff --git a/NTPClient.cpp b/NTPClient.cpp index 2d5b2c2..7c3c0f2 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -82,15 +82,19 @@ void NTPClient::begin(unsigned int port) { } bool NTPClient::forceUpdate() { + #ifdef DEBUG_NTPClient + Serial.println("Update from NTP Server"); + #endif + // flush any existing packets while(this->_udp->parsePacket() != 0) this->_udp->flush(); - uint32_t tik,tok; //tik,tok to record wait time, replace timeout + uint32_t tik,tok; //tik,tok to record wait time this->sendNTPPacket(); tik=millis(); #ifdef DEBUG_NTPClient - Serial.println("sent ntp packet"); + Serial.println("Sent ntp packet"); #endif // Wait till data is there or timeout... @@ -143,14 +147,14 @@ bool NTPClient::forceUpdate() { Serial.print(high_word,HEX);Serial.print(", "); Serial.println(transmit_dec,6); #endif - + float ping_delay; ping_delay=(tok-tik)/1000.0-(transmit_int-receive_int)-(transmit_dec-receive_dec); ping_delay/=2.0; if(ping_delay<=0){ Serial.println("ERROR: ping_delay < 0.0!"); } - + this->_lastUpdate=tok; this->_currentEpoc=transmit_int - SEVENZYYEARS ; this->_current_epoc_dec=ping_delay+transmit_dec; @@ -179,7 +183,7 @@ int8_t NTPClient::update() { } } }else{ //if overflowed - if(now+0xffffffff-this->_lastUpdate >= this->_updateInterval){ + if(now+0xffffffff-this->_lastUpdate >= this->_updateInterval){ if(now+0xffffffff-this->_last_fail >= 1500){ return this->forceUpdate(); }else{ diff --git a/NTPClient.h b/NTPClient.h index 608b4fc..72af433 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -62,7 +62,7 @@ class NTPClient { void setPoolServerIP(IPAddress server_ip); /** - * Set ntp timeout, recommand not above 1000ms + * Set ntp timeout (recommendation < 1000ms) * * @param t_ms */ diff --git a/examples/HiRes/HiRes.ino b/examples/HiRes/HiRes.ino new file mode 100644 index 0000000..73925c2 --- /dev/null +++ b/examples/HiRes/HiRes.ino @@ -0,0 +1,75 @@ +#include +// change next line to use with another board/shield +#include +//#include +//#include +#include + +const char *ssid = ""; +const char *password = ""; + +WiFiUDP ntpUDP; + +// You can specify the time server pool and the offset (in seconds, can be +// changed later with setTimeOffset() ). Additionally you can specify the +// update interval (in milliseconds, can be changed using setUpdateInterval() ). +NTPClient my_time_client(ntpUDP, "europe.pool.ntp.org", 28800, 20000); + +inline void sync_time(){ + my_time_client.begin(); + //smaller timeout will give you more accuracy + //but also larger possibility to fail + my_time_client.setTimeout(800); + Serial.println("syncing..."); + + while(my_time_client.update()!=1){ + delay(2000); + my_time_client.forceUpdate(); + } + + Serial.print("success: "); + Serial.println(my_time_client.getFormattedTime()); +} + +void setup(){ + Serial.begin(115200); + + WiFi.begin(ssid, password); + while ( WiFi.status() != WL_CONNECTED ) { + delay ( 500 ); + Serial.print ( "." ); + } + + sync_time(); +} + +String s_last_time="s_last_time"; + +void loop(){ + String s_time=my_time_client.getFormattedTime(); + if(s_time!=s_last_time){ + Serial.print("a second passed "); + Serial.print(s_time);Serial.print(" "); + Serial.print(my_time_client.get_millis(),3); + Serial.println("ms"); + s_last_time=s_time; + + //please do not update too frequently + int8_t re=my_time_client.update(); + if(re==0){ + Serial.println("0: sync but failed"); + delay(500); + }else if(re==1){ + Serial.println("1: sync and suc"); + }else if(re==2){ + ;//Serial.println("2: not time to sync"); + }else if(re==3){ + Serial.println("3: last failed was just happen"); + }else{ + Serial.print("return value error: "); + Serial.println(re); + } + } + + delay(1); +} diff --git a/examples/ntp_demo.ino b/examples/ntp_demo.ino deleted file mode 100644 index 08568a6..0000000 --- a/examples/ntp_demo.ino +++ /dev/null @@ -1,94 +0,0 @@ -// change next line to use with another board/shield -#include -//#include -//#include -//#include -#include -#include "NTPClient.h" - -WiFiUDP ntpUDP; -NTPClient my_time_client(ntpUDP); - -void connect_wifi(char * ssid,char * password){ - Serial.print("wifi connecting..."); - WiFi.begin(ssid,password); - while(WiFi.status()!= WL_CONNECTED){ - delay(1000); - Serial.print("."); - } - - Serial.println("\nwifi connected!"); - Serial.print("ip: "); - Serial.println(WiFi.localIP()); - Serial.print("netmask: "); - Serial.println(WiFi.subnetMask()); - Serial.print("gateway: "); - Serial.println(WiFi.gatewayIP()); - Serial.print("channel: "); - Serial.println(WiFi.channel()); - Serial.print("auto-reconnect: "); - Serial.println(WiFi.getAutoReconnect()); -} - -inline void sync_time(){ - my_time_client.begin(); - my_time_client.setTimeOffset(28800); - my_time_client.setUpdateInterval(20000); - //my_time_client.setPoolServerName("0.ubuntu.pool.ntp.org"); - my_time_client.setPoolServerName("192.168.1.200"); - //smaller timeout will give you more accuracy - //but also larger possibility to fail - my_time_client.setTimeout(800); - Serial.println("syncing..."); - - while(my_time_client.update()!=1){ - delay(2000); - my_time_client.forceUpdate(); - } - - Serial.print("success: "); - Serial.println(my_time_client.getFormattedTime()); -} - -void setup(){ - Serial.begin(230400); - Serial.println("serial inited"); - - connect_wifi((char *)"ssid",(char *)"pwd"); - sync_time(); - - Serial.println("ready!"); -} - -String s_last_time="s_last_time"; - -void loop(){ - String s_time=my_time_client.getFormattedTime(); - if(s_time!=s_last_time){ - Serial.print("a second passed "); - Serial.print(s_time);Serial.print(" "); - Serial.print(my_time_client.get_millis(),3); - Serial.println("ms"); - s_last_time=s_time; - - //please do not update too frequently - int8_t re=my_time_client.update(); - if(re==0){ - Serial.println("0: sync but failed"); - delay(500); - }else if(re==1){ - Serial.println("1: sync and suc"); - }else if(re==2){ - ;//Serial.println("2: not time to sync"); - }else if(re==3){ - Serial.println("3: last failed was just happen"); - }else{ - Serial.print("return value error: "); - Serial.println(re); - } - } - - //float ms=my_time_client.get_millis(); - - delay(1); -} \ No newline at end of file