File tree Expand file tree Collapse file tree 6 files changed +41
-23
lines changed Expand file tree Collapse file tree 6 files changed +41
-23
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,24 @@ it is not already running. Using this method, the above code becomes:
51
51
void setClock() {
52
52
NTP.begin("pool.ntp.org", "time.nist.gov");
53
53
NTP.waitSet();
54
+ time_t now = time(nullptr);
55
+ struct tm timeinfo;
56
+ gmtime_r(&now, &timeinfo);
57
+ Serial.print("Current time: ");
58
+ Serial.print(asctime(&timeinfo));
59
+ }
60
+
61
+ bool NTP.waitSet(void (\* cb)(), uint32_t timeout)
62
+ -------------------------------------------------
63
+ Allows for a callback that will be called every 1/10th of a second while waiting for
64
+ NTP sync. For example, using lambdas you can simply print "."s:"
65
+
66
+ .. code :: cpp
67
+
68
+ void setClock() {
69
+ NTP.begin("pool.ntp.org", "time.nist.gov");
70
+ NTP.waitSet([]() { Serial.print("."); });
71
+ time_t now = time(nullptr);
54
72
struct tm timeinfo;
55
73
gmtime_r(&now, &timeinfo);
56
74
Serial.print("Current time: ");
Original file line number Diff line number Diff line change @@ -57,13 +57,12 @@ void setClock() {
57
57
NTP.begin (" pool.ntp.org" , " time.nist.gov" );
58
58
59
59
Serial.print (" Waiting for NTP time sync: " );
60
- time_t now = time (nullptr );
61
- while (now < 8 * 3600 * 2 ) {
62
- delay (500 );
60
+ NTP.waitSet ([]() {
63
61
Serial.print (" ." );
64
- now = time (nullptr );
65
- }
62
+ });
66
63
Serial.println (" " );
64
+
65
+ time_t now = time (nullptr );
67
66
struct tm timeinfo;
68
67
gmtime_r (&now, &timeinfo);
69
68
Serial.print (" Current time: " );
Original file line number Diff line number Diff line change @@ -164,13 +164,10 @@ void setClock() {
164
164
NTP.begin (" pool.ntp.org" , " time.nist.gov" );
165
165
166
166
Serial.print (" Waiting for NTP time sync: " );
167
- time_t now = time (nullptr );
168
- while (now < 8 * 3600 * 2 ) {
169
- delay (500 );
170
- Serial.print (" ." );
171
- now = time (nullptr );
172
- }
167
+ NTP.waitSet ([]() { Serial.print (" ." ); } );
173
168
Serial.println (" " );
169
+
170
+ time_t now = time (nullptr );
174
171
struct tm timeinfo;
175
172
gmtime_r (&now, &timeinfo);
176
173
Serial.print (" Current time: " );
Original file line number Diff line number Diff line change @@ -45,13 +45,12 @@ void setup() {
45
45
NTP.begin (" pool.ntp.org" , " time.nist.gov" );
46
46
47
47
Serial.print (" Waiting for NTP time sync: " );
48
- time_t now = time (nullptr );
49
- while (now < 8 * 3600 * 2 ) {
50
- delay (500 );
48
+ NTP.waitSet ([]() {
51
49
Serial.print (" ." );
52
- now = time (nullptr );
53
- }
50
+ });
54
51
Serial.println (" " );
52
+
53
+ time_t now = time (nullptr );
55
54
struct tm timeinfo;
56
55
gmtime_r (&now, &timeinfo);
57
56
Serial.print (" Current time: " );
Original file line number Diff line number Diff line change @@ -22,16 +22,14 @@ const char *path = "/";
22
22
23
23
// Set time via NTP, as required for x.509 validation
24
24
void setClock () {
25
- configTime ( 3 * 3600 , 0 , " pool.ntp.org" , " time.nist.gov" );
25
+ NTP. begin ( " pool.ntp.org" , " time.nist.gov" );
26
26
27
27
Serial.print (" Waiting for NTP time sync: " );
28
- time_t now = time (nullptr );
29
- while (now < 8 * 3600 * 2 ) {
30
- delay (500 );
28
+ NTP.waitSet ([]() {
31
29
Serial.print (" ." );
32
- now = time (nullptr );
33
- }
30
+ });
34
31
Serial.println (" " );
32
+ time_t now = time (nullptr );
35
33
struct tm timeinfo;
36
34
gmtime_r (&now, &timeinfo);
37
35
Serial.print (" Current time: " );
Original file line number Diff line number Diff line change @@ -77,12 +77,19 @@ class NTPClass {
77
77
}
78
78
79
79
bool waitSet (uint32_t timeout = 10000 ) {
80
+ return waitSet (nullptr , timeout);
81
+ }
82
+
83
+ bool waitSet (void (*cb)(), uint32_t timeout = 10000) {
80
84
if (!running ()) {
81
85
begin (" pool.ntp.org" );
82
86
}
83
87
uint32_t start = millis ();
84
88
while ((time (nullptr ) < 10000000 ) && (millis () - start < timeout)) {
85
- delay (10 );
89
+ delay (100 );
90
+ if (cb) {
91
+ cb ();
92
+ }
86
93
}
87
94
return time (nullptr ) < 10000000 ;
88
95
}
You can’t perform that action at this time.
0 commit comments