Build a live ESP32 dashboard with ESP-IDF
Lay out cards, charts, toggles and sliders for an ESP32 in the Clavda console, then feed them from ESP-IDF firmware, in C or C++. No server or MQTT broker to run.
Updated 3 min read
A Clavda dashboard is a live page of cards, gauges, charts and controls for one board. You build it in the console, and the firmware gives it its values and reacts to its controls. There is no web server on the board and no broker to run: the values travel over the same connection the ClavdaOTA component already keeps open.
Every widget has an ID. A widget in the firmware with the same ID gives that widget its values, or runs your code when someone uses it.
What you need
- An ESP32 already connected to Clavda with ESP-IDF.
Build the dashboard in the console
Open the editor
Open the device's Connect sheet, press Dashboard, and switch on Edit.
Add widgets
Drag cards, charts and controls onto the grid, or click them in the list.
Set each widget up
Select a widget to set its title, size and units. Its Widget ID is the name the firmware uses, and the lines to paste into the code are under Firmware, for the platform and language picked beside it.
Save
Press Save. Firmware code gives you a complete program for the whole dashboard: choose ESP-IDF, then C or C++. It builds as it is, with a stand-in reading for you to replace. The console remembers the choice for this device.
Feed it from the firmware
In C, each widget is a clavda_widget_create() with its ID. Display cards and charts get their values from clavda_widget_set_float() and clavda_widget_add_point(), and controls run the function you give clavda_widget_on_change(). In C++, declare one ClavdaWidget per widget, and use set(), add() and onChange().
This program fits a dashboard with a temperature card, a line chart, a status card, a toggle and a slider that drive an LED. The LED and the chip's temperature sensor use ESP-IDF's LEDC and temperature sensor drivers.
#include <stdbool.h>
#include "driver/ledc.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "soc/soc_caps.h"
#if SOC_TEMP_SENSOR_SUPPORTED
#include "driver/temperature_sensor.h"
#endif
// 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"
// 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";
#define LED_PIN 2
// One per widget on the dashboard, created in app_main() by its ID.
static clavda_widget_t *temperature; // Value or Gauge card
static clavda_widget_t *history; // Line chart
static clavda_widget_t *status; // Status card
static clavda_widget_t *led; // Toggle
static clavda_widget_t *brightness; // Slider, 0 to 255
static bool ledOn = false;
static int ledLevel = 128;
// The LED on a PWM channel, so the slider can dim it.
static void setupLed(void) {
ledc_timer_config_t timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_8_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 5000,
.clk_cfg = LEDC_AUTO_CLK,
};
ledc_timer_config(&timer);
ledc_channel_config_t channel = {
.gpio_num = LED_PIN,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
};
ledc_channel_config(&channel);
}
static void showLed(void) {
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, ledOn ? ledLevel : 0);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
}
// Controls: these run when someone uses the toggle or slider.
static void onLed(clavda_widget_t *widget, const clavda_input_t *in, void *user) {
ledOn = clavda_input_as_bool(in);
showLed();
if (ledOn) clavda_widget_set_status(status, "LED on", CLAVDA_SUCCESS);
else clavda_widget_set_status(status, "LED off", CLAVDA_INFO);
}
static void onBrightness(clavda_widget_t *widget, const clavda_input_t *in, void *user) {
ledLevel = (int)clavda_input_as_int(in);
showLed();
}
void app_main(void) {
setupLed();
#if SOC_TEMP_SENSOR_SUPPORTED
// The chip's own sensor. The original ESP32 has none in ESP-IDF, so
// there the temperature card stays empty.
temperature_sensor_handle_t sensor = NULL;
temperature_sensor_config_t sensorConfig = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
temperature_sensor_install(&sensorConfig, &sensor);
temperature_sensor_enable(sensor);
#endif
// The text in quotes is the widget's ID: copy it from the widget's
// settings in the console.
temperature = clavda_widget_create("temperature");
history = clavda_widget_create("temp_history");
status = clavda_widget_create("status");
led = clavda_widget_create("led");
brightness = clavda_widget_create("brightness");
clavda_widget_on_change(led, onLed, NULL);
clavda_widget_on_change(brightness, onBrightness, NULL);
// Starting values, so a dashboard opened now has something to show.
clavda_widget_set_bool(led, ledOn);
clavda_widget_set_int(brightness, ledLevel);
clavda_widget_set_status(status, "Starting", CLAVDA_INFO);
clavda_wifi_begin(ssid, password);
clavda_begin(&CLAVDA_DEVICE);
TickType_t lastSample = 0;
for (;;) {
clavda_loop();
// Display: a reading every 2 s. A value is only sent when it changes.
const TickType_t now = xTaskGetTickCount();
if (now - lastSample >= pdMS_TO_TICKS(2000)) {
lastSample = now;
#if SOC_TEMP_SENSOR_SUPPORTED
float celsius = 0;
if (temperature_sensor_get_celsius(sensor, &celsius) == ESP_OK) {
clavda_widget_set_float(temperature, celsius);
clavda_widget_add_point(history, celsius); // the console adds the time
}
#endif
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
It replaces the project's source file in main/. If main/CMakeLists.txt lists the components it uses, as the PRIV_REQUIRES spi_flash of the hello_world example does, add the two drivers to that line. For a file named main.c, it reads:
idf_component_register(SRCS "main.c"
PRIV_REQUIRES esp_driver_ledc esp_driver_tsens
INCLUDE_DIRS ".")
Before ESP-IDF 5.3, both are the one component driver. A main/CMakeLists.txt with no REQUIRES or PRIV_REQUIRES line, like the one from idf.py create-project, needs nothing added.
The file must end in .cpp, and be the one main/CMakeLists.txt builds, as in Write the code:
idf_component_register(SRCS "main.cpp"
INCLUDE_DIRS ".")
#include "driver/ledc.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "soc/soc_caps.h"
#if SOC_TEMP_SENSOR_SUPPORTED
#include "driver/temperature_sensor.h"
#endif
// 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"
// 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";
static const int LED_PIN = 2;
// One ClavdaWidget per widget on the dashboard. The text in quotes is the
// widget's ID: copy it from the widget's settings in the console.
ClavdaWidget temperature("temperature"); // Value or Gauge card
ClavdaWidget history("temp_history"); // Line chart
ClavdaWidget status("status"); // Status card
ClavdaWidget led("led"); // Toggle
ClavdaWidget brightness("brightness"); // Slider, 0 to 255
static bool ledOn = false;
static int ledLevel = 128;
// The LED on a PWM channel, so the slider can dim it.
static void setupLed() {
ledc_timer_config_t timer = {};
timer.speed_mode = LEDC_LOW_SPEED_MODE;
timer.duty_resolution = LEDC_TIMER_8_BIT;
timer.timer_num = LEDC_TIMER_0;
timer.freq_hz = 5000;
timer.clk_cfg = LEDC_AUTO_CLK;
ledc_timer_config(&timer);
ledc_channel_config_t channel = {};
channel.gpio_num = LED_PIN;
channel.speed_mode = LEDC_LOW_SPEED_MODE;
channel.channel = LEDC_CHANNEL_0;
channel.timer_sel = LEDC_TIMER_0;
ledc_channel_config(&channel);
}
static void showLed() {
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, ledOn ? ledLevel : 0);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
}
extern "C" void app_main(void) {
setupLed();
#if SOC_TEMP_SENSOR_SUPPORTED
// The chip's own sensor. The original ESP32 has none in ESP-IDF, so
// there the temperature card stays empty.
temperature_sensor_handle_t sensor = nullptr;
temperature_sensor_config_t sensorConfig = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
temperature_sensor_install(&sensorConfig, &sensor);
temperature_sensor_enable(sensor);
#endif
// Controls: these run when someone uses the toggle or slider.
led.onChange([](const ClavdaInput &in) {
ledOn = in.asBool();
showLed();
if (ledOn) status.set("LED on", CLAVDA_SUCCESS);
else status.set("LED off", CLAVDA_INFO);
});
brightness.onChange([](const ClavdaInput &in) {
ledLevel = in.asInt();
showLed();
});
// Starting values, so a dashboard opened now has something to show.
led.set(ledOn);
brightness.set(ledLevel);
status.set("Starting", CLAVDA_INFO);
clavda_wifi_begin(ssid, password);
ClavdaOTA.begin(CLAVDA_DEVICE);
TickType_t lastSample = 0;
for (;;) {
ClavdaOTA.loop();
// Display: a reading every 2 s. set() only sends when it changes.
const TickType_t now = xTaskGetTickCount();
if (now - lastSample >= pdMS_TO_TICKS(2000)) {
lastSample = now;
#if SOC_TEMP_SENSOR_SUPPORTED
float celsius = 0;
if (temperature_sensor_get_celsius(sensor, &celsius) == ESP_OK) {
temperature.set(celsius);
history.add(celsius); // the console adds the time
}
#endif
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
set() also takes a std::string. A main/CMakeLists.txt that lists its components needs esp_driver_ledc and esp_driver_tsens added, as in the C tab.
The library's complete ESP-IDF project is examples/idf/Dashboard.
Widget reference
| Widget | C | C++ |
|---|---|---|
| Value, Gauge | clavda_widget_set_float(w, 23.4), _set_int(), _set_text() | w.set(23.4): a number, or text for a Value card |
| Status | clavda_widget_set_status(w, "Door open", CLAVDA_WARNING) | w.set("Door open", CLAVDA_WARNING), also CLAVDA_INFO, CLAVDA_SUCCESS, CLAVDA_DANGER |
| Line chart | clavda_widget_add_point(w, 23.4): one point; the console adds the time | w.add(23.4) |
| Bar chart | clavda_widget_set_series(w, labels, values, count) | w.setSeries(labels, values, count) |
| Toggle, Slider, Button, Text input | clavda_widget_on_change(w, fn, user), reading clavda_input_as_bool(in) and so on | w.onChange(...), reading in.asBool(), in.asInt(), in.asFloat() or in.asText() |
| Device metric | Nothing: free heap, Wi-Fi signal and uptime come from the heartbeat | The same |
Tips
- Only the ID has to match. The text in quotes must match the dashboard; the variable name is yours.
- In C++, avoid short generic variable names. The ESP32 Wi-Fi driver has globals of its own, and
ClavdaWidget bars("bars")fails to link with multiple definition ofbars.barChartworks. In C,staticvariables cannot clash. - Setting a value is cheap. It is only sent when it changes, so setting it on every pass costs nothing extra.
- Flashed the firmware first? In edit mode, From the device lists the IDs the board sends that the dashboard does not show yet, ready to add in one click.
- Controls are protected. Values and controls are end-to-end encrypted. Using a control needs permission to edit devices, and the board itself checks it.