Skip to content

Commit 3e3fb95

Browse files
authored
Merge branch 'master' into ci/improve_requirements
2 parents 8d56f9d + d1df696 commit 3e3fb95

20 files changed

+406
-35
lines changed

libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
3232
#endif
3333

34-
#include "ZigbeeCore.h"
35-
#include "ep/ZigbeeColorDimmableLight.h"
34+
#include "Zigbee.h"
3635

3736
#define LED_PIN RGB_BUILTIN
3837
#define BUTTON_PIN 9 // C6/H2 Boot button

libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
3636
#endif
3737

38-
#include "ZigbeeCore.h"
39-
#include "ep/ZigbeeColorDimmerSwitch.h"
38+
#include "Zigbee.h"
4039

4140
/* Switch configuration */
4241
#define SWITCH_PIN 9 // ESP32-C6/H2 Boot button

libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
3131
#endif
3232

33-
#include "ZigbeeCore.h"
34-
#include "ep/ZigbeeLight.h"
33+
#include "Zigbee.h"
3534

3635
#define LED_PIN RGB_BUILTIN
3736
#define BUTTON_PIN 9 // ESP32-C6/H2 Boot button

libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
3232
#endif
3333

34-
#include "ZigbeeCore.h"
35-
#include "ep/ZigbeeSwitch.h"
34+
#include "Zigbee.h"
3635

3736
#define SWITCH_ENDPOINT_NUMBER 5
3837

libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#error "Zigbee device mode is not selected in Tools->Zigbee mode"
3030
#endif
3131

32-
#include "ZigbeeCore.h"
32+
#include "Zigbee.h"
3333

3434
#ifdef ZIGBEE_MODE_ZCZR
3535
zigbee_role_t role = ZIGBEE_ROUTER; // or can be ZIGBEE_COORDINATOR, but it wont scan itself
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Arduino-ESP32 Zigbee Temperature and Humidity Sensor Sleepy Device Example
2+
3+
This example demonstrates how to use the Zigbee library to create an end device temperature/humidity sensor and use it as a Home Automation (HA) extended temperature sensor.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## Temperature Sensor Functions
13+
14+
1. Initialize a Zigbee temperature and humidity sensor.
15+
2. Measure temperature and humidity values.
16+
3. Report the measured values to the Zigbee network.
17+
4. Put the device to sleep to save power.
18+
19+
## Hardware Required
20+
21+
* ESP32-H2 or ESP32-C6 development board
22+
* A USB cable for power supply and programming
23+
24+
### Configure the Project
25+
26+
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).
28+
29+
#### Using Arduino IDE
30+
31+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
32+
33+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
34+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
35+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
36+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
37+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
38+
39+
## Troubleshooting
40+
41+
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.
42+
You can do the following:
43+
44+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
45+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
46+
47+
By default, the coordinator network is closed after rebooting or flashing new firmware.
48+
To open the network you have 2 options:
49+
50+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
51+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
52+
53+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
54+
55+
* **LED not blinking:** Check the wiring connection and the IO selection.
56+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
57+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
58+
59+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
60+
61+
## Contribute
62+
63+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
64+
65+
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!
66+
67+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
68+
69+
## Resources
70+
71+
* Official ESP32 Forum: [Link](https://esp32.com)
72+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
73+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
74+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
75+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 and humidity sensor Sleepy device.
17+
*
18+
* The example demonstrates how to use Zigbee library to create an end device temperature and humidity sensor.
19+
* The sensor is a Zigbee end device, which is reporting data to the Zigbee network.
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 coordinator mode is not selected in Tools->Zigbee mode"
31+
#endif
32+
33+
#include "Zigbee.h"
34+
35+
#define BUTTON_PIN 9 //Boot button for C6/H2
36+
#define TEMP_SENSOR_ENDPOINT_NUMBER 10
37+
38+
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
39+
#define TIME_TO_SLEEP 55 /* Sleep for 55s will + 5s delay for establishing connection => data reported every 1 minute */
40+
41+
ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);
42+
43+
/************************ Temp sensor *****************************/
44+
void meausureAndSleep() {
45+
// Measure temperature sensor value
46+
float temperature = temperatureRead();
47+
48+
// Use temparture value as humidity value to demonstrate both temperature and humidity
49+
float humidity = temperature;
50+
51+
// Update temperature and humidity values in Temperature sensor EP
52+
zbTempSensor.setTemperature(temperature);
53+
zbTempSensor.setHumidity(humidity);
54+
55+
// Report temperature and humidity values
56+
zbTempSensor.reportTemperature();
57+
zbTempSensor.reportHumidity();
58+
59+
log_d("Temperature: %.2f°C, Humidity: %.2f%", temperature, humidity);
60+
61+
// Put device to deep sleep
62+
esp_deep_sleep_start();
63+
}
64+
65+
/********************* Arduino functions **************************/
66+
void setup() {
67+
// Init button switch
68+
pinMode(BUTTON_PIN, INPUT_PULLUP);
69+
70+
// Configure the wake up source and set to wake up every 5 seconds
71+
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
72+
73+
// Optional: set Zigbee device name and model
74+
zbTempSensor.setManufacturerAndModel("Espressif", "SleepyZigbeeTempSensorTest");
75+
76+
// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
77+
zbTempSensor.setMinMaxValue(10, 50);
78+
79+
// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
80+
zbTempSensor.setTolerance(1);
81+
82+
// Set power source to battery and set battery percentage to measured value (now 100% for demonstration)
83+
// The value can be also updated by calling zbTempSensor.setBatteryPercentage(percentage) anytime
84+
zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100);
85+
86+
// Add humidity cluster to the temperature sensor device with min, max and tolerance values
87+
zbTempSensor.addHumiditySensor(0, 100, 1);
88+
89+
// Add endpoint to Zigbee Core
90+
Zigbee.addEndpoint(&zbTempSensor);
91+
92+
// Create a custom Zigbee configuration for End Device with keep alive 10s to avoid interference with reporting data
93+
esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG();
94+
zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000;
95+
96+
// When all EPs are registered, start Zigbee in End Device mode
97+
Zigbee.begin(&zigbeeConfig, false);
98+
99+
// Wait for Zigbee to start
100+
while (!Zigbee.isStarted()) {
101+
delay(100);
102+
}
103+
104+
// Delay 5s to allow establishing connection with coordinator, needed for sleepy devices
105+
delay(5000);
106+
}
107+
108+
void loop() {
109+
// Checking button for factory reset
110+
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
111+
// Key debounce handling
112+
delay(100);
113+
int startTime = millis();
114+
while (digitalRead(BUTTON_PIN) == LOW) {
115+
delay(50);
116+
if ((millis() - startTime) > 3000) {
117+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
118+
Zigbee.factoryReset();
119+
}
120+
}
121+
}
122+
123+
// Call the function to measure temperature and put the device to sleep
124+
meausureAndSleep();
125+
}
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_Temperature_Sensor/Zigbee_Temperature_Sensor.ino

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
3131
#endif
3232

33-
#include "ZigbeeCore.h"
34-
#include "ep/ZigbeeTempSensor.h"
33+
#include "Zigbee.h"
3534

3635
#define BUTTON_PIN 9 //Boot button for C6/H2
3736
#define TEMP_SENSOR_ENDPOINT_NUMBER 10

libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
3232
#endif
3333

34-
#include "ZigbeeCore.h"
35-
#include "ep/ZigbeeThermostat.h"
34+
#include "Zigbee.h"
3635

3736
#define BUTTON_PIN 9 // Boot button for C6/H2
3837
#define THERMOSTAT_ENDPOINT_NUMBER 5

libraries/Zigbee/src/Zigbee.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Zigbee library header file for includes of all Zigbee library headers.
2+
3+
#pragma once
4+
5+
// Core
6+
#include "ZigbeeCore.h"
7+
#include "ZigbeeEP.h"
8+
9+
// Endpoints
10+
#include "ep/ZigbeeLight.h"
11+
#include "ep/ZigbeeSwitch.h"
12+
#include "ep/ZigbeeColorDimmableLight.h"
13+
#include "ep/ZigbeeColorDimmerSwitch.h"
14+
#include "ep/ZigbeeTempSensor.h"
15+
#include "ep/ZigbeeThermostat.h"

libraries/Zigbee/src/ZigbeeCore.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include "ZigbeeHandlers.cpp"
77
#include "Arduino.h"
88

9+
extern "C" void zb_set_ed_node_descriptor(bool power_src, bool rx_on_when_idle, bool alloc_addr);
10+
static bool edBatteryPowered = false;
11+
912
ZigbeeCore::ZigbeeCore() {
1013
_radio_config.radio_mode = ZB_RADIO_MODE_NATIVE; // Use the native 15.4 radio
1114
_host_config.host_connection_mode = ZB_HOST_CONNECTION_MODE_NONE; // Disable host connection
@@ -73,6 +76,12 @@ void ZigbeeCore::addEndpoint(ZigbeeEP *ep) {
7376
static void esp_zb_task(void *pvParameters) {
7477
/* initialize Zigbee stack */
7578
ESP_ERROR_CHECK(esp_zb_start(false));
79+
80+
//NOTE: This is a workaround to make battery powered devices to be discovered as battery powered
81+
if (((zigbee_role_t)Zigbee.getRole() == ZIGBEE_END_DEVICE) && edBatteryPowered) {
82+
zb_set_ed_node_descriptor(0, 0, 0);
83+
}
84+
7685
esp_zb_stack_main_loop();
7786
}
7887

@@ -109,6 +118,9 @@ bool ZigbeeCore::zigbeeInit(esp_zb_cfg_t *zb_cfg, bool erase_nvs) {
109118
log_i("List of registered Zigbee EPs:");
110119
for (std::list<ZigbeeEP *>::iterator it = ep_objects.begin(); it != ep_objects.end(); ++it) {
111120
log_i("Device type: %s, Endpoint: %d, Device ID: 0x%04x", getDeviceTypeString((*it)->_device_id), (*it)->_endpoint, (*it)->_device_id);
121+
if ((*it)->_power_source == ZB_POWER_SOURCE_BATTERY) {
122+
edBatteryPowered = true;
123+
}
112124
}
113125
}
114126
// Register Zigbee action handler
@@ -155,7 +167,7 @@ void ZigbeeCore::setRebootOpenNetwork(uint8_t time) {
155167
}
156168

157169
void ZigbeeCore::openNetwork(uint8_t time) {
158-
if (_started) {
170+
if (isStarted()) {
159171
log_v("Opening network for joining for %d seconds", time);
160172
esp_zb_bdb_open_network(time);
161173
}
@@ -205,6 +217,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
205217
} else {
206218
/* commissioning failed */
207219
log_e("Failed to initialize Zigbee stack (status: %s)", esp_err_to_name(err_status));
220+
esp_restart();
208221
}
209222
break;
210223
case ESP_ZB_BDB_SIGNAL_FORMATION: // Coordinator
@@ -225,11 +238,11 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
225238
}
226239
break;
227240
case ESP_ZB_BDB_SIGNAL_STEERING: // Router and End Device
228-
Zigbee._started = true;
229241
if ((zigbee_role_t)Zigbee.getRole() == ZIGBEE_COORDINATOR) {
230242
if (err_status == ESP_OK) {
231243
log_i("Network steering started");
232244
}
245+
Zigbee._started = true;
233246
} else {
234247
if (err_status == ESP_OK) {
235248
esp_zb_ieee_addr_t extended_pan_id;
@@ -239,6 +252,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
239252
extended_pan_id[7], extended_pan_id[6], extended_pan_id[5], extended_pan_id[4], extended_pan_id[3], extended_pan_id[2], extended_pan_id[1],
240253
extended_pan_id[0], esp_zb_get_pan_id(), esp_zb_get_current_channel(), esp_zb_get_short_address()
241254
);
255+
Zigbee._started = true;
242256
} else {
243257
log_i("Network steering was not successful (status: %s)", esp_err_to_name(err_status));
244258
esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_STEERING, 1000);
@@ -321,7 +335,7 @@ void ZigbeeCore::scanCompleteCallback(esp_zb_zdp_status_t zdo_status, uint8_t co
321335
}
322336

323337
void ZigbeeCore::scanNetworks(u_int32_t channel_mask, u_int8_t scan_duration) {
324-
if (!_started) {
338+
if (!isStarted()) {
325339
log_e("Zigbee stack is not started, cannot scan networks");
326340
return;
327341
}

0 commit comments

Comments
 (0)