ClavdaClavda

Connect an ESP32 to Clavda with ESP-IDF

Add the ClavdaOTA component to an ESP-IDF project, 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 4 min read

This guide uses ESP-IDF, Espressif's own framework, with idf.py. The ClavdaOTA library is an ESP-IDF component: it runs inside your firmware and connects the board to Clavda over Wi-Fi. It is written against ESP-IDF's own APIs, so the project needs no Arduino component. Once the board is connected, from anywhere, you can:

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.
  • ESP-IDF 5 or later, installed as in Espressif's Get Started guide, and a terminal where idf.py works.
  • 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

Use a project you have, or create one:

idf.py create-project clavda_app
cd clavda_app

Put clavda_device.h in the project's main/ folder, next to its source file.

Add the library

Add the library to main/idf_component.yml. Create the file if the project does not have one yet:

main/idf_component.yml
dependencies:
  clavdaota:
    git: https://github.com/victusx/ClavdaOTA-Release.git

The next idf.py build downloads it into managed_components/, and ArduinoJson with it from the ESP Component Registry. You do not need to install anything else.

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 the project's source file in main/: clavda_app.c from create-project, hello_world_main.c in a copy of hello_world, or main.c.

main/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. main/CMakeLists.txt stays as it is.

Replace the Wi-Fi name and password, and add your own code in the loop.

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 the project's top-level CMakeLists.txt. It has two 1.9 MB app slots on 4 MB of flash:

partitions.csv
# 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 select it in sdkconfig.defaults, next to it. The last line gives app_main() enough stack for the TLS handshake:

sdkconfig.defaults
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192

sdkconfig.defaults is only read when there is no sdkconfig yet. In a project you have built before, delete sdkconfig first, or set the same options in idf.py menuconfig: Serial flasher config → Flash size, Partition Table, and Component config → ESP System Settings → Main task stack size.

Flash it once over USB

Set your chip once, then build, flash and watch the output:

idf.py set-target esp32s3
idf.py -p PORT flash monitor

Use esp32, esp32s2, esp32c3 or esp32c6 for other chips, and your board's port for PORT, such as COM5 or /dev/ttyUSB0. 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.

A complete project with these files is in the library, under examples/idf/CloudDemo.

If it does not connect

Watch the output with idf.py -p PORT monitor. The library logs with the ClavdaOTA tag at the default Info level: 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. Put clavda_device.h in main/, next to the source file.
  • The board restarts with a stack overflow in main. The main task stack is still the default 3.5 KB: set CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192 as above.
  • fatal error: string: No such file or directory. C++ code is in a .c file. Rename it to .cpp, as in the C++ tab above.
  • A red shield on the device: the account is over its device limit.

More in Troubleshooting.

Next steps