From 81c26b960ba7435ac9a9cf4ab4c5356d81fd7c3e Mon Sep 17 00:00:00 2001 From: pwclay Date: Sun, 9 Feb 2025 14:14:50 +1300 Subject: [PATCH 1/6] add analog sensor modules --- .../examples/Zigbee_Analog_Sensor/README.md | 72 ++++++++++++ .../Zigbee_Analog_Sensor.ino | 108 ++++++++++++++++++ .../examples/Zigbee_Analog_Sensor/ci.json | 6 + libraries/Zigbee/src/Zigbee.h | 1 + .../Zigbee/src/ep/ZigbeeAnalogSensor.cpp | 77 +++++++++++++ libraries/Zigbee/src/ep/ZigbeeAnalogSensor.h | 60 ++++++++++ 6 files changed, 324 insertions(+) create mode 100644 libraries/Zigbee/examples/Zigbee_Analog_Sensor/README.md create mode 100644 libraries/Zigbee/examples/Zigbee_Analog_Sensor/Zigbee_Analog_Sensor.ino create mode 100644 libraries/Zigbee/examples/Zigbee_Analog_Sensor/ci.json create mode 100644 libraries/Zigbee/src/ep/ZigbeeAnalogSensor.cpp create mode 100644 libraries/Zigbee/src/ep/ZigbeeAnalogSensor.h diff --git a/libraries/Zigbee/examples/Zigbee_Analog_Sensor/README.md b/libraries/Zigbee/examples/Zigbee_Analog_Sensor/README.md new file mode 100644 index 00000000000..3953a76fa36 --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Analog_Sensor/README.md @@ -0,0 +1,72 @@ +# Arduino-ESP32 Zigbee Analog Sensor Example + +This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) simple sensor device type with analog measuring. + +# Supported Targets + +Currently, this example supports the following targets. + +| Supported Targets | ESP32-C6 | ESP32-H2 | +| ----------------- | -------- | -------- | + +## Analog Sensor Functions + + * After this board first starts up, it would be configured locally to report an analog value on change or every 30 seconds. + * By clicking the button (BOOT) on this board, this board will immediately send a report of the current measured value to the network. + +## Hardware Required + +* A USB cable for power supply and programming + +### Configure the Project + +In this example, the internal temperature sensor is used to demonstrate reading of the analog sensors. +Set the Button GPIO by changing the `button` variable. By default, it's the pin `BOOT_PIN` (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_Analog_Sensor/Zigbee_Analog_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Analog_Sensor/Zigbee_Analog_Sensor.ino new file mode 100644 index 00000000000..d020d2110cc --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Analog_Sensor/Zigbee_Analog_Sensor.ino @@ -0,0 +1,108 @@ +// +// 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 analog sensor. + * + * The example demonstrates how to use Zigbee library to create a end device analog sensor. + * + * 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/) + * Modified by Pat Clay + */ + +#ifndef ZIGBEE_MODE_ED +#error "Zigbee end device mode is not selected in Tools->Zigbee mode" +#endif + +#include "Zigbee.h" + +/* Zigbee analog sensor configuration */ +#define ANALOG_SENSOR_ENDPOINT_NUMBER 10 + +uint8_t button = BOOT_PIN; + +ZigbeeAnalogSensor zbAnalogSensor = ZigbeeAnalogSensor(ANALOG_SENSOR_ENDPOINT_NUMBER); + +void setup() { + Serial.begin(115200); + Serial.println("Starting..."); + + // Init button switch + pinMode(button, INPUT_PULLUP); + + // Optional: set Zigbee device name and model + zbAnalogSensor.setManufacturerAndModel("WekaBuilt", "ZigbeePowerSensor"); + + // Add endpoints to Zigbee Core + Zigbee.addEndpoint(&zbAnalogSensor); + + Serial.println("Starting Zigbee..."); + // When all EPs are registered, start Zigbee in End Device mode + if (!Zigbee.begin()) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } else { + Serial.println("Zigbee started successfully!"); + } + Serial.println("Connecting to network"); + while (!Zigbee.connected()) { + Serial.print("."); + delay(100); + } + Serial.println("Connected"); + + // Set reporting interval for measurement in seconds, must be called after Zigbee.begin() + // min_interval and max_interval in seconds, delta + // if min = 1 and max = 0, reporting is sent only when temperature changes by delta + // if min = 0 and max = 10, reporting is sent every 10 seconds or temperature changes by delta + // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change + zbAnalogSensor.setReporting(0, 2, 0); + Serial.println("reporting set"); + +} + +void loop() { + static uint32_t timeCounter = 0; + + // Read flow and pressure sensors every 2s + if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s + float tsens_value = temperatureRead(); + Serial.printf("Updating voltage value to %.2f°C\r\n", tsens_value); + zbAnalogSensor.setAnalog(tsens_value); + } + + // Checking button for factory reset and reporting + if (digitalRead(button) == LOW) { // Push button pressed + // Key debounce handling + delay(100); + int startTime = millis(); + while (digitalRead(button) == LOW) { + delay(50); + if ((millis() - startTime) > 3000) { + // If key pressed for more than 3secs, factory reset Zigbee and reboot + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); + delay(1000); + Zigbee.factoryReset(); + } + } + } + delay(100); + zbAnalogSensor.report(); + +} diff --git a/libraries/Zigbee/examples/Zigbee_Analog_Sensor/ci.json b/libraries/Zigbee/examples/Zigbee_Analog_Sensor/ci.json new file mode 100644 index 00000000000..7b7ccef8ed7 --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Analog_Sensor/ci.json @@ -0,0 +1,6 @@ +{ + "fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed", + "requires": [ + "CONFIG_SOC_IEEE802154_SUPPORTED=y" + ] +} diff --git a/libraries/Zigbee/src/Zigbee.h b/libraries/Zigbee/src/Zigbee.h index 49bfa44d5c1..fb591bc7c2b 100644 --- a/libraries/Zigbee/src/Zigbee.h +++ b/libraries/Zigbee/src/Zigbee.h @@ -15,6 +15,7 @@ #include "ep/ZigbeeTempSensor.h" #include "ep/ZigbeeThermostat.h" #include "ep/ZigbeePressureSensor.h" +#include "ep/ZigbeeAnalogSensor.h" #include "ep/ZigbeeFlowSensor.h" #include "ep/ZigbeeOccupancySensor.h" #include "ep/ZigbeeCarbonDioxideSensor.h" diff --git a/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.cpp new file mode 100644 index 00000000000..228358734a9 --- /dev/null +++ b/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.cpp @@ -0,0 +1,77 @@ +#include "ZigbeeAnalogSensor.h" +#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED + +esp_zb_cluster_list_t *zigbee_analog_sensor_clusters_create(zigbee_analog_sensor_cfg_t *analog_sensor) { + esp_zb_basic_cluster_cfg_t *basic_cfg = analog_sensor ? &(analog_sensor->basic_cfg) : NULL; + esp_zb_identify_cluster_cfg_t *identify_cfg = analog_sensor ? &(analog_sensor->identify_cfg) : NULL; + esp_zb_analog_value_cluster_cfg_t *analog_value_cfg = analog_sensor ? &(analog_sensor->analog_value_cfg) : NULL; + esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create(); + esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + esp_zb_cluster_list_add_analog_value_cluster( + cluster_list, esp_zb_analog_value_cluster_create(analog_value_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE + ); + return cluster_list; +} + +ZigbeeAnalogSensor::ZigbeeAnalogSensor(uint8_t endpoint) : ZigbeeEP(endpoint) { + _device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID; + + //Create custom analog sensor configuration + zigbee_analog_sensor_cfg_t analog_sensor_cfg = ZIGBEE_DEFAULT_ANALOG_SENSOR_CONFIG(); + _cluster_list = zigbee_analog_sensor_clusters_create(&analog_sensor_cfg); + + _ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID, .app_device_version = 0}; +} + +void ZigbeeAnalogSensor::setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta) { + if (delta > 0) { + log_e("Delta reporting is currently not supported by the sensor"); + } + esp_zb_zcl_reporting_info_t reporting_info; + memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t)); + reporting_info.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV; + reporting_info.ep = _endpoint; + reporting_info.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE; + reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE; + reporting_info.attr_id = ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID; + reporting_info.u.send_info.min_interval = min_interval; + reporting_info.u.send_info.max_interval = max_interval; + reporting_info.u.send_info.def_min_interval = min_interval; + reporting_info.u.send_info.def_max_interval = max_interval; + reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID; + reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC, esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_update_reporting_info(&reporting_info); + esp_zb_lock_release(); + +} + +void ZigbeeAnalogSensor::setAnalog(float analog) { + float zb_analog = analog; + log_v("Updating sensor value..."); + /* Update sensor measured value */ + log_d("Setting value to %0.1f", zb_analog); + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID, + &zb_analog, false + ); + esp_zb_lock_release(); +} + +void ZigbeeAnalogSensor::report() { + /* 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_ANALOG_VALUE_PRESENT_VALUE_ID; + report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI; + report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE; + 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("Analog report sent"); +} + +#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED diff --git a/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.h b/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.h new file mode 100644 index 00000000000..32010993ae5 --- /dev/null +++ b/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.h @@ -0,0 +1,60 @@ +/* Class of Zigbee Analog sensor endpoint inherited from common EP class */ + +#pragma once + +#include "soc/soc_caps.h" +#include "sdkconfig.h" +#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED + +#include "ZigbeeEP.h" +#include "ha/esp_zigbee_ha_standard.h" + +// clang-format off +#define ZIGBEE_DEFAULT_ANALOG_SENSOR_CONFIG() \ + { \ + .basic_cfg = \ + { \ + .zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE, \ + .power_source = ESP_ZB_ZCL_BASIC_POWER_SOURCE_DEFAULT_VALUE, \ + }, \ + .identify_cfg = \ + { \ + .identify_time = ESP_ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE, \ + }, \ + .analog_value_cfg = \ + { \ + .out_of_service = 0, \ + .present_value = 0, \ + .status_flags = 1, \ + }, \ + } +// clang-format on + +typedef struct zigbee_analog_sensor_cfg_s { + esp_zb_basic_cluster_cfg_t basic_cfg; + esp_zb_identify_cluster_cfg_t identify_cfg; + esp_zb_analog_value_cluster_cfg_t analog_value_cfg; +} zigbee_analog_sensor_cfg_t; + +class ZigbeeAnalogSensor : public ZigbeeEP { +public: + ZigbeeAnalogSensor(uint8_t endpoint); + ~ZigbeeAnalogSensor() {} + + // Set the value + void setAnalog(float analog); +/* + // Set the min and max value for the sensor + void setMinMaxValue(float min, float max); + + // Set the tolerance value for the sensor + void setTolerance(float tolerance); +*/ + // Set the reporting interval for in seconds and delta + void setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta); + + // Report the analog value + void report(); +}; + +#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED From 659de82e89a8e856680abcbbe78ab2f0f8fab7e2 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: Mon, 17 Feb 2025 14:34:32 +0100 Subject: [PATCH 2/6] feat(zigbee): Add analog value, input and output support --- CMakeLists.txt | 1 + .../README.md | 8 +- .../Zigbee_Analog_Input_Output.ino | 120 ++++++++++++++++ .../ci.json | 0 .../Zigbee_Analog_Sensor.ino | 108 -------------- libraries/Zigbee/src/Zigbee.h | 2 +- libraries/Zigbee/src/ep/ZigbeeAnalog.cpp | 134 ++++++++++++++++++ .../{ZigbeeAnalogSensor.h => ZigbeeAnalog.h} | 64 +++++---- .../Zigbee/src/ep/ZigbeeAnalogSensor.cpp | 77 ---------- libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp | 3 +- 10 files changed, 300 insertions(+), 217 deletions(-) rename libraries/Zigbee/examples/{Zigbee_Analog_Sensor => Zigbee_Analog_Input_Output}/README.md (92%) create mode 100644 libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino rename libraries/Zigbee/examples/{Zigbee_Analog_Sensor => Zigbee_Analog_Input_Output}/ci.json (100%) delete mode 100644 libraries/Zigbee/examples/Zigbee_Analog_Sensor/Zigbee_Analog_Sensor.ino create mode 100644 libraries/Zigbee/src/ep/ZigbeeAnalog.cpp rename libraries/Zigbee/src/ep/{ZigbeeAnalogSensor.h => ZigbeeAnalog.h} (54%) delete mode 100644 libraries/Zigbee/src/ep/ZigbeeAnalogSensor.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 40567d1b45c..0944b6fef75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -293,6 +293,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS libraries/Zigbee/src/ep/ZigbeeContactSwitch.cpp libraries/Zigbee/src/ep/ZigbeeDoorWindowHandle.cpp libraries/Zigbee/src/ep/ZigbeeWindowCovering.cpp + libraries/Zigbee/src/ep/ZigbeeAnalog.cpp ) set(ARDUINO_LIBRARY_BLE_SRCS diff --git a/libraries/Zigbee/examples/Zigbee_Analog_Sensor/README.md b/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/README.md similarity index 92% rename from libraries/Zigbee/examples/Zigbee_Analog_Sensor/README.md rename to libraries/Zigbee/examples/Zigbee_Analog_Input_Output/README.md index 3953a76fa36..d62941755c7 100644 --- a/libraries/Zigbee/examples/Zigbee_Analog_Sensor/README.md +++ b/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/README.md @@ -1,6 +1,6 @@ -# Arduino-ESP32 Zigbee Analog Sensor Example +# Arduino-ESP32 Zigbee Analog Input Output Example -This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) simple sensor device type with analog measuring. +This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) analog input/output device. # Supported Targets @@ -11,7 +11,7 @@ Currently, this example supports the following targets. ## Analog Sensor Functions - * After this board first starts up, it would be configured locally to report an analog value on change or every 30 seconds. + * After this board first starts up, it would be configured locally to report an analog input on change or every 30 seconds. * By clicking the button (BOOT) on this board, this board will immediately send a report of the current measured value to the network. ## Hardware Required @@ -20,7 +20,7 @@ Currently, this example supports the following targets. ### Configure the Project -In this example, the internal temperature sensor is used to demonstrate reading of the analog sensors. +Set the ADC GPIO by changing the `analogPin` variable. By default, it's the pin `A0`. Set the Button GPIO by changing the `button` variable. By default, it's the pin `BOOT_PIN` (BOOT button on ESP32-C6 and ESP32-H2). #### Using Arduino IDE diff --git a/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino b/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino new file mode 100644 index 00000000000..a33617b1ee3 --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino @@ -0,0 +1,120 @@ +// Copyright 2025 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 analog input / output device. + * + * The example demonstrates how to use Zigbee library to create a end device analog device. + * + * 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/) + * Modified by Pat Clay + */ + + #ifndef ZIGBEE_MODE_ED + #error "Zigbee end device mode is not selected in Tools->Zigbee mode" + #endif + + #include "Zigbee.h" + + /* Zigbee analog device configuration */ + #define ANALOG_DEVICE_ENDPOINT_NUMBER 1 + + uint8_t analogPin = A0; + uint8_t button = BOOT_PIN; + + ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER); + + void onAnalogOutputChange(float analog_output) { + Serial.printf("Received analog output change: %.1f\r\n", analog_output); + } + + void setup() { + Serial.begin(115200); + Serial.println("Starting..."); + + // Init button switch + pinMode(button, INPUT_PULLUP); + + // Set analog resolution to 10 bits + analogReadResolution(10); + + // Optional: set Zigbee device name and model + zbAnalogDevice.setManufacturerAndModel("Espressif", "ZigbeeAnalogDevice"); + + // Add analog sensor cluster to Zigbee Analog accoding your needs + zbAnalogDevice.addAnalogInput(); + zbAnalogDevice.addAnalogOutput(); + + // If analog output cluster is added, set callback function for analog output change + zbAnalogDevice.onAnalogOutputChange(onAnalogOutputChange); + + // Add endpoints to Zigbee Core + Zigbee.addEndpoint(&zbAnalogDevice); + + Serial.println("Starting Zigbee..."); + // When all EPs are registered, start Zigbee in End Device mode + if (!Zigbee.begin()) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } else { + Serial.println("Zigbee started successfully!"); + } + Serial.println("Connecting to network"); + while (!Zigbee.connected()) { + Serial.print("."); + delay(100); + } + Serial.println("Connected"); + + // Optional: Add reporting for analog input + zbAnalogDevice.setAnalogInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10 + } + + void loop() { + static uint32_t timeCounter = 0; + + // Read ADC value and update the analog value every 2s + if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s + float analog = (float)analogRead(analogPin); + Serial.printf("Updating analog input to %.1f\r\n", analog); + zbAnalogDevice.setAnalogInput(analog); + + // Analog input supports reporting + zbAnalogDevice.reportAnalogInput(); + } + + // Checking button for factory reset and reporting + if (digitalRead(button) == LOW) { // Push button pressed + // Key debounce handling + delay(100); + int startTime = millis(); + while (digitalRead(button) == LOW) { + delay(50); + if ((millis() - startTime) > 3000) { + // If key pressed for more than 3secs, factory reset Zigbee and reboot + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); + delay(1000); + Zigbee.factoryReset(); + } + } + } + delay(100); + } + \ No newline at end of file diff --git a/libraries/Zigbee/examples/Zigbee_Analog_Sensor/ci.json b/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/ci.json similarity index 100% rename from libraries/Zigbee/examples/Zigbee_Analog_Sensor/ci.json rename to libraries/Zigbee/examples/Zigbee_Analog_Input_Output/ci.json diff --git a/libraries/Zigbee/examples/Zigbee_Analog_Sensor/Zigbee_Analog_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Analog_Sensor/Zigbee_Analog_Sensor.ino deleted file mode 100644 index d020d2110cc..00000000000 --- a/libraries/Zigbee/examples/Zigbee_Analog_Sensor/Zigbee_Analog_Sensor.ino +++ /dev/null @@ -1,108 +0,0 @@ -// -// 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 analog sensor. - * - * The example demonstrates how to use Zigbee library to create a end device analog sensor. - * - * 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/) - * Modified by Pat Clay - */ - -#ifndef ZIGBEE_MODE_ED -#error "Zigbee end device mode is not selected in Tools->Zigbee mode" -#endif - -#include "Zigbee.h" - -/* Zigbee analog sensor configuration */ -#define ANALOG_SENSOR_ENDPOINT_NUMBER 10 - -uint8_t button = BOOT_PIN; - -ZigbeeAnalogSensor zbAnalogSensor = ZigbeeAnalogSensor(ANALOG_SENSOR_ENDPOINT_NUMBER); - -void setup() { - Serial.begin(115200); - Serial.println("Starting..."); - - // Init button switch - pinMode(button, INPUT_PULLUP); - - // Optional: set Zigbee device name and model - zbAnalogSensor.setManufacturerAndModel("WekaBuilt", "ZigbeePowerSensor"); - - // Add endpoints to Zigbee Core - Zigbee.addEndpoint(&zbAnalogSensor); - - Serial.println("Starting Zigbee..."); - // When all EPs are registered, start Zigbee in End Device mode - if (!Zigbee.begin()) { - Serial.println("Zigbee failed to start!"); - Serial.println("Rebooting..."); - ESP.restart(); - } else { - Serial.println("Zigbee started successfully!"); - } - Serial.println("Connecting to network"); - while (!Zigbee.connected()) { - Serial.print("."); - delay(100); - } - Serial.println("Connected"); - - // Set reporting interval for measurement in seconds, must be called after Zigbee.begin() - // min_interval and max_interval in seconds, delta - // if min = 1 and max = 0, reporting is sent only when temperature changes by delta - // if min = 0 and max = 10, reporting is sent every 10 seconds or temperature changes by delta - // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change - zbAnalogSensor.setReporting(0, 2, 0); - Serial.println("reporting set"); - -} - -void loop() { - static uint32_t timeCounter = 0; - - // Read flow and pressure sensors every 2s - if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s - float tsens_value = temperatureRead(); - Serial.printf("Updating voltage value to %.2f°C\r\n", tsens_value); - zbAnalogSensor.setAnalog(tsens_value); - } - - // Checking button for factory reset and reporting - if (digitalRead(button) == LOW) { // Push button pressed - // Key debounce handling - delay(100); - int startTime = millis(); - while (digitalRead(button) == LOW) { - delay(50); - if ((millis() - startTime) > 3000) { - // If key pressed for more than 3secs, factory reset Zigbee and reboot - Serial.println("Resetting Zigbee to factory and rebooting in 1s."); - delay(1000); - Zigbee.factoryReset(); - } - } - } - delay(100); - zbAnalogSensor.report(); - -} diff --git a/libraries/Zigbee/src/Zigbee.h b/libraries/Zigbee/src/Zigbee.h index fb591bc7c2b..6b2a04eeabe 100644 --- a/libraries/Zigbee/src/Zigbee.h +++ b/libraries/Zigbee/src/Zigbee.h @@ -15,7 +15,7 @@ #include "ep/ZigbeeTempSensor.h" #include "ep/ZigbeeThermostat.h" #include "ep/ZigbeePressureSensor.h" -#include "ep/ZigbeeAnalogSensor.h" +#include "ep/ZigbeeAnalog.h" #include "ep/ZigbeeFlowSensor.h" #include "ep/ZigbeeOccupancySensor.h" #include "ep/ZigbeeCarbonDioxideSensor.h" diff --git a/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp b/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp new file mode 100644 index 00000000000..0af422a40a1 --- /dev/null +++ b/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp @@ -0,0 +1,134 @@ +#include "ZigbeeAnalog.h" +#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED + +esp_zb_cluster_list_t *zigbee_analog_clusters_create(zigbee_analog_cfg_t *analog_sensor) { + esp_zb_basic_cluster_cfg_t *basic_cfg = analog_sensor ? &(analog_sensor->basic_cfg) : NULL; + esp_zb_identify_cluster_cfg_t *identify_cfg = analog_sensor ? &(analog_sensor->identify_cfg) : NULL; + esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create(); + esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + return cluster_list; +} + +ZigbeeAnalog::ZigbeeAnalog(uint8_t endpoint) : ZigbeeEP(endpoint) { + _device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID; + + //Create custom analog sensor configuration + zigbee_analog_cfg_t analog_cfg = ZIGBEE_DEFAULT_ANALOG_CONFIG(); + _cluster_list = zigbee_analog_clusters_create(&analog_cfg); + + _ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID, .app_device_version = 0}; +} + +void ZigbeeAnalog::addAnalogValue() { + esp_zb_cluster_list_add_analog_value_cluster( + _cluster_list, esp_zb_analog_value_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE + ); + _analog_clusters |= ANALOG_VALUE; +} + +void ZigbeeAnalog::addAnalogInput() { + esp_zb_cluster_list_add_analog_input_cluster( + _cluster_list, esp_zb_analog_input_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE + ); + _analog_clusters |= ANALOG_INPUT; +} + +void ZigbeeAnalog::addAnalogOutput() { + esp_zb_cluster_list_add_analog_output_cluster( + _cluster_list, esp_zb_analog_output_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE + ); + _analog_clusters |= ANALOG_OUTPUT; +} + +//set attribute method -> method overridden in child class +void ZigbeeAnalog::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) { + //check the data and call right method + if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT) { + if (message->attribute.id == ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_SINGLE) { + float analog_output = *(float *)message->attribute.data.value; + analogOutputChanged(analog_output); + } else { + log_w("Received message ignored. Attribute ID: %d not supported for On/Off Light", message->attribute.id); + } + } else { + log_w("Received message ignored. Cluster ID: %d not supported for On/Off Light", message->info.cluster); + } +} + +void ZigbeeAnalog::analogOutputChanged(float analog_output) { + if (_on_analog_output_change) { + _on_analog_output_change(analog_output); + } else { + log_w("No callback function set for analog output change"); + } +} + +void ZigbeeAnalog::setAnalogValue(float analog) { + if(!(_analog_clusters & ANALOG_VALUE)) { + log_e("Analog Value cluster not added"); + return; + } + // float zb_analog = analog; + log_d("Setting analog value to %.1f", analog); + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID, + &analog, false + ); + esp_zb_lock_release(); +} + +void ZigbeeAnalog::setAnalogInput(float analog) { + if(!(_analog_clusters & ANALOG_INPUT)) { + log_e("Analog Input cluster not added"); + return; + } + // float zb_analog = analog; + log_d("Setting analog input to %.1f", analog); + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_INPUT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_INPUT_PRESENT_VALUE_ID, + &analog, false + ); + esp_zb_lock_release(); +} + +void ZigbeeAnalog::reportAnalogInput() { + /* 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_ANALOG_INPUT_PRESENT_VALUE_ID; + report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI; + report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_INPUT; + 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("Analog Input report sent"); +} + +void ZigbeeAnalog::setAnalogInputReporting(uint16_t min_interval, uint16_t max_interval, float delta) { + esp_zb_zcl_reporting_info_t reporting_info; + memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t)); + reporting_info.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV; + reporting_info.ep = _endpoint; + reporting_info.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT; + reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE; + reporting_info.attr_id = ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID; + reporting_info.u.send_info.min_interval = min_interval; + reporting_info.u.send_info.max_interval = max_interval; + reporting_info.u.send_info.def_min_interval = min_interval; + reporting_info.u.send_info.def_max_interval = max_interval; + reporting_info.u.send_info.delta.s32 = delta; + reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID; + reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC; + + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_update_reporting_info(&reporting_info); + esp_zb_lock_release(); +} + + +#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED diff --git a/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.h b/libraries/Zigbee/src/ep/ZigbeeAnalog.h similarity index 54% rename from libraries/Zigbee/src/ep/ZigbeeAnalogSensor.h rename to libraries/Zigbee/src/ep/ZigbeeAnalog.h index 32010993ae5..49b1f9189e0 100644 --- a/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.h +++ b/libraries/Zigbee/src/ep/ZigbeeAnalog.h @@ -10,7 +10,7 @@ #include "ha/esp_zigbee_ha_standard.h" // clang-format off -#define ZIGBEE_DEFAULT_ANALOG_SENSOR_CONFIG() \ +#define ZIGBEE_DEFAULT_ANALOG_CONFIG() \ { \ .basic_cfg = \ { \ @@ -21,40 +21,52 @@ { \ .identify_time = ESP_ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE, \ }, \ - .analog_value_cfg = \ - { \ - .out_of_service = 0, \ - .present_value = 0, \ - .status_flags = 1, \ - }, \ } // clang-format on -typedef struct zigbee_analog_sensor_cfg_s { +//enum for bits set to check what analog cluster were added +enum zigbee_analog_clusters { + ANALOG_VALUE = 1, + ANALOG_INPUT = 2, + ANALOG_OUTPUT = 4 +}; + +typedef struct zigbee_analog_cfg_s { esp_zb_basic_cluster_cfg_t basic_cfg; esp_zb_identify_cluster_cfg_t identify_cfg; esp_zb_analog_value_cluster_cfg_t analog_value_cfg; -} zigbee_analog_sensor_cfg_t; + esp_zb_analog_output_cluster_cfg_t analog_output_cfg; + esp_zb_analog_input_cluster_cfg_t analog_input_cfg; +} zigbee_analog_cfg_t; -class ZigbeeAnalogSensor : public ZigbeeEP { +class ZigbeeAnalog : public ZigbeeEP { public: - ZigbeeAnalogSensor(uint8_t endpoint); - ~ZigbeeAnalogSensor() {} - - // Set the value - void setAnalog(float analog); -/* - // Set the min and max value for the sensor - void setMinMaxValue(float min, float max); - - // Set the tolerance value for the sensor - void setTolerance(float tolerance); -*/ - // Set the reporting interval for in seconds and delta - void setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta); - - // Report the analog value + ZigbeeAnalog(uint8_t endpoint); + ~ZigbeeAnalog() {} + + // Add analog clusters + void addAnalogValue(); + void addAnalogInput(); + void addAnalogOutput(); + + // Use to set a cb function to be called on analog output change + void onAnalogOutputChange(void (*callback)(float analog)) { + _on_analog_output_change = callback; + } + + // Set the analog value / input + void setAnalogValue(float analog); + void setAnalogInput(float analog); + void report(); + +private: + void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override; + + void (*_on_analog_output_change)(float); + void analogOutputChanged(float analog_output); + + uint8_t _analog_clusters; }; #endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED diff --git a/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.cpp deleted file mode 100644 index 228358734a9..00000000000 --- a/libraries/Zigbee/src/ep/ZigbeeAnalogSensor.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "ZigbeeAnalogSensor.h" -#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED - -esp_zb_cluster_list_t *zigbee_analog_sensor_clusters_create(zigbee_analog_sensor_cfg_t *analog_sensor) { - esp_zb_basic_cluster_cfg_t *basic_cfg = analog_sensor ? &(analog_sensor->basic_cfg) : NULL; - esp_zb_identify_cluster_cfg_t *identify_cfg = analog_sensor ? &(analog_sensor->identify_cfg) : NULL; - esp_zb_analog_value_cluster_cfg_t *analog_value_cfg = analog_sensor ? &(analog_sensor->analog_value_cfg) : NULL; - esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create(); - esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); - esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); - esp_zb_cluster_list_add_analog_value_cluster( - cluster_list, esp_zb_analog_value_cluster_create(analog_value_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE - ); - return cluster_list; -} - -ZigbeeAnalogSensor::ZigbeeAnalogSensor(uint8_t endpoint) : ZigbeeEP(endpoint) { - _device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID; - - //Create custom analog sensor configuration - zigbee_analog_sensor_cfg_t analog_sensor_cfg = ZIGBEE_DEFAULT_ANALOG_SENSOR_CONFIG(); - _cluster_list = zigbee_analog_sensor_clusters_create(&analog_sensor_cfg); - - _ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID, .app_device_version = 0}; -} - -void ZigbeeAnalogSensor::setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta) { - if (delta > 0) { - log_e("Delta reporting is currently not supported by the sensor"); - } - esp_zb_zcl_reporting_info_t reporting_info; - memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t)); - reporting_info.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV; - reporting_info.ep = _endpoint; - reporting_info.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE; - reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE; - reporting_info.attr_id = ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID; - reporting_info.u.send_info.min_interval = min_interval; - reporting_info.u.send_info.max_interval = max_interval; - reporting_info.u.send_info.def_min_interval = min_interval; - reporting_info.u.send_info.def_max_interval = max_interval; - reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID; - reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC, esp_zb_lock_acquire(portMAX_DELAY); - esp_zb_zcl_update_reporting_info(&reporting_info); - esp_zb_lock_release(); - -} - -void ZigbeeAnalogSensor::setAnalog(float analog) { - float zb_analog = analog; - log_v("Updating sensor value..."); - /* Update sensor measured value */ - log_d("Setting value to %0.1f", zb_analog); - esp_zb_lock_acquire(portMAX_DELAY); - esp_zb_zcl_set_attribute_val( - _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID, - &zb_analog, false - ); - esp_zb_lock_release(); -} - -void ZigbeeAnalogSensor::report() { - /* 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_ANALOG_VALUE_PRESENT_VALUE_ID; - report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI; - report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE; - 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("Analog report sent"); -} - -#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED diff --git a/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp index d419fb14adc..90df37b91f5 100644 --- a/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp @@ -48,7 +48,8 @@ void ZigbeeTempSensor::setReporting(uint16_t min_interval, uint16_t max_interval reporting_info.u.send_info.def_max_interval = max_interval; reporting_info.u.send_info.delta.u16 = (uint16_t)(delta * 100); // Convert delta to ZCL uint16_t reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID; - reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC, esp_zb_lock_acquire(portMAX_DELAY); + reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC; + esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_update_reporting_info(&reporting_info); esp_zb_lock_release(); } From d0166b06c5ee05a3f2c3074d3773981cc89f0851 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: Mon, 17 Feb 2025 14:40:56 +0100 Subject: [PATCH 3/6] fix(zigbee): add missing functiong to header file --- libraries/Zigbee/src/ep/ZigbeeAnalog.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libraries/Zigbee/src/ep/ZigbeeAnalog.h b/libraries/Zigbee/src/ep/ZigbeeAnalog.h index 49b1f9189e0..fe46a6d678d 100644 --- a/libraries/Zigbee/src/ep/ZigbeeAnalog.h +++ b/libraries/Zigbee/src/ep/ZigbeeAnalog.h @@ -58,7 +58,11 @@ class ZigbeeAnalog : public ZigbeeEP { void setAnalogValue(float analog); void setAnalogInput(float analog); - void report(); + // Report Analog Input + void reportAnalogInput(); + + // Set reporting for Analog Input + void setAnalogInputReporting(uint16_t min_interval, uint16_t max_interval, float delta); private: void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override; From fb9d3ec4e47bba472d453746ef97752ce8ef5e96 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, 18 Feb 2025 08:28:33 +0100 Subject: [PATCH 4/6] fix(zigbee): Update log messages --- libraries/Zigbee/src/ep/ZigbeeAnalog.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp b/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp index 0af422a40a1..cda2982af80 100644 --- a/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp @@ -43,16 +43,15 @@ void ZigbeeAnalog::addAnalogOutput() { //set attribute method -> method overridden in child class void ZigbeeAnalog::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) { - //check the data and call right method if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT) { if (message->attribute.id == ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_SINGLE) { float analog_output = *(float *)message->attribute.data.value; analogOutputChanged(analog_output); } else { - log_w("Received message ignored. Attribute ID: %d not supported for On/Off Light", message->attribute.id); + log_w("Received message ignored. Attribute ID: %d not supported for Analog Output", message->attribute.id); } } else { - log_w("Received message ignored. Cluster ID: %d not supported for On/Off Light", message->info.cluster); + log_w("Received message ignored. Cluster ID: %d not supported for Analog endpoint", message->info.cluster); } } From 37560e8e021f5f05ee87217137e39d78cc43634d 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, 19 Feb 2025 22:56:43 +0000 Subject: [PATCH 5/6] ci(pre-commit): Apply automatic fixes --- .../Zigbee_Analog_Input_Output.ino | 185 +++++++++--------- libraries/Zigbee/src/ep/ZigbeeAnalog.cpp | 25 +-- libraries/Zigbee/src/ep/ZigbeeAnalog.h | 2 +- 3 files changed, 101 insertions(+), 111 deletions(-) diff --git a/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino b/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino index a33617b1ee3..90ba5066bb3 100644 --- a/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino +++ b/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino @@ -16,7 +16,7 @@ * @brief This example demonstrates Zigbee analog input / output device. * * The example demonstrates how to use Zigbee library to create a end device analog device. - * + * * Proper Zigbee mode must be selected in Tools->Zigbee mode * and also the correct partition scheme must be selected in Tools->Partition Scheme. * @@ -26,95 +26,94 @@ * Modified by Pat Clay */ - #ifndef ZIGBEE_MODE_ED - #error "Zigbee end device mode is not selected in Tools->Zigbee mode" - #endif - - #include "Zigbee.h" - - /* Zigbee analog device configuration */ - #define ANALOG_DEVICE_ENDPOINT_NUMBER 1 - - uint8_t analogPin = A0; - uint8_t button = BOOT_PIN; - - ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER); - - void onAnalogOutputChange(float analog_output) { - Serial.printf("Received analog output change: %.1f\r\n", analog_output); - } - - void setup() { - Serial.begin(115200); - Serial.println("Starting..."); - - // Init button switch - pinMode(button, INPUT_PULLUP); - - // Set analog resolution to 10 bits - analogReadResolution(10); - - // Optional: set Zigbee device name and model - zbAnalogDevice.setManufacturerAndModel("Espressif", "ZigbeeAnalogDevice"); - - // Add analog sensor cluster to Zigbee Analog accoding your needs - zbAnalogDevice.addAnalogInput(); - zbAnalogDevice.addAnalogOutput(); - - // If analog output cluster is added, set callback function for analog output change - zbAnalogDevice.onAnalogOutputChange(onAnalogOutputChange); - - // Add endpoints to Zigbee Core - Zigbee.addEndpoint(&zbAnalogDevice); - - Serial.println("Starting Zigbee..."); - // When all EPs are registered, start Zigbee in End Device mode - if (!Zigbee.begin()) { - Serial.println("Zigbee failed to start!"); - Serial.println("Rebooting..."); - ESP.restart(); - } else { - Serial.println("Zigbee started successfully!"); - } - Serial.println("Connecting to network"); - while (!Zigbee.connected()) { - Serial.print("."); - delay(100); - } - Serial.println("Connected"); - - // Optional: Add reporting for analog input - zbAnalogDevice.setAnalogInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10 - } - - void loop() { - static uint32_t timeCounter = 0; - - // Read ADC value and update the analog value every 2s - if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s - float analog = (float)analogRead(analogPin); - Serial.printf("Updating analog input to %.1f\r\n", analog); - zbAnalogDevice.setAnalogInput(analog); - - // Analog input supports reporting - zbAnalogDevice.reportAnalogInput(); - } - - // Checking button for factory reset and reporting - if (digitalRead(button) == LOW) { // Push button pressed - // Key debounce handling - delay(100); - int startTime = millis(); - while (digitalRead(button) == LOW) { - delay(50); - if ((millis() - startTime) > 3000) { - // If key pressed for more than 3secs, factory reset Zigbee and reboot - Serial.println("Resetting Zigbee to factory and rebooting in 1s."); - delay(1000); - Zigbee.factoryReset(); - } - } - } - delay(100); - } - \ No newline at end of file +#ifndef ZIGBEE_MODE_ED +#error "Zigbee end device mode is not selected in Tools->Zigbee mode" +#endif + +#include "Zigbee.h" + +/* Zigbee analog device configuration */ +#define ANALOG_DEVICE_ENDPOINT_NUMBER 1 + +uint8_t analogPin = A0; +uint8_t button = BOOT_PIN; + +ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER); + +void onAnalogOutputChange(float analog_output) { + Serial.printf("Received analog output change: %.1f\r\n", analog_output); +} + +void setup() { + Serial.begin(115200); + Serial.println("Starting..."); + + // Init button switch + pinMode(button, INPUT_PULLUP); + + // Set analog resolution to 10 bits + analogReadResolution(10); + + // Optional: set Zigbee device name and model + zbAnalogDevice.setManufacturerAndModel("Espressif", "ZigbeeAnalogDevice"); + + // Add analog sensor cluster to Zigbee Analog accoding your needs + zbAnalogDevice.addAnalogInput(); + zbAnalogDevice.addAnalogOutput(); + + // If analog output cluster is added, set callback function for analog output change + zbAnalogDevice.onAnalogOutputChange(onAnalogOutputChange); + + // Add endpoints to Zigbee Core + Zigbee.addEndpoint(&zbAnalogDevice); + + Serial.println("Starting Zigbee..."); + // When all EPs are registered, start Zigbee in End Device mode + if (!Zigbee.begin()) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } else { + Serial.println("Zigbee started successfully!"); + } + Serial.println("Connecting to network"); + while (!Zigbee.connected()) { + Serial.print("."); + delay(100); + } + Serial.println("Connected"); + + // Optional: Add reporting for analog input + zbAnalogDevice.setAnalogInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10 +} + +void loop() { + static uint32_t timeCounter = 0; + + // Read ADC value and update the analog value every 2s + if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s + float analog = (float)analogRead(analogPin); + Serial.printf("Updating analog input to %.1f\r\n", analog); + zbAnalogDevice.setAnalogInput(analog); + + // Analog input supports reporting + zbAnalogDevice.reportAnalogInput(); + } + + // Checking button for factory reset and reporting + if (digitalRead(button) == LOW) { // Push button pressed + // Key debounce handling + delay(100); + int startTime = millis(); + while (digitalRead(button) == LOW) { + delay(50); + if ((millis() - startTime) > 3000) { + // If key pressed for more than 3secs, factory reset Zigbee and reboot + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); + delay(1000); + Zigbee.factoryReset(); + } + } + } + delay(100); +} diff --git a/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp b/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp index cda2982af80..25c199b345b 100644 --- a/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeAnalog.cpp @@ -21,23 +21,17 @@ ZigbeeAnalog::ZigbeeAnalog(uint8_t endpoint) : ZigbeeEP(endpoint) { } void ZigbeeAnalog::addAnalogValue() { - esp_zb_cluster_list_add_analog_value_cluster( - _cluster_list, esp_zb_analog_value_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE - ); + esp_zb_cluster_list_add_analog_value_cluster(_cluster_list, esp_zb_analog_value_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); _analog_clusters |= ANALOG_VALUE; } void ZigbeeAnalog::addAnalogInput() { - esp_zb_cluster_list_add_analog_input_cluster( - _cluster_list, esp_zb_analog_input_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE - ); + esp_zb_cluster_list_add_analog_input_cluster(_cluster_list, esp_zb_analog_input_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); _analog_clusters |= ANALOG_INPUT; } void ZigbeeAnalog::addAnalogOutput() { - esp_zb_cluster_list_add_analog_output_cluster( - _cluster_list, esp_zb_analog_output_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE - ); + esp_zb_cluster_list_add_analog_output_cluster(_cluster_list, esp_zb_analog_output_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); _analog_clusters |= ANALOG_OUTPUT; } @@ -64,7 +58,7 @@ void ZigbeeAnalog::analogOutputChanged(float analog_output) { } void ZigbeeAnalog::setAnalogValue(float analog) { - if(!(_analog_clusters & ANALOG_VALUE)) { + if (!(_analog_clusters & ANALOG_VALUE)) { log_e("Analog Value cluster not added"); return; } @@ -72,14 +66,13 @@ void ZigbeeAnalog::setAnalogValue(float analog) { log_d("Setting analog value to %.1f", analog); esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_set_attribute_val( - _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID, - &analog, false + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID, &analog, false ); esp_zb_lock_release(); } void ZigbeeAnalog::setAnalogInput(float analog) { - if(!(_analog_clusters & ANALOG_INPUT)) { + if (!(_analog_clusters & ANALOG_INPUT)) { log_e("Analog Input cluster not added"); return; } @@ -87,8 +80,7 @@ void ZigbeeAnalog::setAnalogInput(float analog) { log_d("Setting analog input to %.1f", analog); esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_set_attribute_val( - _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_INPUT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_INPUT_PRESENT_VALUE_ID, - &analog, false + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_INPUT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_INPUT_PRESENT_VALUE_ID, &analog, false ); esp_zb_lock_release(); } @@ -123,11 +115,10 @@ void ZigbeeAnalog::setAnalogInputReporting(uint16_t min_interval, uint16_t max_i reporting_info.u.send_info.delta.s32 = delta; reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID; reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC; - + esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_update_reporting_info(&reporting_info); esp_zb_lock_release(); } - #endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED diff --git a/libraries/Zigbee/src/ep/ZigbeeAnalog.h b/libraries/Zigbee/src/ep/ZigbeeAnalog.h index fe46a6d678d..e3b22594324 100644 --- a/libraries/Zigbee/src/ep/ZigbeeAnalog.h +++ b/libraries/Zigbee/src/ep/ZigbeeAnalog.h @@ -66,7 +66,7 @@ class ZigbeeAnalog : public ZigbeeEP { private: void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override; - + void (*_on_analog_output_change)(float); void analogOutputChanged(float analog_output); From 21a840f239e90b468b3c25d60f1fe4728dd4fb51 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, 20 Feb 2025 09:21:06 +0100 Subject: [PATCH 6/6] fix(example): Fix comment typo --- .../Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino b/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino index 90ba5066bb3..e31407cc8be 100644 --- a/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino +++ b/libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino @@ -57,7 +57,7 @@ void setup() { // Optional: set Zigbee device name and model zbAnalogDevice.setManufacturerAndModel("Espressif", "ZigbeeAnalogDevice"); - // Add analog sensor cluster to Zigbee Analog accoding your needs + // Add analog clusters to Zigbee Analog according your needs zbAnalogDevice.addAnalogInput(); zbAnalogDevice.addAnalogOutput();