-
Notifications
You must be signed in to change notification settings - Fork 7.7k
feat(zigbee): Add pressure, flow, ccupancy and carbon dioxide sensor #10720
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
me-no-dev
merged 12 commits into
espressif:release/v3.1.x
from
P-R-O-C-H-Y:feat/zigbee-multi-sensor
Dec 14, 2024
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
77df256
feat(zigbee): Add pressure, flow and occupancy sensor
P-R-O-C-H-Y aef691e
fix(example): use correct print format
P-R-O-C-H-Y b4d7227
fix(zigbee): Fix functions comment
P-R-O-C-H-Y 5e7139b
fix(cmake): Update CMakeLists.txt
P-R-O-C-H-Y b64b850
feat(zigbee): Fill the keywords.txt
P-R-O-C-H-Y ce9a34d
fix(zigbee): Update example + pressure unit
P-R-O-C-H-Y 44e1559
feat(zigbee): Add carbon dioxide sensor ed
P-R-O-C-H-Y b72e866
fix(zigbee): mention Carbon dioxide delta reporting is not supported
P-R-O-C-H-Y 57339ba
feat(zigbee): Update keywords.txt with co2 sensor
P-R-O-C-H-Y a7e2e64
Apply suggestions from code review
P-R-O-C-H-Y 6bc971c
fix(zigbee): fix formatting of defines
P-R-O-C-H-Y b6d0553
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] 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
57 changes: 57 additions & 0 deletions
57
libraries/Zigbee/examples/Zigbee_Occupancy_Sensor/README.md
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,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) |
104 changes: 104 additions & 0 deletions
104
libraries/Zigbee/examples/Zigbee_Occupancy_Sensor/Zigbee_Occupancy_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,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); | ||
} |
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,6 @@ | ||
{ | ||
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed", | ||
"requires": [ | ||
"CONFIG_SOC_IEEE802154_SUPPORTED=y" | ||
] | ||
} |
72 changes: 72 additions & 0 deletions
72
libraries/Zigbee/examples/Zigbee_Pressure_Flow_Sensor/README.md
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 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) |
134 changes: 134 additions & 0 deletions
134
libraries/Zigbee/examples/Zigbee_Pressure_Flow_Sensor/Zigbee_Pressure_Flow_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,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(); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint16_t pressure_value = (uint16_t)temperatureRead()*100; //*100 for demonstration so the value is in 1-3hPa | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Serial.printf("Updating flow sensor value to %.2f\r\n", flow_value); | ||
zbFlowSensor.setFlow(flow_value); | ||
Serial.printf("Updating pressure sensor value to %d\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); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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) { | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} |
6 changes: 6 additions & 0 deletions
6
libraries/Zigbee/examples/Zigbee_Pressure_Flow_Sensor/ci.json
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,6 @@ | ||
{ | ||
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed", | ||
"requires": [ | ||
"CONFIG_SOC_IEEE802154_SUPPORTED=y" | ||
] | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.