Add chip temperature sensor and RGB LED control, verified live in HA

Quick end-to-end data-flow proof using only what's on the ESP32-C6-
DevKitC-1 itself (no sensors on the bare devkit -- confirmed via
research: BOOT button, WS2812 LED, no external sensors):

- drivers::chip_temperature_* wraps ESP-IDF's built-in
  temperature_sensor driver (SoC die temperature).
- drivers::rgb_led_* wraps the onboard WS2812 (GPIO8) via the
  espressif/led_strip managed component (another external registry
  dependency, like mdns/mqtt).
- profiles::ChipTemperatureSensor is the first real implementation of
  the Sensor interface designed earlier, converting the driver's float
  reading to the fixed-point convention once at the boundary.
- transport/mqtt.cpp publishes chip temperature as an HA "temperature"
  sensor every 30s, and exposes the LED as an HA "light" entity
  (rgb_command_topic) -- the first use of MQTT subscribe in this
  project, not just publish.

Added a ChipTemperature SensorChannel (distinct from
AmbientTemperature -- different meaning/range) to sensor.h.

Verified live: both the chip temperature reading and LED color control
work from Home Assistant against the real device.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Ronny Eia
2026-07-10 19:27:00 +02:00
parent 1bdc19b695
commit 4af0f1b248
16 changed files with 395 additions and 5 deletions

View File

@@ -1,2 +1,5 @@
idf_component_register()
idf_component_register(
SRCS "src/chip_temperature.cpp" "src/rgb_led.cpp"
INCLUDE_DIRS "include"
PRIV_REQUIRES esp_driver_tsens espressif__led_strip
)

View File

@@ -2,4 +2,7 @@
Hardware abstraction: wraps ESP32-C6 peripherals (GPIO, ADC, I2C) behind a board-independent interface used by `sensors`. No dependency on other Kvida components.
- `chip_temperature.h`/`.cpp` -- wraps ESP-IDF's built-in `temperature_sensor` driver (die temperature; there's no external ambient temperature sensor on the ESP32-C6-DevKitC-1).
- `rgb_led.h`/`.cpp` -- wraps the DevKitC-1's onboard WS2812 addressable RGB LED (GPIO8) via the `espressif/led_strip` managed component (`idf_component.yml`).
TODO: mounting the `storage` LittleFS partition (see `partitions.csv`) belongs here once a LittleFS component (e.g. `joltwallet/esp_littlefs` from the IDF Component Registry) is picked and pinned via `idf_component.yml`.

View File

@@ -0,0 +1,3 @@
dependencies:
espressif/led_strip:
version: "*"

View File

@@ -0,0 +1,11 @@
#pragma once
namespace kvida::drivers {
// Wraps ESP-IDF's temperature_sensor driver (die temperature, not
// ambient -- there is no external temperature sensor on the ESP32-C6
// DevKitC). Call init() once before read_celsius().
void chip_temperature_init();
float chip_temperature_read_celsius();
} // namespace kvida::drivers

View File

@@ -0,0 +1,11 @@
#pragma once
#include <cstdint>
namespace kvida::drivers {
// Wraps the WS2812 addressable RGB LED on the ESP32-C6-DevKitC-1 (GPIO8).
void rgb_led_init();
void rgb_led_set_color(uint8_t red, uint8_t green, uint8_t blue);
} // namespace kvida::drivers

View File

@@ -0,0 +1,37 @@
#include "chip_temperature.h"
#include "driver/temperature_sensor.h"
#include "esp_log.h"
namespace kvida::drivers {
namespace {
constexpr const char *TAG = "chip_temperature";
temperature_sensor_handle_t g_handle = nullptr;
} // namespace
void chip_temperature_init()
{
if (g_handle) {
return;
}
temperature_sensor_config_t cfg = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
ESP_ERROR_CHECK(temperature_sensor_install(&cfg, &g_handle));
ESP_ERROR_CHECK(temperature_sensor_enable(g_handle));
}
float chip_temperature_read_celsius()
{
if (!g_handle) {
ESP_LOGE(TAG, "chip_temperature_read_celsius() called before chip_temperature_init()");
return 0.0f;
}
float celsius = 0.0f;
esp_err_t err = temperature_sensor_get_celsius(g_handle, &celsius);
if (err != ESP_OK) {
ESP_LOGW(TAG, "temperature_sensor_get_celsius failed: %s", esp_err_to_name(err));
}
return celsius;
}
} // namespace kvida::drivers

View File

@@ -0,0 +1,47 @@
#include "rgb_led.h"
#include "esp_log.h"
#include "led_strip.h"
namespace kvida::drivers {
namespace {
constexpr const char *TAG = "rgb_led";
constexpr int kGpio = 8; // WS2812 on ESP32-C6-DevKitC-1
led_strip_handle_t g_strip = nullptr;
} // namespace
void rgb_led_init()
{
if (g_strip) {
return;
}
led_strip_config_t strip_config = {};
strip_config.strip_gpio_num = kGpio;
strip_config.max_leds = 1;
strip_config.led_model = LED_MODEL_WS2812;
strip_config.color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB;
led_strip_rmt_config_t rmt_config = {};
rmt_config.resolution_hz = 10 * 1000 * 1000;
esp_err_t err = led_strip_new_rmt_device(&strip_config, &rmt_config, &g_strip);
if (err != ESP_OK) {
ESP_LOGE(TAG, "led_strip_new_rmt_device failed: %s", esp_err_to_name(err));
return;
}
led_strip_clear(g_strip);
}
void rgb_led_set_color(uint8_t red, uint8_t green, uint8_t blue)
{
if (!g_strip) {
ESP_LOGE(TAG, "rgb_led_set_color() called before rgb_led_init()");
return;
}
led_strip_set_pixel(g_strip, 0, red, green, blue);
led_strip_refresh(g_strip);
}
} // namespace kvida::drivers