Layer: L7 Application ยท Build flags: PC_ENABLE_NTP, PC_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. PC_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_rtc() { return RTC_BASE + (uint32_t)(millis() / 1000); }
time_t pc_ntp_epoch()
Current Unix epoch seconds, or 0 if not yet synced (or disabled).
bool pc_ntp_synced()
True once a plausible wall-clock time has been obtained from SNTP.
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:
bool pc_ntp_begin(const char *tz, const char *server1, const char *server2)
Start the SNTP client.
bool pc_time_source_add(const char *, uint8_t, TimeSourceFn)
Register a time source.
pc_time_now() returns the chosen epoch and pc_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=-DPC_ENABLE_NTP=1 -DPC_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 PC_ENABLE_NTP 1
#define PC_ENABLE_TIME_SOURCE 1
#include "services/ntp_service.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static uint32_t src_ntp()
{
}
static const uint32_t RTC_BASE = 1750000000u;
static uint32_t src_rtc()
{
return RTC_BASE + (uint32_t)(millis() / 1000);
}
void setup()
{
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
char body[96];
snprintf(body, sizeof(body), "{\"epoch\":%u,\"source\":\"%s\"}", (unsigned)epoch, src ? src : "none");
server.
send(
id, 200,
"application/json", body);
});
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
void send(uint8_t slot_id, int code, const char *content_type, const char *payload)
Send an HTTP response with a body and close the connection.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
void handle()
Drive the server - call every Arduino loop() iteration.
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.
uint32_t pc_time_now(void)
Current best time.
const char * pc_time_source_active(void)
Name of the source that satisfied the last pc_time_now(), or nullptr.
Multi-source time fallback matrix (PC_ENABLE_TIME_SOURCE).