matter_switch.cpp

ESP32-H2 · File : matter_switch.cpp · Language : C++ · Use case : Allumer une LED depuis un contrôleur Matter/Thread

The other flagship 802.15.4 use case: Matter over Thread. The H2 joins a border router's Thread network, exposes an On/Off device, and a phone (Google Home/Apple Home) pairs it with the QR code printed in the log.

Hardware required

An ESP32-H2 board (RISC-V 96 MHz, IEEE 802.15.4 radio only — no Wi-Fi) such as a DevKitM-1, powered over USB. An operational Thread border router (Raspberry Pi + OpenThread, Home Assistant, Apple TV…) and a Matter smartphone.

Wiring

No wiring; user LED on GPIO8 as the actuator. Antenna within reach of the border router.

How it works

esp_matter::init brings up the stack, on_off_light creates endpoint 1 with its OnOff cluster, start runs BLE commissioning then the Thread join. Matter commands drive the actuator through app_driver. Program it with ESP-IDF (idf.py set-target esp32h2); the USB-Serial/JTAG port (GPIO26/27) acts as console and flash port. Hold Boot + Reset for download mode if needed.

Going further

Replace the LED with a 230 V relay (via a 3.3 V relay module) for a complete Matter smart plug.

matter_switch.cpp

// matter_switch.c — squelette esp-matter : device On/Over sur Thread.
// necessite esp-matter + un border router (OpenThread) deja operationnel.
#include "esp_matter.h"
#include "esp_matter_device_event.h"
#include "app_driver.h"

static const char *TAG = "matter";

extern "C" void app_main(void)
{
    ESP_LOGI(TAG, "Init Matter (mode Thread)");
    esp_matter::init();

    // Endpoint 1 : device On/Off avec un actionneur (la LED)
    esp_matter::endpoint::on_off_light::create(&esp_matter::endpoint::on_off_light::config_t);

    // Rejoint le reseau Thread via l'extended dataset (QR/ble provisioning)
    esp_matter::start([](esp_matter::Event_t *event, void *arg) {
        if (event->type == esp_matter::Event_t::Type::WIFI_CONNECTED ||
            event->type == esp_matter::Event_t::Type::THREAD_CONNECTED) {
            ESP_LOGI(TAG, "Rejoint ! Appairage via le code QR du log.");
        }
    });

    while (1) {
        vTaskDelay(pdMS_TO_TICKS(1000));
        // app_driver_toggle() sur bouton : voir exemple app_driver.c
    }
}

← Back to the board