Skip to content

Commit 83165f3

Browse files
authored
Merge pull request #10704 from P-R-O-C-H-Y/fix/zigbee-rejoin-battery-powered
fix(zigbee): Fix rejoin issue for battery powered devices
2 parents c3ddcd7 + 3a8d4c5 commit 83165f3

File tree

10 files changed

+60
-66
lines changed

10 files changed

+60
-66
lines changed

Diff for: libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino

+12-14
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,21 @@
3333

3434
#include "Zigbee.h"
3535

36-
#define LED_PIN RGB_BUILTIN
37-
#define BUTTON_PIN 9 // C6/H2 Boot button
38-
#define ZIGBEE_LIGHT_ENDPOINT 10
36+
/* Zigbee color dimmable light configuration */
37+
#define ZIGBEE_RGB_LIGHT_ENDPOINT 10
38+
uint8_t led = RGB_BUILTIN;
39+
uint8_t button = BOOT_PIN;
3940

40-
ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
41+
ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_RGB_LIGHT_ENDPOINT);
4142

4243
/********************* RGB LED functions **************************/
4344
void setRGBLight(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level) {
4445
if (!state) {
45-
rgbLedWrite(LED_PIN, 0, 0, 0);
46+
rgbLedWrite(led, 0, 0, 0);
4647
return;
4748
}
4849
float brightness = (float)level / 255;
49-
rgbLedWrite(LED_PIN, red * brightness, green * brightness, blue * brightness);
50+
rgbLedWrite(led, red * brightness, green * brightness, blue * brightness);
5051
}
5152

5253
// Create a task on identify call to handle the identify function
@@ -58,22 +59,19 @@ void identify(uint16_t time) {
5859
zbColorLight.restoreLight();
5960
return;
6061
}
61-
rgbLedWrite(LED_PIN, 255 * blink, 255 * blink, 255 * blink);
62+
rgbLedWrite(led, 255 * blink, 255 * blink, 255 * blink);
6263
blink = !blink;
6364
}
6465

6566
/********************* Arduino functions **************************/
6667
void setup() {
6768
Serial.begin(115200);
68-
while (!Serial) {
69-
delay(10);
70-
}
7169

7270
// Init RMT and leave light OFF
73-
rgbLedWrite(LED_PIN, 0, 0, 0);
71+
rgbLedWrite(led, 0, 0, 0);
7472

7573
// Init button for factory reset
76-
pinMode(BUTTON_PIN, INPUT_PULLUP);
74+
pinMode(button, INPUT_PULLUP);
7775

7876
// Set callback function for light change
7977
zbColorLight.onLightChange(setRGBLight);
@@ -104,11 +102,11 @@ void setup() {
104102

105103
void loop() {
106104
// Checking button for factory reset
107-
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
105+
if (digitalRead(button) == LOW) { // Push button pressed
108106
// Key debounce handling
109107
delay(100);
110108
int startTime = millis();
111-
while (digitalRead(BUTTON_PIN) == LOW) {
109+
while (digitalRead(button) == LOW) {
112110
delay(50);
113111
if ((millis() - startTime) > 3000) {
114112
// If key pressed for more than 3secs, factory reset Zigbee and reboot

Diff for: libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino

+5-8
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,19 @@
3737

3838
#include "Zigbee.h"
3939

40-
/* Switch configuration */
41-
#define SWITCH_PIN 9 // ESP32-C6/H2 Boot button
40+
/* Zigbee color dimmer switch configuration */
4241
#define SWITCH_ENDPOINT_NUMBER 5
42+
uint8_t button = BOOT_PIN;
4343

4444
/* Zigbee switch */
4545
ZigbeeColorDimmerSwitch zbSwitch = ZigbeeColorDimmerSwitch(SWITCH_ENDPOINT_NUMBER);
4646

4747
/********************* Arduino functions **************************/
4848
void setup() {
4949
Serial.begin(115200);
50-
while (!Serial) {
51-
delay(10);
52-
}
5350

5451
//Init button switch
55-
pinMode(SWITCH_PIN, INPUT_PULLUP);
52+
pinMode(button, INPUT_PULLUP);
5653

5754
//Optional: set Zigbee device name and model
5855
zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch");
@@ -84,9 +81,9 @@ void setup() {
8481

8582
void loop() {
8683
// Handle button switch in loop()
87-
if (digitalRead(SWITCH_PIN) == LOW) { // Push button pressed
84+
if (digitalRead(button) == LOW) { // Push button pressed
8885
// Key debounce handling
89-
while (digitalRead(SWITCH_PIN) == LOW) {
86+
while (digitalRead(button) == LOW) {
9087
delay(50);
9188
}
9289
// Toggle light

Diff for: libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino

+10-11
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,28 @@
3232

3333
#include "Zigbee.h"
3434

35-
#define LED_PIN RGB_BUILTIN
36-
#define BUTTON_PIN 9 // ESP32-C6/H2 Boot button
35+
/* Zigbee light bulb configuration */
3736
#define ZIGBEE_LIGHT_ENDPOINT 10
37+
uint8_t led = RGB_BUILTIN;
38+
uint8_t button = BOOT_PIN;
3839

3940
ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);
4041

4142
/********************* RGB LED functions **************************/
4243
void setLED(bool value) {
43-
digitalWrite(LED_PIN, value);
44+
digitalWrite(led, value);
4445
}
4546

4647
/********************* Arduino functions **************************/
4748
void setup() {
4849
Serial.begin(115200);
49-
while (!Serial) {
50-
delay(10);
51-
}
50+
5251
// Init LED and turn it OFF (if LED_PIN == RGB_BUILTIN, the rgbLedWrite() will be used under the hood)
53-
pinMode(LED_PIN, OUTPUT);
54-
digitalWrite(LED_PIN, LOW);
52+
pinMode(led, OUTPUT);
53+
digitalWrite(led, LOW);
5554

5655
// Init button for factory reset
57-
pinMode(BUTTON_PIN, INPUT_PULLUP);
56+
pinMode(button, INPUT_PULLUP);
5857

5958
//Optional: set Zigbee device name and model
6059
zbLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
@@ -82,11 +81,11 @@ void setup() {
8281

8382
void loop() {
8483
// Checking button for factory reset
85-
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
84+
if (digitalRead(button) == LOW) { // Push button pressed
8685
// Key debounce handling
8786
delay(100);
8887
int startTime = millis();
89-
while (digitalRead(BUTTON_PIN) == LOW) {
88+
while (digitalRead(button) == LOW) {
9089
delay(50);
9190
if ((millis() - startTime) > 3000) {
9291
// If key pressed for more than 3secs, factory reset Zigbee and reboot

Diff for: libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino

+2-5
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333

3434
#include "Zigbee.h"
3535

36+
/* Zigbee switch configuration */
3637
#define SWITCH_ENDPOINT_NUMBER 5
3738

38-
/* Switch configuration */
39-
#define GPIO_INPUT_IO_TOGGLE_SWITCH 9
39+
#define GPIO_INPUT_IO_TOGGLE_SWITCH BOOT_PIN
4040
#define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0]))
4141

4242
typedef enum {
@@ -95,9 +95,6 @@ static void enableGpioInterrupt(bool enabled) {
9595
/********************* Arduino functions **************************/
9696
void setup() {
9797
Serial.begin(115200);
98-
while (!Serial) {
99-
delay(10);
100-
}
10198

10299
//Optional: set Zigbee device name and model
103100
zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch");

Diff for: libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino

-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ void printScannedNetworks(uint16_t networksFound) {
7676

7777
void setup() {
7878
Serial.begin(115200);
79-
while (!Serial) {
80-
delay(10);
81-
}
8279

8380
// Initialize Zigbee stack without any EPs just for scanning
8481
if (!Zigbee.begin(role)) {

Diff for: libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232

3333
#include "Zigbee.h"
3434

35-
#define BUTTON_PIN 9 //Boot button for C6/H2
35+
/* Zigbee temperature + humidity sensor configuration */
3636
#define TEMP_SENSOR_ENDPOINT_NUMBER 10
3737

3838
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
3939
#define TIME_TO_SLEEP 55 /* Sleep for 55s will + 5s delay for establishing connection => data reported every 1 minute */
4040

41+
uint8_t button = BOOT_PIN;
42+
4143
ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);
4244

4345
/************************ Temp sensor *****************************/
@@ -66,11 +68,9 @@ void meausureAndSleep() {
6668
/********************* Arduino functions **************************/
6769
void setup() {
6870
Serial.begin(115200);
69-
while (!Serial) {
70-
delay(10);
71-
}
71+
7272
// Init button switch
73-
pinMode(BUTTON_PIN, INPUT_PULLUP);
73+
pinMode(button, INPUT_PULLUP);
7474

7575
// Configure the wake up source and set to wake up every 5 seconds
7676
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
@@ -112,17 +112,17 @@ void setup() {
112112
Serial.println();
113113
Serial.println("Successfully connected to Zigbee network");
114114

115-
// Delay 5s (may be adjusted) to allow establishing proper connection with coordinator, needed for sleepy devices
116-
delay(5000);
115+
// Delay approx 1s (may be adjusted) to allow establishing proper connection with coordinator, needed for sleepy devices
116+
delay(1000);
117117
}
118118

119119
void loop() {
120120
// Checking button for factory reset
121-
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
121+
if (digitalRead(button) == LOW) { // Push button pressed
122122
// Key debounce handling
123123
delay(100);
124124
int startTime = millis();
125-
while (digitalRead(BUTTON_PIN) == LOW) {
125+
while (digitalRead(button) == LOW) {
126126
delay(50);
127127
if ((millis() - startTime) > 3000) {
128128
// If key pressed for more than 3secs, factory reset Zigbee and reboot

Diff for: libraries/Zigbee/examples/Zigbee_Temperature_Sensor/Zigbee_Temperature_Sensor.ino

+6-7
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232

3333
#include "Zigbee.h"
3434

35-
#define BUTTON_PIN 9 //Boot button for C6/H2
35+
/* Zigbee temperature sensor configuration */
3636
#define TEMP_SENSOR_ENDPOINT_NUMBER 10
37+
uint8_t button = BOOT_PIN;
3738

3839
ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);
3940

@@ -52,11 +53,9 @@ static void temp_sensor_value_update(void *arg) {
5253
/********************* Arduino functions **************************/
5354
void setup() {
5455
Serial.begin(115200);
55-
while (!Serial) {
56-
delay(10);
57-
}
56+
5857
// Init button switch
59-
pinMode(BUTTON_PIN, INPUT_PULLUP);
58+
pinMode(button, INPUT_PULLUP);
6059

6160
// Optional: set Zigbee device name and model
6261
zbTempSensor.setManufacturerAndModel("Espressif", "ZigbeeTempSensor");
@@ -99,11 +98,11 @@ void setup() {
9998

10099
void loop() {
101100
// Checking button for factory reset
102-
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
101+
if (digitalRead(button) == LOW) { // Push button pressed
103102
// Key debounce handling
104103
delay(100);
105104
int startTime = millis();
106-
while (digitalRead(BUTTON_PIN) == LOW) {
105+
while (digitalRead(button) == LOW) {
107106
delay(50);
108107
if ((millis() - startTime) > 3000) {
109108
// If key pressed for more than 3secs, factory reset Zigbee and reboot

Diff for: libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino

+5-7
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333

3434
#include "Zigbee.h"
3535

36-
#define BUTTON_PIN 9 // Boot button for C6/H2
36+
/* Zigbee thermostat configuration */
3737
#define THERMOSTAT_ENDPOINT_NUMBER 5
38+
uint8_t button = BOOT_PIN;
3839

3940
ZigbeeThermostat zbThermostat = ZigbeeThermostat(THERMOSTAT_ENDPOINT_NUMBER);
4041

@@ -59,12 +60,9 @@ void recieveSensorConfig(float min_temp, float max_temp, float tolerance) {
5960
/********************* Arduino functions **************************/
6061
void setup() {
6162
Serial.begin(115200);
62-
while (!Serial) {
63-
delay(10);
64-
}
6563

6664
// Init button switch
67-
pinMode(BUTTON_PIN, INPUT_PULLUP);
65+
pinMode(button, INPUT_PULLUP);
6866

6967
// Set callback functions for temperature and configuration receive
7068
zbThermostat.onTempRecieve(recieveSensorTemp);
@@ -100,10 +98,10 @@ void setup() {
10098

10199
void loop() {
102100
// Handle button switch in loop()
103-
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
101+
if (digitalRead(button) == LOW) { // Push button pressed
104102

105103
// Key debounce handling
106-
while (digitalRead(BUTTON_PIN) == LOW) {
104+
while (digitalRead(button) == LOW) {
107105
delay(50);
108106
}
109107

Diff for: libraries/Zigbee/src/ZigbeeCore.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ ZigbeeCore::ZigbeeCore() {
2020
_scan_status = ZB_SCAN_FAILED;
2121
_started = false;
2222
_connected = false;
23-
_scan_duration = 4; // maximum scan duration
23+
_scan_duration = 3; // default scan duration
24+
_rx_on_when_idle = true;
2425
if (!lock) {
2526
lock = xSemaphoreCreateBinary();
2627
if (lock == NULL) {
@@ -98,7 +99,7 @@ static void esp_zb_task(void *pvParameters) {
9899

99100
//NOTE: This is a workaround to make battery powered devices to be discovered as battery powered
100101
if (((zigbee_role_t)Zigbee.getRole() == ZIGBEE_END_DEVICE) && edBatteryPowered) {
101-
zb_set_ed_node_descriptor(0, 0, 0);
102+
zb_set_ed_node_descriptor(0, Zigbee.getRxOnWhenIdle(), 1);
102103
}
103104

104105
esp_zb_stack_main_loop();

Diff for: libraries/Zigbee/src/ZigbeeCore.h

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class ZigbeeCore {
6767
uint32_t _primary_channel_mask;
6868
int16_t _scan_status;
6969
uint8_t _scan_duration;
70+
bool _rx_on_when_idle;
7071

7172
esp_zb_ep_list_t *_zb_ep_list;
7273
zigbee_role_t _role;
@@ -118,6 +119,13 @@ class ZigbeeCore {
118119
return _scan_duration;
119120
}
120121

122+
void setRxOnWhenIdle(bool rx_on_when_idle) {
123+
_rx_on_when_idle = rx_on_when_idle;
124+
}
125+
bool getRxOnWhenIdle() {
126+
return _rx_on_when_idle;
127+
}
128+
121129
void setRebootOpenNetwork(uint8_t time);
122130
void openNetwork(uint8_t time);
123131

0 commit comments

Comments
 (0)