Perform OTA Updates on ESP32 With esp_https_ota
Update ESP32 firmware over HTTPS with esp_https_ota: partition table, certificate, the one-call and advanced APIs, and testing with a local server.

The ESP32 is one of the few microcontrollers where over-the-air updates are close to a solved problem. ESP-IDF ships the bootloader, the partition handling, the HTTPS client and an image checker, and a working updater takes under fifty lines. This post uses ESP-IDF v5.4 to build one: it sets up the partition table that gives the chip two application slots, makes the device download a new app.bin over HTTPS with esp_https_ota(), switches to the advanced API to read the new image's version before installing it, and tests the whole path against a local server. The rollback logic that makes an update safe is covered by its own post in this cluster, and here we focus on the download and install.
How ESP-IDF OTA is laid out
An ESP32 application does not live at a fixed address in the way a bare Cortex-M image does. The flash is divided into partitions listed in a partition table, and the ESP-IDF second-stage bootloader reads an otadata partition to decide which application partition to start. For OTA you need at least two application partitions, ota_0 and ota_1, and the otadata partition that records which one is current.
ESP-IDF includes a ready table for this. In menuconfig, under Partition Table, choose "Factory app, two OTA definitions". It corresponds to partitions_two_ota.csv:
nvs, data, nvs, 0x9000, 0x4000,
otadata, data, ota, 0xd000, 0x2000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
ota_0, app, ota_0, 0x110000, 1M,
ota_1, app, ota_1, 0x210000, 1M,The factory partition holds the image you flashed over USB, and it is the fallback of last resort. A running image never overwrites its own partition. The updater always writes to the other slot: if the device runs from ota_0, the new image goes to ota_1, and the other way round. After a successful download the updater tells the bootloader, through otadata, to boot the new slot. These sizes assume a 4 MB flash module and a 1 MB application limit, which you must raise if your build is larger, and the flash size setting in menuconfig must match the module.
The one-call update
esp_https_ota() is declared in esp_https_ota.h, in the esp_https_ota component. It takes a configuration struct holding an esp_http_client_config_t, downloads the image, writes it to the next OTA partition, checks it and sets it as the boot partition. It returns ESP_OK on success, and does not reboot on its own:
#include "esp_https_ota.h"
#include "esp_http_client.h"
#include "esp_crt_bundle.h"
#include "esp_log.h"
#include "esp_system.h"
static const char *TAG = "ota";
#define OTA_URL "https://updates.example.com/firmware/app.bin"
void ota_update_task(void *arg)
{
ESP_LOGI(TAG, "Starting OTA from %s", OTA_URL);
esp_http_client_config_t http = {
.url = OTA_URL,
.crt_bundle_attach = esp_crt_bundle_attach, /* verify the server */
.timeout_ms = 10000,
.keep_alive_enable = true,
};
esp_https_ota_config_t ota = {
.http_config = &http,
};
esp_err_t err = esp_https_ota(&ota);
if (err == ESP_OK) {
ESP_LOGI(TAG, "OTA upgrade successful. Rebooting");
esp_restart();
} else {
ESP_LOGE(TAG, "OTA failed: %s", esp_err_to_name(err));
}
vTaskDelete(NULL);
}Start it as a task once the Wi-Fi connection is up, for example xTaskCreate(ota_update_task, "ota", 8192, NULL, 5, NULL). The task needs a generous stack, since TLS handshakes are stack hungry, and 8 KB is a common starting point.
Three configuration details deserve attention.
Certificate verification. crt_bundle_attach = esp_crt_bundle_attach uses the CA bundle that ESP-IDF can embed in the firmware, enabled with CONFIG_MBEDTLS_CERTIFICATE_BUNDLE (on by default in new projects), which lets the device verify servers whose certificates chain to a well-known authority. For a private server, embed your own CA certificate instead: add EMBED_TXTFILES certs/ca_cert.pem to idf_component_register() in the component's CMakeLists.txt, declare extern const char ca_start[] asm("_binary_ca_cert_pem_start");, and set .cert_pem = ca_start. Do not turn verification off: an update channel without it lets anyone on the network feed the device code.
HTTP is blocked by default. Plain http:// URLs fail unless you enable CONFIG_ESP_HTTPS_OTA_ALLOW_HTTP. Leave that off outside a lab.
Timeouts. The default network timeout is short for a slow link. timeout_ms bounds each socket operation, not the whole download, so a slow but steady connection still completes.
Building the image to serve
The file to publish is the application binary from the build, build/<project>.bin. It is the same image you flash during development, without the bootloader and partition table:
idf.py build
ls -l build/*.binThe server needs only to answer a GET for that file over HTTPS. The device does not send credentials by default, and a production service adds authentication, typically a token in a request header, set with esp_http_client_set_header() from a callback in the configuration (the http_client_init_cb field). Put the binary behind a version path, such as /firmware/1.4.2/app.bin, so a device never races a half-uploaded file.
The advanced API: check the version first
The one-call function installs whatever it downloads, including the image you are already running. A device that reboots, finds the same version and reinstalls it, forever, is a classic mistake. The advanced API splits the work into begin, read, perform and finish steps, so you can look at the new image's header before committing:
#include <string.h>
#include "esp_https_ota.h"
#include "esp_app_format.h"
#include "esp_ota_ops.h"
#include "esp_log.h"
static const char *TAG = "ota";
static bool version_is_newer(const esp_app_desc_t *new_app)
{
const esp_app_desc_t *running = esp_app_get_description();
ESP_LOGI(TAG, "running %s, offered %s", running->version, new_app->version);
return strcmp(new_app->version, running->version) != 0;
}
esp_err_t ota_run(esp_http_client_config_t *http)
{
esp_https_ota_config_t cfg = { .http_config = http };
esp_https_ota_handle_t h = NULL;
esp_err_t err = esp_https_ota_begin(&cfg, &h);
if (err != ESP_OK) return err;
esp_app_desc_t desc;
err = esp_https_ota_get_img_desc(h, &desc);
if (err != ESP_OK || !version_is_newer(&desc)) {
esp_https_ota_abort(h);
return err != ESP_OK ? err : ESP_ERR_INVALID_VERSION;
}
while ((err = esp_https_ota_perform(h)) == ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
ESP_LOGD(TAG, "downloaded %d bytes",
esp_https_ota_get_image_len_read(h));
}
if (err != ESP_OK || !esp_https_ota_is_complete_data_received(h)) {
esp_https_ota_abort(h);
return ESP_FAIL;
}
return esp_https_ota_finish(h); /* validates and sets boot slot */
}esp_https_ota_get_img_desc() reads the esp_app_desc_t structure from the start of the incoming image. It contains the project name, the version string from PROJECT_VER (set in the top-level CMakeLists.txt or from git describe), the build time and the ELF checksum. Comparing the version strings is a start, and a real policy parses them and accepts only higher versions, or uses a server-side manifest.
esp_https_ota_perform() returns ESP_ERR_HTTPS_OTA_IN_PROGRESS while data remains, so the loop is where you update a progress indicator or feed the task watchdog. esp_https_ota_finish() closes the connection, checks the image with esp_ota_end() and calls esp_ota_set_boot_partition(). If anything fails, esp_https_ota_abort() frees the handle without touching the boot choice, so the old image stays in place.
On a successful update, the serial console shows lines like these. Offsets, sizes and timestamps depend on your partition table and build, so treat this as an example:
I (11874) ota: running 1.0.0, offered 1.1.0
I (11884) esp_https_ota: Starting OTA...
I (11894) esp_https_ota: Writing to partition subtype 16 at offset 0x110000
I (17214) esp_https_ota: Connection closed, all data received
I (17284) esp_image: segment 0: paddr=00110020 vaddr=3f400020 size=1bd54h
I (17414) esp_https_ota: Boot partition updated
I (17424) ota: OTA upgrade successful. RebootingSubtype 16 is ota_0 (the OTA subtypes start at 0x10), so this device was running from the factory or ota_1 slot and wrote its new image to ota_0.
Testing against a local server
Before touching a real service, serve the image from your own computer with the openssl tool, which can act as a trivial HTTPS server. Create a self-signed certificate and serve the build directory:
openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out ca_cert.pem \
-days 30 -subj "/CN=192.168.1.50"
openssl s_server -WWW -key key.pem -cert ca_cert.pem -port 8070Run the second command inside the build folder, so the server can find app.bin relative to it. The certificate's common name must match the host in the URL, and the device must trust it, so embed ca_cert.pem as the CA with .cert_pem. For a self-signed server certificate that is the CA. An openssl s_server -WWW instance is fine for a test and unsuitable for anything else. After the device reboots, the new version string proves the switch: print esp_app_get_description()->version at start.
What goes wrong, and what to check
**
ESP_ERR_OTA_VALIDATE_FAILED.** The image is not a valid ESP32 application, often because you served the wrong file (a merged flash image with the bootloader in it instead of the app binary) or the download was cut short.**
ESP_ERR_NO_MEMor a TLS failure.** The task stack is too small, or the heap is short because other code holds a lot of memory. TLS needs tens of kilobytes of heap during the handshake.Certificate errors. The clock matters: a device without a correct time rejects every certificate as not yet valid. Start SNTP before the update, and check that the server name matches.
The new image is too big for the slot. The write fails partway. Compare the
.binsize with the OTA partition size.Reflashing the same version in a loop. Compare versions before installing, as above.
The update succeeds and the device boots the old image. The
otadatawrite did not complete, or the new image failed the bootloader's check. Look at the boot log foresp_image: ... invalid.
Safety beyond the download
A download that installs cleanly can still ship a broken firmware. ESP-IDF supports automatic rollback: enable CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE, and a new image boots in a pending state. If the application does not call esp_ota_mark_app_valid_cancel_rollback() before the next reset, the bootloader returns to the previous slot. The self-test post in this cluster builds that. For security, enable secure boot and flash encryption so that only images you signed will run, covered by the Secure Boot V2 post. And roll out updates in stages, as the guide in this cluster explains.


