From 40691d188450bb22ce1dfa1f4745e6a39204ae0f Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 14 May 2025 11:28:27 +1000 Subject: [PATCH] Initial commit of example files from SDK --- CMakeLists.txt | 8 +++ README.md | 61 ++++++++++++++++ main/CMakeLists.txt | 4 ++ main/esp_zb_light.c | 156 +++++++++++++++++++++++++++++++++++++++++ main/esp_zb_light.h | 48 +++++++++++++ main/idf_component.yml | 8 +++ partitions.csv | 7 ++ sdkconfig.defaults | 31 ++++++++ 8 files changed, 323 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 main/CMakeLists.txt create mode 100644 main/esp_zb_light.c create mode 100644 main/esp_zb_light.h create mode 100644 main/idf_component.yml create mode 100644 partitions.csv create mode 100644 sdkconfig.defaults diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ea791b6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) +set(EXTRA_COMPONENT_DIRS + ${CMAKE_CURRENT_SOURCE_DIR}/../../common/light_driver + ) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(on_off_light_bulb) diff --git a/README.md b/README.md new file mode 100644 index 0000000..b5b1a91 --- /dev/null +++ b/README.md @@ -0,0 +1,61 @@ +| Supported Targets | ESP32-H2 | ESP32-C6 | +| ----------------- | -------- | -------- | + +# Light Bulb Example + +This example demonstrates how to configure a Home Automation on/off light on a Zigbee end device. + +## Hardware Required + +* One 802.15.4 enabled development board (e.g., ESP32-H2 or ESP32-C6) running this example. +* A second board running as a Zigbee coordinator (see [HA_on_off_switch](../HA_on_off_switch/) example) + +## Configure the project + +Before project configuration and build, make sure to set the correct chip target using `idf.py set-target TARGET` command. + +## Erase the NVRAM + +Before flash it to the board, it is recommended to erase NVRAM if user doesn't want to keep the previous examples or other projects stored info +using `idf.py -p PORT erase-flash` + +## Build and Flash + +Build the project, flash it to the board, and start the monitor tool to view the serial output by running `idf.py -p PORT flash monitor`. + +(To exit the serial monitor, type ``Ctrl-]``.) + +## Application Functions + +- When the program starts, the board will attempt to detect an available Zigbee network every **1 second** until one is found. +``` +I (392) main_task: Calling app_main() +I (412) phy: phy_version: 321,2, 632dc08, Feb 13 2025, 16:29:11 +I (412) phy: libbtbb version: 509a2a6, Feb 13 2025, 16:29:25 +I (422) main_task: Returned from app_main() +I (432) ESP_ZB_ON_OFF_LIGHT: ZDO signal: ZDO Config Ready (0x17), status: ESP_FAIL +I (432) ESP_ZB_ON_OFF_LIGHT: Initialize Zigbee stack +W (442) rmt: channel resolution loss, real=10666666 +I (432) ESP_ZB_ON_OFF_LIGHT: Deferred driver initialization successful +I (442) ESP_ZB_ON_OFF_LIGHT: Device started up in factory-reset mode +I (452) ESP_ZB_ON_OFF_LIGHT: Start network steering +I (3382) ESP_ZB_ON_OFF_LIGHT: Joined network successfully (Extended PAN ID: 74:4d:bd:ff:fe:63:c2:e4, PAN ID: 0x1ce4, Channel:13, Short Address: 0x2638) +``` + +- If the board is on a network, it acts as a Zigbee end device with the `Home Automation On/Off Light` function. + +- If the board receives a `On/Off` command from the joined network, the LED on the board will adjust accordingly. +``` +I (7162) ESP_ZB_ON_OFF_LIGHT: Received message: endpoint(10), cluster(0x6), attribute(0x0), data size(1) +I (7162) ESP_ZB_ON_OFF_LIGHT: Light sets to On +I (7742) ESP_ZB_ON_OFF_LIGHT: Received message: endpoint(10), cluster(0x6), attribute(0x0), data size(1) +I (7742) ESP_ZB_ON_OFF_LIGHT: Light sets to Off +I (8462) ESP_ZB_ON_OFF_LIGHT: Received message: endpoint(10), cluster(0x6), attribute(0x0), data size(1) +I (8462) ESP_ZB_ON_OFF_LIGHT: Light sets to On +I (8932) ESP_ZB_ON_OFF_LIGHT: Received message: endpoint(10), cluster(0x6), attribute(0x0), data size(1) +I (8932) ESP_ZB_ON_OFF_LIGHT: Light sets to Off +``` + +## Troubleshooting + +For any technical queries, please open an [issue](https://github.com/espressif/esp-zigbee-sdk/issues) on GitHub. We will get back to you soon. diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..15343ef --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_register( + SRC_DIRS "." "../../../common/zcl_utility/src" + INCLUDE_DIRS "." "../../../common/zcl_utility/include" +) diff --git a/main/esp_zb_light.c b/main/esp_zb_light.c new file mode 100644 index 0000000..a402971 --- /dev/null +++ b/main/esp_zb_light.c @@ -0,0 +1,156 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: CC0-1.0 + * + * Zigbee HA_on_off_light Example + * + * This example code is in the Public Domain (or CC0 licensed, at your option.) + * + * Unless required by applicable law or agreed to in writing, this + * software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ +#include "esp_zb_light.h" +#include "esp_check.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "ha/esp_zigbee_ha_standard.h" + +#if !defined ZB_ED_ROLE +#error Define ZB_ED_ROLE in idf.py menuconfig to compile light (End Device) source code. +#endif + +static const char *TAG = "ESP_ZB_ON_OFF_LIGHT"; +/********************* Define functions **************************/ +static esp_err_t deferred_driver_init(void) +{ + static bool is_inited = false; + if (!is_inited) { + light_driver_init(LIGHT_DEFAULT_OFF); + is_inited = true; + } + return is_inited ? ESP_OK : ESP_FAIL; +} + +static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask) +{ + ESP_RETURN_ON_FALSE(esp_zb_bdb_start_top_level_commissioning(mode_mask) == ESP_OK, , TAG, "Failed to start Zigbee commissioning"); +} + +void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) +{ + uint32_t *p_sg_p = signal_struct->p_app_signal; + esp_err_t err_status = signal_struct->esp_err_status; + esp_zb_app_signal_type_t sig_type = *p_sg_p; + switch (sig_type) { + case ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP: + ESP_LOGI(TAG, "Initialize Zigbee stack"); + esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_INITIALIZATION); + break; + case ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START: + case ESP_ZB_BDB_SIGNAL_DEVICE_REBOOT: + if (err_status == ESP_OK) { + ESP_LOGI(TAG, "Deferred driver initialization %s", deferred_driver_init() ? "failed" : "successful"); + ESP_LOGI(TAG, "Device started up in%s factory-reset mode", esp_zb_bdb_is_factory_new() ? "" : " non"); + if (esp_zb_bdb_is_factory_new()) { + ESP_LOGI(TAG, "Start network steering"); + esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING); + } else { + ESP_LOGI(TAG, "Device rebooted"); + } + } else { + ESP_LOGW(TAG, "%s failed with status: %s, retrying", esp_zb_zdo_signal_to_string(sig_type), + esp_err_to_name(err_status)); + esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, + ESP_ZB_BDB_MODE_INITIALIZATION, 1000); + } + break; + case ESP_ZB_BDB_SIGNAL_STEERING: + if (err_status == ESP_OK) { + esp_zb_ieee_addr_t extended_pan_id; + esp_zb_get_extended_pan_id(extended_pan_id); + ESP_LOGI(TAG, "Joined network successfully (Extended PAN ID: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx, Channel:%d, Short Address: 0x%04hx)", + extended_pan_id[7], extended_pan_id[6], extended_pan_id[5], extended_pan_id[4], + extended_pan_id[3], extended_pan_id[2], extended_pan_id[1], extended_pan_id[0], + esp_zb_get_pan_id(), esp_zb_get_current_channel(), esp_zb_get_short_address()); + } else { + ESP_LOGI(TAG, "Network steering was not successful (status: %s)", esp_err_to_name(err_status)); + esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_STEERING, 1000); + } + break; + default: + ESP_LOGI(TAG, "ZDO signal: %s (0x%x), status: %s", esp_zb_zdo_signal_to_string(sig_type), sig_type, + esp_err_to_name(err_status)); + break; + } +} + +static esp_err_t zb_attribute_handler(const esp_zb_zcl_set_attr_value_message_t *message) +{ + esp_err_t ret = ESP_OK; + bool light_state = 0; + + ESP_RETURN_ON_FALSE(message, ESP_FAIL, TAG, "Empty message"); + ESP_RETURN_ON_FALSE(message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS, ESP_ERR_INVALID_ARG, TAG, "Received message: error status(%d)", + message->info.status); + ESP_LOGI(TAG, "Received message: endpoint(%d), cluster(0x%x), attribute(0x%x), data size(%d)", message->info.dst_endpoint, message->info.cluster, + message->attribute.id, message->attribute.data.size); + if (message->info.dst_endpoint == HA_ESP_LIGHT_ENDPOINT) { + if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF) { + if (message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_BOOL) { + light_state = message->attribute.data.value ? *(bool *)message->attribute.data.value : light_state; + ESP_LOGI(TAG, "Light sets to %s", light_state ? "On" : "Off"); + light_driver_set_power(light_state); + } + } + } + return ret; +} + +static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id, const void *message) +{ + esp_err_t ret = ESP_OK; + switch (callback_id) { + case ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID: + ret = zb_attribute_handler((esp_zb_zcl_set_attr_value_message_t *)message); + break; + default: + ESP_LOGW(TAG, "Receive Zigbee action(0x%x) callback", callback_id); + break; + } + return ret; +} + +static void esp_zb_task(void *pvParameters) +{ + /* initialize Zigbee stack */ + esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG(); + esp_zb_init(&zb_nwk_cfg); + esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG(); + esp_zb_ep_list_t *esp_zb_on_off_light_ep = esp_zb_on_off_light_ep_create(HA_ESP_LIGHT_ENDPOINT, &light_cfg); + zcl_basic_manufacturer_info_t info = { + .manufacturer_name = ESP_MANUFACTURER_NAME, + .model_identifier = ESP_MODEL_IDENTIFIER, + }; + + esp_zcl_utility_add_ep_basic_manufacturer_info(esp_zb_on_off_light_ep, HA_ESP_LIGHT_ENDPOINT, &info); + esp_zb_device_register(esp_zb_on_off_light_ep); + esp_zb_core_action_handler_register(zb_action_handler); + esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK); + ESP_ERROR_CHECK(esp_zb_start(false)); + esp_zb_stack_main_loop(); +} + +void app_main(void) +{ + esp_zb_platform_config_t config = { + .radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(), + .host_config = ESP_ZB_DEFAULT_HOST_CONFIG(), + }; + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_zb_platform_config(&config)); + xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL); +} diff --git a/main/esp_zb_light.h b/main/esp_zb_light.h new file mode 100644 index 0000000..7d00f62 --- /dev/null +++ b/main/esp_zb_light.h @@ -0,0 +1,48 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: CC0-1.0 + * + * Zigbee HA_on_off_light Example + * + * This example code is in the Public Domain (or CC0 licensed, at your option.) + * + * Unless required by applicable law or agreed to in writing, this + * software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ + +#include "esp_zigbee_core.h" +#include "light_driver.h" +#include "zcl_utility.h" + +/* Zigbee configuration */ +#define INSTALLCODE_POLICY_ENABLE false /* enable the install code policy for security */ +#define ED_AGING_TIMEOUT ESP_ZB_ED_AGING_TIMEOUT_64MIN +#define ED_KEEP_ALIVE 3000 /* 3000 millisecond */ +#define HA_ESP_LIGHT_ENDPOINT 10 /* esp light bulb device endpoint, used to process light controlling commands */ +#define ESP_ZB_PRIMARY_CHANNEL_MASK ESP_ZB_TRANSCEIVER_ALL_CHANNELS_MASK /* Zigbee primary channel mask use in the example */ + +/* Basic manufacturer information */ +#define ESP_MANUFACTURER_NAME "\x09""ESPRESSIF" /* Customized manufacturer name */ +#define ESP_MODEL_IDENTIFIER "\x07"CONFIG_IDF_TARGET /* Customized model identifier */ + +#define ESP_ZB_ZED_CONFIG() \ + { \ + .esp_zb_role = ESP_ZB_DEVICE_TYPE_ED, \ + .install_code_policy = INSTALLCODE_POLICY_ENABLE, \ + .nwk_cfg.zed_cfg = { \ + .ed_timeout = ED_AGING_TIMEOUT, \ + .keep_alive = ED_KEEP_ALIVE, \ + }, \ + } + +#define ESP_ZB_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = ZB_RADIO_MODE_NATIVE, \ + } + +#define ESP_ZB_DEFAULT_HOST_CONFIG() \ + { \ + .host_connection_mode = ZB_HOST_CONNECTION_MODE_NONE, \ + } diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..198da01 --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,8 @@ +## IDF Component Manager Manifest File +dependencies: + espressif/esp-zboss-lib: "~1.6.0" + espressif/esp-zigbee-lib: "~1.6.0" + espressif/led_strip: "~2.0.0" + ## Required IDF version + idf: + version: ">=5.0.0" diff --git a/partitions.csv b/partitions.csv new file mode 100644 index 0000000..a56c0be --- /dev/null +++ b/partitions.csv @@ -0,0 +1,7 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 900K, +zb_storage, data, fat, 0xf1000, 16K, +zb_fct, data, fat, 0xf5000, 1K, diff --git a/sdkconfig.defaults b/sdkconfig.defaults new file mode 100644 index 0000000..51a266b --- /dev/null +++ b/sdkconfig.defaults @@ -0,0 +1,31 @@ +# +# Partition Table +# +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_OFFSET=0x8000 +CONFIG_PARTITION_TABLE_MD5=y +# end of Partition Table + +# +# mbedTLS +# +CONFIG_MBEDTLS_HARDWARE_AES=n +CONFIG_MBEDTLS_HARDWARE_MPI=n +CONFIG_MBEDTLS_HARDWARE_SHA=n +CONFIG_MBEDTLS_CMAC_C=y +CONFIG_MBEDTLS_SSL_PROTO_DTLS=y +CONFIG_MBEDTLS_KEY_EXCHANGE_ECJPAKE=y +# end of TLS Key Exchange Methods + +CONFIG_MBEDTLS_ECJPAKE_C=y +# end of mbedTLS + +# +# Zboss +# +CONFIG_ZB_ENABLED=y +CONFIG_ZB_ZED=y +# end of Zboss +# end of Component config