Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/wifintp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ it is not already running. Using this method, the above code becomes:
void setClock() {
NTP.begin("pool.ntp.org", "time.nist.gov");
NTP.waitSet();
time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}

bool NTP.waitSet(void (\*cb)(), uint32_t timeout)
-------------------------------------------------
Allows for a callback that will be called every 1/10th of a second while waiting for
NTP sync. For example, using lambdas you can simply print "."s:"

.. code :: cpp

void setClock() {
NTP.begin("pool.ntp.org", "time.nist.gov");
NTP.waitSet([]() { Serial.print("."); });
time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ void setClock() {
NTP.begin("pool.ntp.org", "time.nist.gov");

Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
NTP.waitSet([]() {
Serial.print(".");
now = time(nullptr);
}
});
Serial.println("");

time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,10 @@ void setClock() {
NTP.begin("pool.ntp.org", "time.nist.gov");

Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
now = time(nullptr);
}
NTP.waitSet([]() { Serial.print("."); } );
Serial.println("");

time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Expand Down
9 changes: 4 additions & 5 deletions libraries/WiFi/examples/BearSSL_Sessions/BearSSL_Sessions.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ void setup() {
NTP.begin("pool.ntp.org", "time.nist.gov");

Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
NTP.waitSet([]() {
Serial.print(".");
now = time(nullptr);
}
});
Serial.println("");

time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ const char *path = "/";

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

Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
NTP.waitSet([]() {
Serial.print(".");
now = time(nullptr);
}
});
Serial.println("");
time_t now = time(nullptr);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Expand Down
9 changes: 8 additions & 1 deletion libraries/WiFi/src/WiFiNTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,19 @@ class NTPClass {
}

bool waitSet(uint32_t timeout = 10000) {
return waitSet(nullptr, timeout);
}

bool waitSet(void (*cb)(), uint32_t timeout = 10000) {
if (!running()) {
begin("pool.ntp.org");
}
uint32_t start = millis();
while ((time(nullptr) < 10000000) && (millis() - start < timeout)) {
delay(10);
delay(100);
if (cb) {
cb();
}
}
return time(nullptr) < 10000000;
}
Expand Down