Skip to content

Commit 77df256

Browse files
committed
feat(zigbee): Add pressure, flow and occupancy sensor
1 parent a4f96e9 commit 77df256

19 files changed

+844
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Arduino-ESP32 Zigbee Occupancy Sensor Example
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) occupancy sensor (PIR).
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## Hardware Required
13+
14+
* A USB cable for power supply and programming
15+
16+
### Configure the Project
17+
18+
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).
19+
Set the Sensor GPIO by changing the `sensor_pin` variable.
20+
21+
#### Using Arduino IDE
22+
23+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
24+
25+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
26+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
27+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
28+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
29+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
30+
31+
## Troubleshooting
32+
33+
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.
34+
35+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
36+
37+
* **LED not blinking:** Check the wiring connection and the IO selection.
38+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
39+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
40+
41+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
42+
43+
## Contribute
44+
45+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
46+
47+
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!
48+
49+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
50+
51+
## Resources
52+
53+
* Official ESP32 Forum: [Link](https://esp32.com)
54+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
55+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
56+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
57+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 occupancy sensor.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device occupancy sensor.
19+
* The occupancy sensor is a Zigbee end device, which is reporting data to the Zigbee network.
20+
* Tested with PIR sensor HC-SR501 connected to GPIO4.
21+
*
22+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
23+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
24+
*
25+
* Please check the README.md for instructions and more detailed description.
26+
*
27+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
28+
*/
29+
30+
#ifndef ZIGBEE_MODE_ED
31+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
32+
#endif
33+
34+
#include "Zigbee.h"
35+
36+
/* Zigbee occupancy sensor configuration */
37+
#define OCCUPANCY_SENSOR_ENDPOINT_NUMBER 10
38+
uint8_t button = BOOT_PIN;
39+
uint8_t sensor_pin = 4;
40+
41+
ZigbeeOccupancySensor zbOccupancySensor = ZigbeeOccupancySensor(OCCUPANCY_SENSOR_ENDPOINT_NUMBER);
42+
43+
/********************* Arduino functions **************************/
44+
void setup() {
45+
Serial.begin(115200);
46+
47+
// Init button + PIR sensor
48+
pinMode(button, INPUT_PULLUP);
49+
pinMode(sensor_pin, INPUT);
50+
51+
// Optional: set Zigbee device name and model
52+
zbOccupancySensor.setManufacturerAndModel("Espressif", "ZigbeeOccupancyPIRSensor");
53+
54+
// Add endpoint to Zigbee Core
55+
Zigbee.addEndpoint(&zbOccupancySensor);
56+
57+
Serial.println("Starting Zigbee...");
58+
// When all EPs are registered, start Zigbee in End Device mode
59+
if (!Zigbee.begin()) {
60+
Serial.println("Zigbee failed to start!");
61+
Serial.println("Rebooting...");
62+
ESP.restart();
63+
} else {
64+
Serial.println("Zigbee started successfully!");
65+
}
66+
Serial.println("Connecting to network");
67+
while (!Zigbee.connected()) {
68+
Serial.print(".");
69+
delay(100);
70+
}
71+
Serial.println();
72+
}
73+
74+
void loop() {
75+
// Checking PIR sensor for occupancy change
76+
static bool occupancy = false;
77+
if(digitalRead(sensor_pin) == HIGH && !occupancy) {
78+
// Update occupancy sensor value
79+
zbOccupancySensor.setOccupancy(true);
80+
zbOccupancySensor.report();
81+
occupancy = true;
82+
} else if (digitalRead(sensor_pin) == LOW && occupancy) {
83+
zbOccupancySensor.setOccupancy(false);
84+
zbOccupancySensor.report();
85+
occupancy = false;
86+
}
87+
88+
// Checking button for factory reset
89+
if (digitalRead(button) == LOW) { // Push button pressed
90+
// Key debounce handling
91+
delay(100);
92+
int startTime = millis();
93+
while (digitalRead(button) == LOW) {
94+
delay(50);
95+
if ((millis() - startTime) > 3000) {
96+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
97+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
98+
delay(1000);
99+
Zigbee.factoryReset();
100+
}
101+
}
102+
}
103+
delay(100);
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
5+
]
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Arduino-ESP32 Zigbee Pressure + Flow Sensor Example
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 pressure and flow 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 pressure and flow on change or every 30 seconds.
15+
* 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.
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 flow and pressure 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,134 @@
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 temperature sensor.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device temperature sensor.
19+
* The temperature sensor is a Zigbee end device, which is controlled by a Zigbee coordinator.
20+
*
21+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
22+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
23+
*
24+
* Please check the README.md for instructions and more detailed description.
25+
*
26+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
27+
*/
28+
29+
#ifndef ZIGBEE_MODE_ED
30+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
31+
#endif
32+
33+
#include "Zigbee.h"
34+
35+
/* Zigbee flow + pressure sensor configuration */
36+
#define FLOW_SENSOR_ENDPOINT_NUMBER 10
37+
#define PRESSURE_SENSOR_ENDPOINT_NUMBER 11
38+
39+
uint8_t button = BOOT_PIN;
40+
41+
ZigbeeFlowSensor zbFlowSensor = ZigbeeFlowSensor(FLOW_SENSOR_ENDPOINT_NUMBER);
42+
ZigbeePressureSensor zbPressureSensor = ZigbeePressureSensor(PRESSURE_SENSOR_ENDPOINT_NUMBER);
43+
44+
/************************ Temp sensor *****************************/
45+
static void sensors_reading(void *arg) {
46+
for (;;) {
47+
// Read Pressure and Flow sensors value - here is chip temperature used as a dummy value for demonstration
48+
float flow_value = temperatureRead();
49+
uint16_t pressure_value = (uint16_t)temperatureRead()*100; //*100 for demonstration so the value is in 1-3hPa
50+
Serial.printf("Updating flow sensor value to %.2f\r\n", flow_value);
51+
zbFlowSensor.setFlow(flow_value);
52+
Serial.printf("Updating pressure sensor value to %.2f\r\n", pressure_value);
53+
zbPressureSensor.setPressure(pressure_value);
54+
delay(1000);
55+
}
56+
}
57+
58+
/********************* Arduino functions **************************/
59+
void setup() {
60+
Serial.begin(115200);
61+
62+
// Init button switch
63+
pinMode(button, INPUT_PULLUP);
64+
65+
// Optional: set Zigbee device name and model
66+
zbFlowSensor.setManufacturerAndModel("Espressif", "ZigbeeFlowSensor");
67+
68+
// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
69+
zbFlowSensor.setMinMaxValue(0, 100);
70+
71+
// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
72+
zbFlowSensor.setTolerance(1);
73+
74+
// Optional: set Zigbee device name and model
75+
zbPressureSensor.setManufacturerAndModel("Espressif", "ZigbeePressureSensor");
76+
77+
// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
78+
zbPressureSensor.setMinMaxValue(0, 30);
79+
80+
// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
81+
zbPressureSensor.setTolerance(1);
82+
83+
// Add endpoints to Zigbee Core
84+
Zigbee.addEndpoint(&zbFlowSensor);
85+
Zigbee.addEndpoint(&zbPressureSensor);
86+
87+
Serial.println("Starting Zigbee...");
88+
// When all EPs are registered, start Zigbee in End Device mode
89+
if (!Zigbee.begin()) {
90+
Serial.println("Zigbee failed to start!");
91+
Serial.println("Rebooting...");
92+
ESP.restart();
93+
} else {
94+
Serial.println("Zigbee started successfully!");
95+
}
96+
Serial.println("Connecting to network");
97+
while (!Zigbee.connected()) {
98+
Serial.print(".");
99+
delay(100);
100+
}
101+
Serial.println();
102+
103+
// Start Flow and Pressure sensor reading task
104+
xTaskCreate(sensors_reading, "flow_pressure_sensors_read", 2048, NULL, 10, NULL);
105+
106+
// Set reporting interval for flow and pressure measurement in seconds, must be called after Zigbee.begin()
107+
// min_interval and max_interval in seconds, delta (pressure change in Pa, flow change in 0,1 m3/h)
108+
// if min = 1 and max = 0, reporting is sent only when temperature changes by delta
109+
// if min = 0 and max = 10, reporting is sent every 10 seconds or temperature changes by delta
110+
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change
111+
zbFlowSensor.setReporting(0, 30, 1);
112+
zbPressureSensor.setReporting(0, 30, 1);
113+
}
114+
115+
void loop() {
116+
// Checking button for factory reset
117+
if (digitalRead(button) == LOW) { // Push button pressed
118+
// Key debounce handling
119+
delay(100);
120+
int startTime = millis();
121+
while (digitalRead(button) == LOW) {
122+
delay(50);
123+
if ((millis() - startTime) > 3000) {
124+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
125+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
126+
delay(1000);
127+
Zigbee.factoryReset();
128+
}
129+
}
130+
zbFlowSensor.report();
131+
zbPressureSensor.report();
132+
}
133+
delay(100);
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
5+
]
6+
}

libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Currently, this example supports the following targets.
2424
### Configure the Project
2525

2626
In this example, to demonstrate the functionality the chip temperature is used and reported as temperature and humidity.
27-
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).
27+
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).
2828

2929
#### Using Arduino IDE
3030

0 commit comments

Comments
 (0)