Build a live ESP32 dashboard with the Arduino IDE
Lay out cards, charts, toggles and sliders for an ESP32 in the Clavda console, then feed them from an Arduino sketch, 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 sketch 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 library already keeps open.
Every widget has an ID. A widget in the sketch 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 the Arduino IDE.
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 sketch uses, and the lines to paste into the sketch are under Firmware, in the language picked beside it.
Save
Press Save. Firmware code gives you a complete sketch for the whole dashboard: choose Arduino IDE, 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 sketch
In C++, declare one ClavdaWidget per widget, by its ID. Display cards and charts get their values from set() and add(), and controls run the code you give onChange(). In C, each widget is a clavda_widget_create() with its ID, fed with clavda_widget_set_float() and clavda_widget_add_point(), and controls run the function you give clavda_widget_on_change().
This program fits a dashboard with a temperature card, a line chart, a status card, a toggle and a slider that drive an LED:
#include <Arduino.h>
#include <WiFi.h>
// 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"
const char *ssid = "your-wifi";
const char *password = "your-wifi-password";
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
bool ledOn = false;
int ledLevel = 128;
unsigned long lastSample = 0;
void showLed() {
analogWrite(LED_PIN, ledOn ? ledLevel : 0);
}
void setup() {
Serial.begin(115200);
// 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);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
ClavdaOTA.begin(CLAVDA_DEVICE);
}
void loop() {
ClavdaOTA.loop();
// Display: a reading every 2 s. set() only sends when it changes.
if (millis() - lastSample >= 2000) {
lastSample = millis();
float celsius = temperatureRead(); // the chip's own sensor
temperature.set(celsius);
history.add(celsius); // the console adds the time
}
}
The .ino hands setup() and loop() over to the C file, as in Write the sketch:
extern "C" void app_setup(void);
extern "C" void app_loop(void);
void setup() {
app_setup();
}
void loop() {
app_loop();
}
#include <Arduino.h>
// 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_setup() 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;
static unsigned long lastSample = 0;
static void showLed(void) {
analogWrite(LED_PIN, ledOn ? ledLevel : 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_setup(void) {
clavda_serial_begin(115200, 0);
// 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);
}
void app_loop(void) {
clavda_loop();
// Display: a reading every 2 s. A value is only sent when it changes.
if (millis() - lastSample >= 2000) {
lastSample = millis();
float celsius = temperatureRead(); // the chip's own sensor
clavda_widget_set_float(temperature, celsius);
clavda_widget_add_point(history, celsius); // the console adds the time
}
}
The library's examples include a fuller version as cpp/Dashboard and c/Dashboard.
Widget reference
| Widget | C++ | C |
|---|---|---|
| Value, Gauge | w.set(23.4): a number, or text for a Value card | clavda_widget_set_float(w, 23.4), _set_int(), _set_text() |
| Status | w.set("Door open", CLAVDA_WARNING), also CLAVDA_INFO, CLAVDA_SUCCESS, CLAVDA_DANGER | clavda_widget_set_status(w, "Door open", CLAVDA_WARNING) |
| Line chart | w.add(23.4): one point; the console adds the time | clavda_widget_add_point(w, 23.4) |
| Bar chart | w.setSeries(labels, values, count) | clavda_widget_set_series(w, labels, values, count) |
| Toggle, Slider, Button, Text input | w.onChange(...), reading in.asBool(), in.asInt(), in.asFloat() or in.asText() | clavda_widget_on_change(w, fn, user), reading clavda_input_as_bool(in) and so on |
| 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 library has globals of its own, and
ClavdaWidget bars("bars")fails to link with multiple definition ofbars.barChartworks. The console's snippets end every name inWidgetfor that reason. In C,staticvariables cannot clash. - Setting a value is cheap. It is only sent when it changes, so setting it on every loop costs nothing extra.
- Flashed the sketch 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.