Skip to content

Commit 44e1559

Browse files
committed
feat(zigbee): Add carbon dioxide sensor ed
1 parent ce9a34d commit 44e1559

File tree

9 files changed

+352
-5
lines changed

9 files changed

+352
-5
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
281281
libraries/Zigbee/src/ep/ZigbeeFlowSensor.cpp
282282
libraries/Zigbee/src/ep/ZigbeePressureSensor.cpp
283283
libraries/Zigbee/src/ep/ZigbeeOccupancySensor.cpp
284+
libraries/Zigbee/src/ep/ZigbeeCarbonDioxideSensor.cpp
284285
)
285286

286287
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+
}

libraries/Zigbee/examples/Zigbee_Occupancy_Sensor/Zigbee_Occupancy_Sensor.ino

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ uint8_t sensor_pin = 4;
4040

4141
ZigbeeOccupancySensor zbOccupancySensor = ZigbeeOccupancySensor(OCCUPANCY_SENSOR_ENDPOINT_NUMBER);
4242

43-
/********************* Arduino functions **************************/
4443
void setup() {
4544
Serial.begin(115200);
4645

libraries/Zigbee/examples/Zigbee_Pressure_Flow_Sensor/Zigbee_Pressure_Flow_Sensor.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void setup() {
5353
// Set minimum and maximum flow measurement value in 0,1 m3/h
5454
zbFlowSensor.setMinMaxValue(0.0, 100.0);
5555

56-
// Set tolerance for flow measurement in 0,1 m3/h
56+
// Optional: Set tolerance for flow measurement in 0,1 m3/h
5757
zbFlowSensor.setTolerance(1.0);
5858

5959
// Optional: set Zigbee device name and model
@@ -62,7 +62,7 @@ void setup() {
6262
// Set minimum and maximum pressure measurement value in hPa
6363
zbPressureSensor.setMinMaxValue(0, 10000);
6464

65-
// Set tolerance for temperature measurement in hPa
65+
// Optional: Set tolerance for pressure measurement in hPa
6666
zbPressureSensor.setTolerance(1);
6767

6868
// Add endpoints to Zigbee Core
@@ -97,7 +97,7 @@ void setup() {
9797
void loop() {
9898
static uint32_t timeCounter = 0;
9999

100-
// Read flow nad pressure sensors every 2s
100+
// Read flow and pressure sensors every 2s
101101
if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s
102102
float flow_value = temperatureRead();
103103
uint16_t pressure_value = (uint16_t)temperatureRead()*100; //*100 for demonstration so the value is in 1000-3000hPa
@@ -107,7 +107,7 @@ void loop() {
107107
zbPressureSensor.setPressure(pressure_value);
108108
}
109109

110-
// Checking button for factory reset
110+
// Checking button for factory reset and reporting
111111
if (digitalRead(button) == LOW) { // Push button pressed
112112
// Key debounce handling
113113
delay(100);

libraries/Zigbee/src/Zigbee.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
#include "ep/ZigbeePressureSensor.h"
1717
#include "ep/ZigbeeFlowSensor.h"
1818
#include "ep/ZigbeeOccupancySensor.h"
19+
#include "ep/ZigbeeCarbonDioxideSensor.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "ZigbeeCarbonDioxideSensor.h"
2+
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
3+
4+
esp_zb_cluster_list_t *zigbee_carbon_dioxide_sensor_clusters_create(zigbee_carbon_dioxide_sensor_cfg_t *carbon_dioxide_sensor)
5+
{
6+
esp_zb_basic_cluster_cfg_t *basic_cfg = carbon_dioxide_sensor ? &(carbon_dioxide_sensor->basic_cfg) : NULL;
7+
esp_zb_identify_cluster_cfg_t *identify_cfg = carbon_dioxide_sensor ? &(carbon_dioxide_sensor->identify_cfg) : NULL;
8+
esp_zb_carbon_dioxide_measurement_cluster_cfg_t *carbon_dioxide_meas_cfg = carbon_dioxide_sensor ? &(carbon_dioxide_sensor->carbon_dioxide_meas_cfg) : NULL;
9+
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
10+
esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
11+
esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
12+
esp_zb_cluster_list_add_carbon_dioxide_measurement_cluster(cluster_list, esp_zb_carbon_dioxide_measurement_cluster_create(carbon_dioxide_meas_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
13+
esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
14+
return cluster_list;
15+
}
16+
17+
ZigbeeCarbonDioxideSensor::ZigbeeCarbonDioxideSensor(uint8_t endpoint) : ZigbeeEP(endpoint) {
18+
_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID;
19+
20+
//Create custom pressure sensor configuration
21+
zigbee_carbon_dioxide_sensor_cfg_t carbon_dioxide_sensor_cfg = ZIGBEE_DEFAULT_CARBON_DIOXIDE_SENSOR_CONFIG();
22+
_cluster_list = zigbee_carbon_dioxide_sensor_clusters_create(&carbon_dioxide_sensor_cfg);
23+
24+
_ep_config = {
25+
.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
26+
};
27+
}
28+
29+
void ZigbeeCarbonDioxideSensor::setMinMaxValue(float min, float max) {
30+
float zb_min = min/1000000.0f;
31+
float zb_max = max/1000000.0f;
32+
esp_zb_attribute_list_t *carbon_dioxide_measure_cluster =
33+
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_CARBON_DIOXIDE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
34+
esp_zb_cluster_update_attr(carbon_dioxide_measure_cluster, ESP_ZB_ZCL_ATTR_CARBON_DIOXIDE_MEASUREMENT_MIN_MEASURED_VALUE_ID, (void *)&zb_min);
35+
esp_zb_cluster_update_attr(carbon_dioxide_measure_cluster, ESP_ZB_ZCL_ATTR_CARBON_DIOXIDE_MEASUREMENT_MAX_MEASURED_VALUE_ID, (void *)&zb_max);
36+
}
37+
38+
void ZigbeeCarbonDioxideSensor::setTolerance(float tolerance) {
39+
float zb_tolerance = tolerance/1000000.0f;
40+
esp_zb_attribute_list_t *carbon_dioxide_measure_cluster =
41+
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_CARBON_DIOXIDE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
42+
esp_zb_temperature_meas_cluster_add_attr(carbon_dioxide_measure_cluster, ESP_ZB_ZCL_ATTR_CARBON_DIOXIDE_MEASUREMENT_TOLERANCE_ID, (void *)&zb_tolerance);
43+
}
44+
45+
void ZigbeeCarbonDioxideSensor::setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta) {
46+
esp_zb_zcl_reporting_info_t reporting_info = {
47+
.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV,
48+
.ep = _endpoint,
49+
.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_CARBON_DIOXIDE_MEASUREMENT,
50+
.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
51+
.attr_id = ESP_ZB_ZCL_ATTR_CARBON_DIOXIDE_MEASUREMENT_MEASURED_VALUE_ID,
52+
.u =
53+
{
54+
.send_info =
55+
{
56+
.min_interval = min_interval,
57+
.max_interval = max_interval,
58+
.delta =
59+
{
60+
.u16 = delta, // x ppm
61+
},
62+
.def_min_interval = min_interval,
63+
.def_max_interval = max_interval,
64+
},
65+
},
66+
.dst =
67+
{
68+
.profile_id = ESP_ZB_AF_HA_PROFILE_ID,
69+
},
70+
.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC,
71+
};
72+
esp_zb_lock_acquire(portMAX_DELAY);
73+
esp_zb_zcl_update_reporting_info(&reporting_info);
74+
esp_zb_lock_release();
75+
}
76+
77+
void ZigbeeCarbonDioxideSensor::setCarbonDioxide(float carbon_dioxide) {
78+
float zb_carbon_dioxide = carbon_dioxide/1000000.0f;
79+
log_v("Updating carbon dioxide sensor value...");
80+
/* Update carbon dioxide sensor measured value */
81+
log_d("Setting carbon dioxide to %0.1f", carbon_dioxide);
82+
esp_zb_lock_acquire(portMAX_DELAY);
83+
esp_zb_zcl_set_attribute_val(
84+
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_CARBON_DIOXIDE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_CARBON_DIOXIDE_MEASUREMENT_MEASURED_VALUE_ID, &zb_carbon_dioxide, false
85+
);
86+
esp_zb_lock_release();
87+
}
88+
89+
void ZigbeeCarbonDioxideSensor::report() {
90+
/* Send report attributes command */
91+
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
92+
report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
93+
report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_CARBON_DIOXIDE_MEASUREMENT_MEASURED_VALUE_ID;
94+
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI;
95+
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_CARBON_DIOXIDE_MEASUREMENT;
96+
report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint;
97+
98+
esp_zb_lock_acquire(portMAX_DELAY);
99+
esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd);
100+
esp_zb_lock_release();
101+
log_v("Carbon dioxide report sent");
102+
}
103+
104+
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

0 commit comments

Comments
 (0)