Layer: L7 Application · Build flags: PC_ENABLE_CONFIG_STORE, PC_ENABLE_CONFIG_IO
What this example teaches
Backing up or bulk-provisioning a fleet needs the device's persisted settings in a portable form. This declares a schema of persisted fields and exposes them as a text blob: GET /config dumps key=value lines for backup or migration, and POST /config with that body restores them into NVS. It is schema-driven over the typed NVS config store - deterministic and zero-heap.
Declare the schema once, with field types:
static const pc_cfg_field SCHEMA[] = {
{"hostname", pc_cfg_type::PC_CFG_STR},
{"http_port", pc_cfg_type::PC_CFG_U32},
{"location", pc_cfg_type::PC_CFG_STR},
};
Export and import drive off that schema. The config store is opened under a namespace ("app") and seeded; export serializes exactly the schema fields, import parses them back:
pc_config_export("app", SCHEMA, SCHEMA_N, buf, sizeof(buf));
int n = pc_config_import("app", SCHEMA, SCHEMA_N, req->body, req->body_len);
Because both sides share the schema, an unknown or mistyped key in the import body is rejected rather than silently written.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_CONFIG_STORE=1 -DPC_ENABLE_CONFIG_IO=1" \
--lib="." examples/L7-Application/ConfigExport/ConfigExport.ino
curl http://<ip>/config # back up: hostname=sensor-01 ...
curl -X POST http://<ip>/config --data-binary @config.txt # restore into NVS
Annotated source
The complete sketch (ConfigExport.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_CONFIG_STORE 1
#define PC_ENABLE_CONFIG_IO 1
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static const pc_cfg_field SCHEMA[] = {
{"hostname", pc_cfg_type::PC_CFG_STR},
{"http_port", pc_cfg_type::PC_CFG_U32},
{"location", pc_cfg_type::PC_CFG_STR},
};
static const size_t SCHEMA_N = sizeof(SCHEMA) / sizeof(SCHEMA[0]);
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));
pc_config_begin("app");
pc_config_set_str("hostname", "sensor-01");
pc_config_set_u32("http_port", 80);
pc_config_set_str("location", "lab");
char buf[512];
pc_config_export("app", SCHEMA, SCHEMA_N, buf, sizeof(buf));
server.
send(
id, 200,
"text/plain", buf);
});
int n = pc_config_import(
"app", SCHEMA, SCHEMA_N, (
const char *)req->
body, req->
body_len);
char msg[48];
snprintf(msg, sizeof(msg), "imported %d field(s)\n", n);
server.
send(
id, 200,
"text/plain", msg);
});
}
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.
Schema-driven config export / restore (PC_ENABLE_CONFIG_IO).
Typed NVS configuration store (PC_ENABLE_CONFIG_STORE).
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_POST
Non-idempotent create / action.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.
uint8_t body[BODY_BUF_SIZE+1]
Stored body bytes, always null-terminated.
size_t body_len
Bytes stored in body[] (≤ BODY_BUF_SIZE).