Skip to content

Commit 129f8fe

Browse files
authored
Merge branch 'master' into log-redirect
2 parents f4c21dd + b333bf2 commit 129f8fe

File tree

9 files changed

+424
-34
lines changed

9 files changed

+424
-34
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
300300
libraries/Zigbee/src/ep/ZigbeeGateway.cpp
301301
libraries/Zigbee/src/ep/ZigbeeWindSpeedSensor.cpp
302302
libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp
303+
libraries/Zigbee/src/ep/ZigbeePM25Sensor.cpp
303304
)
304305

305306
set(ARDUINO_LIBRARY_BLE_SRCS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Arduino-ESP32 PM2.5 Sensor
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 particulate matter (PM2.5) 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 PM2.5 on every 30 seconds.
15+
* By clicking the button (BOOT) on this board, this board will immediately send a report of the current PM2.5 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 PM2.5 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,109 @@
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 PM2.5 sensor.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device PM2.5 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 PM2.5 sensor configuration */
35+
#define PM2_5_SENSOR_ENDPOINT_NUMBER 1
36+
uint8_t button = BOOT_PIN;
37+
38+
ZigbeePM25Sensor zbPM25Sensor = ZigbeePM25Sensor(PM2_5_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+
zbPM25Sensor.setManufacturerAndModel("Espressif", "ZigbeePM25Sensor");
48+
49+
// Set minimum and maximum PM2.5 measurement value in µg/m³
50+
zbPM25Sensor.setMinMaxValue(0, 350);
51+
52+
// Set tolerance for PM2.5 measurement in µg/m³
53+
zbPM25Sensor.setTolerance(0.1);
54+
55+
// Add endpoints to Zigbee Core
56+
Zigbee.addEndpoint(&zbPM25Sensor);
57+
58+
Serial.println("Starting Zigbee...");
59+
// When all EPs are registered, start Zigbee in End Device mode
60+
if (!Zigbee.begin()) {
61+
Serial.println("Zigbee failed to start!");
62+
Serial.println("Rebooting...");
63+
ESP.restart();
64+
} else {
65+
Serial.println("Zigbee started successfully!");
66+
}
67+
Serial.println("Connecting to network");
68+
while (!Zigbee.connected()) {
69+
Serial.print(".");
70+
delay(100);
71+
}
72+
Serial.println();
73+
74+
// Set reporting interval for PM2.5 measurement to be done every 30 seconds, must be called after Zigbee.begin()
75+
// min_interval and max_interval in seconds, delta (PM2.5 change in µg/m³)
76+
// if min = 1 and max = 0, reporting is sent only when PM2.5 changes by delta
77+
// if min = 0 and max = 10, reporting is sent every 10 seconds or when PM2.5 changes by delta
78+
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change
79+
zbPM25Sensor.setReporting(0, 30, 0);
80+
}
81+
82+
void loop() {
83+
static uint32_t timeCounter = 0;
84+
// Read PM2.5 sensor every 2s
85+
if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s
86+
// Read sensor value - here is chip temperature used + 50 as a dummy value for demonstration
87+
float pm25_value = 50.5 + temperatureRead();
88+
Serial.printf("Updating PM2.5 sensor value to %0.1f µg/m³\r\n", pm25_value);
89+
zbPM25Sensor.setPM25(pm25_value);
90+
}
91+
92+
// Checking button for factory reset and reporting
93+
if (digitalRead(button) == LOW) { // Push button pressed
94+
// Key debounce handling
95+
delay(100);
96+
int startTime = millis();
97+
while (digitalRead(button) == LOW) {
98+
delay(50);
99+
if ((millis() - startTime) > 3000) {
100+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
101+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
102+
delay(1000);
103+
Zigbee.factoryReset();
104+
}
105+
}
106+
zbPM25Sensor.report();
107+
}
108+
delay(100);
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y",
5+
"CONFIG_ZB_ENABLED=y"
6+
]
7+
}

libraries/Zigbee/src/Zigbee.h

+17-11
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,29 @@
77
#include "ZigbeeEP.h"
88

99
// Endpoints
10-
#include "ep/ZigbeeLight.h"
10+
//// Switches
11+
#include "ep/ZigbeeColorDimmerSwitch.h"
1112
#include "ep/ZigbeeSwitch.h"
12-
#include "ep/ZigbeeDimmableLight.h"
13+
//// Lights
1314
#include "ep/ZigbeeColorDimmableLight.h"
14-
#include "ep/ZigbeeColorDimmerSwitch.h"
15-
#include "ep/ZigbeeTempSensor.h"
15+
#include "ep/ZigbeeDimmableLight.h"
16+
#include "ep/ZigbeeLight.h"
17+
//// Controllers
1618
#include "ep/ZigbeeThermostat.h"
17-
#include "ep/ZigbeePressureSensor.h"
19+
//// Sensors
1820
#include "ep/ZigbeeAnalog.h"
19-
#include "ep/ZigbeeFlowSensor.h"
20-
#include "ep/ZigbeeOccupancySensor.h"
21-
#include "ep/ZigbeeIlluminanceSensor.h"
2221
#include "ep/ZigbeeCarbonDioxideSensor.h"
2322
#include "ep/ZigbeeContactSwitch.h"
2423
#include "ep/ZigbeeDoorWindowHandle.h"
25-
#include "ep/ZigbeeWindowCovering.h"
24+
#include "ep/ZigbeeFlowSensor.h"
25+
#include "ep/ZigbeeIlluminanceSensor.h"
26+
#include "ep/ZigbeeOccupancySensor.h"
27+
#include "ep/ZigbeePM25Sensor.h"
28+
#include "ep/ZigbeePressureSensor.h"
29+
#include "ep/ZigbeeTempSensor.h"
2630
#include "ep/ZigbeeVibrationSensor.h"
27-
#include "ep/ZigbeeRangeExtender.h"
28-
#include "ep/ZigbeeGateway.h"
2931
#include "ep/ZigbeeWindSpeedSensor.h"
32+
#include "ep/ZigbeeWindowCovering.h"
33+
//// Other
34+
#include "ep/ZigbeeGateway.h"
35+
#include "ep/ZigbeeRangeExtender.h"

libraries/Zigbee/src/ZigbeeEP.cpp

+36-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) {
1919
_ep_config.endpoint = 0;
2020
_cluster_list = nullptr;
2121
_on_identify = nullptr;
22+
_read_model = NULL;
23+
_read_manufacturer = NULL;
2224
_time_status = 0;
2325
if (!lock) {
2426
lock = xSemaphoreCreateBinary();
@@ -33,16 +35,23 @@ void ZigbeeEP::setVersion(uint8_t version) {
3335
}
3436

3537
bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
38+
// Allocate a new array of size length + 2 (1 for the length, 1 for null terminator)
39+
char zb_name[ZB_MAX_NAME_LENGTH + 2];
40+
char zb_model[ZB_MAX_NAME_LENGTH + 2];
41+
3642
// Convert manufacturer to ZCL string
3743
size_t name_length = strlen(name);
3844
size_t model_length = strlen(model);
39-
if (name_length > 32 || model_length > 32) {
45+
if (name_length > ZB_MAX_NAME_LENGTH || model_length > ZB_MAX_NAME_LENGTH) {
4046
log_e("Manufacturer or model name is too long");
4147
return false;
4248
}
43-
// Allocate a new array of size length + 2 (1 for the length, 1 for null terminator)
44-
char *zb_name = new char[name_length + 2];
45-
char *zb_model = new char[model_length + 2];
49+
// Get and check the basic cluster
50+
esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
51+
if (basic_cluster == nullptr) {
52+
log_e("Failed to get basic cluster");
53+
return false;
54+
}
4655
// Store the length as the first element
4756
zb_name[0] = static_cast<char>(name_length); // Cast size_t to char
4857
zb_model[0] = static_cast<char>(model_length);
@@ -52,9 +61,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
5261
// Null-terminate the array
5362
zb_name[name_length + 1] = '\0';
5463
zb_model[model_length + 1] = '\0';
55-
56-
// Get the basic cluster and update the manufacturer and model attributes
57-
esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
64+
// Update the manufacturer and model attributes
5865
esp_err_t ret_name = esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)zb_name);
5966
if (ret_name != ESP_OK) {
6067
log_e("Failed to set manufacturer: 0x%x: %s", ret_name, esp_err_to_name(ret_name));
@@ -63,8 +70,6 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
6370
if (ret_model != ESP_OK) {
6471
log_e("Failed to set model: 0x%x: %s", ret_model, esp_err_to_name(ret_model));
6572
}
66-
delete[] zb_name;
67-
delete[] zb_model;
6873
return ret_name == ESP_OK && ret_model == ESP_OK;
6974
}
7075

@@ -163,10 +168,10 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i
163168
read_req.attr_number = ZB_ARRAY_LENTH(attributes);
164169
read_req.attr_field = attributes;
165170

166-
if (_read_manufacturer != nullptr) {
171+
if (_read_manufacturer != NULL) {
167172
free(_read_manufacturer);
168173
}
169-
_read_manufacturer = nullptr;
174+
_read_manufacturer = NULL;
170175

171176
esp_zb_lock_acquire(portMAX_DELAY);
172177
esp_zb_zcl_read_attr_cmd_req(&read_req);
@@ -201,10 +206,10 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add
201206
read_req.attr_number = ZB_ARRAY_LENTH(attributes);
202207
read_req.attr_field = attributes;
203208

204-
if (_read_model != nullptr) {
209+
if (_read_model != NULL) {
205210
free(_read_model);
206211
}
207-
_read_model = nullptr;
212+
_read_model = NULL;
208213

209214
esp_zb_lock_acquire(portMAX_DELAY);
210215
esp_zb_zcl_read_attr_cmd_req(&read_req);
@@ -245,20 +250,28 @@ void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute) {
245250
/* Basic cluster attributes */
246251
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
247252
zbstring_t *zbstr = (zbstring_t *)attribute->data.value;
248-
char *string = (char *)malloc(zbstr->len + 1);
249-
memcpy(string, zbstr->data, zbstr->len);
250-
string[zbstr->len] = '\0';
251-
log_i("Peer Manufacturer is \"%s\"", string);
252-
_read_manufacturer = string;
253+
_read_manufacturer = (char *)malloc(zbstr->len + 1);
254+
if (_read_manufacturer == NULL) {
255+
log_e("Failed to allocate memory for manufacturer data");
256+
xSemaphoreGive(lock);
257+
return;
258+
}
259+
memcpy(_read_manufacturer, zbstr->data, zbstr->len);
260+
_read_manufacturer[zbstr->len] = '\0';
261+
log_i("Peer Manufacturer is \"%s\"", _read_manufacturer);
253262
xSemaphoreGive(lock);
254263
}
255264
if (attribute->id == ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID && attribute->data.type == ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING && attribute->data.value) {
256265
zbstring_t *zbstr = (zbstring_t *)attribute->data.value;
257-
char *string = (char *)malloc(zbstr->len + 1);
258-
memcpy(string, zbstr->data, zbstr->len);
259-
string[zbstr->len] = '\0';
260-
log_i("Peer Model is \"%s\"", string);
261-
_read_model = string;
266+
_read_model = (char *)malloc(zbstr->len + 1);
267+
if (_read_model == NULL) {
268+
log_e("Failed to allocate memory for model data");
269+
xSemaphoreGive(lock);
270+
return;
271+
}
272+
memcpy(_read_model, zbstr->data, zbstr->len);
273+
_read_model[zbstr->len] = '\0';
274+
log_i("Peer Model is \"%s\"", _read_model);
262275
xSemaphoreGive(lock);
263276
}
264277
}

libraries/Zigbee/src/ZigbeeEP.h

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ typedef enum {
4141
/* Zigbee End Device Class */
4242
class ZigbeeEP {
4343
public:
44+
// constants and limits
45+
static constexpr size_t ZB_MAX_NAME_LENGTH = 32;
46+
47+
// constructors and destructor
4448
ZigbeeEP(uint8_t endpoint = 10);
4549
~ZigbeeEP() {}
4650

0 commit comments

Comments
 (0)