ClavdaClavda

Build a live ESP32 dashboard with PlatformIO

Lay out cards, charts, toggles and sliders for an ESP32 in the Clavda console, then feed them from PlatformIO 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 library 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

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 PlatformIO, 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.

src/main.c
#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 contents of src/main.c. It uses two ESP-IDF drivers, so add them to the REQUIRES of Add the library. PlatformIO's own build finds them without, but ESP-IDF's tools need them listed:

src/CMakeLists.txt
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)

idf_component_register(SRCS ${app_sources}
                       REQUIRES clavdaota esp_driver_ledc esp_driver_tsens)

Widget reference

WidgetCC++
Value, Gaugeclavda_widget_set_float(w, 23.4), _set_int(), _set_text()w.set(23.4): a number, or text for a Value card
Statusclavda_widget_set_status(w, "Door open", CLAVDA_WARNING)w.set("Door open", CLAVDA_WARNING), also CLAVDA_INFO, CLAVDA_SUCCESS, CLAVDA_DANGER
Line chartclavda_widget_add_point(w, 23.4): one point; the console adds the timew.add(23.4)
Bar chartclavda_widget_set_series(w, labels, values, count)w.setSeries(labels, values, count)
Toggle, Slider, Button, Text inputclavda_widget_on_change(w, fn, user), reading clavda_input_as_bool(in) and so onw.onChange(...), reading in.asBool(), in.asInt(), in.asFloat() or in.asText()
Device metricNothing: free heap, Wi-Fi signal and uptime come from the heartbeatThe 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 of bars. barChart works. In C, static variables 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.

Next steps