Skip to content

fix(zigbee): Fix rejoin issue for battery powered devices #10704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@

#include "Zigbee.h"

#define LED_PIN RGB_BUILTIN
#define BUTTON_PIN 9 // C6/H2 Boot button
#define ZIGBEE_LIGHT_ENDPOINT 10
/* Zigbee color dimmable light configuration */
#define ZIGBEE_RGB_LIGHT_ENDPOINT 10
uint8_t led = RGB_BUILTIN;
uint8_t button = BOOT_PIN;

ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_RGB_LIGHT_ENDPOINT);

/********************* RGB LED functions **************************/
void setRGBLight(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level) {
if (!state) {
rgbLedWrite(LED_PIN, 0, 0, 0);
rgbLedWrite(led, 0, 0, 0);
return;
}
float brightness = (float)level / 255;
rgbLedWrite(LED_PIN, red * brightness, green * brightness, blue * brightness);
rgbLedWrite(led, red * brightness, green * brightness, blue * brightness);
}

// Create a task on identify call to handle the identify function
Expand All @@ -58,22 +59,19 @@ void identify(uint16_t time) {
zbColorLight.restoreLight();
return;
}
rgbLedWrite(LED_PIN, 255 * blink, 255 * blink, 255 * blink);
rgbLedWrite(led, 255 * blink, 255 * blink, 255 * blink);
blink = !blink;
}

/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

// Init RMT and leave light OFF
rgbLedWrite(LED_PIN, 0, 0, 0);
rgbLedWrite(led, 0, 0, 0);

// Init button for factory reset
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(button, INPUT_PULLUP);

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

void loop() {
// Checking button for factory reset
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
if (digitalRead(button) == LOW) { // Push button pressed
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(BUTTON_PIN) == LOW) {
while (digitalRead(button) == LOW) {
delay(50);
if ((millis() - startTime) > 3000) {
// If key pressed for more than 3secs, factory reset Zigbee and reboot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,19 @@

#include "Zigbee.h"

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

/* Zigbee switch */
ZigbeeColorDimmerSwitch zbSwitch = ZigbeeColorDimmerSwitch(SWITCH_ENDPOINT_NUMBER);

/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

//Init button switch
pinMode(SWITCH_PIN, INPUT_PULLUP);
pinMode(button, INPUT_PULLUP);

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

void loop() {
// Handle button switch in loop()
if (digitalRead(SWITCH_PIN) == LOW) { // Push button pressed
if (digitalRead(button) == LOW) { // Push button pressed
// Key debounce handling
while (digitalRead(SWITCH_PIN) == LOW) {
while (digitalRead(button) == LOW) {
delay(50);
}
// Toggle light
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,28 @@

#include "Zigbee.h"

#define LED_PIN RGB_BUILTIN
#define BUTTON_PIN 9 // ESP32-C6/H2 Boot button
/* Zigbee light bulb configuration */
#define ZIGBEE_LIGHT_ENDPOINT 10
uint8_t led = RGB_BUILTIN;
uint8_t button = BOOT_PIN;

ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);

/********************* RGB LED functions **************************/
void setLED(bool value) {
digitalWrite(LED_PIN, value);
digitalWrite(led, value);
}

/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

// Init LED and turn it OFF (if LED_PIN == RGB_BUILTIN, the rgbLedWrite() will be used under the hood)
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);

// Init button for factory reset
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(button, INPUT_PULLUP);

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

void loop() {
// Checking button for factory reset
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
if (digitalRead(button) == LOW) { // Push button pressed
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(BUTTON_PIN) == LOW) {
while (digitalRead(button) == LOW) {
delay(50);
if ((millis() - startTime) > 3000) {
// If key pressed for more than 3secs, factory reset Zigbee and reboot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@

#include "Zigbee.h"

/* Zigbee switch configuration */
#define SWITCH_ENDPOINT_NUMBER 5

/* Switch configuration */
#define GPIO_INPUT_IO_TOGGLE_SWITCH 9
#define GPIO_INPUT_IO_TOGGLE_SWITCH BOOT_PIN
#define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0]))

typedef enum {
Expand Down Expand Up @@ -95,9 +95,6 @@ static void enableGpioInterrupt(bool enabled) {
/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

//Optional: set Zigbee device name and model
zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ void printScannedNetworks(uint16_t networksFound) {

void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

// Initialize Zigbee stack without any EPs just for scanning
if (!Zigbee.begin(role)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@

#include "Zigbee.h"

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

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

uint8_t button = BOOT_PIN;

ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);

/************************ Temp sensor *****************************/
Expand Down Expand Up @@ -66,11 +68,9 @@ void meausureAndSleep() {
/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

// Init button switch
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(button, INPUT_PULLUP);

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

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

void loop() {
// Checking button for factory reset
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
if (digitalRead(button) == LOW) { // Push button pressed
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(BUTTON_PIN) == LOW) {
while (digitalRead(button) == LOW) {
delay(50);
if ((millis() - startTime) > 3000) {
// If key pressed for more than 3secs, factory reset Zigbee and reboot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@

#include "Zigbee.h"

#define BUTTON_PIN 9 //Boot button for C6/H2
/* Zigbee temperature sensor configuration */
#define TEMP_SENSOR_ENDPOINT_NUMBER 10
uint8_t button = BOOT_PIN;

ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);

Expand All @@ -52,11 +53,9 @@ static void temp_sensor_value_update(void *arg) {
/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

// Init button switch
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(button, INPUT_PULLUP);

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

void loop() {
// Checking button for factory reset
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
if (digitalRead(button) == LOW) { // Push button pressed
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(BUTTON_PIN) == LOW) {
while (digitalRead(button) == LOW) {
delay(50);
if ((millis() - startTime) > 3000) {
// If key pressed for more than 3secs, factory reset Zigbee and reboot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@

#include "Zigbee.h"

#define BUTTON_PIN 9 // Boot button for C6/H2
/* Zigbee thermostat configuration */
#define THERMOSTAT_ENDPOINT_NUMBER 5
uint8_t button = BOOT_PIN;

ZigbeeThermostat zbThermostat = ZigbeeThermostat(THERMOSTAT_ENDPOINT_NUMBER);

Expand All @@ -59,12 +60,9 @@ void recieveSensorConfig(float min_temp, float max_temp, float tolerance) {
/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

// Init button switch
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(button, INPUT_PULLUP);

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

void loop() {
// Handle button switch in loop()
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
if (digitalRead(button) == LOW) { // Push button pressed

// Key debounce handling
while (digitalRead(BUTTON_PIN) == LOW) {
while (digitalRead(button) == LOW) {
delay(50);
}

Expand Down
5 changes: 3 additions & 2 deletions libraries/Zigbee/src/ZigbeeCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ ZigbeeCore::ZigbeeCore() {
_scan_status = ZB_SCAN_FAILED;
_started = false;
_connected = false;
_scan_duration = 4; // maximum scan duration
_scan_duration = 3; // default scan duration
_rx_on_when_idle = true;
if (!lock) {
lock = xSemaphoreCreateBinary();
if (lock == NULL) {
Expand Down Expand Up @@ -98,7 +99,7 @@ static void esp_zb_task(void *pvParameters) {

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

esp_zb_stack_main_loop();
Expand Down
8 changes: 8 additions & 0 deletions libraries/Zigbee/src/ZigbeeCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class ZigbeeCore {
uint32_t _primary_channel_mask;
int16_t _scan_status;
uint8_t _scan_duration;
bool _rx_on_when_idle;

esp_zb_ep_list_t *_zb_ep_list;
zigbee_role_t _role;
Expand Down Expand Up @@ -118,6 +119,13 @@ class ZigbeeCore {
return _scan_duration;
}

void setRxOnWhenIdle(bool rx_on_when_idle) {
_rx_on_when_idle = rx_on_when_idle;
}
bool getRxOnWhenIdle() {
return _rx_on_when_idle;
}

void setRebootOpenNetwork(uint8_t time);
void openNetwork(uint8_t time);

Expand Down
Loading