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.
This commit is contained in:
parent
1d64f94a8a
commit
e4d04dd53c
@ -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{
|
||||
|
@ -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
|
||||
*/
|
||||
|
75
examples/HiRes/HiRes.ino
Normal file
75
examples/HiRes/HiRes.ino
Normal file
@ -0,0 +1,75 @@
|
||||
#include <NTPClient.h>
|
||||
// change next line to use with another board/shield
|
||||
#include <ESP8266WiFi.h>
|
||||
//#include <WiFi.h>
|
||||
//#include <WiFi101.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
const char *ssid = "<SSID>";
|
||||
const char *password = "<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);
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
// change next line to use with another board/shield
|
||||
#include <WiFi.h>
|
||||
//#include <ESP8266WiFi.h>
|
||||
//#include <WiFi.h>
|
||||
//#include <WiFi101.h>
|
||||
#include <WiFiUdp.h>
|
||||
#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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user