Layer: L7 Application ยท Build flags: PROTOCORE_ENABLE_NTP, PROTOCORE_ENABLE_TIME_SOURCE
What this example teaches
Real devices have more than one clock: NTP when the network is up, a battery RTC when it is not, maybe a GPS pulse. PROTOCORE_ENABLE_TIME_SOURCE lets you register several time sources with priorities and have the library pick the best available one automatically. This registers NTP (priority 0) and an RTC stand-in (priority 1); GET /time reports the epoch and which source supplied it. It composes the SNTP client from SNTP.
A source is a function returning epoch seconds, or 0 if it has no time.
static uint32_t src_ntp() { return protocore_ntp_synced() ? (uint32_t)protocore_ntp_epoch() : 0; }
static uint32_t src_rtc() { return RTC_BASE + (uint32_t)(millis() / 1000); }
Register by priority; lowest value wins. When NTP returns 0 (not synced yet) the library falls through to the RTC; once NTP syncs it transparently takes over:
protocore_ntp_begin();
protocore_time_source_add("ntp", 0, src_ntp);
protocore_time_source_add("rtc", 1, src_rtc);
protocore_time_now() returns the chosen epoch and protocore_time_source_active() names the source that supplied it. Swap the RTC stand-in for a real DS3231/PCF8523 I2C read, and add a GPS source the same way (returning 0 with no fix).
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPROTOCORE_ENABLE_NTP=1 -DPROTOCORE_ENABLE_TIME_SOURCE=1" \
--lib="." examples/L7-Application/TimeSourceFallback/TimeSourceFallback.ino
curl http://<ip>/time # {"epoch":...,"source":"rtc"} at boot, "ntp" after sync
Annotated source
The complete sketch (TimeSourceFallback.ino), reproduced verbatim with added explanatory comments:
#define PROTOCORE_ENABLE_NTP 1
#define PROTOCORE_ENABLE_TIME_SOURCE 1
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
PC server;
static uint32_t src_ntp()
{
return protocore_ntp_synced() ? (uint32_t)protocore_ntp_epoch() : 0;
}
static const uint32_t RTC_BASE = 1750000000u;
static uint32_t src_rtc()
{
return RTC_BASE + (uint32_t)(millis() / 1000);
}
void setup()
{
Serial.begin(115200);
Physical.wifi->init(SSID, PASSWORD);
Serial.print("Connecting to WiFi");
while (!Physical.wifi->ready())
{
delay(250);
Serial.print('.');
}
uint32_t ip = Physical.link->egress_ip();
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
protocore_ntp_begin();
protocore_time_source_add("ntp", 0, src_ntp);
protocore_time_source_add("rtc", 1, src_rtc);
char body[96];
uint32_t epoch = protocore_time_now();
const char *src = protocore_time_source_active();
snprintf(body, sizeof(body), "{\"epoch\":%u,\"source\":\"%s\"}", (unsigned)epoch, src ? src : "none");
server.send(id, 200, "application/json", body);
});
server.begin(80);
}
void loop()
{
server.handle();
}
@ HTTP_GET
Safe, idempotent read.
Optional SNTP wall-clock time sync (PROTOCORE_ENABLE_NTP).
Layer 1 (Physical) - link bring-up, the interface registry, and live egress reporting.
Multi-source time fallback matrix (PROTOCORE_ENABLE_TIME_SOURCE).