Skip to content

Commit 23ded93

Browse files
feat(zigbee): Add PM2.5 endpoint support (#11205)
* feat(zigbee): Add PM2.5 endpoint support * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 22f07d0 commit 23ded93

File tree

7 files changed

+384
-11
lines changed

7 files changed

+384
-11
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
300300
libraries/Zigbee/src/ep/ZigbeeGateway.cpp
301301
libraries/Zigbee/src/ep/ZigbeeWindSpeedSensor.cpp
302302
libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp
303+
libraries/Zigbee/src/ep/ZigbeePM25Sensor.cpp
303304
)
304305

305306
set(ARDUINO_LIBRARY_BLE_SRCS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Arduino-ESP32 PM2.5 Sensor
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) simple sensor device type with particulate matter (PM2.5) measuring
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## Pressure + Flow Sensor Functions
13+
14+
* After this board first starts up, it would be configured locally to report the PM2.5 on every 30 seconds.
15+
* By clicking the button (BOOT) on this board, this board will immediately send a report of the current PM2.5 to the network.
16+
17+
## Hardware Required
18+
19+
* A USB cable for power supply and programming
20+
21+
### Configure the Project
22+
23+
In this example, the internal temperature sensor is used to demonstrate reading of the PM2.5 sensors.
24+
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).
25+
26+
#### Using Arduino IDE
27+
28+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
29+
30+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
31+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
32+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
33+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
34+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
35+
36+
## Troubleshooting
37+
38+
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.
39+
You can do the following:
40+
41+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
42+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
43+
44+
By default, the coordinator network is closed after rebooting or flashing new firmware.
45+
To open the network you have 2 options:
46+
47+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
48+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
49+
50+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
51+
52+
* **LED not blinking:** Check the wiring connection and the IO selection.
53+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
54+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
55+
56+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
57+
58+
## Contribute
59+
60+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
61+
62+
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!
63+
64+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
65+
66+
## Resources
67+
68+
* Official ESP32 Forum: [Link](https://esp32.com)
69+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
70+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
71+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
72+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @brief This example demonstrates Zigbee PM2.5 sensor.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device PM2.5 sensor.
19+
*
20+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
21+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
22+
*
23+
* Please check the README.md for instructions and more detailed description.
24+
*
25+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
26+
*/
27+
28+
#ifndef ZIGBEE_MODE_ED
29+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
30+
#endif
31+
32+
#include "Zigbee.h"
33+
34+
/* Zigbee PM2.5 sensor configuration */
35+
#define PM2_5_SENSOR_ENDPOINT_NUMBER 1
36+
uint8_t button = BOOT_PIN;
37+
38+
ZigbeePM25Sensor zbPM25Sensor = ZigbeePM25Sensor(PM2_5_SENSOR_ENDPOINT_NUMBER);
39+
40+
void setup() {
41+
Serial.begin(115200);
42+
43+
// Init button switch
44+
pinMode(button, INPUT_PULLUP);
45+
46+
// Optional: set Zigbee device name and model
47+
zbPM25Sensor.setManufacturerAndModel("Espressif", "ZigbeePM25Sensor");
48+
49+
// Set minimum and maximum PM2.5 measurement value in µg/m³
50+
zbPM25Sensor.setMinMaxValue(0, 350);
51+
52+
// Set tolerance for PM2.5 measurement in µg/m³
53+
zbPM25Sensor.setTolerance(0.1);
54+
55+
// Add endpoints to Zigbee Core
56+
Zigbee.addEndpoint(&zbPM25Sensor);
57+
58+
Serial.println("Starting Zigbee...");
59+
// When all EPs are registered, start Zigbee in End Device mode
60+
if (!Zigbee.begin()) {
61+
Serial.println("Zigbee failed to start!");
62+
Serial.println("Rebooting...");
63+
ESP.restart();
64+
} else {
65+
Serial.println("Zigbee started successfully!");
66+
}
67+
Serial.println("Connecting to network");
68+
while (!Zigbee.connected()) {
69+
Serial.print(".");
70+
delay(100);
71+
}
72+
Serial.println();
73+
74+
// Set reporting interval for PM2.5 measurement to be done every 30 seconds, must be called after Zigbee.begin()
75+
// min_interval and max_interval in seconds, delta (PM2.5 change in µg/m³)
76+
// if min = 1 and max = 0, reporting is sent only when PM2.5 changes by delta
77+
// if min = 0 and max = 10, reporting is sent every 10 seconds or when PM2.5 changes by delta
78+
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change
79+
zbPM25Sensor.setReporting(0, 30, 0);
80+
}
81+
82+
void loop() {
83+
static uint32_t timeCounter = 0;
84+
// Read PM2.5 sensor every 2s
85+
if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s
86+
// Read sensor value - here is chip temperature used + 50 as a dummy value for demonstration
87+
float pm25_value = 50.5 + temperatureRead();
88+
Serial.printf("Updating PM2.5 sensor value to %0.1f µg/m³\r\n", pm25_value);
89+
zbPM25Sensor.setPM25(pm25_value);
90+
}
91+
92+
// Checking button for factory reset and reporting
93+
if (digitalRead(button) == LOW) { // Push button pressed
94+
// Key debounce handling
95+
delay(100);
96+
int startTime = millis();
97+
while (digitalRead(button) == LOW) {
98+
delay(50);
99+
if ((millis() - startTime) > 3000) {
100+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
101+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
102+
delay(1000);
103+
Zigbee.factoryReset();
104+
}
105+
}
106+
zbPM25Sensor.report();
107+
}
108+
delay(100);
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y",
5+
"CONFIG_ZB_ENABLED=y"
6+
]
7+
}

libraries/Zigbee/src/Zigbee.h

+17-11
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,29 @@
77
#include "ZigbeeEP.h"
88

99
// Endpoints
10-
#include "ep/ZigbeeLight.h"
10+
//// Switches
11+
#include "ep/ZigbeeColorDimmerSwitch.h"
1112
#include "ep/ZigbeeSwitch.h"
12-
#include "ep/ZigbeeDimmableLight.h"
13+
//// Lights
1314
#include "ep/ZigbeeColorDimmableLight.h"
14-
#include "ep/ZigbeeColorDimmerSwitch.h"
15-
#include "ep/ZigbeeTempSensor.h"
15+
#include "ep/ZigbeeDimmableLight.h"
16+
#include "ep/ZigbeeLight.h"
17+
//// Controllers
1618
#include "ep/ZigbeeThermostat.h"
17-
#include "ep/ZigbeePressureSensor.h"
19+
//// Sensors
1820
#include "ep/ZigbeeAnalog.h"
19-
#include "ep/ZigbeeFlowSensor.h"
20-
#include "ep/ZigbeeOccupancySensor.h"
21-
#include "ep/ZigbeeIlluminanceSensor.h"
2221
#include "ep/ZigbeeCarbonDioxideSensor.h"
2322
#include "ep/ZigbeeContactSwitch.h"
2423
#include "ep/ZigbeeDoorWindowHandle.h"
25-
#include "ep/ZigbeeWindowCovering.h"
24+
#include "ep/ZigbeeFlowSensor.h"
25+
#include "ep/ZigbeeIlluminanceSensor.h"
26+
#include "ep/ZigbeeOccupancySensor.h"
27+
#include "ep/ZigbeePM25Sensor.h"
28+
#include "ep/ZigbeePressureSensor.h"
29+
#include "ep/ZigbeeTempSensor.h"
2630
#include "ep/ZigbeeVibrationSensor.h"
27-
#include "ep/ZigbeeRangeExtender.h"
28-
#include "ep/ZigbeeGateway.h"
2931
#include "ep/ZigbeeWindSpeedSensor.h"
32+
#include "ep/ZigbeeWindowCovering.h"
33+
//// Other
34+
#include "ep/ZigbeeGateway.h"
35+
#include "ep/ZigbeeRangeExtender.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "ZigbeePM25Sensor.h"
2+
#if CONFIG_ZB_ENABLED
3+
4+
esp_zb_cluster_list_t *zigbee_pm2_5_sensor_clusters_create(zigbee_pm2_5_sensor_cfg_t *pm2_5_sensor) {
5+
esp_zb_basic_cluster_cfg_t *basic_cfg = pm2_5_sensor ? &(pm2_5_sensor->basic_cfg) : NULL;
6+
esp_zb_identify_cluster_cfg_t *identify_cfg = pm2_5_sensor ? &(pm2_5_sensor->identify_cfg) : NULL;
7+
esp_zb_pm2_5_measurement_cluster_cfg_t *pm2_5_meas_cfg = pm2_5_sensor ? &(pm2_5_sensor->pm2_5_meas_cfg) : NULL;
8+
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
9+
esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
10+
esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
11+
esp_zb_cluster_list_add_pm2_5_measurement_cluster(cluster_list, esp_zb_pm2_5_measurement_cluster_create(pm2_5_meas_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
12+
return cluster_list;
13+
}
14+
15+
ZigbeePM25Sensor::ZigbeePM25Sensor(uint8_t endpoint) : ZigbeeEP(endpoint) {
16+
_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID;
17+
18+
//Create custom PM2.5 sensor configuration
19+
zigbee_pm2_5_sensor_cfg_t pm2_5_sensor_cfg = ZIGBEE_DEFAULT_PM2_5_SENSOR_CONFIG();
20+
_cluster_list = zigbee_pm2_5_sensor_clusters_create(&pm2_5_sensor_cfg);
21+
22+
_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};
23+
}
24+
25+
bool ZigbeePM25Sensor::setMinMaxValue(float min, float max) {
26+
esp_zb_attribute_list_t *pm2_5_measure_cluster =
27+
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_PM2_5_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
28+
esp_err_t ret = esp_zb_cluster_update_attr(pm2_5_measure_cluster, ESP_ZB_ZCL_ATTR_PM2_5_MEASUREMENT_MIN_MEASURED_VALUE_ID, (void *)&min);
29+
if (ret != ESP_OK) {
30+
log_e("Failed to set min value: 0x%x: %s", ret, esp_err_to_name(ret));
31+
return false;
32+
}
33+
ret = esp_zb_cluster_update_attr(pm2_5_measure_cluster, ESP_ZB_ZCL_ATTR_PM2_5_MEASUREMENT_MAX_MEASURED_VALUE_ID, (void *)&max);
34+
if (ret != ESP_OK) {
35+
log_e("Failed to set max value: 0x%x: %s", ret, esp_err_to_name(ret));
36+
return false;
37+
}
38+
return true;
39+
}
40+
41+
bool ZigbeePM25Sensor::setTolerance(float tolerance) {
42+
esp_zb_attribute_list_t *pm2_5_measure_cluster =
43+
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_PM2_5_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
44+
esp_err_t ret = esp_zb_pm2_5_measurement_cluster_add_attr(pm2_5_measure_cluster, ESP_ZB_ZCL_ATTR_PM2_5_MEASUREMENT_TOLERANCE_ID, (void *)&tolerance);
45+
if (ret != ESP_OK) {
46+
log_e("Failed to set tolerance: 0x%x: %s", ret, esp_err_to_name(ret));
47+
return false;
48+
}
49+
return true;
50+
}
51+
52+
bool ZigbeePM25Sensor::setReporting(uint16_t min_interval, uint16_t max_interval, float delta) {
53+
esp_zb_zcl_reporting_info_t reporting_info;
54+
memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t));
55+
reporting_info.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV;
56+
reporting_info.ep = _endpoint;
57+
reporting_info.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_PM2_5_MEASUREMENT;
58+
reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE;
59+
reporting_info.attr_id = ESP_ZB_ZCL_ATTR_PM2_5_MEASUREMENT_MEASURED_VALUE_ID;
60+
reporting_info.u.send_info.min_interval = min_interval;
61+
reporting_info.u.send_info.max_interval = max_interval;
62+
reporting_info.u.send_info.def_min_interval = min_interval;
63+
reporting_info.u.send_info.def_max_interval = max_interval;
64+
// reporting_info.u.send_info.delta.u16 = (uint16_t)(delta * 100); // Convert delta to ZCL uint16_t
65+
reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID;
66+
reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC;
67+
float delta_f = delta;
68+
memcpy(&reporting_info.u.send_info.delta.s32, &delta_f, sizeof(float));
69+
70+
esp_zb_lock_acquire(portMAX_DELAY);
71+
esp_err_t ret = esp_zb_zcl_update_reporting_info(&reporting_info);
72+
esp_zb_lock_release();
73+
if (ret != ESP_OK) {
74+
log_e("Failed to set reporting: 0x%x: %s", ret, esp_err_to_name(ret));
75+
return false;
76+
}
77+
return true;
78+
}
79+
80+
bool ZigbeePM25Sensor::setPM25(float pm25) {
81+
esp_zb_zcl_status_t ret = ESP_ZB_ZCL_STATUS_SUCCESS;
82+
log_v("Updating PM2.5 sensor value...");
83+
/* Update PM2.5 sensor measured value */
84+
log_d("Setting PM2.5 to %0.1f", pm25);
85+
esp_zb_lock_acquire(portMAX_DELAY);
86+
ret = esp_zb_zcl_set_attribute_val(
87+
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_PM2_5_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_PM2_5_MEASUREMENT_MEASURED_VALUE_ID, &pm25, false
88+
);
89+
esp_zb_lock_release();
90+
if (ret != ESP_ZB_ZCL_STATUS_SUCCESS) {
91+
log_e("Failed to set PM2.5: 0x%x: %s", ret, esp_zb_zcl_status_to_name(ret));
92+
return false;
93+
}
94+
return true;
95+
}
96+
97+
bool ZigbeePM25Sensor::report() {
98+
/* Send report attributes command */
99+
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
100+
report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
101+
report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_PM2_5_MEASUREMENT_MEASURED_VALUE_ID;
102+
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI;
103+
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_PM2_5_MEASUREMENT;
104+
report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint;
105+
report_attr_cmd.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC;
106+
107+
esp_zb_lock_acquire(portMAX_DELAY);
108+
esp_err_t ret = esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd);
109+
esp_zb_lock_release();
110+
if (ret != ESP_OK) {
111+
log_e("Failed to send PM2.5 report: 0x%x: %s", ret, esp_err_to_name(ret));
112+
return false;
113+
}
114+
log_v("PM2.5 report sent");
115+
return true;
116+
}
117+
118+
#endif // CONFIG_ZB_ENABLED

0 commit comments

Comments
 (0)