From c2e04f8d2c42b4a3aaeda745468c9798bba172c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:30:05 +0100 Subject: [PATCH 1/8] feat(zigbee): Add power source and battery level --- .../README.md | 79 +++++++++++ .../Zigbee_Temperature_Sensor_Sleepy.ino | 132 ++++++++++++++++++ .../Zigbee_Temperature_Sensor_Sleepy/ci.json | 6 + libraries/Zigbee/src/ZigbeeCore.cpp | 12 +- libraries/Zigbee/src/ZigbeeEP.cpp | 40 ++++++ libraries/Zigbee/src/ZigbeeEP.h | 10 +- 6 files changed, 273 insertions(+), 6 deletions(-) create mode 100644 libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/README.md create mode 100644 libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/Zigbee_Temperature_Sensor_Sleepy.ino create mode 100644 libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/ci.json diff --git a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/README.md b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/README.md new file mode 100644 index 00000000000..f3dd9248f87 --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/README.md @@ -0,0 +1,79 @@ +# Arduino-ESP32 Zigbee Temperature Sensor Example + +This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) temperature sensor. + +# Supported Targets + +Currently, this example supports the following targets. + +| Supported Targets | ESP32-C6 | ESP32-H2 | +| ----------------- | -------- | -------- | + +## Temperature Sensor Functions + +Note: + * This board means the board (e.g. ESP32-H2 / C6) loaded with `Zigbee_Temperature_Sensor` example. + * The remote board means the board (e.g. ESP32-H2 / C6) loaded with `Zigbee_Thermostat` example. + +Functions: + * After this board first starts up, it would be configured locally to report the temperature on 1 degree change and no periodic reporting to the remote board. + * By clicking the button (BOOT) on this board, this board will immediately send a report of the current measured temperature to the remote board. + +## Hardware Required + +* One development board (ESP32-H2 or ESP32-C6) acting as Zigbee coordinator (loaded with `Zigbee_Thermostat` example) +* A USB cable for power supply and programming +* Choose another board (ESP32-H2 or ESP32-C6) as Zigbee end device and upload the `Zigbee_Temperature_Sensor` example + +### Configure the Project + +In this example, the internal temperature sensor task is reading the chip temperature. +Set the Button GPIO by changing the `BUTTON_PIN` definition. By default, it's the pin `9` (BOOT button on ESP32-C6 and ESP32-H2). + +#### Using Arduino IDE + +To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits). + +* Before Compile/Verify, select the correct board: `Tools -> Board`. +* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)` +* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs` +* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port. +* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`. + +## Troubleshooting + +If the End device flashed with this example is not connecting to the coordinator, erase the flash of the End device before flashing the example to the board. It is recommended to do this if you re-flash the coordinator. +You can do the following: + +* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`. +* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack. + +By default, the coordinator network is closed after rebooting or flashing new firmware. +To open the network you have 2 options: + +* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`. +* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join. + +***Important: Make sure you are using a good quality USB cable and that you have a reliable power source*** + +* **LED not blinking:** Check the wiring connection and the IO selection. +* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed. +* **COM port not detected:** Check the USB cable and the USB to Serial driver installation. + +If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute). + +## Contribute + +To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst) + +If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome! + +Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else. + +## Resources + +* Official ESP32 Forum: [Link](https://esp32.com) +* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32) +* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf) +* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf) +* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com) diff --git a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/Zigbee_Temperature_Sensor_Sleepy.ino b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/Zigbee_Temperature_Sensor_Sleepy.ino new file mode 100644 index 00000000000..f0889757492 --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/Zigbee_Temperature_Sensor_Sleepy.ino @@ -0,0 +1,132 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @brief This example demonstrates Zigbee temperature sensor Sleepy device. + * + * The example demonstrates how to use Zigbee library to create a end device temperature sensor. + * The temperature sensor is a Zigbee end device, which is controlled by a Zigbee coordinator. + * + * Proper Zigbee mode must be selected in Tools->Zigbee mode + * and also the correct partition scheme must be selected in Tools->Partition Scheme. + * + * Please check the README.md for instructions and more detailed description. + * + * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) + */ + +#ifndef ZIGBEE_MODE_ED +#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" +#endif + +#include "ZigbeeCore.h" +#include "ep/ZigbeeTempSensor.h" +#include + +#define BUTTON_PIN 9 //Boot button for C6/H2 +#define TEMP_SENSOR_ENDPOINT_NUMBER 10 + +#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */ +#define TIME_TO_SLEEP 30 /* Time ESP32 will go to sleep (in seconds) */ + +ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER); + +/************************ Temp sensor *****************************/ +void meausureAndSleep(){ + // Measure temperature sensor value + float tsens_value = temperatureRead(); + + // Initialize random seed + std::srand(static_cast(std::time(0))); + + // Calculate the 1% fluctuation range + float fluctuation = tsens_value * 0.01f; + + // Generate a random number between -1% and +1% + float randomFactor = (static_cast(std::rand()) / RAND_MAX) * 2.0f - 1.0f; + + // Apply the fluctuation to the original temperature + tsens_value = tsens_value + randomFactor * fluctuation; + + // Update temperature value in Temperature sensor EP + zbTempSensor.setTemperature(tsens_value); + + // Report temperature value + zbTempSensor.reportTemperature(); + + // Set battery percentage to 80% and report it + zbTempSensor.setBatteryPercentage(80); + zbTempSensor.reportBatteryPercentage(); + + // Put device to deep sleep + esp_deep_sleep_start(); +} + +/********************* Arduino functions **************************/ +void setup() { + // Init button switch + pinMode(BUTTON_PIN, INPUT_PULLUP); + + // Optional: set Zigbee device name and model + zbTempSensor.setManufacturerAndModel("Espressif", "SleepyZigbeeTempSensor"); + + // Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement) + zbTempSensor.setMinMaxValue(10, 50); + + // Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C) + zbTempSensor.setTolerance(1); + + // Set power source to battery and battery percentage to 100% + zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100); + + // Add endpoint to Zigbee Core + Zigbee.addEndpoint(&zbTempSensor); + + // Configure the wake up source and set to wake up every 5 seconds + esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); + + // Create a custom Zigbee configuration for End Device + esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG(); + zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000; + + // When all EPs are registered, start Zigbee in End Device mode + Zigbee.begin(&zigbeeConfig, false); + + // Wait for Zigbee to start + while(!Zigbee.isStarted()){ + delay(100); + } + + // Delay 5s to allow establishing connection with coordinator, needed for sleepy devices + delay(5000); +} + +void loop() { + // Checking button for factory reset + if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed + // Key debounce handling + delay(100); + int startTime = millis(); + while (digitalRead(BUTTON_PIN) == LOW) { + delay(50); + if ((millis() - startTime) > 3000) { + // If key pressed for more than 3secs, factory reset Zigbee and reboot + Zigbee.factoryReset(); + } + } + } + + // Call the function to measure temperature and put the device to sleep + meausureAndSleep(); +} diff --git a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/ci.json b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/ci.json new file mode 100644 index 00000000000..7b7ccef8ed7 --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/ci.json @@ -0,0 +1,6 @@ +{ + "fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed", + "requires": [ + "CONFIG_SOC_IEEE802154_SUPPORTED=y" + ] +} diff --git a/libraries/Zigbee/src/ZigbeeCore.cpp b/libraries/Zigbee/src/ZigbeeCore.cpp index 9edbe737cfe..cfd8a64a324 100644 --- a/libraries/Zigbee/src/ZigbeeCore.cpp +++ b/libraries/Zigbee/src/ZigbeeCore.cpp @@ -155,7 +155,7 @@ void ZigbeeCore::setRebootOpenNetwork(uint8_t time) { } void ZigbeeCore::openNetwork(uint8_t time) { - if (_started) { + if (isStarted()) { log_v("Opening network for joining for %d seconds", time); esp_zb_bdb_open_network(time); } @@ -201,10 +201,15 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) { log_i("Opening network for joining for %d seconds", Zigbee._open_network); esp_zb_bdb_open_network(Zigbee._open_network); } + if(Zigbee._sleepy_device) { + log_i("Device is a sleepy end device"); + Zigbee._can_sleep = true; + } } } else { /* commissioning failed */ log_e("Failed to initialize Zigbee stack (status: %s)", esp_err_to_name(err_status)); + esp_restart(); } break; case ESP_ZB_BDB_SIGNAL_FORMATION: // Coordinator @@ -225,11 +230,11 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) { } break; case ESP_ZB_BDB_SIGNAL_STEERING: // Router and End Device - Zigbee._started = true; if ((zigbee_role_t)Zigbee.getRole() == ZIGBEE_COORDINATOR) { if (err_status == ESP_OK) { log_i("Network steering started"); } + Zigbee._started = true; } else { if (err_status == ESP_OK) { esp_zb_ieee_addr_t extended_pan_id; @@ -239,6 +244,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) { 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() ); + Zigbee._started = true; } else { log_i("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); @@ -321,7 +327,7 @@ void ZigbeeCore::scanCompleteCallback(esp_zb_zdp_status_t zdo_status, uint8_t co } void ZigbeeCore::scanNetworks(u_int32_t channel_mask, u_int8_t scan_duration) { - if (!_started) { + if (!isStarted()) { log_e("Zigbee stack is not started, cannot scan networks"); return; } diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index af237739327..2ed9a2a74c0 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -5,6 +5,7 @@ #if SOC_IEEE802154_SUPPORTED #include "esp_zigbee_cluster.h" +#include "zcl/esp_zigbee_zcl_power_config.h" uint8_t ZigbeeEP::_endpoint = 0; bool ZigbeeEP::_is_bound = false; @@ -65,6 +66,45 @@ void ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, (void *)zb_model); } +void ZigbeeEP::setPowerSource(zb_power_source_t power_source, uint8_t percentage) { + esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + esp_zb_cluster_update_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_POWER_SOURCE_ID, (void *)&power_source); + + if(power_source == ZB_POWER_SOURCE_BATTERY){ + //TODO: + //UPDATE MAC CAPABILITIES TO SET POWER SOURCE TO BATTERY + //currently its not possible to update mac capabilities + // Add power config cluster and battery percentage attribute + esp_zb_attribute_list_t *power_config_cluster = esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG); + esp_zb_power_config_cluster_add_attr(power_config_cluster, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID, (void *)&percentage); + esp_zb_cluster_list_add_power_config_cluster(_cluster_list, power_config_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + } +} + +void ZigbeeEP::setBatteryPercentage(uint8_t percentage) { + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID, &percentage, false + ); + esp_zb_lock_release(); + log_v("Battery percentage updated"); +} + +void ZigbeeEP::reportBatteryPercentage() { + /* Send report attributes command */ + esp_zb_zcl_report_attr_cmd_t report_attr_cmd; + report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT; + report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID; + report_attr_cmd.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE; + report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG; + report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint; + + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd); + esp_zb_lock_release(); + log_v("Battery percentage reported"); +} + char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr) { /* Read peer Manufacture Name & Model Identifier */ esp_zb_zcl_read_attr_cmd_t read_req; diff --git a/libraries/Zigbee/src/ZigbeeEP.h b/libraries/Zigbee/src/ZigbeeEP.h index e7deefdb32e..ce92ad1aacd 100644 --- a/libraries/Zigbee/src/ZigbeeEP.h +++ b/libraries/Zigbee/src/ZigbeeEP.h @@ -46,9 +46,10 @@ typedef struct zb_device_params_s { } zb_device_params_t; typedef enum { - SINGLE_COLOR = 0, - RGB = 1 -} zb_identify_led_type_t; + ZB_POWER_SOURCE_UNKNOWN = 0x00, + ZB_POWER_SOURCE_MAINS = 0x01, + ZB_POWER_SOURCE_BATTERY = 0x03, +} zb_power_source_t; /* Zigbee End Device Class */ class ZigbeeEP { @@ -81,6 +82,9 @@ class ZigbeeEP { // Manufacturer name and model implemented void setManufacturerAndModel(const char *name, const char *model); + void setPowerSource(zb_power_source_t power_source, uint8_t percentage = 255); + void setBatteryPercentage(uint8_t percentage); + void reportBatteryPercentage(); // Methods to read manufacturer and model name from selected endpoint and short address char *readManufacturer(uint8_t endpoint, uint16_t short_addr); From a108a1a2f53bc773c31d7b44e41eee92fb21542f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 30 Oct 2024 14:00:12 +0100 Subject: [PATCH 2/8] fix(zigbee): Make sure Indentify callback in not null --- libraries/Zigbee/src/ZigbeeEP.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index 2ed9a2a74c0..493ddc3fe88 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -16,6 +16,7 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) { _endpoint = endpoint; _ep_config.endpoint = 0; _cluster_list = nullptr; + _on_identify = nullptr; #if !CONFIG_DISABLE_HAL_LOCKS if (!lock) { lock = xSemaphoreCreateBinary(); @@ -193,7 +194,9 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { void ZigbeeEP::zbIdentify(const esp_zb_zcl_set_attr_value_message_t *message) { if (message->attribute.id == ESP_ZB_ZCL_CMD_IDENTIFY_IDENTIFY_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) { - _on_identify(*(uint16_t *)message->attribute.data.value); + if(_on_identify != NULL) { + _on_identify(*(uint16_t *)message->attribute.data.value); + } } else { log_w("Other identify commands are not implemented yet."); } From 11760b7b6f2f577933c86dc9430b73d24bf50842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:03:08 +0100 Subject: [PATCH 3/8] fix(zigbee): Remove leftovers and use workaround for mac capabilities --- libraries/Zigbee/src/ZigbeeCore.cpp | 4 ---- libraries/Zigbee/src/ZigbeeEP.cpp | 3 +++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeCore.cpp b/libraries/Zigbee/src/ZigbeeCore.cpp index cfd8a64a324..80fda18fc76 100644 --- a/libraries/Zigbee/src/ZigbeeCore.cpp +++ b/libraries/Zigbee/src/ZigbeeCore.cpp @@ -201,10 +201,6 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) { log_i("Opening network for joining for %d seconds", Zigbee._open_network); esp_zb_bdb_open_network(Zigbee._open_network); } - if(Zigbee._sleepy_device) { - log_i("Device is a sleepy end device"); - Zigbee._can_sleep = true; - } } } else { /* commissioning failed */ diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index 493ddc3fe88..0ded3a6e313 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -11,6 +11,8 @@ uint8_t ZigbeeEP::_endpoint = 0; bool ZigbeeEP::_is_bound = false; bool ZigbeeEP::_allow_multiple_binding = false; +extern void zb_set_ed_node_descriptor(bool power_src, bool rx_on_when_idle, bool alloc_addr); + /* Zigbee End Device Class */ ZigbeeEP::ZigbeeEP(uint8_t endpoint) { _endpoint = endpoint; @@ -75,6 +77,7 @@ void ZigbeeEP::setPowerSource(zb_power_source_t power_source, uint8_t percentage //TODO: //UPDATE MAC CAPABILITIES TO SET POWER SOURCE TO BATTERY //currently its not possible to update mac capabilities + zb_set_ed_node_descriptor(0,0,0); // Add power config cluster and battery percentage attribute esp_zb_attribute_list_t *power_config_cluster = esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG); esp_zb_power_config_cluster_add_attr(power_config_cluster, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID, (void *)&percentage); From 9f5480ac3ef7b9c111fa3dda88565f59b29c07f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Fri, 1 Nov 2024 13:03:45 +0100 Subject: [PATCH 4/8] feat(zigbee) Add humidity optional cluster + fixes --- .../README.md | 20 ++--- .../Zigbee_Temp_Hum_Sensor_Sleepy.ino} | 52 ++++++------- .../ci.json | 0 libraries/Zigbee/src/ZigbeeCore.cpp | 12 +++ libraries/Zigbee/src/ZigbeeEP.cpp | 32 ++++++-- libraries/Zigbee/src/ZigbeeEP.h | 3 +- .../Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp | 4 +- libraries/Zigbee/src/ep/ZigbeeLight.cpp | 3 +- libraries/Zigbee/src/ep/ZigbeeSwitch.cpp | 4 +- libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp | 78 ++++++++++++++++++- libraries/Zigbee/src/ep/ZigbeeTempSensor.h | 16 +++- libraries/Zigbee/src/ep/ZigbeeThermostat.cpp | 6 +- 12 files changed, 167 insertions(+), 63 deletions(-) rename libraries/Zigbee/examples/{Zigbee_Temperature_Sensor_Sleepy => Zigbee_Temp_Hum_Sensor_Sleepy}/README.md (76%) rename libraries/Zigbee/examples/{Zigbee_Temperature_Sensor_Sleepy/Zigbee_Temperature_Sensor_Sleepy.ino => Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino} (72%) rename libraries/Zigbee/examples/{Zigbee_Temperature_Sensor_Sleepy => Zigbee_Temp_Hum_Sensor_Sleepy}/ci.json (100%) diff --git a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/README.md b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md similarity index 76% rename from libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/README.md rename to libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md index f3dd9248f87..b85da980c2e 100644 --- a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/README.md +++ b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md @@ -1,6 +1,6 @@ -# Arduino-ESP32 Zigbee Temperature Sensor Example +# Arduino-ESP32 Zigbee Temperature and Humidity Sensor Sleepy Device Example -This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) temperature sensor. +This example demonstrates how to use the Zigbee library to create an end device temperature/humidity sensor and use it as a Home Automation (HA) extended temperature sensor. # Supported Targets @@ -11,23 +11,19 @@ Currently, this example supports the following targets. ## Temperature Sensor Functions -Note: - * This board means the board (e.g. ESP32-H2 / C6) loaded with `Zigbee_Temperature_Sensor` example. - * The remote board means the board (e.g. ESP32-H2 / C6) loaded with `Zigbee_Thermostat` example. - -Functions: - * After this board first starts up, it would be configured locally to report the temperature on 1 degree change and no periodic reporting to the remote board. - * By clicking the button (BOOT) on this board, this board will immediately send a report of the current measured temperature to the remote board. +1. Initialize a Zigbee temperature and humidity sensor. +2. Measure temperature and humidity values. +3. Report the measured values to the Zigbee network. +4. Put the device to sleep to save power. ## Hardware Required -* One development board (ESP32-H2 or ESP32-C6) acting as Zigbee coordinator (loaded with `Zigbee_Thermostat` example) +* ESP32-H2 or ESP32-C6 development board * A USB cable for power supply and programming -* Choose another board (ESP32-H2 or ESP32-C6) as Zigbee end device and upload the `Zigbee_Temperature_Sensor` example ### Configure the Project -In this example, the internal temperature sensor task is reading the chip temperature. +In this example, to demonstrate the funcionality the chip temperature is used and reported as temperature and humidity. Set the Button GPIO by changing the `BUTTON_PIN` definition. By default, it's the pin `9` (BOOT button on ESP32-C6 and ESP32-H2). #### Using Arduino IDE diff --git a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/Zigbee_Temperature_Sensor_Sleepy.ino b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino similarity index 72% rename from libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/Zigbee_Temperature_Sensor_Sleepy.ino rename to libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino index f0889757492..af0c771e39b 100644 --- a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/Zigbee_Temperature_Sensor_Sleepy.ino +++ b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino @@ -13,10 +13,10 @@ // limitations under the License. /** - * @brief This example demonstrates Zigbee temperature sensor Sleepy device. + * @brief This example demonstrates Zigbee temperature and humidity sensor Sleepy device. * - * The example demonstrates how to use Zigbee library to create a end device temperature sensor. - * The temperature sensor is a Zigbee end device, which is controlled by a Zigbee coordinator. + * The example demonstrates how to use Zigbee library to create an end device temperature and humidity sensor. + * The sensor is a Zigbee end device, which is reporting data to the Zigbee network. * * Proper Zigbee mode must be selected in Tools->Zigbee mode * and also the correct partition scheme must be selected in Tools->Partition Scheme. @@ -32,42 +32,32 @@ #include "ZigbeeCore.h" #include "ep/ZigbeeTempSensor.h" -#include #define BUTTON_PIN 9 //Boot button for C6/H2 #define TEMP_SENSOR_ENDPOINT_NUMBER 10 #define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */ -#define TIME_TO_SLEEP 30 /* Time ESP32 will go to sleep (in seconds) */ +#define TIME_TO_SLEEP 55 /* Sleep for 55s will + 5s delay for establishing connection => data reported every 1 minute */ ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER); /************************ Temp sensor *****************************/ void meausureAndSleep(){ // Measure temperature sensor value - float tsens_value = temperatureRead(); + float temperature = temperatureRead(); - // Initialize random seed - std::srand(static_cast(std::time(0))); + // Use temparture value as humidity value to demonstrate both temperature and humidity + float humidity = temperature; - // Calculate the 1% fluctuation range - float fluctuation = tsens_value * 0.01f; + // Update temperature and humidity values in Temperature sensor EP + zbTempSensor.setTemperature(temperature); + zbTempSensor.setHumidity(humidity); - // Generate a random number between -1% and +1% - float randomFactor = (static_cast(std::rand()) / RAND_MAX) * 2.0f - 1.0f; - - // Apply the fluctuation to the original temperature - tsens_value = tsens_value + randomFactor * fluctuation; - - // Update temperature value in Temperature sensor EP - zbTempSensor.setTemperature(tsens_value); - - // Report temperature value + // Report temperature and humidity values zbTempSensor.reportTemperature(); + zbTempSensor.reportHumidity(); - // Set battery percentage to 80% and report it - zbTempSensor.setBatteryPercentage(80); - zbTempSensor.reportBatteryPercentage(); + log_d("Temperature: %.2f°C, Humidity: %.2f%", temperature, humidity); // Put device to deep sleep esp_deep_sleep_start(); @@ -78,8 +68,11 @@ void setup() { // Init button switch pinMode(BUTTON_PIN, INPUT_PULLUP); + // Configure the wake up source and set to wake up every 5 seconds + esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); + // Optional: set Zigbee device name and model - zbTempSensor.setManufacturerAndModel("Espressif", "SleepyZigbeeTempSensor"); + zbTempSensor.setManufacturerAndModel("Espressif", "SleepyZigbeeTempSensorTest"); // Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement) zbTempSensor.setMinMaxValue(10, 50); @@ -87,16 +80,17 @@ void setup() { // Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C) zbTempSensor.setTolerance(1); - // Set power source to battery and battery percentage to 100% + // Set power source to battery and set battery percentage to measured value (now 100% for demonstration) + // The value can be also updated by calling zbTempSensor.setBatteryPercentage(percentage) anytime zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100); + // Add humidity cluster to the temperature sensor device with min, max and tolerance values + zbTempSensor.addHumiditySensor(0,100,1); + // Add endpoint to Zigbee Core Zigbee.addEndpoint(&zbTempSensor); - // Configure the wake up source and set to wake up every 5 seconds - esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); - - // Create a custom Zigbee configuration for End Device + // Create a custom Zigbee configuration for End Device with keep alive 10s to avoid interference with reporting data esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG(); zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000; diff --git a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/ci.json b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/ci.json similarity index 100% rename from libraries/Zigbee/examples/Zigbee_Temperature_Sensor_Sleepy/ci.json rename to libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/ci.json diff --git a/libraries/Zigbee/src/ZigbeeCore.cpp b/libraries/Zigbee/src/ZigbeeCore.cpp index 80fda18fc76..6aaae711abf 100644 --- a/libraries/Zigbee/src/ZigbeeCore.cpp +++ b/libraries/Zigbee/src/ZigbeeCore.cpp @@ -6,6 +6,9 @@ #include "ZigbeeHandlers.cpp" #include "Arduino.h" +extern "C" void zb_set_ed_node_descriptor(bool power_src, bool rx_on_when_idle, bool alloc_addr); +static bool edBatteryPowered = false; + ZigbeeCore::ZigbeeCore() { _radio_config.radio_mode = ZB_RADIO_MODE_NATIVE; // Use the native 15.4 radio _host_config.host_connection_mode = ZB_HOST_CONNECTION_MODE_NONE; // Disable host connection @@ -73,6 +76,12 @@ void ZigbeeCore::addEndpoint(ZigbeeEP *ep) { static void esp_zb_task(void *pvParameters) { /* initialize Zigbee stack */ ESP_ERROR_CHECK(esp_zb_start(false)); + + //NOTE: This is a workaround to make battery powered devices to be discovered as battery powered + if(((zigbee_role_t)Zigbee.getRole() == ZIGBEE_END_DEVICE) && edBatteryPowered ) { + zb_set_ed_node_descriptor(0,0,0); + } + esp_zb_stack_main_loop(); } @@ -109,6 +118,9 @@ bool ZigbeeCore::zigbeeInit(esp_zb_cfg_t *zb_cfg, bool erase_nvs) { log_i("List of registered Zigbee EPs:"); for (std::list::iterator it = ep_objects.begin(); it != ep_objects.end(); ++it) { log_i("Device type: %s, Endpoint: %d, Device ID: 0x%04x", getDeviceTypeString((*it)->_device_id), (*it)->_endpoint, (*it)->_device_id); + if((*it)->_power_source == ZB_POWER_SOURCE_BATTERY) { + edBatteryPowered = true; + } } } // Register Zigbee action handler diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index 0ded3a6e313..f08f95be524 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -7,15 +7,27 @@ #include "esp_zigbee_cluster.h" #include "zcl/esp_zigbee_zcl_power_config.h" -uint8_t ZigbeeEP::_endpoint = 0; +#ifdef __cplusplus +extern "C" +{ +#endif + +extern void zb_set_ed_node_descriptor(bool power_src, bool rx_on_when_idle, bool alloc_addr); + +#ifdef __cplusplus +} +#endif + +// uint8_t ZigbeeEP::_endpoint = 0; bool ZigbeeEP::_is_bound = false; bool ZigbeeEP::_allow_multiple_binding = false; -extern void zb_set_ed_node_descriptor(bool power_src, bool rx_on_when_idle, bool alloc_addr); +//TODO: is_bound and allow_multiple_binding to make not static /* Zigbee End Device Class */ ZigbeeEP::ZigbeeEP(uint8_t endpoint) { _endpoint = endpoint; + log_v("Endpoint: %d", _endpoint); _ep_config.endpoint = 0; _cluster_list = nullptr; _on_identify = nullptr; @@ -69,23 +81,27 @@ void ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) { esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, (void *)zb_model); } -void ZigbeeEP::setPowerSource(zb_power_source_t power_source, uint8_t percentage) { +void ZigbeeEP::setPowerSource(zb_power_source_t power_source, uint8_t battery_percentage) { esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); esp_zb_cluster_update_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_POWER_SOURCE_ID, (void *)&power_source); if(power_source == ZB_POWER_SOURCE_BATTERY){ - //TODO: - //UPDATE MAC CAPABILITIES TO SET POWER SOURCE TO BATTERY - //currently its not possible to update mac capabilities - zb_set_ed_node_descriptor(0,0,0); // Add power config cluster and battery percentage attribute + battery_percentage = battery_percentage * 2; esp_zb_attribute_list_t *power_config_cluster = esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG); - esp_zb_power_config_cluster_add_attr(power_config_cluster, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID, (void *)&percentage); + esp_zb_power_config_cluster_add_attr(power_config_cluster, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID, (void *)&battery_percentage); esp_zb_cluster_list_add_power_config_cluster(_cluster_list, power_config_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); } + _power_source = power_source; } void ZigbeeEP::setBatteryPercentage(uint8_t percentage) { + // 100% = 200 in decimal, 0% = 0 + // Convert percentage to 0-200 range + if (percentage > 100) { + percentage = 100; + } + percentage = percentage * 2; esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_set_attribute_val( _endpoint, ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID, &percentage, false diff --git a/libraries/Zigbee/src/ZigbeeEP.h b/libraries/Zigbee/src/ZigbeeEP.h index ce92ad1aacd..3a86617e2f4 100644 --- a/libraries/Zigbee/src/ZigbeeEP.h +++ b/libraries/Zigbee/src/ZigbeeEP.h @@ -114,13 +114,14 @@ class ZigbeeEP { void (*_on_identify)(uint16_t time); protected: - static uint8_t _endpoint; + uint8_t _endpoint; esp_zb_ha_standard_devices_t _device_id; esp_zb_endpoint_config_t _ep_config; esp_zb_cluster_list_t *_cluster_list; static bool _is_bound; std::list _bound_devices; SemaphoreHandle_t lock; + zb_power_source_t _power_source; friend class ZigbeeCore; }; diff --git a/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp b/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp index c30599aadac..e539a66b4b0 100644 --- a/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp @@ -57,7 +57,7 @@ void ZigbeeColorDimmerSwitch::findCb(esp_zb_zdp_status_t zdo_status, uint16_t ad light->short_addr = addr; esp_zb_ieee_address_by_short(light->short_addr, light->ieee_addr); esp_zb_get_long_address(bind_req.src_address); - bind_req.src_endp = _endpoint; + bind_req.src_endp = *((uint8_t*)user_ctx); //_endpoint; bind_req.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_ON_OFF; bind_req.dst_addr_mode = ESP_ZB_ZDO_BIND_DST_ADDR_MODE_64_BIT_EXTENDED; memcpy(bind_req.dst_address_u.addr_long, light->ieee_addr, sizeof(esp_zb_ieee_addr_t)); @@ -88,7 +88,7 @@ void ZigbeeColorDimmerSwitch::findEndpoint(esp_zb_zdo_match_desc_req_param_t *cm .num_out_clusters = 3, .cluster_list = cluster_list, }; - esp_zb_zdo_match_cluster(&color_dimmable_light_req, findCb, NULL); + esp_zb_zdo_match_cluster(&color_dimmable_light_req, findCb, &_endpoint); } // Methods to control the light diff --git a/libraries/Zigbee/src/ep/ZigbeeLight.cpp b/libraries/Zigbee/src/ep/ZigbeeLight.cpp index 0577ede1788..1f59cd82325 100644 --- a/libraries/Zigbee/src/ep/ZigbeeLight.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeLight.cpp @@ -6,7 +6,8 @@ ZigbeeLight::ZigbeeLight(uint8_t endpoint) : ZigbeeEP(endpoint) { esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG(); _cluster_list = esp_zb_on_off_light_clusters_create(&light_cfg); // use esp_zb_zcl_cluster_list_create() instead of esp_zb_on_off_light_clusters_create() - _ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_ON_OFF_LIGHT_DEVICE_ID, .app_device_version = 0}; + _ep_config = {.endpoint = endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_ON_OFF_LIGHT_DEVICE_ID, .app_device_version = 0}; + log_v("Light endpoint created %d", _endpoint); } //set attribute method -> method overridden in child class diff --git a/libraries/Zigbee/src/ep/ZigbeeSwitch.cpp b/libraries/Zigbee/src/ep/ZigbeeSwitch.cpp index 9152732e376..eb1268b701b 100644 --- a/libraries/Zigbee/src/ep/ZigbeeSwitch.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeSwitch.cpp @@ -35,7 +35,7 @@ void ZigbeeSwitch::findCb(esp_zb_zdp_status_t zdo_status, uint16_t addr, uint8_t light->short_addr = addr; esp_zb_ieee_address_by_short(light->short_addr, light->ieee_addr); esp_zb_get_long_address(bind_req.src_address); - bind_req.src_endp = _endpoint; //_dev_endpoint; + bind_req.src_endp = *((uint8_t*)user_ctx); //_endpoint; bind_req.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_ON_OFF; bind_req.dst_addr_mode = ESP_ZB_ZDO_BIND_DST_ADDR_MODE_64_BIT_EXTENDED; memcpy(bind_req.dst_address_u.addr_long, light->ieee_addr, sizeof(esp_zb_ieee_addr_t)); @@ -60,7 +60,7 @@ void ZigbeeSwitch::findEndpoint(esp_zb_zdo_match_desc_req_param_t *cmd_req) { .cluster_list = cluster_list, }; - esp_zb_zdo_match_cluster(&on_off_req, findCb, NULL); + esp_zb_zdo_match_cluster(&on_off_req, findCb, &_endpoint); } // Methods to control the light diff --git a/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp index e0dba03da5c..9d4f1163a7e 100644 --- a/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp @@ -12,13 +12,13 @@ ZigbeeTempSensor::ZigbeeTempSensor(uint8_t endpoint) : ZigbeeEP(endpoint) { }; } -static int16_t zb_temperature_to_s16(float temp) { +static int16_t zb_float_to_s16(float temp) { return (int16_t)(temp * 100); } void ZigbeeTempSensor::setMinMaxValue(float min, float max) { - int16_t zb_min = zb_temperature_to_s16(min); - int16_t zb_max = zb_temperature_to_s16(max); + int16_t zb_min = zb_float_to_s16(min); + int16_t zb_max = zb_float_to_s16(max); esp_zb_attribute_list_t *temp_measure_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); esp_zb_cluster_update_attr(temp_measure_cluster, ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_MIN_VALUE_ID, (void *)&zb_min); @@ -64,7 +64,7 @@ void ZigbeeTempSensor::setReporting(uint16_t min_interval, uint16_t max_interval } void ZigbeeTempSensor::setTemperature(float temperature) { - int16_t zb_temperature = zb_temperature_to_s16(temperature); + int16_t zb_temperature = zb_float_to_s16(temperature); log_v("Updating temperature sensor value..."); /* Update temperature sensor measured value */ log_d("Setting temperature to %d", zb_temperature); @@ -90,4 +90,74 @@ void ZigbeeTempSensor::reportTemperature() { log_v("Temperature report sent"); } +void ZigbeeTempSensor::addHumiditySensor(float min, float max, float tolerance) { + int16_t zb_min = zb_float_to_s16(min); + int16_t zb_max = zb_float_to_s16(max); + uint16_t zb_tolerance = (uint16_t)(tolerance * 100); + int16_t default_hum = ESP_ZB_ZCL_REL_HUMIDITY_MEASUREMENT_MEASURED_VALUE_DEFAULT; + esp_zb_attribute_list_t *humidity_cluster = esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_REL_HUMIDITY_MEASUREMENT); + esp_zb_humidity_meas_cluster_add_attr(humidity_cluster, ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_VALUE_ID, &default_hum); + esp_zb_humidity_meas_cluster_add_attr(humidity_cluster, ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_MIN_VALUE_ID, &zb_min); + esp_zb_humidity_meas_cluster_add_attr(humidity_cluster, ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_MAX_VALUE_ID, &zb_max); + esp_zb_humidity_meas_cluster_add_attr(humidity_cluster, ESP_ZB_ZCL_ATTR_REL_HUMIDITY_TOLERANCE_ID, &zb_tolerance); + esp_zb_cluster_list_add_humidity_meas_cluster(_cluster_list, humidity_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); +} + +void ZigbeeTempSensor::setHumidity(float humidity) { + int16_t zb_humidity = zb_float_to_s16(humidity); + log_v("Updating humidity sensor value..."); + /* Update humidity sensor measured value */ + log_d("Setting humidity to %d", zb_humidity); + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_REL_HUMIDITY_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_VALUE_ID, &zb_humidity, false + ); + esp_zb_lock_release(); +} + +void ZigbeeTempSensor::reportHumidity() { + /* Send report attributes command */ + esp_zb_zcl_report_attr_cmd_t report_attr_cmd; + report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT; + report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_VALUE_ID; + report_attr_cmd.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE; + report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_REL_HUMIDITY_MEASUREMENT; + report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint; + + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd); + esp_zb_lock_release(); + log_v("Humidity report sent"); +} + +void ZigbeeTempSensor::setHumidityReporting(uint16_t min_interval, uint16_t max_interval, float delta) { + esp_zb_zcl_reporting_info_t reporting_info = { + .direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV, + .ep = _endpoint, + .cluster_id = ESP_ZB_ZCL_CLUSTER_ID_REL_HUMIDITY_MEASUREMENT, + .cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, + .attr_id = ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_VALUE_ID, + .u = + { + .send_info = + { + .min_interval = min_interval, + .max_interval = max_interval, + .delta = + { + .u16 = (uint16_t)(delta * 100), // Convert delta to ZCL uint16_t + }, + .def_min_interval = min_interval, + .def_max_interval = max_interval, + }, + }, + .dst = + { + .profile_id = ESP_ZB_AF_HA_PROFILE_ID, + }, + .manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC, + }; + esp_zb_zcl_update_reporting_info(&reporting_info); +} + #endif //SOC_IEEE802154_SUPPORTED diff --git a/libraries/Zigbee/src/ep/ZigbeeTempSensor.h b/libraries/Zigbee/src/ep/ZigbeeTempSensor.h index 22317721fc4..0f2040ff917 100644 --- a/libraries/Zigbee/src/ep/ZigbeeTempSensor.h +++ b/libraries/Zigbee/src/ep/ZigbeeTempSensor.h @@ -1,4 +1,4 @@ -/* Class of Zigbee Temperature sensor endpoint inherited from common EP class */ +/* Class of Zigbee Temperature + Humidity sensor endpoint inherited from common EP class */ #pragma once @@ -24,7 +24,21 @@ class ZigbeeTempSensor : public ZigbeeEP { // Set the reporting interval for temperature measurement in seconds and delta (temp change in 0,01 °C) void setReporting(uint16_t min_interval, uint16_t max_interval, float delta); + + // Report the temperature value void reportTemperature(); + + // Add humidity cluster to the temperature sensor device + void addHumiditySensor(float min, float max, float tolerance); + + // Set the humidity value in 0,01% + void setHumidity(float value); + + // Set the reporting interval for humidity measurement in seconds and delta (humidity change in 0,01%) + void setHumidityReporting(uint16_t min_interval, uint16_t max_interval, float delta); + + // Report the humidity value + void reportHumidity(); }; #endif //SOC_IEEE802154_SUPPORTED diff --git a/libraries/Zigbee/src/ep/ZigbeeThermostat.cpp b/libraries/Zigbee/src/ep/ZigbeeThermostat.cpp index 28ed2a70cd2..032b2443bd9 100644 --- a/libraries/Zigbee/src/ep/ZigbeeThermostat.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeThermostat.cpp @@ -67,7 +67,7 @@ void ZigbeeThermostat::findCb(esp_zb_zdp_status_t zdo_status, uint16_t addr, uin /* populate the dst information of the binding */ bind_req.dst_addr_mode = ESP_ZB_ZDO_BIND_DST_ADDR_MODE_64_BIT_EXTENDED; esp_zb_get_long_address(bind_req.dst_address_u.addr_long); - bind_req.dst_endp = _endpoint; + bind_req.dst_endp = *((uint8_t*)user_ctx); //_endpoint; log_i("Request temperature sensor to bind us"); esp_zb_zdo_device_bind_req(&bind_req, bindCb, NULL); @@ -77,7 +77,7 @@ void ZigbeeThermostat::findCb(esp_zb_zdp_status_t zdo_status, uint16_t addr, uin /* populate the src information of the binding */ esp_zb_get_long_address(bind_req.src_address); - bind_req.src_endp = _endpoint; + bind_req.src_endp = *((uint8_t*)user_ctx); //_endpoint; bind_req.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT; /* populate the dst information of the binding */ @@ -96,7 +96,7 @@ void ZigbeeThermostat::findEndpoint(esp_zb_zdo_match_desc_req_param_t *param) { param->num_in_clusters = 1; param->num_out_clusters = 0; param->cluster_list = cluster_list; - esp_zb_zdo_match_cluster(param, findCb, NULL); + esp_zb_zdo_match_cluster(param, findCb, &_endpoint); } void ZigbeeThermostat::zbAttributeRead(uint16_t cluster_id, const esp_zb_zcl_attribute_t *attribute) { From 64e0fdebb81229ca7bb7276a50f1a0a66493806a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:14:01 +0100 Subject: [PATCH 5/8] feat(zigbee): Add common Zigbee.h header file --- .../Zigbee_Color_Dimmable_Light.ino | 3 +-- .../Zigbee_Color_Dimmer_Switch.ino | 3 +-- .../Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino | 3 +-- .../Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino | 3 +-- .../Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino | 2 +- .../Zigbee_Temp_Hum_Sensor_Sleepy.ino | 3 +-- .../Zigbee_Temperature_Sensor.ino | 3 +-- .../Zigbee_Thermostat/Zigbee_Thermostat.ino | 3 +-- libraries/Zigbee/src/Zigbee.h | 15 +++++++++++++++ 9 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 libraries/Zigbee/src/Zigbee.h diff --git a/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino b/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino index 01e8c24900f..c03d26d3aba 100644 --- a/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino +++ b/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino @@ -31,8 +31,7 @@ #error "Zigbee end device mode is not selected in Tools->Zigbee mode" #endif -#include "ZigbeeCore.h" -#include "ep/ZigbeeColorDimmableLight.h" +#include "Zigbee.h" #define LED_PIN RGB_BUILTIN #define BUTTON_PIN 9 // C6/H2 Boot button diff --git a/libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino b/libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino index 0514464e8e9..6d6c7b163dd 100644 --- a/libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino +++ b/libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino @@ -35,8 +35,7 @@ #error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" #endif -#include "ZigbeeCore.h" -#include "ep/ZigbeeColorDimmerSwitch.h" +#include "Zigbee.h" /* Switch configuration */ #define SWITCH_PIN 9 // ESP32-C6/H2 Boot button diff --git a/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino b/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino index e0bc3747eb3..30e3cd2d109 100644 --- a/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino +++ b/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino @@ -30,8 +30,7 @@ #error "Zigbee end device mode is not selected in Tools->Zigbee mode" #endif -#include "ZigbeeCore.h" -#include "ep/ZigbeeLight.h" +#include "Zigbee.h" #define LED_PIN RGB_BUILTIN #define BUTTON_PIN 9 // ESP32-C6/H2 Boot button diff --git a/libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino b/libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino index 09487dea05d..69cf6654a41 100644 --- a/libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino +++ b/libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino @@ -31,8 +31,7 @@ #error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" #endif -#include "ZigbeeCore.h" -#include "ep/ZigbeeSwitch.h" +#include "Zigbee.h" #define SWITCH_ENDPOINT_NUMBER 5 diff --git a/libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino b/libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino index a72ade201ae..7d59fb2907d 100644 --- a/libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino +++ b/libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino @@ -29,7 +29,7 @@ #error "Zigbee device mode is not selected in Tools->Zigbee mode" #endif -#include "ZigbeeCore.h" +#include "Zigbee.h" #ifdef ZIGBEE_MODE_ZCZR zigbee_role_t role = ZIGBEE_ROUTER; // or can be ZIGBEE_COORDINATOR, but it wont scan itself diff --git a/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino index af0c771e39b..9b19e4aaf0a 100644 --- a/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino +++ b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino @@ -30,8 +30,7 @@ #error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" #endif -#include "ZigbeeCore.h" -#include "ep/ZigbeeTempSensor.h" +#include "Zigbee.h" #define BUTTON_PIN 9 //Boot button for C6/H2 #define TEMP_SENSOR_ENDPOINT_NUMBER 10 diff --git a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor/Zigbee_Temperature_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor/Zigbee_Temperature_Sensor.ino index b614b136bef..c5ca00decd6 100644 --- a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor/Zigbee_Temperature_Sensor.ino +++ b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor/Zigbee_Temperature_Sensor.ino @@ -30,8 +30,7 @@ #error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" #endif -#include "ZigbeeCore.h" -#include "ep/ZigbeeTempSensor.h" +#include "Zigbee.h" #define BUTTON_PIN 9 //Boot button for C6/H2 #define TEMP_SENSOR_ENDPOINT_NUMBER 10 diff --git a/libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino b/libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino index ac7b023d6d9..565d9e64919 100644 --- a/libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino +++ b/libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino @@ -31,8 +31,7 @@ #error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" #endif -#include "ZigbeeCore.h" -#include "ep/ZigbeeThermostat.h" +#include "Zigbee.h" #define BUTTON_PIN 9 // Boot button for C6/H2 #define THERMOSTAT_ENDPOINT_NUMBER 5 diff --git a/libraries/Zigbee/src/Zigbee.h b/libraries/Zigbee/src/Zigbee.h new file mode 100644 index 00000000000..98674a9d115 --- /dev/null +++ b/libraries/Zigbee/src/Zigbee.h @@ -0,0 +1,15 @@ +// Zigbee library header file for includes of all Zigbee library headers. + +#pragma once + +// Core +#include "ZigbeeCore.h" +#include "ZigbeeEP.h" + +// Endpoints +#include "ep/ZigbeeLight.h" +#include "ep/ZigbeeSwitch.h" +#include "ep/ZigbeeColorDimmableLight.h" +#include "ep/ZigbeeColorDimmerSwitch.h" +#include "ep/ZigbeeTempSensor.h" +#include "ep/ZigbeeThermostat.h" From c7dd76aa87b3cc753dbc0829fe239052f11d08e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:18:23 +0100 Subject: [PATCH 6/8] fix(zigbee): remove leftover code --- libraries/Zigbee/src/ZigbeeEP.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index f08f95be524..d4725145375 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -7,18 +7,6 @@ #include "esp_zigbee_cluster.h" #include "zcl/esp_zigbee_zcl_power_config.h" -#ifdef __cplusplus -extern "C" -{ -#endif - -extern void zb_set_ed_node_descriptor(bool power_src, bool rx_on_when_idle, bool alloc_addr); - -#ifdef __cplusplus -} -#endif - -// uint8_t ZigbeeEP::_endpoint = 0; bool ZigbeeEP::_is_bound = false; bool ZigbeeEP::_allow_multiple_binding = false; From f352c9ad2a17f4e8d9f6c620f6ebd9ba6c8dd6c3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 6 Nov 2024 08:45:15 +0000 Subject: [PATCH 7/8] ci(pre-commit): Apply automatic fixes --- .../Zigbee_Temp_Hum_Sensor_Sleepy.ino | 6 +++--- libraries/Zigbee/src/ZigbeeCore.cpp | 6 +++--- libraries/Zigbee/src/ZigbeeEP.cpp | 9 +++++---- libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp | 2 +- libraries/Zigbee/src/ep/ZigbeeSwitch.cpp | 2 +- libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp | 3 ++- libraries/Zigbee/src/ep/ZigbeeThermostat.cpp | 4 ++-- 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino index 9b19e4aaf0a..265c6a28ee8 100644 --- a/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino +++ b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino @@ -41,7 +41,7 @@ ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER); /************************ Temp sensor *****************************/ -void meausureAndSleep(){ +void meausureAndSleep() { // Measure temperature sensor value float temperature = temperatureRead(); @@ -84,7 +84,7 @@ void setup() { zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100); // Add humidity cluster to the temperature sensor device with min, max and tolerance values - zbTempSensor.addHumiditySensor(0,100,1); + zbTempSensor.addHumiditySensor(0, 100, 1); // Add endpoint to Zigbee Core Zigbee.addEndpoint(&zbTempSensor); @@ -97,7 +97,7 @@ void setup() { Zigbee.begin(&zigbeeConfig, false); // Wait for Zigbee to start - while(!Zigbee.isStarted()){ + while (!Zigbee.isStarted()) { delay(100); } diff --git a/libraries/Zigbee/src/ZigbeeCore.cpp b/libraries/Zigbee/src/ZigbeeCore.cpp index 6aaae711abf..31f9ab1910f 100644 --- a/libraries/Zigbee/src/ZigbeeCore.cpp +++ b/libraries/Zigbee/src/ZigbeeCore.cpp @@ -78,8 +78,8 @@ static void esp_zb_task(void *pvParameters) { ESP_ERROR_CHECK(esp_zb_start(false)); //NOTE: This is a workaround to make battery powered devices to be discovered as battery powered - if(((zigbee_role_t)Zigbee.getRole() == ZIGBEE_END_DEVICE) && edBatteryPowered ) { - zb_set_ed_node_descriptor(0,0,0); + if (((zigbee_role_t)Zigbee.getRole() == ZIGBEE_END_DEVICE) && edBatteryPowered) { + zb_set_ed_node_descriptor(0, 0, 0); } esp_zb_stack_main_loop(); @@ -118,7 +118,7 @@ bool ZigbeeCore::zigbeeInit(esp_zb_cfg_t *zb_cfg, bool erase_nvs) { log_i("List of registered Zigbee EPs:"); for (std::list::iterator it = ep_objects.begin(); it != ep_objects.end(); ++it) { log_i("Device type: %s, Endpoint: %d, Device ID: 0x%04x", getDeviceTypeString((*it)->_device_id), (*it)->_endpoint, (*it)->_device_id); - if((*it)->_power_source == ZB_POWER_SOURCE_BATTERY) { + if ((*it)->_power_source == ZB_POWER_SOURCE_BATTERY) { edBatteryPowered = true; } } diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index d4725145375..8597f4404c2 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -73,7 +73,7 @@ void ZigbeeEP::setPowerSource(zb_power_source_t power_source, uint8_t battery_pe esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); esp_zb_cluster_update_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_POWER_SOURCE_ID, (void *)&power_source); - if(power_source == ZB_POWER_SOURCE_BATTERY){ + if (power_source == ZB_POWER_SOURCE_BATTERY) { // Add power config cluster and battery percentage attribute battery_percentage = battery_percentage * 2; esp_zb_attribute_list_t *power_config_cluster = esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG); @@ -92,14 +92,15 @@ void ZigbeeEP::setBatteryPercentage(uint8_t percentage) { percentage = percentage * 2; esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_set_attribute_val( - _endpoint, ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID, &percentage, false + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID, &percentage, + false ); esp_zb_lock_release(); log_v("Battery percentage updated"); } void ZigbeeEP::reportBatteryPercentage() { - /* Send report attributes command */ + /* Send report attributes command */ esp_zb_zcl_report_attr_cmd_t report_attr_cmd; report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT; report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID; @@ -201,7 +202,7 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) { void ZigbeeEP::zbIdentify(const esp_zb_zcl_set_attr_value_message_t *message) { if (message->attribute.id == ESP_ZB_ZCL_CMD_IDENTIFY_IDENTIFY_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) { - if(_on_identify != NULL) { + if (_on_identify != NULL) { _on_identify(*(uint16_t *)message->attribute.data.value); } } else { diff --git a/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp b/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp index e539a66b4b0..d9a9e1c1014 100644 --- a/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp @@ -57,7 +57,7 @@ void ZigbeeColorDimmerSwitch::findCb(esp_zb_zdp_status_t zdo_status, uint16_t ad light->short_addr = addr; esp_zb_ieee_address_by_short(light->short_addr, light->ieee_addr); esp_zb_get_long_address(bind_req.src_address); - bind_req.src_endp = *((uint8_t*)user_ctx); //_endpoint; + bind_req.src_endp = *((uint8_t *)user_ctx); //_endpoint; bind_req.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_ON_OFF; bind_req.dst_addr_mode = ESP_ZB_ZDO_BIND_DST_ADDR_MODE_64_BIT_EXTENDED; memcpy(bind_req.dst_address_u.addr_long, light->ieee_addr, sizeof(esp_zb_ieee_addr_t)); diff --git a/libraries/Zigbee/src/ep/ZigbeeSwitch.cpp b/libraries/Zigbee/src/ep/ZigbeeSwitch.cpp index eb1268b701b..17f0b6703c9 100644 --- a/libraries/Zigbee/src/ep/ZigbeeSwitch.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeSwitch.cpp @@ -35,7 +35,7 @@ void ZigbeeSwitch::findCb(esp_zb_zdp_status_t zdo_status, uint16_t addr, uint8_t light->short_addr = addr; esp_zb_ieee_address_by_short(light->short_addr, light->ieee_addr); esp_zb_get_long_address(bind_req.src_address); - bind_req.src_endp = *((uint8_t*)user_ctx); //_endpoint; + bind_req.src_endp = *((uint8_t *)user_ctx); //_endpoint; bind_req.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_ON_OFF; bind_req.dst_addr_mode = ESP_ZB_ZDO_BIND_DST_ADDR_MODE_64_BIT_EXTENDED; memcpy(bind_req.dst_address_u.addr_long, light->ieee_addr, sizeof(esp_zb_ieee_addr_t)); diff --git a/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp index 9d4f1163a7e..a20f6255746 100644 --- a/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp @@ -110,7 +110,8 @@ void ZigbeeTempSensor::setHumidity(float humidity) { log_d("Setting humidity to %d", zb_humidity); esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_set_attribute_val( - _endpoint, ESP_ZB_ZCL_CLUSTER_ID_REL_HUMIDITY_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_VALUE_ID, &zb_humidity, false + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_REL_HUMIDITY_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_VALUE_ID, &zb_humidity, + false ); esp_zb_lock_release(); } diff --git a/libraries/Zigbee/src/ep/ZigbeeThermostat.cpp b/libraries/Zigbee/src/ep/ZigbeeThermostat.cpp index 032b2443bd9..b9805a37a19 100644 --- a/libraries/Zigbee/src/ep/ZigbeeThermostat.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeThermostat.cpp @@ -67,7 +67,7 @@ void ZigbeeThermostat::findCb(esp_zb_zdp_status_t zdo_status, uint16_t addr, uin /* populate the dst information of the binding */ bind_req.dst_addr_mode = ESP_ZB_ZDO_BIND_DST_ADDR_MODE_64_BIT_EXTENDED; esp_zb_get_long_address(bind_req.dst_address_u.addr_long); - bind_req.dst_endp = *((uint8_t*)user_ctx); //_endpoint; + bind_req.dst_endp = *((uint8_t *)user_ctx); //_endpoint; log_i("Request temperature sensor to bind us"); esp_zb_zdo_device_bind_req(&bind_req, bindCb, NULL); @@ -77,7 +77,7 @@ void ZigbeeThermostat::findCb(esp_zb_zdp_status_t zdo_status, uint16_t addr, uin /* populate the src information of the binding */ esp_zb_get_long_address(bind_req.src_address); - bind_req.src_endp = *((uint8_t*)user_ctx); //_endpoint; + bind_req.src_endp = *((uint8_t *)user_ctx); //_endpoint; bind_req.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT; /* populate the dst information of the binding */ From 6906b0e2787d6697ab643b9e0884fe834c167737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 6 Nov 2024 09:47:34 +0100 Subject: [PATCH 8/8] fix(zigbee): Fix codespell issues --- .../Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md index b85da980c2e..d88b01cda3a 100644 --- a/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md +++ b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md @@ -23,7 +23,7 @@ Currently, this example supports the following targets. ### Configure the Project -In this example, to demonstrate the funcionality the chip temperature is used and reported as temperature and humidity. +In this example, to demonstrate the functionality the chip temperature is used and reported as temperature and humidity. Set the Button GPIO by changing the `BUTTON_PIN` definition. By default, it's the pin `9` (BOOT button on ESP32-C6 and ESP32-H2). #### Using Arduino IDE