Skip to content

Commit 417c7ee

Browse files
authored
Merge pull request #10720 from P-R-O-C-H-Y/feat/zigbee-multi-sensor
feat(zigbee): Add pressure, flow, ccupancy and carbon dioxide sensor
2 parents 7dc1c92 + b6d0553 commit 417c7ee

26 files changed

+1222
-15
lines changed

Diff for: CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
279279
libraries/Zigbee/src/ep/ZigbeeSwitch.cpp
280280
libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp
281281
libraries/Zigbee/src/ep/ZigbeeThermostat.cpp
282+
libraries/Zigbee/src/ep/ZigbeeFlowSensor.cpp
283+
libraries/Zigbee/src/ep/ZigbeePressureSensor.cpp
284+
libraries/Zigbee/src/ep/ZigbeeOccupancySensor.cpp
285+
libraries/Zigbee/src/ep/ZigbeeCarbonDioxideSensor.cpp
282286
)
283287

284288
set(ARDUINO_LIBRARY_BLE_SRCS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Arduino-ESP32 Carbon dioxide (CO2) 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 carbon dioxide 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 carbon dioxide on every 30 seconds.
15+
* By clicking the button (BOOT) on this board, this board will immediately send a report of the current measured carbon dioxide 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 carbon dioxide 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,106 @@
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 carbon dioxide sensor.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device carbon dioxide 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 carbon dioxide sensor configuration */
35+
#define CARBON_DIOXIDE_SENSOR_ENDPOINT_NUMBER 10
36+
uint8_t button = BOOT_PIN;
37+
38+
ZigbeeCarbonDioxideSensor zbCarbonDioxideSensor = ZigbeeCarbonDioxideSensor(CARBON_DIOXIDE_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+
zbCarbonDioxideSensor.setManufacturerAndModel("Espressif", "ZigbeeCarbonDioxideSensor");
48+
49+
// Set minimum and maximum carbon dioxide measurement value in ppm
50+
zbCarbonDioxideSensor.setMinMaxValue(0, 1500);
51+
52+
// Add endpoints to Zigbee Core
53+
Zigbee.addEndpoint(&zbCarbonDioxideSensor);
54+
55+
Serial.println("Starting Zigbee...");
56+
// When all EPs are registered, start Zigbee in End Device mode
57+
if (!Zigbee.begin()) {
58+
Serial.println("Zigbee failed to start!");
59+
Serial.println("Rebooting...");
60+
ESP.restart();
61+
} else {
62+
Serial.println("Zigbee started successfully!");
63+
}
64+
Serial.println("Connecting to network");
65+
while (!Zigbee.connected()) {
66+
Serial.print(".");
67+
delay(100);
68+
}
69+
Serial.println();
70+
71+
// Set reporting interval for carbon dioxide measurement to be done every 30 seconds, must be called after Zigbee.begin()
72+
// min_interval and max_interval in seconds, delta (carbon dioxide change in ppm)
73+
// if min = 1 and max = 0, reporting is sent only when carbon dioxide changes by delta
74+
// if min = 0 and max = 10, reporting is sent every 10 seconds or when carbon dioxide changes by delta
75+
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change
76+
zbCarbonDioxideSensor.setReporting(0, 30, 0);
77+
}
78+
79+
void loop() {
80+
static uint32_t timeCounter = 0;
81+
// Read carbon dioxide sensor every 2s
82+
if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s
83+
// Read sensor value - here is chip temperature used + 300 as a dummy value for demonstration
84+
uint16_t carbon_dioxide_value = 300 + (uint16_t)temperatureRead();
85+
Serial.printf("Updating carbon dioxide sensor value to %d ppm\r\n", carbon_dioxide_value);
86+
zbCarbonDioxideSensor.setCarbonDioxide(carbon_dioxide_value);
87+
}
88+
89+
// Checking button for factory reset and reporting
90+
if (digitalRead(button) == LOW) { // Push button pressed
91+
// Key debounce handling
92+
delay(100);
93+
int startTime = millis();
94+
while (digitalRead(button) == LOW) {
95+
delay(50);
96+
if ((millis() - startTime) > 3000) {
97+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
98+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
99+
delay(1000);
100+
Zigbee.factoryReset();
101+
}
102+
}
103+
zbCarbonDioxideSensor.report();
104+
}
105+
delay(100);
106+
}
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,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,103 @@
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+
void setup() {
44+
Serial.begin(115200);
45+
46+
// Init button + PIR sensor
47+
pinMode(button, INPUT_PULLUP);
48+
pinMode(sensor_pin, INPUT);
49+
50+
// Optional: set Zigbee device name and model
51+
zbOccupancySensor.setManufacturerAndModel("Espressif", "ZigbeeOccupancyPIRSensor");
52+
53+
// Add endpoint to Zigbee Core
54+
Zigbee.addEndpoint(&zbOccupancySensor);
55+
56+
Serial.println("Starting Zigbee...");
57+
// When all EPs are registered, start Zigbee in End Device mode
58+
if (!Zigbee.begin()) {
59+
Serial.println("Zigbee failed to start!");
60+
Serial.println("Rebooting...");
61+
ESP.restart();
62+
} else {
63+
Serial.println("Zigbee started successfully!");
64+
}
65+
Serial.println("Connecting to network");
66+
while (!Zigbee.connected()) {
67+
Serial.print(".");
68+
delay(100);
69+
}
70+
Serial.println();
71+
}
72+
73+
void loop() {
74+
// Checking PIR sensor for occupancy change
75+
static bool occupancy = false;
76+
if (digitalRead(sensor_pin) == HIGH && !occupancy) {
77+
// Update occupancy sensor value
78+
zbOccupancySensor.setOccupancy(true);
79+
zbOccupancySensor.report();
80+
occupancy = true;
81+
} else if (digitalRead(sensor_pin) == LOW && occupancy) {
82+
zbOccupancySensor.setOccupancy(false);
83+
zbOccupancySensor.report();
84+
occupancy = false;
85+
}
86+
87+
// Checking button for factory reset
88+
if (digitalRead(button) == LOW) { // Push button pressed
89+
// Key debounce handling
90+
delay(100);
91+
int startTime = millis();
92+
while (digitalRead(button) == LOW) {
93+
delay(50);
94+
if ((millis() - startTime) > 3000) {
95+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
96+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
97+
delay(1000);
98+
Zigbee.factoryReset();
99+
}
100+
}
101+
}
102+
delay(100);
103+
}
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+
}

0 commit comments

Comments
 (0)