Over-the-air (OTA) updates with Zephyr
Enable and customize Spotflow OTA updates on Zephyr devices.
This guide explains how to set up the Spotflow device module to automatically perform over-the-air (OTA) updates of the firmware that runs the module itself (the main firmware). If you want to update firmware on external MCUs connected to your Zephyr device, see Over-the-air (OTA) updates of external MCUs with Zephyr.
This guide targets standard Zephyr projects with MCUboot bootloader. For TI CC35X1 devices using PSA FWU, follow Over-the-air (OTA) updates on TI CC35X1.
Prerequisites
Before you start integrating your device with Spotflow, make sure you:
- have development environment with Zephyr 3.7, 4.1, 4.2, 4.3, or 4.4,
- have established a connection to the internet from the device.
When creating a new west workspace, make sure to use a supported Zephyr version, for example, using west init --mr v4.3.0.
By default, west init checks out the main branch, which may contain changes breaking the build.
Alternatively, you can follow the Quickstart: Zephyr Integration Guide to integrate Spotflow using sample application, instead of adding the spotflow module to an existing project.
OTA updates additionally require:
- A persistent Zephyr settings backend (typically NVS) so that the update state and results survive reboot.
- MCUboot enabled through Zephyr sysbuild.
- Flash support (
CONFIG_FLASH,CONFIG_FLASH_MAP,CONFIG_STREAM_FLASH) for automatic main-firmware handling. - A flash partition layout with an MCUboot secondary (update) slot. See the MCUboot documentation for details.
- Zephyr binary descriptors so that the Spotflow build ID can be embedded in the firmware image.
See the dependencies of
CONFIG_BINDESC.
Install Spotflow Device Module
Add the spotflow module as a west dependency to your west.yml file.
manifest:
projects:
- name: spotflow
revision: main
path: modules/lib/spotflow
url: https://github.com/spotflow-io/device-sdkThen, install the west dependencies using the following command:
west updateEnable OTA updates in Kconfig
Add the following to your prj.conf:
CONFIG_SPOTFLOW=y
CONFIG_SPOTFLOW_DEVICE_ID="zephyr-device-001"
CONFIG_SPOTFLOW_INGEST_KEY="{your-ingest-key}"
CONFIG_SPOTFLOW_OTA=y
# Persistent storage for OTA attempt state and results
CONFIG_SETTINGS=y
CONFIG_NVS=y
CONFIG_SETTINGS_NVS=y
# Required by automatic main-firmware handling
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_STREAM_FLASH=yAdditionally, enable the MCUboot bootloader in your sysbuild.conf file:
SB_CONFIG_BOOTLOADER_MCUBOOT=yConfirm main firmware image after reboot
After MCUboot starts the new image in test mode, the image remains unconfirmed.
Your application must validate that the new firmware works correctly and then call spotflow_confirm_main_firmware_image():
#include <zephyr/sys/reboot.h>
#include <spotflow/ota.h>
static void confirm_if_main_firmware_is_valid(void)
{
struct spotflow_ota_main_firmware_state state;
if (spotflow_get_main_firmware_update_state(&state) < 0) {
return;
}
if (state.phase != SPOTFLOW_OTA_PHASE_UNCONFIRMED) {
return;
}
/* Run your own validation first: hardware checks, self-tests, connectivity, etc. */
if (!application_self_test_passed()) {
/* Rebooting without confirmation makes MCUboot roll back to the previous image. */
sys_reboot(SYS_REBOOT_COLD);
}
spotflow_confirm_main_firmware_image(&state);
}The call confirms the image with MCUboot and schedules reporting success to Spotflow. If the image is not confirmed before the next reboot, MCUboot rolls back to the previous image and the module reports the main firmware update as failed.
Build and flash firmware
Build with sysbuild so the firmware image is signed for MCUboot and the bootloader is included in the build:
west build --sysbuild --board <board> <your-app> --pristine --build-dir build-v1Flash the first version to the device together with MCUboot:
west flash --build-dir build-v1Then prepare the version you want to deploy remotely and build it:
west build --sysbuild -b <board> <your-app> --pristine -d build-v2Deploy OTA update
Once the device is online and connected to Spotflow MQTT, create a deployment in the portal as described in Deploy Over-the-Air (OTA) Updates. For the firmware image, use the signed image from the second build. It should be located in the following path:
<your-app>/build-v2/<your-app>/zephyr/zephyr.signed.binThe default development key used to sign firmware images is publicly available. You should use a custom key in production. See Signing Binaries in Zephyr documentation.
When you start the deployment, the Spotflow device module will download the firmware image to the secondary slot, schedule the new image to run once on the next reboot (while keeping the current image as a fallback), and reboot.
After reboot, MCUboot will swap the images and the device will run the new firmware.
After your code calls spotflow_confirm_main_firmware_image(), the module makes the image permanent with MCUboot and eventually reports success to Spotflow.
If the new firmware is not confirmed before the next reboot, MCUboot will roll back to the previous image and the module will report the update as failed.
Advanced: Customize main firmware update handling
Although the device module handles the main firmware update automatically, your application code can observe and influence its progress. The main firmware update can be in one of the following phases:
NOT_RUNNING: No main firmware update in progress.PENDING_DOWNLOAD: Download of the main firmware image is ready to start.DOWNLOADING: Downloading the image into the MCUboot update slot.PENDING_UPGRADE: The image has been downloaded and the module is about to perform the MCUboot test upgrade.PENDING_REBOOT: The test upgrade has been requested and the device is about to reboot.UNCONFIRMED: Device rebooted into the new image; awaiting confirmation.
Progress observation
Define the function spotflow_on_main_firmware_update_progressed() to be informed when the phase changes:
void spotflow_on_main_firmware_update_progressed(const struct spotflow_ota_main_firmware_state *state)
{
/* React to state->phase and state->result. For example, update UI, logs, or LEDs. */
}When the phase changes to NOT_RUNNING, the module has finished the current update attempt and state->result contains its result.
Pausing and resuming
In many real-world applications, the device might not be ready to update firmware immediately.
Because spotflow_on_main_firmware_update_progressed() is called on the OTA worker thread for most phases (PENDING_DOWNLOAD, DOWNLOADING, PENDING_UPGRADE, PENDING_REBOOT), blocking its execution effectively postpones the update:
void spotflow_on_main_firmware_update_progressed(const struct spotflow_ota_main_firmware_state *state)
{
if (state->phase == SPOTFLOW_OTA_PHASE_PENDING_REBOOT) {
LOG_INF("Device will reboot in 10 seconds to finish the firmware update.");
k_sleep(K_SECONDS(10));
}
}While this approach works for simple cases, it can be cumbersome in more complex scenarios, such as when the device needs confirmation from the user before continuing.
You can use spotflow_pause_main_firmware_update() and spotflow_resume_main_firmware_update() for this purpose:
void spotflow_on_main_firmware_update_progressed(const struct spotflow_ota_main_firmware_state *state)
{
if (state->phase == SPOTFLOW_OTA_PHASE_PENDING_DOWNLOAD) {
LOG_INF("New firmware update is ready to be downloaded. Press A to perform the update.");
spotflow_pause_main_firmware_update(NULL);
}
}
void on_button_a_pressed(void)
{
struct spotflow_ota_main_firmware_state state;
if (spotflow_get_main_firmware_update_state(&state) < 0) {
return;
}
if (!state.is_paused) {
return;
}
if (spotflow_resume_main_firmware_update(&state) < 0) {
return;
}
LOG_INF("Proceeding with the firmware update.");
}Pausing an update effectively puts the OTA worker thread to sleep in its current phase until it is resumed.
While pausing an update in spotflow_on_main_firmware_update_progressed() is most common, you can do it from any thread.
When your application pauses an update in the phase DOWNLOADING, the download will stop after the next firmware image block is received.
After the update is resumed, the download will resume from the point where it was paused.
This feature is useful, for example, when the device does not have stable network connectivity and needs to prioritize other communication when the signal is low.
Pausing in PENDING_REBOOT postpones only the reboot initiated by the Spotflow device module.
The MCUboot test upgrade has already been scheduled.
If the device reboots for another reason while paused, the device still boots into the new image and continues with the UNCONFIRMED phase.
An update cannot be paused in the phase UNCONFIRMED, because it is not under the Spotflow device module's control when your application confirms the image.
Aborting
In certain cases, you might want to allow your application to completely reject the update instead of just pausing it.
You can do this by calling spotflow_abort_main_firmware_update().
This aborts the update and reports the main firmware update as failed.
The function spotflow_abort_main_firmware_update() cannot be used in the phases PENDING_REBOOT and UNCONFIRMED.
The only way to fail an update in these phases is to force a rollback by rebooting in the UNCONFIRMED phase.
How device module works
The Spotflow device module exchanges CBOR OTA messages with the cloud, either directly over MQTT or through a BLE gateway. This walkthrough uses direct MQTT; the same OTA API and firmware backend also work with BLE. See Over-the-air (OTA) updates with MQTT for the cloud message schemas. In order to store state across reboots, the module persists the results of the latest update attempt, installed versions of each firmware, and main firmware probation information.
Each update attempt received by the device module contains one or more firmware updates. The module processes each firmware update as follows:
- If the installed version of the firmware already matches the requested version, the module immediately reports success.
- If the firmware is not main or
CONFIG_SPOTFLOW_OTA_AUTO_HANDLE_MAIN_FIRMWAREis disabled, the module passes it tospotflow_on_handle_firmware_update()and reports the result to the cloud. This guide explains how to use this API to handle firmware updates of external MCUs. - If the firmware is main and automatic handling is enabled, the module downloads the image into the MCUboot secondary slot, persists the expected build ID, requests a test upgrade, and reboots. The selected transport obtains the bytes directly over HTTPS or through the BLE gateway.
On the next boot, it compares the running image build ID with the expected build ID:
- If they match, the module waits in
UNCONFIRMEDuntil your application confirms the image. After confirmation, the module reports the main firmware update as succeeded. - If they do not match (because of a rollback or an unsuccessful swap), the module reports the main firmware update as failed.
- If they match, the module waits in
- When the firmware update does not succeed, the module cancels the remaining firmware updates in the attempt.
The module also automatically informs the cloud about the last OTA update attempt received by the device. This information is sent whenever the device connects to the cloud as part of the session metadata. The cloud can use this information to repeat a device update attempt if the device was updated manually. See this section for more details.
See the implementation notes in the GitHub repository for more details.
Threading
The selected transport handles OTA message delivery, while a dedicated OTA update worker thread handles downloads and firmware updates. Callbacks run on the following threads for both MQTT and BLE:
| Callback | Thread |
|---|---|
spotflow_on_handle_firmware_update() | update worker |
spotflow_on_main_firmware_update_progressed() | update worker in most cases; notification about the phases NOT_RUNNING and UNCONFIRMED might happen on the first thread that calls the public API of OTA updates |
spotflow_on_update_canceled() | Zephyr system workqueue |
Prefer not to perform blocking work in spotflow_on_update_canceled() unless you are sure that it will not negatively impact other work items in the system workqueue.
Security
With direct MQTT, firmware images are downloaded over HTTPS using the same TLS configuration as the Spotflow MQTT connection.
Along with the URL, the device receives an OTA secret for each firmware image and uses it in the HTTP Authorization header. With BLE, the trusted gateway receives the URL and secret and performs the HTTPS download. See BLE security.
When handling firmware updates manually, do not log their URLs or secrets to reduce the risk of accidental exposure.
Main firmware image requirements
Automatic main-firmware handling requires a valid Spotflow build ID. A downloaded image whose build ID matches the currently running firmware is rejected before installation, even if the cloud version string differs. Build a changed image for each update.
The MCUboot backend accepts directly readable MCUboot v1 images. Encrypted, compressed and split images are not supported. Use the complete signed image produced by the build.
Kconfig options
Summary of OTA Kconfig options:
| Option | Default | Purpose |
|---|---|---|
CONFIG_SPOTFLOW_OTA | n | Enable support of Spotflow OTA updates |
CONFIG_SPOTFLOW_OTA_AUTO_HANDLE_MAIN_FIRMWARE | y | Automatically handle main firmware OTA updates |
CONFIG_SPOTFLOW_OTA_LOG_LEVEL | CONFIG_SPOTFLOW_MODULE_DEFAULT_LOG_LEVEL | Log level for OTA update messages |
CONFIG_SPOTFLOW_OTA_MAX_ARTIFACTS | 4 | Maximum number of firmware images in an OTA update attempt |
CONFIG_SPOTFLOW_OTA_THREAD_STACK_SIZE | 6144 | OTA update worker thread stack; includes TLS/HTTP downloads with direct MQTT |
CONFIG_SPOTFLOW_OTA_HTTP_TIMEOUT_MS | 120000 | Direct MQTT only: per-download-attempt time budget; blocking DNS, TCP, and TLS operations use separate timeouts and can exceed it |
CONFIG_SPOTFLOW_OTA_BLE_REQUEST_TIMEOUT_MS | 120000 | BLE only: deadline for one OPEN, READ or CLOSE exchange, including retransmissions every two seconds; the complete artifact may take longer |
CONFIG_SPOTFLOW_OTA_DOWNLOAD_BUFFER_SIZE | CONFIG_IMG_BLOCK_BUF_SIZE if image management is enabled; otherwise 512 | HTTP receive buffer or BLE artifact block size in bytes; BLE range is 1–4096 |
CONFIG_SPOTFLOW_OTA_DOWNLOAD_RETRY_INITIAL_DELAY_MS | 5000 | Initial ceiling for randomized transient download retry delays |
CONFIG_SPOTFLOW_OTA_DOWNLOAD_RETRY_MAX_DELAY_MS | 300000 | Maximum ceiling for randomized transient download retry delays |
Transient download failures resume from the last consumed offset, using HTTP Range requests or a new BLE artifact transfer. The retry delay ceiling doubles after each failure until it reaches the configured maximum. Each actual delay is randomized between half and all of the current ceiling to avoid synchronized retry traffic from multiple devices. Retries continue until the download succeeds, is canceled, or encounters a non-retryable error.
Reference repository materials
- Public API:
spotflow/ota.h,spotflow/downloader.h - OTA sample
- OTA Kconfig options