wb-idf-iot-server
ESP-IDF component for the LwM2M connection to the WhirlingBits IoT Server.
Builds on wb-idf-lwm2m and provides a simplified, IoT Server specific API.
Deutsche Fassung: README.de.md
Features
- LwM2M registration with the WhirlingBits IoT Server (Leshan-based)
- Automatic registration updates (keepalive)
- Device Object (/3) with device information
- Firmware Update Object (/5) for OTA via the server
- Complete OTA procedure (HTTP download, flash, verify, reboot)
- Auto and manual mode for download and apply
- Rollback protection (mark_valid after a successful boot)
- DTLS-PSK and X.509 certificates (via wb-idf-coap-client)
- Event-based architecture (callback)
- Configurable via
menuconfig - Custom LwM2M objects (e.g. IPSO sensors) can be added
Architecture
┌──────────────────────────────────────────┐
│ User Application │
├──────────────────────────────────────────┤
│ wb-idf-iot-server │
│ ├── IoT Server specific logic │
│ ├── Device Object (/3) │
│ ├── Firmware Update Object (/5) │
│ └── Event forwarding │
├──────────────────────────────────────────┤
│ wb-idf-lwm2m │
│ ├── LwM2M Registration Interface │
│ ├── Object/Resource Model │
│ └── SenML-JSON / Link-Format Encoding │
├──────────────────────────────────────────┤
│ wb-idf-coap-client │
│ └── CoAP + DTLS (PSK / PKI) │
├──────────────────────────────────────────┤
│ libcoap + mbedTLS + ESP-IDF │
└──────────────────────────────────────────┘
OTA flow (LwM2M Object 5)
IoT Server ESP32 (wb-idf-iot-server)
│ │
│ 1. Write /5/0/1 (Package URI) │
│──────────────────────────────────────>│
│ │ → State = DOWNLOADING
│ │ → HTTP GET firmware.bin
│ │ → Flash to OTA partition
│ │ → Verify image
│ │ → State = DOWNLOADED
│ │
│ 2. Read /5/0/3 (State) │
│──────────────────────────────────────>│
│<──────────────────────────────────────│ → "2" (DOWNLOADED)
│ │
│ 3. Execute /5/0/2 (Update) │
│──────────────────────────────────────>│
│ │ → State = UPDATING
│ │ → Set boot partition
│ │ → esp_restart()
│ │
│ ~~~ REBOOT ~~~ │
│ │
│ 4. Re-Registration │
│<──────────────────────────────────────│
│ │ → mark_valid()
│ 5. Read /5/0/5 (Result) │ → Result = SUCCESS
│──────────────────────────────────────>│
│<──────────────────────────────────────│ → "1" (SUCCESS)
│ │
Requirements
- ESP-IDF >= 5.0
- Network connection (Wi-Fi or Ethernet)
- A running WhirlingBits IoT Server
- Partition table with at least two OTA partitions (for OTA updates)
Installation
As a local component
cd components
git clone https://gitlab.whirlingbits.de/whirlingbits/wb-idf-iot-server.git
git clone https://gitlab.whirlingbits.de/whirlingbits/wb-idf-lwm2m.git
git clone https://gitlab.whirlingbits.de/whirlingbits/wb-idf-coap-client.git
Via idf_component.yml
dependencies:
whirlingbits/wb-idf-iot-server:
version: "*"
git: https://gitlab.whirlingbits.de/whirlingbits/wb-idf-iot-server.git
Configuration
idf.py menuconfig
Under WhirlingBits IoT Server Client:
| Option | Default | Description |
|---|---|---|
| Server Host | 192.168.1.100 |
IP or hostname of the IoT Server |
| CoAP Port | 5683 |
Plain CoAP |
| CoAPS Port | 5684 |
DTLS encrypted |
| Endpoint Name | esp32-device |
LwM2M endpoint name |
| Lifetime | 300 s |
Registration lifetime |
Usage
Minimal (plain CoAP, without DTLS)
#include "wb_iot_server.h"
static void iot_event_handler(wb_iot_server_event_data_t *event, void *user_ctx)
{
switch (event->type) {
case WB_IOT_EVT_REGISTERED:
ESP_LOGI("app", "Registered with the IoT Server!");
break;
case WB_IOT_EVT_DISCONNECTED:
ESP_LOGW("app", "Connection lost");
break;
case WB_IOT_EVT_OTA_PACKAGE_URI:
ESP_LOGI("app", "OTA URL: %.*s", (int)event->data_len, (const char *)event->data);
break;
case WB_IOT_EVT_OTA_EXECUTE:
ESP_LOGI("app", "Start OTA update!");
break;
default:
break;
}
}
void app_main(void)
{
/* Initialize Wi-Fi ... */
wb_iot_server_config_t config = {
.server_host = "192.168.1.100",
.endpoint_name = "my-esp32",
.security = WB_IOT_SEC_NONE,
.lifetime = 300,
.device_info = {
.manufacturer = "WhirlingBits",
.model_number = "ESP32-S3",
.firmware_version = "1.0.0",
.device_type = "esp32s3",
},
.event_handler = iot_event_handler,
};
wb_iot_server_handle_t iot = wb_iot_server_init(&config);
wb_iot_server_start(iot);
}
With DTLS-PSK
/* PSK key (from the IoT Server admin API or bootstrap) */
static const uint8_t psk_key[] = { 0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 };
wb_iot_server_config_t config = {
.server_host = "iot.example.com",
.endpoint_name = "device-001",
.security = WB_IOT_SEC_PSK,
.psk = {
.identity = "device-001",
.key = psk_key,
.key_len = sizeof(psk_key),
},
.lifetime = 3600,
.device_info = {
.manufacturer = "WhirlingBits",
.model_number = "ESP32-S3",
.firmware_version = "1.0.0",
.device_type = "esp32s3",
},
.event_handler = iot_event_handler,
};
Custom telemetry objects
/* IPSO Temperature Object (3303) */
static float current_temperature = 22.5f;
static esp_err_t temp_read_cb(uint16_t obj_id, uint16_t inst_id, uint16_t res_id,
wb_lwm2m_value_t *value, void *user_ctx)
{
if (res_id == 5700) { /* Sensor Value */
*value = WB_LWM2M_VALUE_FLOAT(current_temperature);
return ESP_OK;
}
return ESP_ERR_NOT_FOUND;
}
static const wb_lwm2m_object_def_t temp_obj = {
.id = 3303,
.name = "Temperature",
.instances = {{ .id = 0 }},
.instance_count = 1,
.resources = {
{ .id = 5700, .type = WB_LWM2M_RES_TYPE_FLOAT,
.operations = WB_LWM2M_OP_READ, .read_cb = temp_read_cb },
},
.resource_count = 1,
};
/* Register before wb_iot_server_start() */
wb_iot_server_add_object(iot, &temp_obj);
wb_iot_server_start(iot);
/* Later: notify the server about a new value */
current_temperature = 23.1f;
wb_iot_server_notify(iot, 3303, 0, 5700);
Handling OTA updates
Auto mode (default)
In auto mode the component handles the complete OTA procedure:
wb_iot_server_config_t config = {
.server_host = "192.168.1.100",
.endpoint_name = "my-esp32",
.security = WB_IOT_SEC_PSK,
.psk = { .identity = "my-esp32", .key = psk_key, .key_len = sizeof(psk_key) },
.device_info = {
.manufacturer = "WhirlingBits",
.model_number = "ESP32-S3",
.firmware_version = "1.0.0",
.device_type = "esp32s3",
},
.ota = {
.mode = WB_IOT_OTA_MODE_AUTO, /* download starts automatically */
.apply_mode = WB_IOT_OTA_APPLY_AUTO, /* reboot after the download */
.http_timeout_ms = 30000,
.skip_cert_verify = true, /* development only! */
},
.event_handler = iot_event_handler,
};
Flow in auto mode:
- The server writes the Package URI to
/5/0/1→ the download starts automatically - The firmware is downloaded via HTTP and flashed to the OTA partition
- The image is verified (header + SHA256)
- The boot partition is set → reboot
- After the reboot: call
wb_iot_server_ota_mark_valid()
Manual mode
In manual mode the application has full control:
wb_iot_server_config_t config = {
/* ... */
.ota = {
.mode = WB_IOT_OTA_MODE_MANUAL,
.apply_mode = WB_IOT_OTA_APPLY_MANUAL,
},
.event_handler = iot_event_handler,
.user_ctx = NULL, /* set later */
};
static void iot_event_handler(wb_iot_server_event_data_t *event, void *user_ctx)
{
wb_iot_server_handle_t iot = (wb_iot_server_handle_t)user_ctx;
wb_iot_ota_handle_t ota = wb_iot_server_get_ota(iot);
switch (event->type) {
case WB_IOT_EVT_OTA_PACKAGE_URI: {
const char *url = (const char *)event->data;
ESP_LOGI("app", "OTA available: %s", url);
/* Check whether an update is wanted (e.g. battery level, user confirmation) */
if (should_update()) {
wb_iot_server_set_fw_state(iot, WB_LWM2M_FW_STATE_DOWNLOADING);
wb_iot_ota_start_download(ota, url);
}
break;
}
case WB_IOT_EVT_OTA_DOWNLOAD_DONE:
ESP_LOGI("app", "Download finished, waiting for Execute from the server");
wb_iot_server_set_fw_state(iot, WB_LWM2M_FW_STATE_DOWNLOADED);
break;
case WB_IOT_EVT_OTA_EXECUTE:
ESP_LOGI("app", "Execute received, applying update...");
wb_iot_server_set_fw_state(iot, WB_LWM2M_FW_STATE_UPDATING);
wb_iot_ota_apply(ota); /* reboots */
break;
case WB_IOT_EVT_OTA_DOWNLOAD_FAILED:
ESP_LOGE("app", "OTA download failed");
wb_iot_server_set_fw_state(iot, WB_LWM2M_FW_STATE_IDLE);
wb_iot_server_set_fw_result(iot, WB_LWM2M_FW_RESULT_CONN_LOST);
break;
default:
break;
}
}
Rollback protection
After every OTA reboot the new firmware must be confirmed:
void app_main(void)
{
/* Check whether we booted after an OTA update */
if (wb_iot_ota_is_pending_verify()) {
ESP_LOGI("app", "New firmware started — verifying...");
/* Run a self test here (Wi-Fi, sensors, etc.) */
if (self_test_passed()) {
wb_iot_server_ota_mark_valid();
ESP_LOGI("app", "Firmware confirmed");
} else {
ESP_LOGE("app", "Self test failed — rollback on next reboot");
esp_restart();
}
}
/* Normal application start ... */
}
Querying OTA progress
wb_iot_ota_handle_t ota = wb_iot_server_get_ota(iot);
wb_iot_ota_progress_t progress;
wb_iot_ota_get_progress(ota, &progress);
ESP_LOGI("app", "OTA: %u%% (%zu / %zu bytes)",
progress.progress_percent, progress.bytes_written, progress.total_size);
API
The complete reference with all parameters is part of the API documentation (pages IoT Server Client, OTA Update and Configuration).
| Function | Description |
|---|---|
wb_iot_server_init() |
Create and configure the client |
wb_iot_server_add_object() |
Register custom LwM2M objects |
wb_iot_server_start() |
Connect and register |
wb_iot_server_stop() |
Deregister and disconnect |
wb_iot_server_destroy() |
Release resources |
wb_iot_server_is_registered() |
Check the registration status |
wb_iot_server_notify() |
Notify the server about a value change |
wb_iot_server_set_fw_state() |
Set the OTA state (/5/0/3) |
wb_iot_server_set_fw_result() |
Set the OTA result (/5/0/5) |
wb_iot_server_get_lwm2m_client() |
Access the LwM2M client |
wb_iot_server_get_ota() |
Access the OTA module |
wb_iot_server_ota_mark_valid() |
Confirm the firmware after OTA |
wb_iot_server_config_load_nvs() |
Load the configuration from provisioning |
OTA module
| Function | Description |
|---|---|
wb_iot_ota_init() |
Initialize the OTA module |
wb_iot_ota_start_download() |
Start the HTTP download (asynchronous) |
wb_iot_ota_apply() |
Apply the update and reboot |
wb_iot_ota_abort() |
Abort a running download |
wb_iot_ota_get_progress() |
Query the progress |
wb_iot_ota_get_status() |
Query the status |
wb_iot_ota_mark_valid() |
Confirm the new firmware |
wb_iot_ota_is_pending_verify() |
Check the rollback status |
wb_iot_ota_destroy() |
Release resources |
Events
| Event | Description |
|---|---|
WB_IOT_EVT_CONNECTED |
CoAP connection established |
WB_IOT_EVT_DISCONNECTED |
Connection lost |
WB_IOT_EVT_REGISTERED |
LwM2M registration succeeded |
WB_IOT_EVT_REG_FAILED |
Registration failed |
WB_IOT_EVT_REG_UPDATED |
Registration update succeeded |
WB_IOT_EVT_OTA_PACKAGE_URI |
The server wrote the download URL |
WB_IOT_EVT_OTA_EXECUTE |
The server triggered the update |
WB_IOT_EVT_OTA_DOWNLOAD_START |
OTA download started |
WB_IOT_EVT_OTA_DOWNLOAD_DONE |
Download completed successfully |
WB_IOT_EVT_OTA_DOWNLOAD_FAILED |
Download failed |
WB_IOT_EVT_OTA_APPLYING |
Reboot imminent |
WB_IOT_EVT_ERROR |
Error |
Project structure
wb-idf-iot-server/
├── CMakeLists.txt ← ESP-IDF component registration
├── Kconfig ← menuconfig integration
├── idf_component.yml ← dependency on wb-idf-lwm2m
├── README.md / README.de.md ← overview (English / German)
├── Doxyfile, DoxygenLayout.xml, wb-idf-iot-server.dox ← documentation
├── include/
│ ├── wb_iot_server.h ← public API (connection, LwM2M, provisioning)
│ ├── wb_iot_server_config.h ← defaults and limits
│ └── wb_iot_ota.h ← OTA module API
└── src/
├── wb_iot_server.c ← main implementation and LwM2M integration
├── wb_iot_ota.c ← OTA: HTTP download, flash, verify, reboot
└── wb_iot_nvs_config.c ← configuration from provisioning (NVS)
Documentation
The headers are commented in two languages (Doxygen language filters \~english / \~german):
/**
* \~english
* @brief Starts the download.
* @param url Firmware URL.
*
* \~german
* @brief Startet den Download.
* @param url Firmware-URL.
* \~
*/
Short texts fit on one line: /**< \~english Event type \~german Art des Events */.
Group titles are defined in wb-idf-iot-server.dox (@defgroup id Title \~german Titel).
README.de.md is the German translation of this file. After changing this file, update the
translation and mark it as current with python3 <wb-docs>/tools/i18n.py readme-stamp README.de.md.
Provisioning on the IoT Server
Before a device can connect, it must be known to the server:
# Log in to the IoT Server
TOKEN=$(curl -s http://localhost:18080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"sysadmin","password":"..."}' | jq -r .token)
# Provision the device with a PSK
curl -X POST http://localhost:18080/api/admin/bootstrap/devices \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"endpoint":"my-esp32","pskIdentity":"my-esp32","pskKey":"deadbeef01020304"}'
Alternatively: LWM2M_ALLOW_UNKNOWN_DEVICES=true for development (not for production).
Setup via app (Bluetooth)
The IoT Server app creates the device on the server and transfers the server address, endpoint
name and PSK during Bluetooth provisioning (esp-idf-provisioning, endpoint wb-iot) into the NVS
namespace wb_iot. The application loads them:
static wb_iot_nvs_config_t s_provisioned; /* must live as long as the handle */
wb_iot_server_config_t cfg = { .device_info = { ... }, .event_handler = on_event };
if (wb_iot_server_config_load_nvs(&s_provisioned, &cfg) != ESP_OK) {
/* not set up: start provisioning or use the Kconfig values */
}
wb_iot_server_handle_t iot = wb_iot_server_init(&cfg);
License
Apache-2.0 — see LICENSE.
Components
IoT Server Client
Connection, registration, custom objects and firmware state.
OTA Update
HTTP(S) download, flashing, verification, reboot and rollback protection.
Configuration
Defaults (Kconfig) and internal limits.
