ESP32 OTA updates with ESP-IDF: the simpleota 1.0 component
Add managed ESP32 OTA to an ESP-IDF app with the simpleota 1.0 component: partitions, rollback, polling mode, signed firmware and the 0.2.x upgrade.
SimpleOTA has a first-party ESP-IDF component on the
Espressif Component Registry,
and version 1.0.0 is the one this guide uses. It checks for updates, streams
each image into the inactive OTA slot while verifying it, boots the new build as
a trial, and reports every step back to your project, so the dashboard shows
which devices confirmed, failed or rolled back. With
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE, a build that crashes or resets before
it confirms boots the previous image, and a build that runs but is not confirmed
within 300 seconds is rolled back by the component.
idf.py add-dependency "wassie-dev/simpleota^1.0.0"
Since 1.0.0 the component is published as wassie-dev/simpleota. The old
xanderwasserman/simpleota name keeps the 0.2.x releases but gets no new ones,
and the source now lives at
wassie-dev/simpleOTAClient-ESP-IDF.
If you are coming from 0.2.x, the upgrade section below
covers what changes.
If you want the background first, how ESP32 OTA updates work explains the two app slots, the switch and the confirm step that this component automates.
What you will build
You will add the component to an ESP-IDF project, flash it once over USB, then deploy a second build over HTTPS and watch it confirm in the dashboard.
Jump to: add the component · partitions and rollback · initialize the client · first flash · upload and deploy · polling mode · rollback · signed firmware · upgrading from 0.2.x · troubleshooting
What you need
- ESP-IDF v5.1 or newer, with the toolchain installed and
idf.pyon your path. The component is tested through 6.x. - A board with one of the supported targets (
esp32,esp32s2,esp32s3,esp32c3,esp32c6oresp32h2) and at least 4 MB of flash. - A Wi-Fi network the board can join.
- A SimpleOTA account, a project created with ESP-IDF as its default framework, and a project token. The framework is chosen when you create the project, and a device is offered only builds of its own framework.
If you are starting from nothing, the quickest route is the ESP-IDF starter, a GitHub template with the partition table, Wi-Fi bring-up and OTA settings already in place. You can also copy one of the component’s example projects:
idf.py create-project-from-example "wassie-dev/simpleota^1.0.0:basic"
The component ships three examples: basic for managed updates with bootloader
rollback, polling for checks on your own schedule, and signed for on-device
Ed25519 verification with a pinned key.
1. Add the component
For an existing project, run add-dependency from the project root. The
component manager writes the dependency into main/idf_component.yml and
fetches it on the next build.
idf.py add-dependency "wassie-dev/simpleota^1.0.0"
There is nothing else to install. The component uses what ships with ESP-IDF
plus a vendored copy of Monocypher for Ed25519, and
it does not depend on cJSON, because the json component left the IDF core in
6.0. One manifest therefore covers v5.1 through 6.x without version-conditional
rules, and CI builds the examples across that range on both Xtensa and RISC-V
targets.
2. Set up partitions and rollback
Get this step right before the first flash, because the partition table and the bootloader settings go out with that serial flash and cannot be changed over the air afterwards. If you flash a board without them and change your mind, you need physical access and a USB cable again.
Save this partition table as partitions.csv in your project directory. It is
the table the component’s examples ship: two 1700 KB app slots and no factory
partition, on 4 MB of flash.
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, , 0x6000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
ota_0, app, ota_0, , 1700K,
ota_1, app, ota_1, , 1700K,
On ESP-IDF 5.4 and later, CONFIG_PARTITION_TABLE_TWO_OTA_LARGE=y selects the
same table without a CSV. Otherwise, put these lines in your project’s
sdkconfig.defaults:
# Two OTA app slots from partitions.csv; a serial flash boots ota_0.
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
# A new image gets exactly one attempt until it is confirmed.
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
# The partition table needs 4MB of flash.
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
# Size-optimized builds leave more of each app slot for the application.
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
ESP-IDF reads sdkconfig.defaults only when it generates sdkconfig, so
delete an existing sdkconfig after adding these lines.
One more setting is worth adding before the first flash:
CONFIG_ESP_TASK_WDT_TIMEOUT_S=30
A TLS handshake is synchronous and CPU-bound inside mbedTLS, so while the
certificate chain is verified the idle task can be starved for longer than the
default watchdog window. It shows up as an alarming but harmless
task_wdt: ... IDLE warning during the first check after boot, and widening the
window keeps the watchdog able to catch real hangs, which disabling it would not.
3. Initialize the client
The configuration is a simpleota_config_t filled with designated initializers,
in the same style as esp_http_client_config_t. Only the token is required, and
every other field has a working default:
#include "simpleota.h"
simpleota_config_t cfg = {
.token = "YOUR_PROJECT_TOKEN",
};
ESP_ERROR_CHECK(simpleota_init(&cfg));
ESP_ERROR_CHECK(simpleota_start());
A real application wants two more things: a way for the component to know the
network is up, and an event callback so the serial log shows what it is doing.
esp_wifi_connect() returns before the station has an IP, so set
.is_connected and the component waits for it before each check:
#include <inttypes.h>
#include "esp_event.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "simpleota.h"
static const char *TAG = "example";
static volatile bool s_have_ip;
static void on_ip_event(void *arg, esp_event_base_t base, int32_t id, void *data) {
s_have_ip = true; /* clear it again on WIFI_EVENT_STA_DISCONNECTED */
}
static bool is_connected(void *ctx) {
return s_have_ip;
}
static void on_ota_event(const simpleota_event_t *evt, void *ctx) {
switch (evt->id) {
case SIMPLEOTA_EVENT_UPDATE_AVAILABLE:
ESP_LOGI(TAG, "update available: build %" PRIu32, evt->build_number);
break;
case SIMPLEOTA_EVENT_DOWNLOAD_PROGRESS:
if (evt->bytes_total) {
ESP_LOGI(TAG, "downloading: %u/%u bytes",
(unsigned)evt->bytes_done, (unsigned)evt->bytes_total);
}
break;
case SIMPLEOTA_EVENT_UPDATE_FAILED:
ESP_LOGW(TAG, "update failed: %s", evt->reason ? evt->reason : "?");
break;
case SIMPLEOTA_EVENT_TRIAL_BOOT:
ESP_LOGI(TAG, "trial boot of build %" PRIu32 " (unconfirmed)", evt->build_number);
break;
case SIMPLEOTA_EVENT_CONFIRMED:
ESP_LOGI(TAG, "build %" PRIu32 " confirmed", evt->build_number);
break;
default:
break;
}
}
void app_main(void) {
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
on_ip_event, NULL));
/* ... your usual Wi-Fi station setup and esp_wifi_connect() ... */
simpleota_config_t cfg = {
.token = "YOUR_PROJECT_TOKEN",
.version_label = "1.0.0", /* shown on the dashboard */
.check_interval_s = 300, /* default 3600; short for this test */
.is_connected = is_connected,
.event_cb = on_ota_event,
};
ESP_ERROR_CHECK(simpleota_init(&cfg));
ESP_ERROR_CHECK(simpleota_start());
ESP_LOGI(TAG, "running build %" PRIu32, simpleota_current_build());
}
Call simpleota_init() early in app_main, because on a trial boot it is what
starts the supervision of the new image. The device identifies itself with its
Wi-Fi MAC unless you set .device_id, and chip_family defaults to the build
target, so you only set them when you need something different. Keep event_cb
short, because it runs on the task doing the work, and the only SimpleOTA call
it may make is simpleota_reboot_for_update().
TLS is always verified. By default the component uses ESP-IDF’s certificate
bundle, so there is no CA certificate to embed or rotate, and .cert_pem pins a
specific CA if your policy requires it. Redirects are followed only to https
URLs.
4. Flash the first build over USB
Build and flash normally. This first image establishes the partition layout and the rollback-capable bootloader, which is why it has to arrive over the cable:
idf.py build flash monitor
Once the board has an IP, the component checks in. Because the config does not set a device ID, it registers under its Wi-Fi MAC, and the project token lets the server accept that registration on first contact. The API answers that no update is available, which is correct, since you have not uploaded one yet.
Refresh the project in the dashboard and the device should appear with a recent
last_seen and a framework of esp_idf. If it does not, jump to
troubleshooting before going further, because everything
below assumes the device is checking in.
5. Upload and deploy
Make a visible change and set .version_label to "1.0.1" so the new build is
easy to recognize, then build again and upload build/<your-project>.bin in the
dashboard, with the chip family set to match your target. In Simple mode the
upload takes its framework from the project, which is why the project needs
ESP-IDF as its framework; in Advanced mode, pick ESP-IDF on the upload form.
Upload the application binary from build/, not a merged factory image.
Upload only the application image. ESP-IDF also produces bootloader.bin and
partition-table.bin, and those belong to a factory flash rather than an OTA
slot. A merged image is far larger than one application partition, and the component refuses an image that does not fit its slot before writing any of it.
SimpleOTA hashes the binary, stores it and allocates the next build number. In Simple mode the upload also creates and starts a deployment. In Advanced mode you create the deployment yourself and press Start on it. Update ordering comes from build numbers, never from the human-readable version label.
6. Watch the update
At the 300-second interval above, the board checks in, is offered the new build, and installs it:
I (11842) example: update available: build 2
I (12984) example: downloading: 262144/902144 bytes
I (18761) example: downloading: 902144/902144 bytes
*** device reboots into the new image ***
I (3612) example: trial boot of build 2 (unconfirmed)
I (9188) example: build 2 confirmed
The API hands back a short-lived, pre-signed download URL, and the firmware travels straight from object storage to the device, so there is no permanent public address for your binaries.
As the bytes arrive, the component streams them into the inactive slot and verifies them on the way past. The SHA-256 digest and, in signed mode, the Ed25519 signature are computed as each chunk is written, and the partition is made bootable only once both pass.
A rejected image never becomes bootable, and the running firmware is untouched.
The rollout view separates eligible, assigned, in-progress, confirmed, and
failed devices.
Polling mode
Managed mode is the right default, but some products need to decide when a
check happens, for example only while mains-powered or between two measurement
cycles. In that case skip simpleota_start() and call simpleota_check() and
simpleota_apply() from your own task:
bool update = false;
if (simpleota_check(&update) == ESP_OK && update) {
simpleota_result_t r = simpleota_apply(); // reboots on success
if (r != SIMPLEOTA_OK) ESP_LOGW("app", "update failed: %d", r);
}
Both calls run on a short-lived task of their own while the caller waits, so
they are safe to call from app_main without enlarging its stack. In polling
mode you confirm every trial boot yourself with simpleota_confirm(), and the
two modes do not mix: while the managed task runs, simpleota_check() returns
ESP_ERR_INVALID_STATE. If you stay in managed mode but want a check right now,
for example after a button press, simpleota_check_now() wakes the managed task
instead.
Anti-rollback
Version 1.0.0 refuses an offer whose build number is not above the running
build. simpleota_apply() returns SIMPLEOTA_DOWNGRADE before any download and
reports downgrade_refused, once per boot:
bool update = false;
if (simpleota_check(&update) == ESP_OK && update) {
if (simpleota_apply() == SIMPLEOTA_DOWNGRADE) {
ESP_LOGW("simpleota", "offered build is not above the running one");
}
}
To go back to known-good firmware, upload the old code again so SimpleOTA gives it a new, higher build number, and deploy that.
An image flashed over USB reports build 0, so the server offers it the current
release. If you flash release images over USB, for example at a factory, see
When to set image_build
in the client README.
Automatic rollback on ESP-IDF
With CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE, a freshly installed image reboots
in the PENDING_VERIFY state, and the bootloader gives it exactly one attempt.
If the application does not mark itself valid before the next reset, the
bootloader boots the previous slot on its own.
The previous image stays in its slot until the trial is resolved either way.
The point is that recovery does not depend on the firmware that just failed. An
image that panics in app_main, or a board that loses power thirty seconds into
its trial, still comes back on the previous build. For an image that runs but
never manages a successful check-in, the component adds a confirm timeout
(300 seconds by default), after which it rolls back and reports
rolled_back with the reason confirm_timeout. The device stays on the previous build, and
the dashboard shows which devices rolled back.
By default the component confirms a trial on the first successful check-in after the reboot. That proves the new image booted, joined the network, completed a TLS handshake and authenticated against the API, which is a reasonable health gate for a prototype. For a product that needs its sensors, storage or backend session working before you would call a build good, make the decision yourself:
simpleota_config_t cfg = {
.token = "YOUR_PROJECT_TOKEN",
.manual_confirm = true,
.confirm_timeout_s = 600, /* default 300, clamped to [10, 86400] */
};
/* Later, once the application is genuinely healthy: */
simpleota_confirm();
If you also need to control the moment of restart, set .disable_auto_reboot and
call simpleota_reboot_for_update() when the application reaches a safe point.
If bootloader rollback is off, 1.0.0 enables CONFIG_SIMPLEOTA_SOFTWARE_ROLLBACK
by default, so new images still run as trials, and any reset before
confirmation, including deep sleep or esp_restart(), rolls the image back as
long as simpleota_init() ran on that boot. It cannot catch a crash that happens before
simpleota_init() runs, which the bootloader does catch, so new devices should
still ship with CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE. The
trial install and rollback guide
covers the full lifecycle and every reason a rollback reports.
Signed firmware
Pin the public key from your project’s signing keys, and the device verifies an Ed25519 signature over the exact firmware bytes before the image is allowed to boot:
static const char PUBKEY_PEM[] =
"-----BEGIN PUBLIC KEY-----\n"
"...from the dashboard's Signing keys section...\n"
"-----END PUBLIC KEY-----\n";
const simpleota_signing_key_t keys[] = {{.key_id = "prod-2026", .pem = PUBKEY_PEM}};
simpleota_config_t cfg = {
.token = "YOUR_PROJECT_TOKEN",
.security_mode = SIMPLEOTA_SECURITY_SIGNED,
.signing_keys = keys,
.num_signing_keys = 1,
};
The gate fails closed. A missing, stripped or invalid signature rejects the
update as signature_invalid and the device stays on its current build, and
asking for signed mode without a key makes simpleota_init() fail with
ESP_ERR_INVALID_ARG instead of leaving the device running unverified. You can
pin two keys at once, so a key can be rotated without stranding devices: ship a build that
trusts both the old and the new key, wait for the fleet to take it, then drop the
old one.
Signing an update is a different thing from ESP32 Secure Boot. The signature
check stops a device from installing a tampered update, while Secure Boot stops
it from running unsigned firmware at all. If you run both and the bootloader
rejects an image, the component reports it as update_end_failed. The threat
model and the move from unsigned to signed builds are covered in
Signed firmware, verified on the device
and the signed firmware guide.
Arduino and ESP-IDF
Since 1.0.0 the two clients are the same code. The component and the
SimpleOTAClient Arduino
library are both built from SimpleOTAClient-core and ship the same simpleota.h,
so the same calls, events and results apply on both, and a device keeps its
identity if you move its firmware from one framework to the other. Two things
differ:
| Arduino | ESP-IDF | |
|---|---|---|
| Build-time options | -D flags |
Kconfig (menuconfig) |
TLS roots without cert_pem |
ISRG Root X1 and X2 | ESP-IDF certificate bundle |
The full reference for every function, field, event and result is in the API reference.
Upgrading from 0.2.x
Devices running 0.2.x take a 1.0.0 build over the air like any other update,
even in the middle of a trial. Version 1.0.0 adds one NVS key, sota_image, and
leaves the rest of the stored state as it was.
On the build side, change the dependency to the new registry name:
idf.py add-dependency "wassie-dev/simpleota^1.0.0"
Then remove the old xanderwasserman/simpleota line from
main/idf_component.yml. The behavior changes to check for are the ones covered
above: offers at or below the running build are refused, SIMPLEOTA_SECURITY_SIGNED
needs a pinned key, and software rollback is on by default when bootloader
rollback is off. The step-by-step checklist is in
Upgrading to 1.0.0, and the
CHANGELOG
lists every change. Later 1.x releases only add functions, fields, result values
and event ids, and never change or remove what is already there.
Troubleshooting
The build cannot find simpleota.h
Check that add-dependency wrote to the manifest of the component that includes
it, normally main/idf_component.yml, then run idf.py reconfigure. If you have
been switching between a local checkout and the registry version, or between the
old and new registry names, delete managed_components/ and dependencies.lock
and build again.
The device never appears in the dashboard
Confirm the board actually gets an IP, that the token was copied whole, and that
the board can reach https://simpleota.com. Check that the plan has capacity for
another active device. The serial log names the failure, and a token problem and
a TLS problem look nothing alike.
task_wdt: ... IDLE warnings during the first check
This is expected on a slow first TLS handshake. Set
CONFIG_ESP_TASK_WDT_TIMEOUT_S=30 as described above rather than disabling the
watchdog.
The update fails before anything is written
If the log reports update_begin_failed, either the uploaded file is not the application image or it does not fit one app slot. Upload build/<project>.bin, never a merged image, and compare its size
with a single slot in your partition table. If the log reports
downgrade_refused instead, the offered build is not above the running one; see
anti-rollback.
The device never leaves the trial state
Either the check-in after the reboot is not succeeding, or you set
.manual_confirm (or use polling mode) and never call simpleota_confirm().
Watch the serial log across the reboot: the component logs the trial boot on
startup and the confirmation when the gate passes.
The device downloads the same build repeatedly
The installed build number lives in the simpleota NVS namespace. If that
namespace is erased, or the image was flashed over USB, the device reports
build 0 and can be offered the same deployment again. Do not write to that
namespace from application code.
Where to go next
You now have a board that takes firmware over HTTPS, verifies it before trusting
it, and rolls back on its own when a new build fails its trial. Before pointing
this at a fleet, replace the project token with per-device credentials, raise the
check interval to suit your power budget, define a real health check with
.manual_confirm, turn on signed firmware, and roll each release out to a small
percentage of devices before the rest.
The full configuration reference is in the ESP-IDF integration guide, and the source is on GitHub. If you put it on hardware and something behaves oddly, open an issue with the serial log.
Create a SimpleOTA project and ship your first ESP-IDF update over HTTPS.