Layer: L7 Application ยท Build flags: PC_ENABLE_TOTP
What this example teaches
TOTP is the six-digit code an authenticator app (Google Authenticator, Authy) shows. This decodes a base32 shared secret, computes the current code, and verifies a submitted one within a +/-1 step window - so you can add a second factor to a protected route. GET /totp shows the current code (demo only) and GET /totp/verify?code=NNNNNN checks one.
Decode the secret once, then compute / verify against the clock:
int n = pc_base32_decode(SECRET_B32, g_secret, sizeof(g_secret));
g_secret_len = (n > 0) ? (size_t)n : 0;
uint32_t code = pc_totp(g_secret, g_secret_len, now_unix(), 30, 6);
bool ok = pc_totp_verify(g_secret, g_secret_len, now_unix(), code, 30, 6, 1);
The last argument to pc_totp_verify is the allowed step skew, which absorbs clock drift between the device and the authenticator. For codes that match a real app, sync the device clock to real time via NTP (SNTP); this example uses a fixed clock base so it is self-contained.
Security note. The /totp endpoint that reveals the live code exists only to make the demo testable - never expose the current code in production.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_TOTP=1" \
--lib="." examples/L7-Application/Totp/Totp.ino
code=$(curl -s http://<ip>/totp)
curl "http://<ip>/totp/verify?code=$code" # {"ok":true}
Annotated source
The complete sketch (Totp.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_TOTP 1
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static const char *SECRET_B32 = "JBSWY3DPEHPK3PXP";
static uint8_t g_secret[32];
static size_t g_secret_len = 0;
static uint64_t now_unix()
{
return 1700000000ull + millis() / 1000;
}
void setup()
{
delay(250);
Serial.print("IP: ");
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
int n = pc_base32_decode(SECRET_B32, g_secret, sizeof(g_secret));
g_secret_len = (n > 0) ? (size_t)n : 0;
uint32_t code = pc_totp(g_secret, g_secret_len, now_unix(), 30, 6);
char b[16];
snprintf(b, sizeof(b), "%06u", code);
server.
send(
id, 200,
"text/plain", b);
});
uint32_t code = code_s ? (uint32_t)strtoul(code_s, nullptr, 10) : 0;
bool ok = pc_totp_verify(g_secret, g_secret_len, now_unix(), code, 30, 6, 1);
server.
send(
id, 200,
"application/json", ok ?
"{\"ok\":true}" :
"{\"ok\":false}");
});
}
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.
const char * http_get_query(const HttpReq *req, const char *key)
Look up a query parameter value by name (case-sensitive).
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.
TOTP two-factor auth (RFC 6238) (PC_ENABLE_TOTP).