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];
server.begin(80);
} else {
server.begin(80);
}
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
@ 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:
#define PC_ENABLE_PROVISIONING 1
#include "services/provisioning_service.h"
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];
{
Serial.print("Connecting to ");
Serial.println(ssid);
delay(250);
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
Serial.println("Station mode; serving on port 80");
}
else
{
Serial.println("Provisioning: join WiFi 'PC-Setup' and open any page");
}
}
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.
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.
Fully-parsed HTTP/1.1 request.