@@ -64,8 +64,9 @@ static time_t now;
64
64
static uint32_t now_ms, now_us;
65
65
66
66
static esp8266::polledTimeout::periodicMs showTimeNow (60000 );
67
- static int time_machine_days = 0 ; // 0 = now
67
+ static int time_machine_days = 0 ; // 0 = present
68
68
static bool time_machine_running = false ;
69
+ static bool time_machine_run_once = false ;
69
70
70
71
// OPTIONAL: change SNTP startup delay
71
72
// a weak function is already defined and returns 0 (RFC violation)
@@ -112,7 +113,7 @@ void showTime() {
112
113
// time from boot
113
114
Serial.print (" clock: " );
114
115
Serial.print ((uint32_t )tp.tv_sec );
115
- Serial.print (" s / " );
116
+ Serial.print (" s + " );
116
117
Serial.print ((uint32_t )tp.tv_nsec );
117
118
Serial.println (" ns" );
118
119
@@ -125,7 +126,7 @@ void showTime() {
125
126
// EPOCH+tz+dst
126
127
Serial.print (" gtod: " );
127
128
Serial.print ((uint32_t )tv.tv_sec );
128
- Serial.print (" s / " );
129
+ Serial.print (" s + " );
129
130
Serial.print ((uint32_t )tv.tv_usec );
130
131
Serial.println (" us" );
131
132
@@ -140,7 +141,7 @@ void showTime() {
140
141
Serial.print (" ctime: " );
141
142
Serial.print (ctime (&now));
142
143
143
- // LwIP v2 is able to list more details about the currently configured SNTP servers
144
+ // lwIP v2 is able to list more details about the currently configured SNTP servers
144
145
for (int i = 0 ; i < SNTP_MAX_SERVERS; i++) {
145
146
IPAddress sntp = *sntp_getserver (i);
146
147
const char * name = sntp_getservername (i);
@@ -151,48 +152,67 @@ void showTime() {
151
152
} else {
152
153
Serial.printf (" %s " , sntp.toString ().c_str ());
153
154
}
154
- Serial.printf (" IPv6: %s Reachability: %o\n " ,
155
+ Serial.printf (" - IPv6: %s - Reachability: %o\n " ,
155
156
sntp.isV6 () ? " Yes" : " No" ,
156
157
sntp_getreachability (i));
157
158
}
158
159
}
159
160
160
161
Serial.println ();
161
162
162
- // subsecond synchronisation
163
- gettimeofday (&tv, nullptr );
164
- time_t sec = tv.tv_sec ;
165
- do {
163
+ // show subsecond synchronisation
164
+ timeval prevtv;
165
+ time_t prevtime = time (nullptr );
166
+ gettimeofday (&prevtv, nullptr );
167
+
168
+ while (true ) {
166
169
gettimeofday (&tv, nullptr );
167
- Serial.printf (" time(): %u gettimeofday(): %u.%06u" ,
168
- (uint32_t )time (nullptr ),
169
- (uint32_t )tv.tv_sec , (uint32_t )tv.tv_usec );
170
- if (tv.tv_sec == sec) {
171
- Serial.println (" second unchanged" );
172
- } else {
173
- Serial.println (" <-- second changed" );
170
+ if (tv.tv_sec != prevtv.tv_sec ) {
171
+ Serial.printf (" time(): %u gettimeofday(): %u.%06u seconds are unchanged\n " ,
172
+ (uint32_t )prevtime,
173
+ (uint32_t )prevtv.tv_sec , (uint32_t )prevtv.tv_usec );
174
+ Serial.printf (" time(): %u gettimeofday(): %u.%06u <-- seconds have changed\n " ,
175
+ (uint32_t )(prevtime = time (nullptr )),
176
+ (uint32_t )tv.tv_sec , (uint32_t )tv.tv_usec );
177
+ break ;
174
178
}
179
+ prevtv = tv;
175
180
delay (50 );
176
- } while (tv. tv_sec == sec);
181
+ }
177
182
178
183
Serial.println ();
179
184
}
180
185
181
- void time_is_set_scheduled () {
182
- // everything is allowed in this function
186
+ void time_is_set (bool from_sntp /* <= this parameter is optional */ ) {
187
+ // in CONT stack, unlike ISRs,
188
+ // any function is allowed in this callback
183
189
184
190
if (time_machine_days == 0 ) {
185
- time_machine_running = !time_machine_running;
191
+ if (time_machine_running) {
192
+ time_machine_run_once = true ;
193
+ time_machine_running = false ;
194
+ } else {
195
+ time_machine_running = from_sntp && !time_machine_run_once;
196
+ }
197
+ if (time_machine_running) {
198
+ Serial.printf (" \n -- \n -- Starting time machine demo to show libc's "
199
+ " automatic DST handling\n -- \n " );
200
+ }
201
+ }
202
+
203
+ Serial.print (" settimeofday(" );
204
+ if (from_sntp) {
205
+ Serial.print (" SNTP" );
206
+ } else {
207
+ Serial.print (" USER" );
186
208
}
209
+ Serial.print (" )" );
187
210
188
211
// time machine demo
189
212
if (time_machine_running) {
190
- if (time_machine_days == 0 )
191
- Serial.printf (" ---- settimeofday() has been called - possibly from SNTP\n "
192
- " (starting time machine demo to show libc's automatic DST handling)\n\n " );
193
213
now = time (nullptr );
194
214
const tm * tm = localtime (&now);
195
- Serial.printf (" future=%3ddays: DST=%s - " ,
215
+ Serial.printf (" : future=%3ddays: DST=%s - " ,
196
216
time_machine_days,
197
217
tm ->tm_isdst ? " true " : " false" );
198
218
Serial.print (ctime (&now));
@@ -207,49 +227,55 @@ void time_is_set_scheduled() {
207
227
}
208
228
settimeofday (&tv, nullptr );
209
229
} else {
210
- showTime ();
230
+ Serial. println ();
211
231
}
212
232
}
213
233
214
234
void setup () {
235
+ WiFi.persistent (false );
236
+ WiFi.mode (WIFI_OFF);
237
+
215
238
Serial.begin (115200 );
216
- Serial.println (" \n Starting...\n " );
239
+ Serial.println (" \n Starting in 2secs...\n " );
240
+ delay (2000 );
241
+
242
+ // install callback - called when settimeofday is called (by SNTP or user)
243
+ // once enabled (by DHCP), SNTP is updated every hour by default
244
+ // ** optional boolean in callback function is true when triggerred by SNTP **
245
+ settimeofday_cb (time_is_set);
217
246
218
247
// setup RTC time
219
248
// it will be used until NTP server will send us real current time
249
+ Serial.println (" Manually setting some time from some RTC:" );
220
250
time_t rtc = RTC_UTC_TEST;
221
251
timeval tv = { rtc, 0 };
222
252
settimeofday (&tv, nullptr );
223
253
224
- // install callback - called when settimeofday is called (by SNTP or us)
225
- // once enabled (by DHCP), SNTP is updated every hour
226
- settimeofday_cb (time_is_set_scheduled);
227
-
228
254
// NTP servers may be overriden by your DHCP server for a more local one
229
255
// (see below)
230
256
231
257
// ----> Here is the ONLY ONE LINE needed in your sketch
232
-
233
258
configTime (MYTZ, " pool.ntp.org" );
259
+ // <----
260
+ // Replace MYTZ by a value from TZ.h (search for this file in your filesystem).
234
261
235
- // Here is the ONLY ONE LINE needed in your sketch <----
236
- // pick a value from TZ.h (search for this file in your filesystem) for MYTZ
237
-
238
- // former configTime is still valid, here is the call for 7 hours to the west
262
+ // Former configTime is still valid, here is the call for 7 hours to the west
239
263
// with an enabled 30mn DST
240
264
// configTime(7 * 3600, 3600 / 2, "pool.ntp.org");
241
265
242
266
// OPTIONAL: disable obtaining SNTP servers from DHCP
243
267
// sntp_servermode_dhcp(0); // 0: disable obtaining SNTP servers from DHCP (enabled by default)
244
268
269
+ // Give now a chance to the settimeofday callback,
270
+ // because it is *always* deferred to the next yield()/loop()-call.
271
+ yield ();
272
+
245
273
// start network
246
- WiFi.persistent (false );
247
274
WiFi.mode (WIFI_STA);
248
275
WiFi.begin (STASSID, STAPSK);
249
276
250
277
// don't wait for network, observe time changing
251
278
// when NTP timestamp is received
252
- Serial.printf (" Time is currently set by a constant:\n " );
253
279
showTime ();
254
280
}
255
281
0 commit comments