feat(zigbee): Add pressure, flow and occupancy sensor

This commit is contained in:
Jan Procházka 2024-12-12 15:07:31 +01:00
parent a4f96e9280
commit 77df256f5d
19 changed files with 844 additions and 7 deletions

View file

@ -0,0 +1,57 @@
# Arduino-ESP32 Zigbee Occupancy Sensor Example
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) occupancy sensor (PIR).
# Supported Targets
Currently, this example supports the following targets.
| Supported Targets | ESP32-C6 | ESP32-H2 |
| ----------------- | -------- | -------- |
## Hardware Required
* A USB cable for power supply and programming
### Configure the Project
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).
Set the Sensor GPIO by changing the `sensor_pin` variable.
#### 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.
***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)

View file

@ -0,0 +1,104 @@
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @brief This example demonstrates Zigbee occupancy sensor.
*
* The example demonstrates how to use Zigbee library to create a end device occupancy sensor.
* The occupancy sensor is a Zigbee end device, which is reporting data to the Zigbee network.
* Tested with PIR sensor HC-SR501 connected to GPIO4.
*
* Proper Zigbee mode must be selected in Tools->Zigbee mode
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
*
* Please check the README.md for instructions and more detailed description.
*
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
*/
#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#endif
#include "Zigbee.h"
/* Zigbee occupancy sensor configuration */
#define OCCUPANCY_SENSOR_ENDPOINT_NUMBER 10
uint8_t button = BOOT_PIN;
uint8_t sensor_pin = 4;
ZigbeeOccupancySensor zbOccupancySensor = ZigbeeOccupancySensor(OCCUPANCY_SENSOR_ENDPOINT_NUMBER);
/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
// Init button + PIR sensor
pinMode(button, INPUT_PULLUP);
pinMode(sensor_pin, INPUT);
// Optional: set Zigbee device name and model
zbOccupancySensor.setManufacturerAndModel("Espressif", "ZigbeeOccupancyPIRSensor");
// Add endpoint to Zigbee Core
Zigbee.addEndpoint(&zbOccupancySensor);
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();
}
void loop() {
// Checking PIR sensor for occupancy change
static bool occupancy = false;
if(digitalRead(sensor_pin) == HIGH && !occupancy) {
// Update occupancy sensor value
zbOccupancySensor.setOccupancy(true);
zbOccupancySensor.report();
occupancy = true;
} else if (digitalRead(sensor_pin) == LOW && occupancy) {
zbOccupancySensor.setOccupancy(false);
zbOccupancySensor.report();
occupancy = false;
}
// 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 3secs, factory reset Zigbee and reboot
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
delay(1000);
Zigbee.factoryReset();
}
}
}
delay(100);
}

View file

@ -0,0 +1,6 @@
{
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
"requires": [
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
]
}

View file

@ -0,0 +1,72 @@
# Arduino-ESP32 Zigbee Pressure + Flow 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 pressure and flow measuring.
# Supported Targets
Currently, this example supports the following targets.
| Supported Targets | ESP32-C6 | ESP32-H2 |
| ----------------- | -------- | -------- |
## Pressure + Flow Sensor Functions
* After this board first starts up, it would be configured locally to report the pressure and flow 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 flow and pressure 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 flow and pressure 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)

View file

@ -0,0 +1,134 @@
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @brief This example demonstrates Zigbee temperature sensor.
*
* The example demonstrates how to use Zigbee library to create a end device temperature sensor.
* The temperature sensor is a Zigbee end device, which is controlled by a Zigbee coordinator.
*
* Proper Zigbee mode must be selected in Tools->Zigbee mode
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
*
* Please check the README.md for instructions and more detailed description.
*
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
*/
#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#endif
#include "Zigbee.h"
/* Zigbee flow + pressure sensor configuration */
#define FLOW_SENSOR_ENDPOINT_NUMBER 10
#define PRESSURE_SENSOR_ENDPOINT_NUMBER 11
uint8_t button = BOOT_PIN;
ZigbeeFlowSensor zbFlowSensor = ZigbeeFlowSensor(FLOW_SENSOR_ENDPOINT_NUMBER);
ZigbeePressureSensor zbPressureSensor = ZigbeePressureSensor(PRESSURE_SENSOR_ENDPOINT_NUMBER);
/************************ Temp sensor *****************************/
static void sensors_reading(void *arg) {
for (;;) {
// Read Pressure and Flow sensors value - here is chip temperature used as a dummy value for demonstration
float flow_value = temperatureRead();
uint16_t pressure_value = (uint16_t)temperatureRead()*100; //*100 for demonstration so the value is in 1-3hPa
Serial.printf("Updating flow sensor value to %.2f\r\n", flow_value);
zbFlowSensor.setFlow(flow_value);
Serial.printf("Updating pressure sensor value to %.2f\r\n", pressure_value);
zbPressureSensor.setPressure(pressure_value);
delay(1000);
}
}
/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
// Init button switch
pinMode(button, INPUT_PULLUP);
// Optional: set Zigbee device name and model
zbFlowSensor.setManufacturerAndModel("Espressif", "ZigbeeFlowSensor");
// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
zbFlowSensor.setMinMaxValue(0, 100);
// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
zbFlowSensor.setTolerance(1);
// Optional: set Zigbee device name and model
zbPressureSensor.setManufacturerAndModel("Espressif", "ZigbeePressureSensor");
// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
zbPressureSensor.setMinMaxValue(0, 30);
// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
zbPressureSensor.setTolerance(1);
// Add endpoints to Zigbee Core
Zigbee.addEndpoint(&zbFlowSensor);
Zigbee.addEndpoint(&zbPressureSensor);
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 Flow and Pressure sensor reading task
xTaskCreate(sensors_reading, "flow_pressure_sensors_read", 2048, NULL, 10, NULL);
// Set reporting interval for flow and pressure measurement in seconds, must be called after Zigbee.begin()
// min_interval and max_interval in seconds, delta (pressure change in Pa, flow change in 0,1 m3/h)
// 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
zbFlowSensor.setReporting(0, 30, 1);
zbPressureSensor.setReporting(0, 30, 1);
}
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 3secs, factory reset Zigbee and reboot
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
delay(1000);
Zigbee.factoryReset();
}
}
zbFlowSensor.report();
zbPressureSensor.report();
}
delay(100);
}

View file

@ -0,0 +1,6 @@
{
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
"requires": [
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
]
}

View file

@ -24,7 +24,7 @@ Currently, this example supports the following targets.
### Configure the Project
In this example, to demonstrate the functionality the chip temperature is used and reported as temperature and humidity.
Set the Button GPIO by changing the `BUTTON_PIN` definition. By default, it's the pin `9` (BOOT button on ESP32-C6 and ESP32-H2).
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

View file

@ -55,11 +55,12 @@ void meausureAndSleep() {
zbTempSensor.setHumidity(humidity);
// Report temperature and humidity values
zbTempSensor.reportTemperature();
zbTempSensor.reportHumidity();
zbTempSensor.report();
Serial.printf("Reported temperature: %.2f°C, Humidity: %.2f%%\r\n", temperature, humidity);
// Add small delay to allow the data to be sent before going to sleep
delay(100);
// Put device to deep sleep
Serial.println("Going to sleep now");
esp_deep_sleep_start();

View file

@ -28,7 +28,7 @@ Functions:
### Configure the Project
In this example, the internal temperature sensor task is reading the chip temperature.
Set the Button GPIO by changing the `BUTTON_PIN` definition. By default, it's the pin `9` (BOOT button on ESP32-C6 and ESP32-H2).
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

View file

@ -63,7 +63,7 @@ void setup() {
// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
zbTempSensor.setMinMaxValue(10, 50);
// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
// Optional: Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
zbTempSensor.setTolerance(1);
// Add endpoint to Zigbee Core
@ -89,7 +89,7 @@ void setup() {
xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 2048, NULL, 10, NULL);
// Set reporting interval for temperature measurement in seconds, must be called after Zigbee.begin()
// min_interval and max_interval in seconds, delta (temp change in °C)
// min_interval and max_interval in seconds, delta (temp change in 0,1 °C)
// 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 temperature change

View file

@ -13,3 +13,6 @@
#include "ep/ZigbeeColorDimmerSwitch.h"
#include "ep/ZigbeeTempSensor.h"
#include "ep/ZigbeeThermostat.h"
#include "ep/ZigbeePressureSensor.h"
#include "ep/ZigbeeFlowSensor.h"
#include "ep/ZigbeeOccupancySensor.h"

View file

@ -0,0 +1,105 @@
#include "ZigbeeFlowSensor.h"
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
esp_zb_cluster_list_t *zigbee_flow_sensor_clusters_create(zigbee_flow_sensor_cfg_t *flow_sensor)
{
esp_zb_basic_cluster_cfg_t *basic_cfg = flow_sensor ? &(flow_sensor->basic_cfg) : NULL;
esp_zb_identify_cluster_cfg_t *identify_cfg = flow_sensor ? &(flow_sensor->identify_cfg) : NULL;
esp_zb_flow_meas_cluster_cfg_t *flow_meas_cfg = flow_sensor ? &(flow_sensor->flow_meas_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_flow_meas_cluster(cluster_list, esp_zb_flow_meas_cluster_create(flow_meas_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
return cluster_list;
}
ZigbeeFlowSensor::ZigbeeFlowSensor(uint8_t endpoint) : ZigbeeEP(endpoint) {
_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID;
//Create custom pressure sensor configuration
zigbee_flow_sensor_cfg_t flow_sensor_cfg = ZIGBEE_DEFAULT_FLOW_SENSOR_CONFIG();
_cluster_list = zigbee_flow_sensor_clusters_create(&flow_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 ZigbeeFlowSensor::setMinMaxValue(float min, float max) {
uint16_t zb_min = (uint16_t)(min * 10);
uint16_t zb_max = (uint16_t)(max * 10);
esp_zb_attribute_list_t *flow_measure_cluster =
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_FLOW_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_cluster_update_attr(flow_measure_cluster, ESP_ZB_ZCL_ATTR_FLOW_MEASUREMENT_MIN_VALUE_ID, (void *)&zb_min);
esp_zb_cluster_update_attr(flow_measure_cluster, ESP_ZB_ZCL_ATTR_FLOW_MEASUREMENT_MAX_VALUE_ID, (void *)&zb_max);
}
void ZigbeeFlowSensor::setTolerance(float tolerance) {
// Convert tolerance to ZCL uint16_t
uint16_t zb_tolerance = (uint16_t)(tolerance * 10);
esp_zb_attribute_list_t *flow_measure_cluster =
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_FLOW_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_flow_meas_cluster_add_attr(flow_measure_cluster, ESP_ZB_ZCL_ATTR_FLOW_MEASUREMENT_TOLERANCE_ID, (void *)&zb_tolerance);
}
void ZigbeeFlowSensor::setReporting(uint16_t min_interval, uint16_t max_interval, float delta) {
esp_zb_zcl_reporting_info_t reporting_info = {
.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV,
.ep = _endpoint,
.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_FLOW_MEASUREMENT,
.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
.attr_id = ESP_ZB_ZCL_ATTR_FLOW_MEASUREMENT_VALUE_ID,
.u =
{
.send_info =
{
.min_interval = min_interval,
.max_interval = max_interval,
.delta =
{
.u16 = (uint16_t)(delta * 10), // Convert delta to ZCL uint16_t
},
.def_min_interval = min_interval,
.def_max_interval = max_interval,
},
},
.dst =
{
.profile_id = ESP_ZB_AF_HA_PROFILE_ID,
},
.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC,
};
esp_zb_lock_acquire(portMAX_DELAY);
esp_zb_zcl_update_reporting_info(&reporting_info);
esp_zb_lock_release();
}
void ZigbeeFlowSensor::setFlow(float flow) {
uint16_t zb_flow = (uint16_t)(flow * 10);
log_v("Updating flow sensor value...");
/* Update temperature sensor measured value */
log_d("Setting flow to %d", zb_flow);
esp_zb_lock_acquire(portMAX_DELAY);
esp_zb_zcl_set_attribute_val(
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_FLOW_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_FLOW_MEASUREMENT_VALUE_ID, &zb_flow, false
);
esp_zb_lock_release();
}
void ZigbeeFlowSensor::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_FLOW_MEASUREMENT_VALUE_ID;
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI;
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_FLOW_MEASUREMENT;
report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint;
esp_zb_lock_acquire(portMAX_DELAY);
esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd);
esp_zb_lock_release();
log_v("Flow report sent");
}
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

View file

@ -0,0 +1,58 @@
/* Class of Zigbee Flow 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"
#define ZIGBEE_DEFAULT_FLOW_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, \
}, \
.flow_meas_cfg = \
{ \
.measured_value = 0, \
.min_value = 0, \
.max_value = 0x7FFF, \
}, \
}
typedef struct zigbee_flow_sensor_cfg_s {
esp_zb_basic_cluster_cfg_t basic_cfg;
esp_zb_identify_cluster_cfg_t identify_cfg;
esp_zb_flow_meas_cluster_cfg_t flow_meas_cfg;
} zigbee_flow_sensor_cfg_t;
class ZigbeeFlowSensor : public ZigbeeEP {
public:
ZigbeeFlowSensor(uint8_t endpoint);
~ZigbeeFlowSensor();
// Set the flow value in 0,01 m3/h
void setFlow(float value);
// Set the min and max value for the flow sensor in 0,1 m3/h
void setMinMaxValue(float min, float max);
// Set the tolerance value for the flow sensor in 0,01 m3/h
void setTolerance(float tolerance);
// Set the reporting interval for flow measurement in seconds and delta (temp change in 0,01 m3/h)
void setReporting(uint16_t min_interval, uint16_t max_interval, float delta);
// Report the flow value
void report();
};
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

View file

@ -0,0 +1,63 @@
#include "ZigbeeOccupancySensor.h"
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
esp_zb_cluster_list_t *zigbee_occupancy_sensor_clusters_create(zigbee_occupancy_sensor_cfg_t *occupancy_sensor)
{
esp_zb_basic_cluster_cfg_t *basic_cfg = occupancy_sensor ? &(occupancy_sensor->basic_cfg) : NULL;
esp_zb_identify_cluster_cfg_t *identify_cfg = occupancy_sensor ? &(occupancy_sensor->identify_cfg) : NULL;
esp_zb_occupancy_sensing_cluster_cfg_t *occupancy_meas_cfg = occupancy_sensor ? &(occupancy_sensor->occupancy_meas_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_occupancy_sensing_cluster(cluster_list, esp_zb_occupancy_sensing_cluster_create(occupancy_meas_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
return cluster_list;
}
ZigbeeOccupancySensor::ZigbeeOccupancySensor(uint8_t endpoint) : ZigbeeEP(endpoint) {
_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID;
//Create custom occupancy sensor configuration
zigbee_occupancy_sensor_cfg_t occupancy_sensor_cfg = ZIGBEE_DEFAULT_OCCUPANCY_SENSOR_CONFIG();
_cluster_list = zigbee_occupancy_sensor_clusters_create(&occupancy_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 ZigbeeOccupancySensor::setSensorType(uint8_t sensor_type) {
uint8_t sensor_type_bitmap = 1 << sensor_type;
esp_zb_attribute_list_t *occupancy_sens_cluster =
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_OCCUPANCY_SENSING, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_cluster_update_attr(occupancy_sens_cluster, ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_OCCUPANCY_SENSOR_TYPE_ID, (void *)&sensor_type);
esp_zb_cluster_update_attr(occupancy_sens_cluster, ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_OCCUPANCY_SENSOR_TYPE_BITMAP_ID, (void *)&sensor_type_bitmap);
}
void ZigbeeOccupancySensor::setOccupancy(bool occupied) {
log_v("Updating occupancy sensor value...");
/* Update occupancy sensor value */
log_d("Setting occupancy to %d", occupied);
esp_zb_lock_acquire(portMAX_DELAY);
esp_zb_zcl_set_attribute_val(
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_OCCUPANCY_SENSING, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_OCCUPANCY_ID, &occupied, false
);
esp_zb_lock_release();
}
void ZigbeeOccupancySensor::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_OCCUPANCY_SENSING_OCCUPANCY_ID;
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI;
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_OCCUPANCY_SENSING;
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("Occupancy report sent");
}
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

View file

@ -0,0 +1,52 @@
/* Class of Zigbee Pressure 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"
#define ZIGBEE_DEFAULT_OCCUPANCY_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, \
}, \
.occupancy_meas_cfg = \
{ \
.occupancy = ESP_ZB_ZCL_OCCUPANCY_SENSING_OCCUPANCY_UNOCCUPIED, \
.sensor_type = ESP_ZB_ZCL_OCCUPANCY_SENSING_OCCUPANCY_SENSOR_TYPE_PIR, \
.sensor_type_bitmap = (1 << ESP_ZB_ZCL_OCCUPANCY_SENSING_OCCUPANCY_SENSOR_TYPE_PIR), \
}, \
}
typedef struct zigbee_occupancy_sensor_cfg_s {
esp_zb_basic_cluster_cfg_t basic_cfg;
esp_zb_identify_cluster_cfg_t identify_cfg;
esp_zb_occupancy_sensing_cluster_cfg_t occupancy_meas_cfg;
} zigbee_occupancy_sensor_cfg_t;
class ZigbeeOccupancySensor : public ZigbeeEP {
public:
ZigbeeOccupancySensor(uint8_t endpoint);
~ZigbeeOccupancySensor();
// Set the occupancy value. True for occupied, false for unoccupied
void setOccupancy(bool occupied);
// Set the sensor type, see esp_zb_zcl_occupancy_sensing_occupancy_sensor_type_t
void setSensorType(uint8_t sensor_type);
// Report the occupancy value
void report();
};
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

View file

@ -0,0 +1,101 @@
#include "ZigbeePressureSensor.h"
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
esp_zb_cluster_list_t *zigbee_pressure_sensor_clusters_create(zigbee_pressure_sensor_cfg_t *pressure_sensor)
{
esp_zb_basic_cluster_cfg_t *basic_cfg = pressure_sensor ? &(pressure_sensor->basic_cfg) : NULL;
esp_zb_identify_cluster_cfg_t *identify_cfg = pressure_sensor ? &(pressure_sensor->identify_cfg) : NULL;
esp_zb_pressure_meas_cluster_cfg_t *pressure_meas_cfg = pressure_sensor ? &(pressure_sensor->pressure_meas_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_pressure_meas_cluster(cluster_list, esp_zb_pressure_meas_cluster_create(pressure_meas_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
return cluster_list;
}
ZigbeePressureSensor::ZigbeePressureSensor(uint8_t endpoint) : ZigbeeEP(endpoint) {
_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID;
//Create custom pressure sensor configuration
zigbee_pressure_sensor_cfg_t presssure_sensor_cfg = ZIGBEE_DEFAULT_PRESSURE_SENSOR_CONFIG();
_cluster_list = zigbee_pressure_sensor_clusters_create(&presssure_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 ZigbeePressureSensor::setMinMaxValue(int16_t min, int16_t max) {
esp_zb_attribute_list_t *pressure_measure_cluster =
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_cluster_update_attr(pressure_measure_cluster, ESP_ZB_ZCL_ATTR_PRESSURE_MEASUREMENT_MIN_VALUE_ID, (void *)&min);
esp_zb_cluster_update_attr(pressure_measure_cluster, ESP_ZB_ZCL_ATTR_PRESSURE_MEASUREMENT_MAX_VALUE_ID, (void *)&max);
}
void ZigbeePressureSensor::setTolerance(uint16_t tolerance) {
esp_zb_attribute_list_t *pressure_measure_cluster =
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_temperature_meas_cluster_add_attr(pressure_measure_cluster, ESP_ZB_ZCL_ATTR_PRESSURE_MEASUREMENT_TOLERANCE_ID, (void *)&tolerance);
}
void ZigbeePressureSensor::setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta) {
esp_zb_zcl_reporting_info_t reporting_info = {
.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV,
.ep = _endpoint,
.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT,
.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
.attr_id = ESP_ZB_ZCL_ATTR_PRESSURE_MEASUREMENT_VALUE_ID,
.u =
{
.send_info =
{
.min_interval = min_interval,
.max_interval = max_interval,
.delta =
{
.u16 = delta, // x Pa
},
.def_min_interval = min_interval,
.def_max_interval = max_interval,
},
},
.dst =
{
.profile_id = ESP_ZB_AF_HA_PROFILE_ID,
},
.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC,
};
esp_zb_lock_acquire(portMAX_DELAY);
esp_zb_zcl_update_reporting_info(&reporting_info);
esp_zb_lock_release();
}
void ZigbeePressureSensor::setPressure(int16_t pressure) {
log_v("Updating pressure sensor value...");
/* Update temperature sensor measured value */
log_d("Setting pressure to %d Pa", pressure);
esp_zb_lock_acquire(portMAX_DELAY);
esp_zb_zcl_set_attribute_val(
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_PRESSURE_MEASUREMENT_VALUE_ID, &pressure, false
);
esp_zb_lock_release();
}
void ZigbeePressureSensor::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_PRESSURE_MEASUREMENT_VALUE_ID;
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI;
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT;
report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint;
esp_zb_lock_acquire(portMAX_DELAY);
esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd);
esp_zb_lock_release();
log_v("Pressure report sent");
}
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

View file

@ -0,0 +1,58 @@
/* Class of Zigbee Pressure 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"
#define ZIGBEE_DEFAULT_PRESSURE_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, \
}, \
.pressure_meas_cfg = \
{ \
.measured_value = ESP_ZB_ZCL_ATTR_PRESSURE_MEASUREMENT_VALUE_DEFAULT_VALUE, \
.min_value = ESP_ZB_ZCL_PATTR_RESSURE_MEASUREMENT_MIN_VALUE_DEFAULT_VALUE, \
.max_value = ESP_ZB_ZCL_PATTR_RESSURE_MEASUREMENT_MAX_VALUE_DEFAULT_VALUE, \
}, \
}
typedef struct zigbee_pressure_sensor_cfg_s {
esp_zb_basic_cluster_cfg_t basic_cfg;
esp_zb_identify_cluster_cfg_t identify_cfg;
esp_zb_pressure_meas_cluster_cfg_t pressure_meas_cfg;
} zigbee_pressure_sensor_cfg_t;
class ZigbeePressureSensor : public ZigbeeEP {
public:
ZigbeePressureSensor(uint8_t endpoint);
~ZigbeePressureSensor();
// Set the pressure value in 1 Pa
void setPressure(int16_t value);
// Set the min and max value for the pressure sensor in 1 Pa
void setMinMaxValue(int16_t min, int16_t max);
// Set the tolerance value for the pressure sensor in 1 Pa
void setTolerance(uint16_t tolerance);
// Set the reporting interval for pressure measurement in seconds and delta (pressure change in 1 Pa)
void setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta);
// Report the pressure value
void report();
};
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

View file

@ -3,6 +3,7 @@
ZigbeeTempSensor::ZigbeeTempSensor(uint8_t endpoint) : ZigbeeEP(endpoint) {
_device_id = ESP_ZB_HA_TEMPERATURE_SENSOR_DEVICE_ID;
_humidity_sensor = false;
esp_zb_temperature_sensor_cfg_t temp_sensor_cfg = ESP_ZB_DEFAULT_TEMPERATURE_SENSOR_CONFIG();
_cluster_list = esp_zb_temperature_sensor_clusters_create(&temp_sensor_cfg);
@ -103,6 +104,7 @@ void ZigbeeTempSensor::addHumiditySensor(float min, float max, float tolerance)
esp_zb_humidity_meas_cluster_add_attr(humidity_cluster, ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_MAX_VALUE_ID, &zb_max);
esp_zb_humidity_meas_cluster_add_attr(humidity_cluster, ESP_ZB_ZCL_ATTR_REL_HUMIDITY_TOLERANCE_ID, &zb_tolerance);
esp_zb_cluster_list_add_humidity_meas_cluster(_cluster_list, humidity_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
_humidity_sensor = true;
}
void ZigbeeTempSensor::setHumidity(float humidity) {
@ -165,4 +167,11 @@ void ZigbeeTempSensor::setHumidityReporting(uint16_t min_interval, uint16_t max_
esp_zb_lock_release();
}
void ZigbeeTempSensor::report() {
reportTemperature();
if (_humidity_sensor) {
reportHumidity();
}
}
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

View file

@ -40,6 +40,14 @@ public:
// Report the humidity value
void reportHumidity();
// Report the temperature and humidity values if humidity sensor is added
void report();
private:
bool _humidity_sensor;
};
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED