Skip to content

Commit c889a3c

Browse files
Add NTP waitSet callback (#705)
Allows printing "."s or flashing an LED while NTP is waiting for sync.
1 parent cfc05da commit c889a3c

File tree

6 files changed

+41
-23
lines changed

6 files changed

+41
-23
lines changed

docs/wifintp.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ it is not already running. Using this method, the above code becomes:
5151
void setClock() {
5252
NTP.begin("pool.ntp.org", "time.nist.gov");
5353
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);
5472
struct tm timeinfo;
5573
gmtime_r(&now, &timeinfo);
5674
Serial.print("Current time: ");

libraries/WiFi/examples/BearSSL_CertStore/BearSSL_CertStore.ino

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ void setClock() {
5757
NTP.begin("pool.ntp.org", "time.nist.gov");
5858

5959
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([]() {
6361
Serial.print(".");
64-
now = time(nullptr);
65-
}
62+
});
6663
Serial.println("");
64+
65+
time_t now = time(nullptr);
6766
struct tm timeinfo;
6867
gmtime_r(&now, &timeinfo);
6968
Serial.print("Current time: ");

libraries/WiFi/examples/BearSSL_ServerClientCert/BearSSL_ServerClientCert.ino

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,10 @@ void setClock() {
164164
NTP.begin("pool.ntp.org", "time.nist.gov");
165165

166166
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("."); } );
173168
Serial.println("");
169+
170+
time_t now = time(nullptr);
174171
struct tm timeinfo;
175172
gmtime_r(&now, &timeinfo);
176173
Serial.print("Current time: ");

libraries/WiFi/examples/BearSSL_Sessions/BearSSL_Sessions.ino

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ void setup() {
4545
NTP.begin("pool.ntp.org", "time.nist.gov");
4646

4747
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([]() {
5149
Serial.print(".");
52-
now = time(nullptr);
53-
}
50+
});
5451
Serial.println("");
52+
53+
time_t now = time(nullptr);
5554
struct tm timeinfo;
5655
gmtime_r(&now, &timeinfo);
5756
Serial.print("Current time: ");

libraries/WiFi/examples/BearSSL_Validation/BearSSL_Validation.ino

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ const char *path = "/";
2222

2323
// Set time via NTP, as required for x.509 validation
2424
void setClock() {
25-
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
25+
NTP.begin("pool.ntp.org", "time.nist.gov");
2626

2727
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([]() {
3129
Serial.print(".");
32-
now = time(nullptr);
33-
}
30+
});
3431
Serial.println("");
32+
time_t now = time(nullptr);
3533
struct tm timeinfo;
3634
gmtime_r(&now, &timeinfo);
3735
Serial.print("Current time: ");

libraries/WiFi/src/WiFiNTP.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,19 @@ class NTPClass {
7777
}
7878

7979
bool waitSet(uint32_t timeout = 10000) {
80+
return waitSet(nullptr, timeout);
81+
}
82+
83+
bool waitSet(void (*cb)(), uint32_t timeout = 10000) {
8084
if (!running()) {
8185
begin("pool.ntp.org");
8286
}
8387
uint32_t start = millis();
8488
while ((time(nullptr) < 10000000) && (millis() - start < timeout)) {
85-
delay(10);
89+
delay(100);
90+
if (cb) {
91+
cb();
92+
}
8693
}
8794
return time(nullptr) < 10000000;
8895
}

0 commit comments

Comments
 (0)