Connect an ESP32 to Clavda with the Arduino IDE
Add the ClavdaOTA library to an Arduino IDE sketch, in C++ or in C, flash the ESP32 once over USB, and manage it over Wi-Fi from then on.
Updated 3 min read
This guide uses the Arduino IDE and Espressif's Arduino core for the ESP32. The ClavdaOTA library runs inside your sketch and 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. The board dials out to the relay, so it needs no public address and no port forwarding. You can write the sketch in C++ or in C; every step shows both.
What you need
- An ESP32 board and a USB cable, for the first flash only.
- The Arduino IDE 2, with the esp32 boards package by Espressif, version 3.x, from Tools → Board → Boards Manager…
- 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 ArduinoJson by Benoit Blanchon from Tools → Manage Libraries…
Write the sketch
Create a sketch and save it. Open its folder with Sketch → Show Sketch Folder and put clavda_device.h there, next to the .ino.
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.
#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.
}
In C, the program goes in a .c file in the sketch folder. Arduino calls setup() and loop() as C++, so the .ino stays, and only hands them over to the C file:
extern "C" void app_setup(void);
extern "C" void app_loop(void);
void setup() {
app_setup();
}
void loop() {
app_loop();
}
Add the C file with the ⋯ menu at the right of the editor's tabs, New Tab, named app.c:
// 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_setup(void) {
// Starts Serial at 115200 baud too, and prints there.
clavda_serial_begin(115200, 0);
// 3. Join Wi-Fi. No need to wait: the library connects once it is up.
clavda_wifi_begin(ssid, password);
// 4. Connect to Clavda. False means clavda_device.h is missing.
if (!clavda_begin(&CLAVDA_DEVICE)) {
clavda_serial_println("No credentials: add clavda_device.h from the console.");
}
}
void app_loop(void) {
// 5. Keep the connection alive; updates install from here too.
// Call it on every loop, and avoid long delay() calls.
clavda_loop();
// Your application code.
}
From a .c file, ClavdaOTA.h gives you the library's C API, the clavda_* functions.
Replace the Wi-Fi name and password, and add your own code after the loop call.
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. Set Tools → Partition Scheme → Minimal SPIFFS (1.9MB APP with OTA).
Flash it once over USB
Choose your board under Tools → Board → esp32 and its port under Tools → Port, then press Upload. 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 library comes with these programs as examples, under File → Examples → ClavdaOTA: cpp/CloudDemo in C++ and c/CloudDemo in C.
If it does not connect
Open Tools → Serial Monitor at 115200 baud, with Tools → Core Debug Level → Info, and press the board's reset button. The library logs 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 sketch.
No credentials: add clavda_device.h from the console.The sketch was built without the device file. Putclavda_device.hin the sketch folder, next to the.ino.- A red shield on the device: the account is over its device limit.
More in Troubleshooting.