ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
Provisioning - first-boot WiFi setup via a captive portal

Layer: L7 Application ยท Build flags: PC_ENABLE_PROVISIONING

What this example teaches

On first boot, with no stored credentials, the device brings up a softAP ("PC-Setup") and a catch-all DNS responder (raw lwIP UDP, no add-on library), so joining the AP with a phone pops a credentials form. The submitted SSID/PSK persist to NVS; on the next boot the device finds them, connects as a station, and serves normally. No external libraries are needed: just WiFi softAP, lwIP UDP, and Preferences (NVS).

The boot decision: load credentials, else open the portal.

char ssid[33], psk[64];
if (pc_provisioning_load(ssid, sizeof(ssid), psk, sizeof(psk))) {
init_wifi_physical(ssid, psk); // we have creds -> normal station
server.on("/", HttpMethod::HTTP_GET, handle_root);
server.begin(80);
} else {
server.begin(80);
pc_provisioning_begin(server, "PC-Setup"); // no creds -> captive portal
}
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
Definition physical.cpp:41
@ HTTP_GET
Safe, idempotent read.
void pc_provisioning_begin(PC &server, const char *ap_ssid)
Start the captive portal: softAP ap_ssid + catch-all DNS + form routes.
bool pc_provisioning_load(char *ssid, size_t ssid_cap, char *psk, size_t psk_cap)
Load stored WiFi credentials from NVS.

pc_provisioning_load() returns false until the device has been provisioned; pc_provisioning_begin() stands up the softAP, the catch-all DNS, and the form handler that writes NVS and reboots. To re-provision later, call pc_provisioning_clear() (for example from a button handler).

Build and run

pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_PROVISIONING=1" \
--lib="." examples/L7-Application/Provisioning/Provisioning.ino

Flash, join the WiFi network "PC-Setup" with a phone, open any page to reach the captive portal, and submit your network's SSID/password. The device reboots into station mode.

Annotated source

The complete sketch (Provisioning.ino), reproduced verbatim with added explanatory comments:

// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
#define PC_ENABLE_PROVISIONING 1
#include "protocore.h"
#include "services/provisioning_service.h"
PC server;
void handle_root(uint8_t slot_id, HttpReq *req)
{
(void)req;
server.send(slot_id, 200, "text/plain", "Provisioned - hello from station mode!");
}
void setup()
{
Serial.begin(115200);
char ssid[33];
char psk[64];
if (pc_provisioning_load(ssid, sizeof(ssid), psk, sizeof(psk)))
{
// Credentials present: connect as a normal station.
init_wifi_physical(ssid, psk);
Serial.print("Connecting to ");
Serial.println(ssid);
while (!wifi_ready())
delay(250);
uint32_t ip = pc_net_egress_ip(); // library egress IP (network byte order), no Arduino WiFi
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
server.on("/", HttpMethod::HTTP_GET, handle_root);
server.begin(80);
Serial.println("Station mode; serving on port 80");
}
else
{
// No credentials: bring up the captive portal (softAP + catch-all DNS + form).
server.begin(80);
pc_provisioning_begin(server, "PC-Setup");
Serial.println("Provisioning: join WiFi 'PC-Setup' and open any page");
}
}
void loop()
{
server.handle();
}
Single-port HTTP server with deterministic, zero-allocation execution.
Definition protocore.h:348
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.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
Definition physical.cpp:77
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Definition physical.cpp:45
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
Fully-parsed HTTP/1.1 request.