Connect an ESP32 to Clavda with PlatformIO and ESP-IDF
Add the ClavdaOTA library to a PlatformIO project with the ESP-IDF framework, in C or C++, give it a partition table with two app slots, flash the ESP32 once over USB, and manage it over Wi-Fi from then on.
Updated 5 min read
This guide uses PlatformIO with the ESP-IDF framework, framework = espidf: there is no Arduino core in the project. The ClavdaOTA library runs inside your firmware and connects the board to Clavda over Wi-Fi. It is written against ESP-IDF's own APIs, so it needs nothing from Arduino. Once the board is connected, from anywhere, you can:
- update its firmware over the air, with no cable,
- watch its serial output,
ESP_LOGxincluded, and type commands back, - build a live dashboard of its readings and controls.
It works on the ESP32, ESP32-S2, S3, C3 and C6. The board dials out to the relay, so it needs no public address and no port forwarding. You can write the firmware in C or in C++; every step shows both.
What you need
- An ESP32 board and a USB cable, for the first flash only.
- VS Code with the PlatformIO IDE extension, or PlatformIO Core on the command line.
- A 2.4 GHz Wi-Fi network the board can join.
- A Clavda account. See Before you start.
Set up the board
Add the device and download its file
In Clavda, open Devices → Add Device, choose the installation platform ESP32, and save. Reopen the device and press Get Device File to download clavda_device.h: the relay address and this board's credentials, compiled into the firmware.
clavda_device.h is a secret
It works like a password for this one board. Keep it out of git, for example with a .gitignore
entry. Every download is recorded in your Clavda notifications.
Create a project
In VS Code, open PlatformIO Home → New Project, pick your board, and choose the framework Espidf. Put clavda_device.h in the project's src/ folder, next to main.c.
Set up platformio.ini
Replace the project's platformio.ini with this. board_build.partitions points at the partition table of a later step, and board_upload.flash_size matches the 4 MB it is laid out for:
[env:esp32s3]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.11/platform-espressif32.zip
board = esp32-s3-devkitc-1
framework = espidf
monitor_speed = 115200
board_build.partitions = partitions.csv
board_upload.flash_size = 4MB
Set board to yours: esp32dev, esp32-s2-saola-1, esp32-s3-devkitc-1, esp32-c3-devkitm-1 or esp32-c6-devkitc-1 for Espressif's own boards.
There is no lib_deps: the next step adds the library as an ESP-IDF component instead. That way PlatformIO and ESP-IDF's own tools build the project alike, and the library is compiled once. With lib_deps as well, PlatformIO would build a second copy.
Add the library
Create src/idf_component.yml. On the first build, ESP-IDF's component manager downloads ClavdaOTA, and its dependency ArduinoJson with it:
dependencies:
clavdaota:
git: https://github.com/victusx/ClavdaOTA-Release.git
Then name it in src/CMakeLists.txt. PlatformIO generated this file; add REQUIRES clavdaota to its last line:
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
idf_component_register(SRCS ${app_sources} REQUIRES clavdaota)
Last, the CMakeLists.txt next to platformio.ini needs one more line, before include(). ESP-IDF's own tools, such as the ESP-IDF extension for VS Code and idf.py, look for app_main() in main/; the line points them at src/, where PlatformIO keeps it. Keep the name PlatformIO gave the project in project():
cmake_minimum_required(VERSION 3.16.0)
list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/src")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(my_project)
Code that uses an ESP-IDF driver
With a REQUIRES list, ESP-IDF's tools let the code in src/ use only the components on it. Add
any driver your code includes, for example esp_driver_ledc for driver/ledc.h, as the
dashboard does. PlatformIO's own build does not need it, but
the list keeps both builds the same.
Give it a partition table with room for updates
An over-the-air update needs two app slots, and ESP-IDF's default table has a single app. Put this partitions.csv next to platformio.ini. It has two 1.9 MB app slots, and fits boards with 4 MB of flash or more:
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x1E0000,
app1, app, ota_1, 0x1F0000, 0x1E0000,
spiffs, data, spiffs, 0x3D0000, 0x20000,
coredump, data, coredump, 0x3F0000, 0x10000,
Then add sdkconfig.defaults, next to it. It tells ESP-IDF the same flash size as platformio.ini, selects the table, and the last line gives app_main() enough stack for the TLS handshake:
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
PlatformIO only reads sdkconfig.defaults when the project has no sdkconfig.<env> yet, sdkconfig.esp32s3 here. If you built the project before, delete that file first, or set the same options with pio run -t menuconfig. ESP-IDF's own tools follow the same rule with a file named sdkconfig.
Write the code
This is a complete program. The five numbered comments are what every Clavda program does, and it is the same for every board: what makes it this board is the clavda_device.h beside it.
Replace the contents of src/main.c:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// 1. Your firmware version. Change it for every build you upload: the
// console counts an update done once the board reports it.
#define CLAVDA_FIRMWARE_VERSION "1.0.0"
// 2. The library, then this board's credentials from the console.
#include <ClavdaOTA.h>
#include "clavda_device.h"
static const char *ssid = "your-wifi";
static const char *password = "your-wifi-password";
void app_main(void) {
// 3. Join Wi-Fi. This also sets up NVS, esp_netif and the event loop, and
// reconnects whenever the network drops. No need to wait for it.
clavda_wifi_begin(ssid, password);
// 4. Connect to Clavda. False means clavda_device.h is missing.
if (!clavda_begin(&CLAVDA_DEVICE)) {
printf("No credentials: add clavda_device.h from the console.\n");
}
for (;;) {
// 5. Keep the connection alive; updates install from here too.
clavda_loop();
// Your application code.
// Lets the idle task run, so the task watchdog stays quiet.
vTaskDelay(pdMS_TO_TICKS(10));
}
}
From a .c file, ClavdaOTA.h gives you the library's C API, the clavda_* functions.
C++ needs a source file ending in .cpp: rename src/main.c to src/main.cpp. PlatformIO builds every file in src/, so nothing else changes.
#include <cstdio>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// 1. Your firmware version. Change it for every build you upload: the
// console counts an update done once the board reports it.
#define CLAVDA_FIRMWARE_VERSION "1.0.0"
// 2. The library, then this board's credentials from the console.
#include <ClavdaOTA.h>
#include "clavda_device.h"
static const char *ssid = "your-wifi";
static const char *password = "your-wifi-password";
// ESP-IDF calls app_main() as C, so it is declared extern "C".
extern "C" void app_main(void) {
// 3. Join Wi-Fi. This also sets up NVS, esp_netif and the event loop, and
// reconnects whenever the network drops. No need to wait for it.
clavda_wifi_begin(ssid, password);
// 4. Connect to Clavda. False means clavda_device.h is missing.
if (!ClavdaOTA.begin(CLAVDA_DEVICE)) {
printf("No credentials: add clavda_device.h from the console.\n");
}
for (;;) {
// 5. Keep the connection alive; updates install from here too.
ClavdaOTA.loop();
// Your application code.
// Lets the idle task run, so the task watchdog stays quiet.
vTaskDelay(pdMS_TO_TICKS(10));
}
}
From a .cpp file, ClavdaOTA.h gives you the C++ API, ClavdaOTA, ClavdaSerial and ClavdaWidget. Wi-Fi still starts with clavda_wifi_begin().
Replace the Wi-Fi name and password, and add your own code in the loop.
Flash it once over USB
Press Upload in the PlatformIO toolbar, then Serial Monitor. From a terminal:
pio run -t upload
pio device monitor
The first build downloads ESP-IDF and its tools, which takes a few minutes. As soon as the board is on Wi-Fi, it turns green under Devices in Clavda. From here on you can update it without the cable.
The same project also builds with the ESP-IDF extension for VS Code, or idf.py build, once its target is set to your chip. Those tools read sdkconfig.defaults but not platformio.ini, and write the firmware to build/ instead of .pio/build/.
If it does not build
ClavdaOTA.h: No such file or directory. The build has not found the library: checksrc/idf_component.yml, andREQUIRES clavdaotainsrc/CMakeLists.txt, as in Add the library.undefined reference to app_main, building with the ESP-IDF extension oridf.py. Those tools have not foundsrc/: add thelist(APPEND EXTRA_COMPONENT_DIRS ...)line to theCMakeLists.txtnext toplatformio.ini.driver/ledc.h: No such file or directory, or another ESP-IDF header, with the same tools: add its component toREQUIRESinsrc/CMakeLists.txt.fatal error: string: No such file or directory. C++ code is in a.cfile. Rename it to.cpp, as in the C++ tab above.
If it does not connect
Watch the output with Serial Monitor, or pio device monitor. The library logs with the ClavdaOTA tag: when it reaches the relay, why the relay refused it, or that it found no credentials. Then check:
- Wi-Fi. The board must join a 2.4 GHz network. Check the name and password in the code.
No credentials: add clavda_device.h from the console.The firmware was built without the device file. Putclavda_device.hinsrc/, next tomain.c.- The board restarts with a stack overflow in
main. The main task stack is still the default 3.5 KB: setCONFIG_ESP_MAIN_TASK_STACK_SIZE=8192as above, and deletesdkconfig.<env>so it is read. - A red shield on the device: the account is over its device limit.
More in Troubleshooting.