Layer: L7 Application ยท Build flags: PC_ENABLE_OTA_ROLLBACK
What this example teaches
A bad OTA image that boots but cannot do its job will soft-brick a remote device. The ESP32 bootloader can mark a freshly flashed image PENDING_VERIFY and roll back to the previous one unless the new firmware confirms itself. This wraps that mechanism: each loop it runs a self-test (here WiFi up + healthy heap) and ticks the rollback service - a passing self-test commits the image, a failing one (or no confirm within PC_OTA_CONFIRM_WINDOW_MS) rolls back. So a bad update self-heals instead of bricking. It is the safety net for the OTA upload in OTA.
Define a self-test, then tick until committed:
static bool self_test() {
return wifi_ready() && ESP.getFreeHeap() > 20000; }
void loop() {
static bool done = false;
if (!done) {
pc_ota_action a = pc_ota_rollback_tick(self_test());
if (a == pc_ota_action::PC_OTA_COMMIT) { Serial.println("[ota] image committed"); done = true; }
}
server.handle();
}
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
pc_ota_rollback_tick(ok) is a no-op once the image is committed or on a normally-booted image, so it is safe to call every loop. pc_ota_img_state() reports the current image state for the /ota-state endpoint.
Requirement. Actual rollback needs the bootloader's app-rollback support (CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE) enabled in the build.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_OTA_ROLLBACK=1" \
--lib="." examples/L7-Application/OtaRollback/OtaRollback.ino
curl http://<ip>/ota-state # {"img_state":...} - the running image's verify state
Annotated source
The complete sketch (OtaRollback.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_OTA_ROLLBACK 1
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static bool self_test()
{
}
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));
char b[48];
snprintf(b, sizeof(b), "{\"img_state\":%u}", pc_ota_img_state());
server.
send(
id, 200,
"application/json", b);
});
}
void loop()
{
static bool done = false;
if (!done)
{
pc_ota_action a = pc_ota_rollback_tick(self_test());
if (a == pc_ota_action::PC_OTA_COMMIT)
{
Serial.println("[ota] image committed");
done = true;
}
}
}
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.
OTA rollback protection / soft-brick safeguard (PC_ENABLE_OTA_ROLLBACK).
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.
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.