-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat(zigbee): Add PM2.5 endpoint support #11205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Arduino-ESP32 PM2.5 Sensor | ||
|
||
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 | ||
|
||
# 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 PM2.5 on every 30 seconds. | ||
* By clicking the button (BOOT) on this board, this board will immediately send a report of the current PM2.5 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 PM2.5 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) |
109 changes: 109 additions & 0 deletions
109
libraries/Zigbee/examples/Zigbee_PM25_Sensor/Zigbee_PM25_Sensor.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// 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 PM2.5 sensor. | ||
* | ||
* The example demonstrates how to use Zigbee library to create a end device PM2.5 sensor. | ||
* | ||
* Proper Zigbee mode must be selected in Tools->Zigbee mode | ||
* and also the correct partition scheme must be selected in Tools->Partition Scheme. | ||
* | ||
* Please check the README.md for instructions and more detailed description. | ||
* | ||
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) | ||
*/ | ||
|
||
#ifndef ZIGBEE_MODE_ED | ||
#error "Zigbee end device mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "Zigbee.h" | ||
|
||
/* Zigbee PM2.5 sensor configuration */ | ||
#define PM2_5_SENSOR_ENDPOINT_NUMBER 1 | ||
uint8_t button = BOOT_PIN; | ||
|
||
ZigbeePM25Sensor zbPM25Sensor = ZigbeePM25Sensor(PM2_5_SENSOR_ENDPOINT_NUMBER); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
// Init button switch | ||
pinMode(button, INPUT_PULLUP); | ||
|
||
// Optional: set Zigbee device name and model | ||
zbPM25Sensor.setManufacturerAndModel("Espressif", "ZigbeePM25Sensor"); | ||
|
||
// Set minimum and maximum PM2.5 measurement value in µg/m³ | ||
zbPM25Sensor.setMinMaxValue(0, 350); | ||
|
||
// Set tolerance for PM2.5 measurement in µg/m³ | ||
zbPM25Sensor.setTolerance(0.1); | ||
|
||
// Add endpoints to Zigbee Core | ||
Zigbee.addEndpoint(&zbPM25Sensor); | ||
|
||
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(); | ||
|
||
// Set reporting interval for PM2.5 measurement to be done every 30 seconds, must be called after Zigbee.begin() | ||
// min_interval and max_interval in seconds, delta (PM2.5 change in µg/m³) | ||
// if min = 1 and max = 0, reporting is sent only when PM2.5 changes by delta | ||
// if min = 0 and max = 10, reporting is sent every 10 seconds or when PM2.5 changes by delta | ||
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change | ||
zbPM25Sensor.setReporting(0, 30, 0); | ||
} | ||
|
||
void loop() { | ||
static uint32_t timeCounter = 0; | ||
// Read PM2.5 sensor every 2s | ||
if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s | ||
// Read sensor value - here is chip temperature used + 50 as a dummy value for demonstration | ||
float pm25_value = 50.5 + temperatureRead(); | ||
Serial.printf("Updating PM2.5 sensor value to %0.1f µg/m³\r\n", pm25_value); | ||
zbPM25Sensor.setPM25(pm25_value); | ||
} | ||
|
||
// Checking button for factory reset and reporting | ||
if (digitalRead(button) == LOW) { // Push button pressed | ||
// Key debounce handling | ||
delay(100); | ||
int startTime = millis(); | ||
while (digitalRead(button) == LOW) { | ||
delay(50); | ||
if ((millis() - startTime) > 3000) { | ||
// If key pressed for more than 3secs, factory reset Zigbee and reboot | ||
Serial.println("Resetting Zigbee to factory and rebooting in 1s."); | ||
delay(1000); | ||
Zigbee.factoryReset(); | ||
} | ||
} | ||
zbPM25Sensor.report(); | ||
} | ||
delay(100); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed", | ||
"requires": [ | ||
"CONFIG_SOC_IEEE802154_SUPPORTED=y", | ||
"CONFIG_ZB_ENABLED=y" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include "ZigbeePM25Sensor.h" | ||
#if CONFIG_ZB_ENABLED | ||
|
||
esp_zb_cluster_list_t *zigbee_pm2_5_sensor_clusters_create(zigbee_pm2_5_sensor_cfg_t *pm2_5_sensor) { | ||
esp_zb_basic_cluster_cfg_t *basic_cfg = pm2_5_sensor ? &(pm2_5_sensor->basic_cfg) : NULL; | ||
esp_zb_identify_cluster_cfg_t *identify_cfg = pm2_5_sensor ? &(pm2_5_sensor->identify_cfg) : NULL; | ||
esp_zb_pm2_5_measurement_cluster_cfg_t *pm2_5_meas_cfg = pm2_5_sensor ? &(pm2_5_sensor->pm2_5_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_pm2_5_measurement_cluster( | ||
cluster_list, esp_zb_pm2_5_measurement_cluster_create(pm2_5_meas_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE | ||
); | ||
return cluster_list; | ||
} | ||
|
||
ZigbeePM25Sensor::ZigbeePM25Sensor(uint8_t endpoint) : ZigbeeEP(endpoint) { | ||
_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID; | ||
|
||
//Create custom PM2.5 sensor configuration | ||
zigbee_pm2_5_sensor_cfg_t pm2_5_sensor_cfg = ZIGBEE_DEFAULT_PM2_5_SENSOR_CONFIG(); | ||
_cluster_list = zigbee_pm2_5_sensor_clusters_create(&pm2_5_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}; | ||
} | ||
|
||
bool ZigbeePM25Sensor::setMinMaxValue(float min, float max) { | ||
esp_zb_attribute_list_t *pm2_5_measure_cluster = | ||
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_PM2_5_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); | ||
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); | ||
if (ret != ESP_OK) { | ||
log_e("Failed to set min value: 0x%x: %s", ret, esp_err_to_name(ret)); | ||
return false; | ||
} | ||
ret = esp_zb_cluster_update_attr(pm2_5_measure_cluster, ESP_ZB_ZCL_ATTR_PM2_5_MEASUREMENT_MAX_MEASURED_VALUE_ID, (void *)&max); | ||
if (ret != ESP_OK) { | ||
log_e("Failed to set max value: 0x%x: %s", ret, esp_err_to_name(ret)); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool ZigbeePM25Sensor::setTolerance(float tolerance) { | ||
esp_zb_attribute_list_t *pm2_5_measure_cluster = | ||
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_PM2_5_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); | ||
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 | ||
); | ||
if (ret != ESP_OK) { | ||
log_e("Failed to set tolerance: 0x%x: %s", ret, esp_err_to_name(ret)); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool ZigbeePM25Sensor::setReporting(uint16_t min_interval, uint16_t max_interval, float delta) { | ||
esp_zb_zcl_reporting_info_t reporting_info; | ||
memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t)); | ||
reporting_info.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV; | ||
reporting_info.ep = _endpoint; | ||
reporting_info.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_PM2_5_MEASUREMENT; | ||
reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE; | ||
reporting_info.attr_id = ESP_ZB_ZCL_ATTR_PM2_5_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 * 100); // Convert delta to ZCL uint16_t | ||
reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID; | ||
reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC; | ||
float delta_f = delta; | ||
memcpy(&reporting_info.u.send_info.delta.s32, &delta_f, sizeof(float)); | ||
|
||
esp_zb_lock_acquire(portMAX_DELAY); | ||
esp_err_t ret = esp_zb_zcl_update_reporting_info(&reporting_info); | ||
esp_zb_lock_release(); | ||
if (ret != ESP_OK) { | ||
log_e("Failed to set reporting: 0x%x: %s", ret, esp_err_to_name(ret)); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool ZigbeePM25Sensor::setPM25(float pm25) { | ||
esp_zb_zcl_status_t ret = ESP_ZB_ZCL_STATUS_SUCCESS; | ||
log_v("Updating PM2.5 sensor value..."); | ||
/* Update PM2.5 sensor measured value */ | ||
log_d("Setting PM2.5 to %0.1f", pm25); | ||
esp_zb_lock_acquire(portMAX_DELAY); | ||
ret = esp_zb_zcl_set_attribute_val( | ||
_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 | ||
); | ||
esp_zb_lock_release(); | ||
if (ret != ESP_ZB_ZCL_STATUS_SUCCESS) { | ||
log_e("Failed to set PM2.5: 0x%x: %s", ret, esp_zb_zcl_status_to_name(ret)); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool ZigbeePM25Sensor::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_PM2_5_MEASUREMENT_MEASURED_VALUE_ID; | ||
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI; | ||
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_PM2_5_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_err_t ret = esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd); | ||
esp_zb_lock_release(); | ||
if (ret != ESP_OK) { | ||
log_e("Failed to send PM2.5 report: 0x%x: %s", ret, esp_err_to_name(ret)); | ||
return false; | ||
} | ||
log_v("PM2.5 report sent"); | ||
return true; | ||
} | ||
|
||
#endif // CONFIG_ZB_ENABLED |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using memcpy to assign a float value into an integer field (s32) may lead to unintended behavior; consider applying an explicit conversion or scaling as suggested by the commented-out code to properly format the delta value.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.