Connect an ESP32 to Clavda with the ClavdaOTA library
Add the ClavdaOTA library to an Arduino IDE or PlatformIO sketch, flash the ESP32 once over USB, and manage it over Wi-Fi from then on.
Updated 3 min read
An ESP32 does not run the Clavda agent. It runs the ClavdaOTA library inside your own firmware, which connects the board to Clavda over Wi-Fi. Once it is connected, from anywhere, you can:
- update its firmware over the air, with no cable,
- watch its serial output and type commands back,
- build a live dashboard of its readings and controls.
It works on the ESP32, ESP32-S2, S3, C3 and C6. Like every Clavda device, the board dials out to the relay, so it needs no public address and no port forwarding.
What you need
- An ESP32 board and a USB cable, for the first flash only.
- The Arduino IDE or PlatformIO, set up for ESP32 boards.
- 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.
Install the library
Open the ClavdaOTA library on GitHub, choose Code → Download ZIP, then in the IDE Sketch → Include Library → Add .ZIP Library…
A ZIP install does not fetch dependencies, so also install these two from Tools → Manage Libraries…:
- WebSockets by Markus Sattler
- ArduinoJson by Benoit Blanchon
Add the library to lib_deps; its dependencies come with it. This platformio.ini also sets a partition table with room for an update:
[env:esp32]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.11/platform-espressif32.zip
board = esp32dev
framework = arduino
monitor_speed = 115200
board_build.partitions = min_spiffs.csv
lib_deps =
https://github.com/victusx/ClavdaOTA-Release.git
Add the Clavda code to your sketch
Put clavda_device.h next to the sketch (Arduino IDE) or in include/ (PlatformIO). This is a complete sketch. The five numbered comments are what every Clavda sketch does, and it is the same for every board: what makes it this board is the clavda_device.h beside it.
#include <Arduino.h>
#include <WiFi.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"
const char *ssid = "your-wifi";
const char *password = "your-wifi-password";
void setup() {
Serial.begin(115200);
// 3. Join Wi-Fi. No need to wait: the library connects once it is up.
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// 4. Connect to Clavda. False means clavda_device.h is missing.
if (!ClavdaOTA.begin(CLAVDA_DEVICE)) {
Serial.println("No credentials: add clavda_device.h from the console.");
}
}
void loop() {
// 5. Keep the connection alive; updates install from here too.
// Call it on every loop, and avoid long delay() calls.
ClavdaOTA.loop();
// Your application code.
}
Replace the Wi-Fi name and password, and add your own code after ClavdaOTA.loop().
Choose a partition scheme with room for updates
An over-the-air update needs two app slots, and the default layout leaves too little room. In the Arduino IDE, set Tools → Partition Scheme → Minimal SPIFFS (1.9MB APP with OTA). With PlatformIO, the board_build.partitions = min_spiffs.csv line above does the same.
Flash it once over USB
Upload the sketch as usual. 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.
If it does not connect
Check that the board joined Wi-Fi, then open the USB serial monitor at 115200 baud with Tools → Core Debug Level → Info. The library logs when it reaches the relay, why the relay refused it, or that it found no credentials. More in Troubleshooting.