-
Notifications
You must be signed in to change notification settings - Fork 383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable fractional second timestamps (re-integration of #102) #146
Open
tickelton
wants to merge
4
commits into
arduino-libraries:master
Choose a base branch
from
tickelton:hires-timestamp_reintegrate-pr-102
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure you need this logic.
millis() - this->_lastUpdate
should always give a positive integer by the power of integer arithmetic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
millis() rollover (depending on mcu) every ~49 days