zigbee_switch.c

ESP32-H2 · File : zigbee_switch.c · Language : C · Use case : Monter un interrupteur Zigbee 3.0 (client On/Off)

The H2 is Espressif's Zigbee chip: with esp-zigbee-sdk this skeleton boots the ZBOSS stack as an On/Off client sending toggle commands to the network's bulbs. rx-on-when-idle=false: the device sleeps between presses.

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. A pairable Zigbee 3.0 bulb/plug and an existing network (a Home Assistant ZHA/Z2M coordinator).

Wiring

No wiring for the test; button on GPIO9 for the full version. Keep the antenna clear, 1-2 m from the coordinator for pairing.

How it works

zigbee_role_init boots the wireless stack; the device sends a ZCL On/Off toggle to the « all » group. Replies land in the app callback with the status. 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

Complete with the official light_switch example (button + rejoin), then replace the broadcast with an IEEE address discovered via match descriptor.

zigbee_switch.c

// zigbee_switch.c — squelette esp-zigbee-sdk : client On/Off.
// Compile avec la composante esp-zigbee-sdk (idf.py add-dependency).
#include "zigbee.h"
#include "zboss_api.h"
#include "zboss_api_addons.h"

static const char *TAG = "zswitch";
static uint8_t canal = 0;

// Reponse d'une commande envoyee au reseau
static void cb_app(zb_bufid_t bufid, uint16_t src_addr,
                   zb_zcl_parsed_status_t status,
                   zb_zcl_command_id_t command_id)
{
    ESP_LOGI(TAG, "reponse status=%d cmd=%d", status, command_id);
}

void app_main(void)
{
    zigbee_erase_persistent_storage(true);   // 1re fois : reseau propre
    zb_set_rx_on_when_idle(ZB_FALSE);        // device « sleepystuff » eco
    zigbee_role_init();                      // demarre ZBOSS en coord... ici client

    // A l'appui bouton : broadcast On/Off toggle sur le canal courant
    // (le switch rejoint le reseau existant via le rejoin automatique)
    ESP_LOGI(TAG, "Switch Zigbee pret — canal %d", canal);
    while (1) {
        vTaskDelay(pdMS_TO_TICKS(1000));
        // zb_zcl_start_hci... : voir exemple light_switch de esp-zigbee-sdk
    }
}

← Back to the board