From 082762dcf98cd54d0f0a2528f840eb977c612732 Mon Sep 17 00:00:00 2001 From: MikaFromTheRoof Date: Mon, 24 Mar 2025 20:11:33 +0100 Subject: [PATCH 01/12] Added Zigbee light sensor --- .../examples/Zigbee_Light_Sensor/README.md | 78 ++++++++++ .../Zigbee_Light_Sensor.ino | 139 ++++++++++++++++++ .../examples/Zigbee_Light_Sensor/ci.json | 7 + libraries/Zigbee/keywords.txt | 5 + libraries/Zigbee/src/Zigbee.h | 1 + libraries/Zigbee/src/ep/ZigbeeLightSensor.cpp | 83 +++++++++++ libraries/Zigbee/src/ep/ZigbeeLightSensor.h | 57 +++++++ 7 files changed, 370 insertions(+) create mode 100644 libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md create mode 100644 libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino create mode 100644 libraries/Zigbee/examples/Zigbee_Light_Sensor/ci.json create mode 100644 libraries/Zigbee/src/ep/ZigbeeLightSensor.cpp create mode 100644 libraries/Zigbee/src/ep/ZigbeeLightSensor.h diff --git a/libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md b/libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md new file mode 100644 index 00000000000..64961e103bc --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md @@ -0,0 +1,78 @@ +# Arduino-ESP32 Zigbee Light Sensor Sleepy Device Example + +This example demonstrates how to use the Zigbee library to create an end device light sensor and use it as a Home Automation (HA) extended light sensor. + +# Supported Targets + +Currently, this example supports the following targets. + +| Supported Targets | ESP32-C6 | ESP32-H2 | +| ----------------- | -------- | -------- | + +## Light Sensor Functions + +1. Initialize a Zigbee light sensor. +2. Measure illuminance value. +3. Report the measured value to the Zigbee network. + +## Hardware Required + +* ESP32-H2 or ESP32-C6 development board +* A USB cable for power supply and programming +* Some kind of light sensor, such as a photoresistor + +### Configure the Project + +In this example the raw analog value of a light sensor is used to calculate illuminance. +Alter the calculation according to your use case and calibrate it to receive correct lux values. +Set the light sensor GPIO by changing the `light_sensor_pin` variable to the pin to the pin to which your sensor is connected. +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` +* Enable USB CDC to be able to use the serial monitor: `Tools -> USB CDC On Boot: Enabled` +* 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 that you are using a good quality USB cable with data lines 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_Light_Sensor/Zigbee_Light_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino new file mode 100644 index 00000000000..a0c98ca8023 --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino @@ -0,0 +1,139 @@ +// 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 light sensor. + * + * The example demonstrates how to use Zigbee library to create a end device light sensor. + * The light 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 MikaFromTheRoof (https://github.com/MikaFromTheRoof) + */ + + #ifndef ZIGBEE_MODE_ED + #error "Zigbee end device mode is not selected in Tools->Zigbee mode" + #endif + + #include "Zigbee.h" + + #define ZIGBEE_LIGHT_SENSOR_ENDPOINT 9 + uint8_t button = BOOT_PIN; + uint8_t light_sensor_pin = 6; // Insert the analog pin to which the sensor (e.g. photoresistor) is connected + + ZigbeeLightSensor zbLightSensor = ZigbeeLightSensor(ZIGBEE_LIGHT_SENSOR_ENDPOINT); + + /********************* Light sensor **************************/ + static void light_sensor_value_update(void *arg) { + for (;;) { + // read the raw analog value from the sensor + int lsens_analog = analogRead(light_sensor_pin); + Serial.printf("[Light Sensor] raw analog value: %d°C\r\n", lsens_analog); + + // conversion into zigbee illuminance number value (typically between 0 in darkness and 50000 in direct sunlight) + // depends on the value range of the raw analog sensor values and will need calibration for correct lux values + int lsens_value = lsens_analog * 10; // mult by 10 for the sake of this example + Serial.printf("[Light Sensor] number value: %d°C\r\n", lsens_value); + + // zigbee uses the formular below to calculate lx (lux) values from the number values + int lsens_value_lx = (int) 10^(lsens_value/10000)-1; + Serial.printf("[Light Sensor] lux value: %d°C\r\n", lsens_value_lx); + + // Update illuminance in light sensor EP + zbLightSensor.setIlluminance(lsens_value); // use the number value here! + + delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance + } + } + + /********************* Arduino functions **************************/ + void setup() { + Serial.begin(115200); + + // Optional: configure analog input + analogSetAttenuation(ADC_11db); // set analog to digital converter (ADC) attenuation to 11 dB (up to ~3.3V input) + analogReadResolution(12); // set analog read resolution to 12 bits (value range from 0 to 4095), 12 is default + + // Init button for factory reset + pinMode(button, INPUT_PULLUP); + + // Optional: Set Zigbee device name and model + zbLightSensor.setManufacturerAndModel("Espressif", "ZigbeeLightSensor"); + + // Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown + zbLightSensor.setPowerSource(ZB_POWER_SOURCE_MAINS); + + // Set minimum and maximum illuminance number value (0 min and 50000 max equals to 0 lx - 100,000 lx) + zbLightSensor.setMinMaxValue(0, 50000); + + // Optional: Set tolerance for illuminance measurement + zbLightSensor.setTolerance(1); + + // Add endpoint to Zigbee Core + Serial.println("Adding Zigbee light sensor endpoint to Zigbee Core"); + Zigbee.addEndpoint(&zbLightSensor); + + 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(); + + // Start light sensor reading task + xTaskCreate(light_sensor_value_update, "light_sensor_update", 2048, NULL, 10, NULL); + + // Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin() + // min_interval and max_interval in seconds, delta + // if min = 1 and max = 0, delta = 1000, reporting is sent when illuminance value changes by 1000, but at most once per second + // if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if illuminance value changes by 1000 + // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change + // Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings + zbLightSensor.setReporting(1, 0, 1000); + } + + /********************* Main loop **************************/ + void loop() { + // Checking button for factory reset + 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 3 secs, factory reset Zigbee and reboot + Serial.println("Resetting Zigbee to factory and rebooting in 1s"); + delay(1000); + Zigbee.factoryReset(); + } + } + // force report of illuminance when button is pressed + zbLightSensor.reportIlluminance(); + } + delay(100); + } \ No newline at end of file diff --git a/libraries/Zigbee/examples/Zigbee_Light_Sensor/ci.json b/libraries/Zigbee/examples/Zigbee_Light_Sensor/ci.json new file mode 100644 index 00000000000..ceacc367801 --- /dev/null +++ b/libraries/Zigbee/examples/Zigbee_Light_Sensor/ci.json @@ -0,0 +1,7 @@ +{ + "fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed", + "requires": [ + "CONFIG_SOC_IEEE802154_SUPPORTED=y", + "CONFIG_ZB_ENABLED=y" + ] +} diff --git a/libraries/Zigbee/keywords.txt b/libraries/Zigbee/keywords.txt index 2eeed5878a7..83d9a231ce9 100644 --- a/libraries/Zigbee/keywords.txt +++ b/libraries/Zigbee/keywords.txt @@ -29,6 +29,7 @@ ZigbeeGateway KEYWORD1 ZigbeeRangeExtender KEYWORD1 ZigbeeVibrationSensor KEYWORD1 ZigbeeWindowCovering KEYWORD1 +ZigbeeLightSensor KEYWORD1 # Other zigbee_role_t KEYWORD1 @@ -123,6 +124,10 @@ setHumidity KEYWORD2 setHumidityReporting KEYWORD2 reportHumidity KEYWORD2 +# ZigbeeLightSensor +setLightValue KEYWORD2 +reportLightValue KEYWORD2 + # ZigbeeFlowSensor setFlow KEYWORD2 diff --git a/libraries/Zigbee/src/Zigbee.h b/libraries/Zigbee/src/Zigbee.h index e5f669ba899..7cdbf3dce85 100644 --- a/libraries/Zigbee/src/Zigbee.h +++ b/libraries/Zigbee/src/Zigbee.h @@ -18,6 +18,7 @@ #include "ep/ZigbeeAnalog.h" #include "ep/ZigbeeFlowSensor.h" #include "ep/ZigbeeOccupancySensor.h" +#include "ep/ZigbeeLightSensor.h" #include "ep/ZigbeeCarbonDioxideSensor.h" #include "ep/ZigbeeContactSwitch.h" #include "ep/ZigbeeDoorWindowHandle.h" diff --git a/libraries/Zigbee/src/ep/ZigbeeLightSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeLightSensor.cpp new file mode 100644 index 00000000000..a5e23c97ed1 --- /dev/null +++ b/libraries/Zigbee/src/ep/ZigbeeLightSensor.cpp @@ -0,0 +1,83 @@ +#include "ZigbeeLightSensor.h" +#if CONFIG_ZB_ENABLED + +ZigbeeLightSensor::ZigbeeLightSensor(uint8_t endpoint) : ZigbeeEP(endpoint) { + _device_id = ESP_ZB_HA_LIGHT_SENSOR_DEVICE_ID; + + esp_zb_light_sensor_cfg_t light_sensor_cfg = ESP_ZB_DEFAULT_LIGHT_SENSOR_CONFIG(); + _cluster_list = esp_zb_light_sensor_clusters_create(&light_sensor_cfg); + + _ep_config = { + .endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_LIGHT_SENSOR_DEVICE_ID, .app_device_version = 0 + }; +} + +static int16_t zb_int_to_s16(int value) { + return (int16_t) value; +} + +void ZigbeeLightSensor::setMinMaxValue(int min, int max) { + int16_t zb_min = zb_int_to_s16(min); + int16_t zb_max = zb_int_to_s16(max); + esp_zb_attribute_list_t *light_measure_cluster = + esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + esp_zb_cluster_update_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_ID, (void *)&zb_min); + esp_zb_cluster_update_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_ID, (void *)&zb_max); +} + +void ZigbeeLightSensor::setTolerance(int tolerance) { + uint16_t zb_tolerance = (uint16_t) tolerance; + esp_zb_attribute_list_t *light_measure_cluster = + esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); + esp_zb_illuminance_meas_cluster_add_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_TOLERANCE_ID, (void *)&zb_tolerance); +} + +void ZigbeeLightSensor::setReporting(uint16_t min_interval, uint16_t max_interval, int 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_ILLUMINANCE_MEASUREMENT; + reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE; + reporting_info.attr_id = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MEASURED_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.u16 = (uint16_t) 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(); +} + +void ZigbeeLightSensor::setIlluminance(int illuminanceValue) { + int16_t zb_illuminanceValue = zb_int_to_s16(illuminanceValue); + log_v("Updating Illuminance..."); + /* Update light sensor measured illuminance */ + log_d("Setting Illuminance to %d", zb_illuminanceValue); + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MEASURED_VALUE_ID, &zb_illuminanceValue, false + ); + esp_zb_lock_release(); +} + +void ZigbeeLightSensor::reportIlluminance() { + /* 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_ILLUMINANCE_MEASUREMENT_MEASURED_VALUE_ID; + report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI; + report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT; + report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint; + report_attr_cmd.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC; + + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd); + esp_zb_lock_release(); + log_v("Illuminance report sent"); +} + +#endif // CONFIG_ZB_ENABLED diff --git a/libraries/Zigbee/src/ep/ZigbeeLightSensor.h b/libraries/Zigbee/src/ep/ZigbeeLightSensor.h new file mode 100644 index 00000000000..08919c0928d --- /dev/null +++ b/libraries/Zigbee/src/ep/ZigbeeLightSensor.h @@ -0,0 +1,57 @@ +/* Class of Zigbee Light sensor endpoint inherited from common EP class */ + +#pragma once + +#include "soc/soc_caps.h" +#include "sdkconfig.h" +#if CONFIG_ZB_ENABLED + +#include "ZigbeeEP.h" +#include "ha/esp_zigbee_ha_standard.h" + +/**/ +#define ESP_ZB_DEFAULT_LIGHT_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, \ + }, \ + .illuminance_cfg = \ + { \ + .measured_value = ESP_ZB_ZCL_ILLUMINANCE_MEASUREMENT_LIGHT_SENSOR_TYPE_DEFAULT_VALUE, \ + /*not sure with next two values, but there are no*/ \ + /*ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_DEFAULT and*/ \ + /*ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_DEFAULT*/ \ + /*thats why I chose MIN_VALUE and MAX_VALUE*/ \ + .min_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_MIN_VALUE, \ + .max_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_MAX_VALUE, \ + }, \ + } + +class ZigbeeLightSensor : public ZigbeeEP { +public: + ZigbeeLightSensor(uint8_t endpoint); + ~ZigbeeLightSensor() {} + + // Set the light value + void setIlluminance(int value); + + // Set the min and max value for the light sensor + void setMinMaxValue(int min, int max); + + // Set the tolerance value for the light sensor + void setTolerance(int tolerance); + + // Set the reporting interval for light measurement in seconds and delta + void setReporting(uint16_t min_interval, uint16_t max_interval, int delta); + + // Report the light value + void reportIlluminance(); +}; + +#endif // CONFIG_ZB_ENABLED From 8fc75dbd8d835bc243131267d35dd9c25d7614e4 Mon Sep 17 00:00:00 2001 From: MikaFromTheRoof Date: Mon, 24 Mar 2025 20:19:16 +0100 Subject: [PATCH 02/12] Add comment for macro --- libraries/Zigbee/src/ep/ZigbeeLightSensor.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/Zigbee/src/ep/ZigbeeLightSensor.h b/libraries/Zigbee/src/ep/ZigbeeLightSensor.h index 08919c0928d..d2c3fb4a3b0 100644 --- a/libraries/Zigbee/src/ep/ZigbeeLightSensor.h +++ b/libraries/Zigbee/src/ep/ZigbeeLightSensor.h @@ -9,7 +9,10 @@ #include "ZigbeeEP.h" #include "ha/esp_zigbee_ha_standard.h" -/**/ +/* +the new macro works here, but should better be added to +esp-zigbee-sdk/components/esp-zigbee-lib/include/ha/esp_zigbee_ha_standard.h +*/ #define ESP_ZB_DEFAULT_LIGHT_SENSOR_CONFIG() \ { \ .basic_cfg = \ From 6d7b54441fac658f6cde61ae49eda692c2bafba4 Mon Sep 17 00:00:00 2001 From: MikaFromTheRoof Date: Mon, 24 Mar 2025 21:58:43 +0100 Subject: [PATCH 03/12] Some last corrections --- .../Zigbee_Light_Sensor.ino | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino index a0c98ca8023..e2c5007d307 100644 --- a/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino +++ b/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino @@ -42,20 +42,21 @@ static void light_sensor_value_update(void *arg) { for (;;) { // read the raw analog value from the sensor - int lsens_analog = analogRead(light_sensor_pin); - Serial.printf("[Light Sensor] raw analog value: %d°C\r\n", lsens_analog); + int lsens_analog_raw = analogRead(light_sensor_pin); + Serial.printf("[Light Sensor] raw analog value: %d\r\n", lsens_analog_raw); - // conversion into zigbee illuminance number value (typically between 0 in darkness and 50000 in direct sunlight) + // conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight) // depends on the value range of the raw analog sensor values and will need calibration for correct lux values - int lsens_value = lsens_analog * 10; // mult by 10 for the sake of this example - Serial.printf("[Light Sensor] number value: %d°C\r\n", lsens_value); + int lsens_illuminance_raw = lsens_analog_raw * 10; // multiply by 10 for the sake of this example + Serial.printf("[Light Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw); - // zigbee uses the formular below to calculate lx (lux) values from the number values - int lsens_value_lx = (int) 10^(lsens_value/10000)-1; - Serial.printf("[Light Sensor] lux value: %d°C\r\n", lsens_value_lx); + // according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value + // Note: Zigbee2MQTT seems to be using the formular 10^(lsens_illuminance_raw/10000) instead (without -1) + int lsens_illuminance_lux = round(pow(10,(lsens_illuminance_raw / 10000.0))-1); + Serial.printf("[Light Sensor] lux value: %d lux\r\n", lsens_illuminance_lux); // Update illuminance in light sensor EP - zbLightSensor.setIlluminance(lsens_value); // use the number value here! + zbLightSensor.setIlluminance(lsens_illuminance_raw); // use raw illuminance here! delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance } @@ -78,10 +79,10 @@ // Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown zbLightSensor.setPowerSource(ZB_POWER_SOURCE_MAINS); - // Set minimum and maximum illuminance number value (0 min and 50000 max equals to 0 lx - 100,000 lx) + // Set minimum and maximum for raw illuminance value (0 min and 50000 max equals to 0 lux - 100,000 lux) zbLightSensor.setMinMaxValue(0, 50000); - // Optional: Set tolerance for illuminance measurement + // Optional: Set tolerance for raw illuminance value zbLightSensor.setTolerance(1); // Add endpoint to Zigbee Core @@ -109,8 +110,8 @@ // Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin() // min_interval and max_interval in seconds, delta - // if min = 1 and max = 0, delta = 1000, reporting is sent when illuminance value changes by 1000, but at most once per second - // if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if illuminance value changes by 1000 + // if min = 1 and max = 0, delta = 1000, reporting is sent when raw illuminance value changes by 1000, but at most once per second + // if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if raw illuminance value changes by 1000 // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change // Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings zbLightSensor.setReporting(1, 0, 1000); From 8a0f8df825144d330482fbc7e17b6cd1524134ec Mon Sep 17 00:00:00 2001 From: MikaFromTheRoof Date: Tue, 25 Mar 2025 21:43:21 +0100 Subject: [PATCH 04/12] refactor(zigbee): changed class name to ZigbeeIlluminanceSensor --- .../examples/Zigbee_Light_Sensor/README.md | 10 ++-- ...nsor.ino => Zigbee_Illuminance_Sensor.ino} | 48 +++++++++---------- libraries/Zigbee/keywords.txt | 6 +-- libraries/Zigbee/src/Zigbee.h | 2 +- ...Sensor.cpp => ZigbeeIlluminanceSensor.cpp} | 16 +++---- ...ightSensor.h => ZigbeeIlluminanceSensor.h} | 20 ++++---- 6 files changed, 51 insertions(+), 51 deletions(-) rename libraries/Zigbee/examples/Zigbee_Light_Sensor/{Zigbee_Light_Sensor.ino => Zigbee_Illuminance_Sensor.ino} (73%) rename libraries/Zigbee/src/ep/{ZigbeeLightSensor.cpp => ZigbeeIlluminanceSensor.cpp} (86%) rename libraries/Zigbee/src/ep/{ZigbeeLightSensor.h => ZigbeeIlluminanceSensor.h} (84%) diff --git a/libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md b/libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md index 64961e103bc..fe723696d5a 100644 --- a/libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md +++ b/libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md @@ -1,6 +1,6 @@ -# Arduino-ESP32 Zigbee Light Sensor Sleepy Device Example +# Arduino-ESP32 Zigbee Illuminance Sensor Example -This example demonstrates how to use the Zigbee library to create an end device light sensor and use it as a Home Automation (HA) extended light sensor. +This example demonstrates how to use the Zigbee library to create an end device illuminance sensor and use it as a Home Automation (HA) extended illuminance sensor. # Supported Targets @@ -9,9 +9,9 @@ Currently, this example supports the following targets. | Supported Targets | ESP32-C6 | ESP32-H2 | | ----------------- | -------- | -------- | -## Light Sensor Functions +## Illuminance Sensor Functions -1. Initialize a Zigbee light sensor. +1. Initialize a Zigbee illuminance sensor. 2. Measure illuminance value. 3. Report the measured value to the Zigbee network. @@ -25,7 +25,7 @@ Currently, this example supports the following targets. In this example the raw analog value of a light sensor is used to calculate illuminance. Alter the calculation according to your use case and calibrate it to receive correct lux values. -Set the light sensor GPIO by changing the `light_sensor_pin` variable to the pin to the pin to which your sensor is connected. +Set the illuminance sensor GPIO by changing the `illuminance_sensor_pin` variable to the pin to the pin to which your sensor is connected. 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_Light_Sensor/Zigbee_Light_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Illuminance_Sensor.ino similarity index 73% rename from libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino rename to libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Illuminance_Sensor.ino index e2c5007d307..e8b89205388 100644 --- a/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino +++ b/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Illuminance_Sensor.ino @@ -13,10 +13,10 @@ // limitations under the License. /** - * @brief This example demonstrates Zigbee light sensor. + * @brief This example demonstrates Zigbee illuminance sensor. * - * The example demonstrates how to use Zigbee library to create a end device light sensor. - * The light sensor is a Zigbee end device, which is controlled by a Zigbee coordinator. + * The example demonstrates how to use Zigbee library to create a end device illuminance sensor. + * The illuminance 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. @@ -32,31 +32,31 @@ #include "Zigbee.h" - #define ZIGBEE_LIGHT_SENSOR_ENDPOINT 9 + #define ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT 9 uint8_t button = BOOT_PIN; - uint8_t light_sensor_pin = 6; // Insert the analog pin to which the sensor (e.g. photoresistor) is connected + uint8_t illuminance_sensor_pin = 6; // Insert the analog pin to which the sensor (e.g. photoresistor) is connected - ZigbeeLightSensor zbLightSensor = ZigbeeLightSensor(ZIGBEE_LIGHT_SENSOR_ENDPOINT); + ZigbeeIlluminanceSensor zbIlluminanceSensor = ZigbeeIlluminanceSensor(ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT); - /********************* Light sensor **************************/ - static void light_sensor_value_update(void *arg) { + /********************* Illuminance sensor **************************/ + static void illuminance_sensor_value_update(void *arg) { for (;;) { // read the raw analog value from the sensor - int lsens_analog_raw = analogRead(light_sensor_pin); - Serial.printf("[Light Sensor] raw analog value: %d\r\n", lsens_analog_raw); + int lsens_analog_raw = analogRead(illuminance_sensor_pin); + Serial.printf("[Illuminance Sensor] raw analog value: %d\r\n", lsens_analog_raw); // conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight) // depends on the value range of the raw analog sensor values and will need calibration for correct lux values int lsens_illuminance_raw = lsens_analog_raw * 10; // multiply by 10 for the sake of this example - Serial.printf("[Light Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw); + Serial.printf("[Illuminance Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw); // according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value // Note: Zigbee2MQTT seems to be using the formular 10^(lsens_illuminance_raw/10000) instead (without -1) int lsens_illuminance_lux = round(pow(10,(lsens_illuminance_raw / 10000.0))-1); - Serial.printf("[Light Sensor] lux value: %d lux\r\n", lsens_illuminance_lux); + Serial.printf("[Illuminance Sensor] lux value: %d lux\r\n", lsens_illuminance_lux); - // Update illuminance in light sensor EP - zbLightSensor.setIlluminance(lsens_illuminance_raw); // use raw illuminance here! + // Update illuminance in illuminance sensor EP + zbIlluminanceSensor.setIlluminance(lsens_illuminance_raw); // use raw illuminance here! delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance } @@ -74,20 +74,20 @@ pinMode(button, INPUT_PULLUP); // Optional: Set Zigbee device name and model - zbLightSensor.setManufacturerAndModel("Espressif", "ZigbeeLightSensor"); + zbIlluminanceSensor.setManufacturerAndModel("Espressif", "ZigbeeIlluminanceSensor"); // Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown - zbLightSensor.setPowerSource(ZB_POWER_SOURCE_MAINS); + zbIlluminanceSensor.setPowerSource(ZB_POWER_SOURCE_MAINS); // Set minimum and maximum for raw illuminance value (0 min and 50000 max equals to 0 lux - 100,000 lux) - zbLightSensor.setMinMaxValue(0, 50000); + zbIlluminanceSensor.setMinMaxValue(0, 50000); // Optional: Set tolerance for raw illuminance value - zbLightSensor.setTolerance(1); + zbIlluminanceSensor.setTolerance(1); // Add endpoint to Zigbee Core - Serial.println("Adding Zigbee light sensor endpoint to Zigbee Core"); - Zigbee.addEndpoint(&zbLightSensor); + Serial.println("Adding Zigbee illuminance sensor endpoint to Zigbee Core"); + Zigbee.addEndpoint(&zbIlluminanceSensor); Serial.println("Starting Zigbee..."); // When all EPs are registered, start Zigbee in End Device mode @@ -105,8 +105,8 @@ } Serial.println(); - // Start light sensor reading task - xTaskCreate(light_sensor_value_update, "light_sensor_update", 2048, NULL, 10, NULL); + // Start illuminance sensor reading task + xTaskCreate(illuminance_sensor_value_update, "illuminance_sensor_update", 2048, NULL, 10, NULL); // Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin() // min_interval and max_interval in seconds, delta @@ -114,7 +114,7 @@ // if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if raw illuminance value changes by 1000 // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change // Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings - zbLightSensor.setReporting(1, 0, 1000); + zbIlluminanceSensor.setReporting(1, 0, 1000); } /********************* Main loop **************************/ @@ -134,7 +134,7 @@ } } // force report of illuminance when button is pressed - zbLightSensor.reportIlluminance(); + zbIlluminanceSensor.reportIlluminance(); } delay(100); } \ No newline at end of file diff --git a/libraries/Zigbee/keywords.txt b/libraries/Zigbee/keywords.txt index 83d9a231ce9..145792b9757 100644 --- a/libraries/Zigbee/keywords.txt +++ b/libraries/Zigbee/keywords.txt @@ -29,7 +29,7 @@ ZigbeeGateway KEYWORD1 ZigbeeRangeExtender KEYWORD1 ZigbeeVibrationSensor KEYWORD1 ZigbeeWindowCovering KEYWORD1 -ZigbeeLightSensor KEYWORD1 +ZigbeeIlluminanceSensor KEYWORD1 # Other zigbee_role_t KEYWORD1 @@ -125,8 +125,8 @@ setHumidityReporting KEYWORD2 reportHumidity KEYWORD2 # ZigbeeLightSensor -setLightValue KEYWORD2 -reportLightValue KEYWORD2 +setIlluminance KEYWORD2 +reportIlluminance KEYWORD2 # ZigbeeFlowSensor setFlow KEYWORD2 diff --git a/libraries/Zigbee/src/Zigbee.h b/libraries/Zigbee/src/Zigbee.h index 7cdbf3dce85..ea42e9dd41a 100644 --- a/libraries/Zigbee/src/Zigbee.h +++ b/libraries/Zigbee/src/Zigbee.h @@ -18,7 +18,7 @@ #include "ep/ZigbeeAnalog.h" #include "ep/ZigbeeFlowSensor.h" #include "ep/ZigbeeOccupancySensor.h" -#include "ep/ZigbeeLightSensor.h" +#include "ep/ZigbeeIlluminanceSensor.h" #include "ep/ZigbeeCarbonDioxideSensor.h" #include "ep/ZigbeeContactSwitch.h" #include "ep/ZigbeeDoorWindowHandle.h" diff --git a/libraries/Zigbee/src/ep/ZigbeeLightSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp similarity index 86% rename from libraries/Zigbee/src/ep/ZigbeeLightSensor.cpp rename to libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp index a5e23c97ed1..61017a20f33 100644 --- a/libraries/Zigbee/src/ep/ZigbeeLightSensor.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp @@ -1,7 +1,7 @@ -#include "ZigbeeLightSensor.h" +#include "ZigbeeIlluminanceSensor.h" #if CONFIG_ZB_ENABLED -ZigbeeLightSensor::ZigbeeLightSensor(uint8_t endpoint) : ZigbeeEP(endpoint) { +ZigbeeIlluminanceSensor::ZigbeeIlluminanceSensor(uint8_t endpoint) : ZigbeeEP(endpoint) { _device_id = ESP_ZB_HA_LIGHT_SENSOR_DEVICE_ID; esp_zb_light_sensor_cfg_t light_sensor_cfg = ESP_ZB_DEFAULT_LIGHT_SENSOR_CONFIG(); @@ -16,7 +16,7 @@ static int16_t zb_int_to_s16(int value) { return (int16_t) value; } -void ZigbeeLightSensor::setMinMaxValue(int min, int max) { +void ZigbeeIlluminanceSensor::setMinMaxValue(int min, int max) { int16_t zb_min = zb_int_to_s16(min); int16_t zb_max = zb_int_to_s16(max); esp_zb_attribute_list_t *light_measure_cluster = @@ -25,14 +25,14 @@ void ZigbeeLightSensor::setMinMaxValue(int min, int max) { esp_zb_cluster_update_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_ID, (void *)&zb_max); } -void ZigbeeLightSensor::setTolerance(int tolerance) { +void ZigbeeIlluminanceSensor::setTolerance(int tolerance) { uint16_t zb_tolerance = (uint16_t) tolerance; esp_zb_attribute_list_t *light_measure_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); esp_zb_illuminance_meas_cluster_add_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_TOLERANCE_ID, (void *)&zb_tolerance); } -void ZigbeeLightSensor::setReporting(uint16_t min_interval, uint16_t max_interval, int delta) { +void ZigbeeIlluminanceSensor::setReporting(uint16_t min_interval, uint16_t max_interval, int 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; @@ -52,10 +52,10 @@ void ZigbeeLightSensor::setReporting(uint16_t min_interval, uint16_t max_interva esp_zb_lock_release(); } -void ZigbeeLightSensor::setIlluminance(int illuminanceValue) { +void ZigbeeIlluminanceSensor::setIlluminance(int illuminanceValue) { int16_t zb_illuminanceValue = zb_int_to_s16(illuminanceValue); log_v("Updating Illuminance..."); - /* Update light sensor measured illuminance */ + /* Update illuminance sensor measured illuminance */ log_d("Setting Illuminance to %d", zb_illuminanceValue); esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_set_attribute_val( @@ -64,7 +64,7 @@ void ZigbeeLightSensor::setIlluminance(int illuminanceValue) { esp_zb_lock_release(); } -void ZigbeeLightSensor::reportIlluminance() { +void ZigbeeIlluminanceSensor::reportIlluminance() { /* 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; diff --git a/libraries/Zigbee/src/ep/ZigbeeLightSensor.h b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h similarity index 84% rename from libraries/Zigbee/src/ep/ZigbeeLightSensor.h rename to libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h index d2c3fb4a3b0..ddf354254cb 100644 --- a/libraries/Zigbee/src/ep/ZigbeeLightSensor.h +++ b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h @@ -1,4 +1,4 @@ -/* Class of Zigbee Light sensor endpoint inherited from common EP class */ +/* Class of Zigbee Illuminance sensor endpoint inherited from common EP class */ #pragma once @@ -13,7 +13,7 @@ the new macro works here, but should better be added to esp-zigbee-sdk/components/esp-zigbee-lib/include/ha/esp_zigbee_ha_standard.h */ -#define ESP_ZB_DEFAULT_LIGHT_SENSOR_CONFIG() \ +#define ESP_ZB_DEFAULT_LIGHT_SENSOR_CONFIG() \ { \ .basic_cfg = \ { \ @@ -36,24 +36,24 @@ esp-zigbee-sdk/components/esp-zigbee-lib/include/ha/esp_zigbee_ha_standard.h }, \ } -class ZigbeeLightSensor : public ZigbeeEP { +class ZigbeeIlluminanceSensor : public ZigbeeEP { public: - ZigbeeLightSensor(uint8_t endpoint); - ~ZigbeeLightSensor() {} + ZigbeeIlluminanceSensor(uint8_t endpoint); + ~ZigbeeIlluminanceSensor() {} - // Set the light value + // Set the illuminance value void setIlluminance(int value); - // Set the min and max value for the light sensor + // Set the min and max value for the illuminance sensor void setMinMaxValue(int min, int max); - // Set the tolerance value for the light sensor + // Set the tolerance value for the illuminance sensor void setTolerance(int tolerance); - // Set the reporting interval for light measurement in seconds and delta + // Set the reporting interval for illuminance measurement in seconds and delta void setReporting(uint16_t min_interval, uint16_t max_interval, int delta); - // Report the light value + // Report the illuminance value void reportIlluminance(); }; From 9560a8a4228fbbb27d7a2a05ed66f1c43f19d24a 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, 26 Mar 2025 09:06:28 +0100 Subject: [PATCH 05/12] feat(zigbee): Illumanance sensor update --- .../README.md | 0 .../Zigbee_Illuminance_Sensor.ino | 0 .../ci.json | 0 .../Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp | 32 +++++++------------ .../Zigbee/src/ep/ZigbeeIlluminanceSensor.h | 20 ++++-------- 5 files changed, 18 insertions(+), 34 deletions(-) rename libraries/Zigbee/examples/{Zigbee_Light_Sensor => Zigbee_Illuminance_Sensor}/README.md (100%) rename libraries/Zigbee/examples/{Zigbee_Light_Sensor => Zigbee_Illuminance_Sensor}/Zigbee_Illuminance_Sensor.ino (100%) rename libraries/Zigbee/examples/{Zigbee_Light_Sensor => Zigbee_Illuminance_Sensor}/ci.json (100%) diff --git a/libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/README.md similarity index 100% rename from libraries/Zigbee/examples/Zigbee_Light_Sensor/README.md rename to libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/README.md diff --git a/libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Illuminance_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino similarity index 100% rename from libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Illuminance_Sensor.ino rename to libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino diff --git a/libraries/Zigbee/examples/Zigbee_Light_Sensor/ci.json b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/ci.json similarity index 100% rename from libraries/Zigbee/examples/Zigbee_Light_Sensor/ci.json rename to libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/ci.json diff --git a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp index 61017a20f33..80a60efe9c8 100644 --- a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp @@ -4,7 +4,7 @@ ZigbeeIlluminanceSensor::ZigbeeIlluminanceSensor(uint8_t endpoint) : ZigbeeEP(endpoint) { _device_id = ESP_ZB_HA_LIGHT_SENSOR_DEVICE_ID; - esp_zb_light_sensor_cfg_t light_sensor_cfg = ESP_ZB_DEFAULT_LIGHT_SENSOR_CONFIG(); + esp_zb_light_sensor_cfg_t light_sensor_cfg = ESP_ZB_DEFAULT_ILLUMINANCE_SENSOR_CONFIG(); _cluster_list = esp_zb_light_sensor_clusters_create(&light_sensor_cfg); _ep_config = { @@ -12,27 +12,20 @@ ZigbeeIlluminanceSensor::ZigbeeIlluminanceSensor(uint8_t endpoint) : ZigbeeEP(en }; } -static int16_t zb_int_to_s16(int value) { - return (int16_t) value; -} - -void ZigbeeIlluminanceSensor::setMinMaxValue(int min, int max) { - int16_t zb_min = zb_int_to_s16(min); - int16_t zb_max = zb_int_to_s16(max); +void ZigbeeIlluminanceSensor::setMinMaxValue(uint16_t min, uint16_t max) { esp_zb_attribute_list_t *light_measure_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); - esp_zb_cluster_update_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_ID, (void *)&zb_min); - esp_zb_cluster_update_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_ID, (void *)&zb_max); + esp_zb_cluster_update_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_ID, (void *)&min); + esp_zb_cluster_update_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_ID, (void *)&max); } -void ZigbeeIlluminanceSensor::setTolerance(int tolerance) { - uint16_t zb_tolerance = (uint16_t) tolerance; +void ZigbeeIlluminanceSensor::setTolerance(uint16_t tolerance) { esp_zb_attribute_list_t *light_measure_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); - esp_zb_illuminance_meas_cluster_add_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_TOLERANCE_ID, (void *)&zb_tolerance); + esp_zb_illuminance_meas_cluster_add_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_TOLERANCE_ID, (void *)&tolerance); } -void ZigbeeIlluminanceSensor::setReporting(uint16_t min_interval, uint16_t max_interval, int delta) { +void ZigbeeIlluminanceSensor::setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t 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; @@ -44,7 +37,7 @@ void ZigbeeIlluminanceSensor::setReporting(uint16_t min_interval, uint16_t max_i 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.u16 = (uint16_t) delta; + reporting_info.u.send_info.delta.u16 = 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); @@ -52,19 +45,18 @@ void ZigbeeIlluminanceSensor::setReporting(uint16_t min_interval, uint16_t max_i esp_zb_lock_release(); } -void ZigbeeIlluminanceSensor::setIlluminance(int illuminanceValue) { - int16_t zb_illuminanceValue = zb_int_to_s16(illuminanceValue); +void ZigbeeIlluminanceSensor::setIlluminance(uint16_t illuminanceValue) { log_v("Updating Illuminance..."); /* Update illuminance sensor measured illuminance */ - log_d("Setting Illuminance to %d", zb_illuminanceValue); + log_d("Setting Illuminance to %d", illuminanceValue); esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_set_attribute_val( - _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MEASURED_VALUE_ID, &zb_illuminanceValue, false + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MEASURED_VALUE_ID, &illuminanceValue, false ); esp_zb_lock_release(); } -void ZigbeeIlluminanceSensor::reportIlluminance() { +void ZigbeeIlluminanceSensor::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; diff --git a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h index ddf354254cb..a4b31e0078e 100644 --- a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h +++ b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h @@ -9,11 +9,7 @@ #include "ZigbeeEP.h" #include "ha/esp_zigbee_ha_standard.h" -/* -the new macro works here, but should better be added to -esp-zigbee-sdk/components/esp-zigbee-lib/include/ha/esp_zigbee_ha_standard.h -*/ -#define ESP_ZB_DEFAULT_LIGHT_SENSOR_CONFIG() \ +#define ESP_ZB_DEFAULT_ILLUMINANCE_SENSOR_CONFIG() \ { \ .basic_cfg = \ { \ @@ -27,10 +23,6 @@ esp-zigbee-sdk/components/esp-zigbee-lib/include/ha/esp_zigbee_ha_standard.h .illuminance_cfg = \ { \ .measured_value = ESP_ZB_ZCL_ILLUMINANCE_MEASUREMENT_LIGHT_SENSOR_TYPE_DEFAULT_VALUE, \ - /*not sure with next two values, but there are no*/ \ - /*ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_DEFAULT and*/ \ - /*ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_DEFAULT*/ \ - /*thats why I chose MIN_VALUE and MAX_VALUE*/ \ .min_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_MIN_VALUE, \ .max_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_MAX_VALUE, \ }, \ @@ -42,19 +34,19 @@ class ZigbeeIlluminanceSensor : public ZigbeeEP { ~ZigbeeIlluminanceSensor() {} // Set the illuminance value - void setIlluminance(int value); + void setIlluminance(uint16_t value); // Set the min and max value for the illuminance sensor - void setMinMaxValue(int min, int max); + void setMinMaxValue(uint16_t min, uint16_t max); // Set the tolerance value for the illuminance sensor - void setTolerance(int tolerance); + void setTolerance(uint16_t tolerance); // Set the reporting interval for illuminance measurement in seconds and delta - void setReporting(uint16_t min_interval, uint16_t max_interval, int delta); + void setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta); // Report the illuminance value - void reportIlluminance(); + void report(); }; #endif // CONFIG_ZB_ENABLED From ad7ef1be3a542810b3b5a450ca5cf42b434f1c3d 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, 26 Mar 2025 09:08:51 +0100 Subject: [PATCH 06/12] fix(zigbee): Change name of macro to avoid possible conflict with esp-zigbee-sdk --- libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp | 2 +- libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp index 80a60efe9c8..6f5d7701d70 100644 --- a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp @@ -4,7 +4,7 @@ ZigbeeIlluminanceSensor::ZigbeeIlluminanceSensor(uint8_t endpoint) : ZigbeeEP(endpoint) { _device_id = ESP_ZB_HA_LIGHT_SENSOR_DEVICE_ID; - esp_zb_light_sensor_cfg_t light_sensor_cfg = ESP_ZB_DEFAULT_ILLUMINANCE_SENSOR_CONFIG(); + esp_zb_light_sensor_cfg_t light_sensor_cfg = ZIGBEE_DEFAULT_ILLUMINANCE_SENSOR_CONFIG(); _cluster_list = esp_zb_light_sensor_clusters_create(&light_sensor_cfg); _ep_config = { diff --git a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h index a4b31e0078e..ab2ca4cb57b 100644 --- a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h +++ b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h @@ -9,7 +9,7 @@ #include "ZigbeeEP.h" #include "ha/esp_zigbee_ha_standard.h" -#define ESP_ZB_DEFAULT_ILLUMINANCE_SENSOR_CONFIG() \ +#define ZIGBEE_DEFAULT_ILLUMINANCE_SENSOR_CONFIG() \ { \ .basic_cfg = \ { \ From d7c7e412b822425245d35bd12132b608c4bb0ac3 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, 26 Mar 2025 09:09:48 +0100 Subject: [PATCH 07/12] Update keywords.txt --- libraries/Zigbee/keywords.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/Zigbee/keywords.txt b/libraries/Zigbee/keywords.txt index 145792b9757..586d2bdc677 100644 --- a/libraries/Zigbee/keywords.txt +++ b/libraries/Zigbee/keywords.txt @@ -124,9 +124,8 @@ setHumidity KEYWORD2 setHumidityReporting KEYWORD2 reportHumidity KEYWORD2 -# ZigbeeLightSensor +# ZigbeeIlluminanceSensor setIlluminance KEYWORD2 -reportIlluminance KEYWORD2 # ZigbeeFlowSensor setFlow KEYWORD2 From b22d4476b60d6113f928cc0ee9530b35023eb4c4 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, 26 Mar 2025 09:11:33 +0100 Subject: [PATCH 08/12] fix(zigbee): Add Illuminance to Cmake file --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d2a9162a9ba..7539601d9e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -299,6 +299,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS libraries/Zigbee/src/ep/ZigbeeRangeExtender.cpp libraries/Zigbee/src/ep/ZigbeeGateway.cpp libraries/Zigbee/src/ep/ZigbeeWindSpeedSensor.cpp + libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp ) set(ARDUINO_LIBRARY_BLE_SRCS From 261cb0664c7c976ceefabfed4b28f442f6777574 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, 26 Mar 2025 09:14:22 +0100 Subject: [PATCH 09/12] fix(example): Update Illuminance example --- .../Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino index e8b89205388..767de8db8c5 100644 --- a/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino +++ b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino @@ -47,7 +47,7 @@ // conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight) // depends on the value range of the raw analog sensor values and will need calibration for correct lux values - int lsens_illuminance_raw = lsens_analog_raw * 10; // multiply by 10 for the sake of this example + int lsens_illuminance_raw = map(lsens_analog_raw, 0, 4095, 0, 50000); // to demonstate map the 12-bit ADC value (0-4095) to Zigbee illuminance range (0-50000) Serial.printf("[Illuminance Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw); // according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value @@ -134,7 +134,7 @@ } } // force report of illuminance when button is pressed - zbIlluminanceSensor.reportIlluminance(); + zbIlluminanceSensor.report(); } delay(100); } \ No newline at end of file From c17ddf61c103a1ec241e065f723e2e2b3edb2704 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, 26 Mar 2025 10:12:44 +0000 Subject: [PATCH 10/12] ci(pre-commit): Apply automatic fixes --- .../Zigbee_Illuminance_Sensor.ino | 225 +++++++++--------- .../Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp | 9 +- .../Zigbee/src/ep/ZigbeeIlluminanceSensor.h | 35 ++- 3 files changed, 134 insertions(+), 135 deletions(-) diff --git a/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino index 767de8db8c5..0ca13559778 100644 --- a/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino +++ b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino @@ -26,115 +26,116 @@ * Created by MikaFromTheRoof (https://github.com/MikaFromTheRoof) */ - #ifndef ZIGBEE_MODE_ED - #error "Zigbee end device mode is not selected in Tools->Zigbee mode" - #endif - - #include "Zigbee.h" - - #define ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT 9 - uint8_t button = BOOT_PIN; - uint8_t illuminance_sensor_pin = 6; // Insert the analog pin to which the sensor (e.g. photoresistor) is connected - - ZigbeeIlluminanceSensor zbIlluminanceSensor = ZigbeeIlluminanceSensor(ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT); - - /********************* Illuminance sensor **************************/ - static void illuminance_sensor_value_update(void *arg) { - for (;;) { - // read the raw analog value from the sensor - int lsens_analog_raw = analogRead(illuminance_sensor_pin); - Serial.printf("[Illuminance Sensor] raw analog value: %d\r\n", lsens_analog_raw); - - // conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight) - // depends on the value range of the raw analog sensor values and will need calibration for correct lux values - int lsens_illuminance_raw = map(lsens_analog_raw, 0, 4095, 0, 50000); // to demonstate map the 12-bit ADC value (0-4095) to Zigbee illuminance range (0-50000) - Serial.printf("[Illuminance Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw); - - // according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value - // Note: Zigbee2MQTT seems to be using the formular 10^(lsens_illuminance_raw/10000) instead (without -1) - int lsens_illuminance_lux = round(pow(10,(lsens_illuminance_raw / 10000.0))-1); - Serial.printf("[Illuminance Sensor] lux value: %d lux\r\n", lsens_illuminance_lux); - - // Update illuminance in illuminance sensor EP - zbIlluminanceSensor.setIlluminance(lsens_illuminance_raw); // use raw illuminance here! - - delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance - } - } - - /********************* Arduino functions **************************/ - void setup() { - Serial.begin(115200); - - // Optional: configure analog input - analogSetAttenuation(ADC_11db); // set analog to digital converter (ADC) attenuation to 11 dB (up to ~3.3V input) - analogReadResolution(12); // set analog read resolution to 12 bits (value range from 0 to 4095), 12 is default - - // Init button for factory reset - pinMode(button, INPUT_PULLUP); - - // Optional: Set Zigbee device name and model - zbIlluminanceSensor.setManufacturerAndModel("Espressif", "ZigbeeIlluminanceSensor"); - - // Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown - zbIlluminanceSensor.setPowerSource(ZB_POWER_SOURCE_MAINS); - - // Set minimum and maximum for raw illuminance value (0 min and 50000 max equals to 0 lux - 100,000 lux) - zbIlluminanceSensor.setMinMaxValue(0, 50000); - - // Optional: Set tolerance for raw illuminance value - zbIlluminanceSensor.setTolerance(1); - - // Add endpoint to Zigbee Core - Serial.println("Adding Zigbee illuminance sensor endpoint to Zigbee Core"); - Zigbee.addEndpoint(&zbIlluminanceSensor); - - 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(); - - // Start illuminance sensor reading task - xTaskCreate(illuminance_sensor_value_update, "illuminance_sensor_update", 2048, NULL, 10, NULL); - - // Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin() - // min_interval and max_interval in seconds, delta - // if min = 1 and max = 0, delta = 1000, reporting is sent when raw illuminance value changes by 1000, but at most once per second - // if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if raw illuminance value changes by 1000 - // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change - // Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings - zbIlluminanceSensor.setReporting(1, 0, 1000); - } - - /********************* Main loop **************************/ - void loop() { - // Checking button for factory reset - 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 3 secs, factory reset Zigbee and reboot - Serial.println("Resetting Zigbee to factory and rebooting in 1s"); - delay(1000); - Zigbee.factoryReset(); - } - } - // force report of illuminance when button is pressed - zbIlluminanceSensor.report(); - } - 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" + +#define ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT 9 +uint8_t button = BOOT_PIN; +uint8_t illuminance_sensor_pin = 6; // Insert the analog pin to which the sensor (e.g. photoresistor) is connected + +ZigbeeIlluminanceSensor zbIlluminanceSensor = ZigbeeIlluminanceSensor(ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT); + +/********************* Illuminance sensor **************************/ +static void illuminance_sensor_value_update(void *arg) { + for (;;) { + // read the raw analog value from the sensor + int lsens_analog_raw = analogRead(illuminance_sensor_pin); + Serial.printf("[Illuminance Sensor] raw analog value: %d\r\n", lsens_analog_raw); + + // conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight) + // depends on the value range of the raw analog sensor values and will need calibration for correct lux values + int lsens_illuminance_raw = + map(lsens_analog_raw, 0, 4095, 0, 50000); // to demonstate map the 12-bit ADC value (0-4095) to Zigbee illuminance range (0-50000) + Serial.printf("[Illuminance Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw); + + // according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value + // Note: Zigbee2MQTT seems to be using the formular 10^(lsens_illuminance_raw/10000) instead (without -1) + int lsens_illuminance_lux = round(pow(10, (lsens_illuminance_raw / 10000.0)) - 1); + Serial.printf("[Illuminance Sensor] lux value: %d lux\r\n", lsens_illuminance_lux); + + // Update illuminance in illuminance sensor EP + zbIlluminanceSensor.setIlluminance(lsens_illuminance_raw); // use raw illuminance here! + + delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance + } +} + +/********************* Arduino functions **************************/ +void setup() { + Serial.begin(115200); + + // Optional: configure analog input + analogSetAttenuation(ADC_11db); // set analog to digital converter (ADC) attenuation to 11 dB (up to ~3.3V input) + analogReadResolution(12); // set analog read resolution to 12 bits (value range from 0 to 4095), 12 is default + + // Init button for factory reset + pinMode(button, INPUT_PULLUP); + + // Optional: Set Zigbee device name and model + zbIlluminanceSensor.setManufacturerAndModel("Espressif", "ZigbeeIlluminanceSensor"); + + // Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown + zbIlluminanceSensor.setPowerSource(ZB_POWER_SOURCE_MAINS); + + // Set minimum and maximum for raw illuminance value (0 min and 50000 max equals to 0 lux - 100,000 lux) + zbIlluminanceSensor.setMinMaxValue(0, 50000); + + // Optional: Set tolerance for raw illuminance value + zbIlluminanceSensor.setTolerance(1); + + // Add endpoint to Zigbee Core + Serial.println("Adding Zigbee illuminance sensor endpoint to Zigbee Core"); + Zigbee.addEndpoint(&zbIlluminanceSensor); + + 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(); + + // Start illuminance sensor reading task + xTaskCreate(illuminance_sensor_value_update, "illuminance_sensor_update", 2048, NULL, 10, NULL); + + // Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin() + // min_interval and max_interval in seconds, delta + // if min = 1 and max = 0, delta = 1000, reporting is sent when raw illuminance value changes by 1000, but at most once per second + // if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if raw illuminance value changes by 1000 + // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change + // Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings + zbIlluminanceSensor.setReporting(1, 0, 1000); +} + +/********************* Main loop **************************/ +void loop() { + // Checking button for factory reset + 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 3 secs, factory reset Zigbee and reboot + Serial.println("Resetting Zigbee to factory and rebooting in 1s"); + delay(1000); + Zigbee.factoryReset(); + } + } + // force report of illuminance when button is pressed + zbIlluminanceSensor.report(); + } + delay(100); +} diff --git a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp index 6f5d7701d70..d38e9b1065f 100644 --- a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp @@ -7,9 +7,7 @@ ZigbeeIlluminanceSensor::ZigbeeIlluminanceSensor(uint8_t endpoint) : ZigbeeEP(en esp_zb_light_sensor_cfg_t light_sensor_cfg = ZIGBEE_DEFAULT_ILLUMINANCE_SENSOR_CONFIG(); _cluster_list = esp_zb_light_sensor_clusters_create(&light_sensor_cfg); - _ep_config = { - .endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_LIGHT_SENSOR_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_LIGHT_SENSOR_DEVICE_ID, .app_device_version = 0}; } void ZigbeeIlluminanceSensor::setMinMaxValue(uint16_t min, uint16_t max) { @@ -22,7 +20,7 @@ void ZigbeeIlluminanceSensor::setMinMaxValue(uint16_t min, uint16_t max) { void ZigbeeIlluminanceSensor::setTolerance(uint16_t tolerance) { esp_zb_attribute_list_t *light_measure_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); - esp_zb_illuminance_meas_cluster_add_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_TOLERANCE_ID, (void *)&tolerance); + esp_zb_illuminance_meas_cluster_add_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_TOLERANCE_ID, (void *)&tolerance); } void ZigbeeIlluminanceSensor::setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta) { @@ -51,7 +49,8 @@ void ZigbeeIlluminanceSensor::setIlluminance(uint16_t illuminanceValue) { log_d("Setting Illuminance to %d", illuminanceValue); esp_zb_lock_acquire(portMAX_DELAY); esp_zb_zcl_set_attribute_val( - _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MEASURED_VALUE_ID, &illuminanceValue, false + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MEASURED_VALUE_ID, + &illuminanceValue, false ); esp_zb_lock_release(); } diff --git a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h index ab2ca4cb57b..9c9d9e3fcaf 100644 --- a/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h +++ b/libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h @@ -9,24 +9,23 @@ #include "ZigbeeEP.h" #include "ha/esp_zigbee_ha_standard.h" -#define ZIGBEE_DEFAULT_ILLUMINANCE_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, \ - }, \ - .illuminance_cfg = \ - { \ - .measured_value = ESP_ZB_ZCL_ILLUMINANCE_MEASUREMENT_LIGHT_SENSOR_TYPE_DEFAULT_VALUE, \ - .min_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_MIN_VALUE, \ - .max_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_MAX_VALUE, \ - }, \ - } +#define ZIGBEE_DEFAULT_ILLUMINANCE_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, \ + }, \ + .illuminance_cfg = { \ + .measured_value = ESP_ZB_ZCL_ILLUMINANCE_MEASUREMENT_LIGHT_SENSOR_TYPE_DEFAULT_VALUE, \ + .min_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_MIN_VALUE, \ + .max_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_MAX_VALUE, \ + }, \ + } class ZigbeeIlluminanceSensor : public ZigbeeEP { public: From b80ad66c62c7d56201bf06c7b0e628a971e8ec3a 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, 26 Mar 2025 11:21:00 +0100 Subject: [PATCH 11/12] Update Zigbee_Illuminance_Sensor.ino --- .../Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino index 0ca13559778..370f1db97fe 100644 --- a/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino +++ b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino @@ -47,8 +47,8 @@ static void illuminance_sensor_value_update(void *arg) { // conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight) // depends on the value range of the raw analog sensor values and will need calibration for correct lux values - int lsens_illuminance_raw = - map(lsens_analog_raw, 0, 4095, 0, 50000); // to demonstate map the 12-bit ADC value (0-4095) to Zigbee illuminance range (0-50000) + // for demonstration purpose map the 12-bit ADC value (0-4095) to Zigbee illuminance range (0-50000) + int lsens_illuminance_raw = map(lsens_analog_raw, 0, 4095, 0, 50000); Serial.printf("[Illuminance Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw); // according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value From d377a17e677df6dce176972ee06fdd7752c72570 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, 26 Mar 2025 10:22:14 +0000 Subject: [PATCH 12/12] ci(pre-commit): Apply automatic fixes --- .../Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino index 370f1db97fe..bbb2cba569f 100644 --- a/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino +++ b/libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino @@ -48,7 +48,7 @@ static void illuminance_sensor_value_update(void *arg) { // conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight) // depends on the value range of the raw analog sensor values and will need calibration for correct lux values // for demonstration purpose map the 12-bit ADC value (0-4095) to Zigbee illuminance range (0-50000) - int lsens_illuminance_raw = map(lsens_analog_raw, 0, 4095, 0, 50000); + int lsens_illuminance_raw = map(lsens_analog_raw, 0, 4095, 0, 50000); Serial.printf("[Illuminance Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw); // according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value