From 1730e4e57e73a35abd11bd3b79b36c0b1fb91579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 25 Nov 2024 12:13:28 +0100 Subject: [PATCH] fix(zigbee): Add connected(), minor fixes, example update (#10636) * fix(zigbee): Add connected and minor fixes, example update * fix(example): Use correct API call for connected * fix(zigbee): Increase timeout and add semaphore to begin * feat(zigbee): Add option to select debug libs for zigbee * fix(example): fix warning by double percentage symbol * fix(example): Fix serial prints for Windows os Co-authored-by: Rodrigo Garcia * ci(pre-commit): Apply automatic fixes * fix(example): Fix precommit spelling --------- Co-authored-by: Rodrigo Garcia Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- boards.txt | 19 ++++++- .../Zigbee_Color_Dimmable_Light.ino | 25 +++++++-- .../Zigbee_Color_Dimmer_Switch.ino | 9 ++- .../Zigbee_On_Off_Light.ino | 22 ++++++-- .../Zigbee_On_Off_Switch.ino | 17 +++--- .../Zigbee_Scan_Networks.ino | 15 +++-- .../Zigbee_Temp_Hum_Sensor_Sleepy.ino | 28 +++++++--- .../Zigbee_Temperature_Sensor.ino | 24 ++++++-- .../Zigbee_Thermostat/Zigbee_Thermostat.ino | 15 +++-- libraries/Zigbee/keywords.txt | 55 ++++++++++++------- libraries/Zigbee/src/ZigbeeCore.cpp | 38 ++++++++++--- libraries/Zigbee/src/ZigbeeCore.h | 7 ++- libraries/Zigbee/src/ZigbeeEP.cpp | 2 - libraries/Zigbee/src/ZigbeeEP.h | 2 +- .../Zigbee/src/ep/ZigbeeColorDimmerSwitch.h | 8 --- 15 files changed, 198 insertions(+), 88 deletions(-) diff --git a/boards.txt b/boards.txt index e0523a326fe..92d6ae599c2 100644 --- a/boards.txt +++ b/boards.txt @@ -531,7 +531,15 @@ esp32h2.menu.ZigbeeMode.zczr.build.zigbee_libs=-lesp_zb_api_zczr -lesp_zb_cli_co esp32h2.menu.ZigbeeMode.rcp=Zigbee RCP (radio co-processor) esp32h2.menu.ZigbeeMode.rcp.build.zigbee_mode=-DZIGBEE_MODE_RCP esp32h2.menu.ZigbeeMode.rcp.build.zigbee_libs=-lesp_zb_api_rcp -lesp_zb_cli_command -lzboss_stack.rcp -lzboss_port - +esp32h2.menu.ZigbeeMode.ed_debug=Zigbee ED (end device) - Debug +esp32h2.menu.ZigbeeMode.ed_debug.build.zigbee_mode=-DZIGBEE_MODE_ED +esp32h2.menu.ZigbeeMode.ed_debug.build.zigbee_libs=-lesp_zb_api_ed.debug -lesp_zb_cli_command -lzboss_stack.ed.debug -lzboss_port.debug +esp32h2.menu.ZigbeeMode.zczr_debug=Zigbee ZCZR (coordinator/router) - Debug +esp32h2.menu.ZigbeeMode.zczr_debug.build.zigbee_mode=-DZIGBEE_MODE_ZCZR +esp32h2.menu.ZigbeeMode.zczr_debug.build.zigbee_libs=-lesp_zb_api_zczr.debug -lesp_zb_cli_command -lzboss_stack.zczr.debug -lzboss_port.debug +esp32h2.menu.ZigbeeMode.rcp_debug=Zigbee RCP (radio co-processor) - Debug +esp32h2.menu.ZigbeeMode.rcp_debug.build.zigbee_mode=-DZIGBEE_MODE_RCP +esp32h2.menu.ZigbeeMode.rcp_debug.build.zigbee_libs=-lesp_zb_api_rcp.debug -lesp_zb_cli_command -lzboss_stack.rcp.debug -lzboss_port.debug ############################################################## @@ -723,6 +731,15 @@ esp32c6.menu.ZigbeeMode.zczr.build.zigbee_libs=-lesp_zb_api_zczr -lesp_zb_cli_co esp32c6.menu.ZigbeeMode.rcp=Zigbee RCP (radio co-processor) esp32c6.menu.ZigbeeMode.rcp.build.zigbee_mode=-DZIGBEE_MODE_RCP esp32c6.menu.ZigbeeMode.rcp.build.zigbee_libs=-lesp_zb_api_rcp -lesp_zb_cli_command -lzboss_stack.rcp -lzboss_port +esp32c6.menu.ZigbeeMode.ed_debug=Zigbee ED (end device) - Debug +esp32c6.menu.ZigbeeMode.ed_debug.build.zigbee_mode=-DZIGBEE_MODE_ED +esp32c6.menu.ZigbeeMode.ed_debug.build.zigbee_libs=-lesp_zb_api_ed.debug -lesp_zb_cli_command -lzboss_stack.ed.debug -lzboss_port.debug +esp32c6.menu.ZigbeeMode.zczr_debug=Zigbee ZCZR (coordinator/router) - Debug +esp32c6.menu.ZigbeeMode.zczr_debug.build.zigbee_mode=-DZIGBEE_MODE_ZCZR +esp32c6.menu.ZigbeeMode.zczr_debug.build.zigbee_libs=-lesp_zb_api_zczr.debug -lesp_zb_cli_command -lzboss_stack.zczr.debug -lzboss_port.debug +esp32c6.menu.ZigbeeMode.rcp_debug=Zigbee RCP (radio co-processor) - Debug +esp32c6.menu.ZigbeeMode.rcp_debug.build.zigbee_mode=-DZIGBEE_MODE_RCP +esp32c6.menu.ZigbeeMode.rcp_debug.build.zigbee_libs=-lesp_zb_api_rcp.debug -lesp_zb_cli_command -lzboss_stack.rcp.debug -lzboss_port.debug ############################################################## diff --git a/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino b/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino index 7acf6e362df..7ff7a020632 100644 --- a/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino +++ b/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino @@ -64,6 +64,11 @@ void identify(uint16_t time) { /********************* Arduino functions **************************/ void setup() { + Serial.begin(115200); + while (!Serial) { + delay(10); + } + // Init RMT and leave light OFF rgbLedWrite(LED_PIN, 0, 0, 0); @@ -80,12 +85,21 @@ void setup() { zbColorLight.setManufacturerAndModel("Espressif", "ZBColorLightBulb"); // Add endpoint to Zigbee Core - log_d("Adding ZigbeeLight endpoint to Zigbee Core"); + Serial.println("Adding ZigbeeLight endpoint to Zigbee Core"); Zigbee.addEndpoint(&zbColorLight); - // When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE - log_d("Calling Zigbee.begin()"); - Zigbee.begin(); + // When all EPs are registered, start Zigbee in End Device mode + if (!Zigbee.begin()) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } + Serial.println("Connecting to network"); + while (!Zigbee.connected()) { + Serial.print("."); + delay(100); + } + Serial.println(); } void loop() { @@ -98,7 +112,8 @@ void loop() { delay(50); if ((millis() - startTime) > 3000) { // If key pressed for more than 3secs, factory reset Zigbee and reboot - Serial.printf("Resetting Zigbee to factory settings, reboot.\n"); + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); + delay(1000); Zigbee.factoryReset(); } } diff --git a/libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino b/libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino index 6d6c7b163dd..00e3cd170ef 100644 --- a/libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino +++ b/libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino @@ -46,7 +46,6 @@ ZigbeeColorDimmerSwitch zbSwitch = ZigbeeColorDimmerSwitch(SWITCH_ENDPOINT_NUMBE /********************* Arduino functions **************************/ void setup() { - Serial.begin(115200); while (!Serial) { delay(10); @@ -68,11 +67,15 @@ void setup() { Zigbee.setRebootOpenNetwork(180); //When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode - Zigbee.begin(ZIGBEE_COORDINATOR); + if (!Zigbee.begin(ZIGBEE_COORDINATOR)) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } Serial.println("Waiting for Light to bound to the switch"); //Wait for switch to bound to a light: - while (!zbSwitch.isBound()) { + while (!zbSwitch.bound()) { Serial.printf("."); delay(500); } diff --git a/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino b/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino index 53d43895365..92c59044cae 100644 --- a/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino +++ b/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino @@ -45,6 +45,10 @@ void setLED(bool 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); @@ -59,12 +63,21 @@ void setup() { zbLight.onLightChange(setLED); //Add endpoint to Zigbee Core - log_d("Adding ZigbeeLight endpoint to Zigbee Core"); + Serial.println("Adding ZigbeeLight endpoint to Zigbee Core"); Zigbee.addEndpoint(&zbLight); // When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE - log_d("Calling Zigbee.begin()"); - Zigbee.begin(); + if (!Zigbee.begin()) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } + Serial.println("Connecting to network"); + while (!Zigbee.connected()) { + Serial.print("."); + delay(100); + } + Serial.println(); } void loop() { @@ -77,7 +90,8 @@ void loop() { delay(50); if ((millis() - startTime) > 3000) { // If key pressed for more than 3secs, factory reset Zigbee and reboot - Serial.printf("Resetting Zigbee to factory settings, reboot.\n"); + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); + delay(1000); Zigbee.factoryReset(); } } diff --git a/libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino b/libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino index 69cf6654a41..e38734a052a 100644 --- a/libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino +++ b/libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino @@ -70,6 +70,7 @@ ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER); static void onZbButton(SwitchData *button_func_pair) { if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) { // Send toggle command to the light + Serial.println("Toggling light"); zbSwitch.lightToggle(); } } @@ -93,7 +94,6 @@ static void enableGpioInterrupt(bool enabled) { /********************* Arduino functions **************************/ void setup() { - Serial.begin(115200); while (!Serial) { delay(10); @@ -106,7 +106,7 @@ void setup() { zbSwitch.allowMultipleBinding(true); //Add endpoint to Zigbee Core - log_d("Adding ZigbeeSwitch endpoint to Zigbee Core"); + Serial.println("Adding ZigbeeSwitch endpoint to Zigbee Core"); Zigbee.addEndpoint(&zbSwitch); //Open network for 180 seconds after boot @@ -118,19 +118,22 @@ void setup() { /* create a queue to handle gpio event from isr */ gpio_evt_queue = xQueueCreate(10, sizeof(SwitchData)); if (gpio_evt_queue == 0) { - log_e("Queue was not created and must not be used"); - while (1); + Serial.println("Queue creating failed, rebooting..."); + ESP.restart(); } attachInterruptArg(buttonFunctionPair[i].pin, onGpioInterrupt, (void *)(buttonFunctionPair + i), FALLING); } // When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode - log_d("Calling Zigbee.begin()"); - Zigbee.begin(ZIGBEE_COORDINATOR); + if (!Zigbee.begin(ZIGBEE_COORDINATOR)) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } Serial.println("Waiting for Light to bound to the switch"); //Wait for switch to bound to a light: - while (!zbSwitch.isBound()) { + while (!zbSwitch.bound()) { Serial.printf("."); delay(500); } diff --git a/libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino b/libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino index 7d59fb2907d..c3a74e0aee5 100644 --- a/libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino +++ b/libraries/Zigbee/examples/Zigbee_Scan_Networks/Zigbee_Scan_Networks.ino @@ -32,7 +32,7 @@ #include "Zigbee.h" #ifdef ZIGBEE_MODE_ZCZR -zigbee_role_t role = ZIGBEE_ROUTER; // or can be ZIGBEE_COORDINATOR, but it wont scan itself +zigbee_role_t role = ZIGBEE_ROUTER; // or can be ZIGBEE_COORDINATOR, but it won't scan itself #else zigbee_role_t role = ZIGBEE_END_DEVICE; #endif @@ -81,14 +81,13 @@ void setup() { } // Initialize Zigbee stack without any EPs just for scanning - Zigbee.begin(role); - - // Waint until Zigbee stack is ready - while (!Zigbee.isStarted()) { - delay(100); + if (!Zigbee.begin(role)) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); } - Serial.println("Setup done"); + Serial.println("Setup done, starting Zigbee network scan..."); // Start Zigbee Network Scan with default parameters (all channels, scan time 5) Zigbee.scanNetworks(); } @@ -98,7 +97,7 @@ void loop() { int16_t ZigbeeScanStatus = Zigbee.scanComplete(); if (ZigbeeScanStatus < 0) { // it is busy scanning or got an error if (ZigbeeScanStatus == ZB_SCAN_FAILED) { - Serial.println("WiFi Scan has failed. Starting again."); + Serial.println("Zigbee scan has failed. Starting again."); Zigbee.scanNetworks(); } // other option is status ZB_SCAN_RUNNING - just wait. diff --git a/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino index 530995a8427..e13e6a877a6 100644 --- a/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino +++ b/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino @@ -27,7 +27,7 @@ */ #ifndef ZIGBEE_MODE_ED -#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" +#error "Zigbee end device mode is not selected in Tools->Zigbee mode" #endif #include "Zigbee.h" @@ -56,14 +56,19 @@ void meausureAndSleep() { zbTempSensor.reportTemperature(); zbTempSensor.reportHumidity(); - log_d("Temperature: %.2f°C, Humidity: %.2f%", temperature, humidity); + Serial.printf("Reported temperature: %.2f°C, Humidity: %.2f%%\r\n", temperature, humidity); // Put device to deep sleep + Serial.println("Going to sleep now"); esp_deep_sleep_start(); } /********************* Arduino functions **************************/ void setup() { + Serial.begin(115200); + while (!Serial) { + delay(10); + } // Init button switch pinMode(BUTTON_PIN, INPUT_PULLUP); @@ -94,14 +99,20 @@ void setup() { zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000; // When all EPs are registered, start Zigbee in End Device mode - Zigbee.begin(&zigbeeConfig, false); - - // Wait for Zigbee to start - while (!Zigbee.isStarted()) { + if (!Zigbee.begin(&zigbeeConfig, false)) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } + Serial.println("Connecting to network"); + while (!Zigbee.connected()) { + Serial.print("."); delay(100); } + Serial.println(); + Serial.println("Successfully connected to Zigbee network"); - // Delay 5s to allow establishing connection with coordinator, needed for sleepy devices + // Delay 5s (may be adjusted) to allow establishing proper connection with coordinator, needed for sleepy devices delay(5000); } @@ -115,7 +126,8 @@ void loop() { delay(50); if ((millis() - startTime) > 3000) { // If key pressed for more than 3secs, factory reset Zigbee and reboot - Zigbee.factoryReset(); + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); + delay(1000); } } } diff --git a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor/Zigbee_Temperature_Sensor.ino b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor/Zigbee_Temperature_Sensor.ino index c5ca00decd6..255b074265e 100644 --- a/libraries/Zigbee/examples/Zigbee_Temperature_Sensor/Zigbee_Temperature_Sensor.ino +++ b/libraries/Zigbee/examples/Zigbee_Temperature_Sensor/Zigbee_Temperature_Sensor.ino @@ -27,7 +27,7 @@ */ #ifndef ZIGBEE_MODE_ED -#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" +#error "Zigbee end device mode is not selected in Tools->Zigbee mode" #endif #include "Zigbee.h" @@ -42,7 +42,7 @@ static void temp_sensor_value_update(void *arg) { for (;;) { // Read temperature sensor value float tsens_value = temperatureRead(); - log_v("Temperature sensor value: %.2f°C", tsens_value); + Serial.printf("Updated temperature sensor value to %.2f°C\r\n", tsens_value); // Update temperature value in Temperature sensor EP zbTempSensor.setTemperature(tsens_value); delay(1000); @@ -51,12 +51,10 @@ 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); @@ -72,8 +70,21 @@ void setup() { // Add endpoint to Zigbee Core Zigbee.addEndpoint(&zbTempSensor); + Serial.println("Starting Zigbee..."); // When all EPs are registered, start Zigbee in End Device mode - Zigbee.begin(); + if (!Zigbee.begin()) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } else { + Serial.println("Zigbee started successfully!"); + } + Serial.println("Connecting to network"); + while (!Zigbee.connected()) { + Serial.print("."); + delay(100); + } + Serial.println(); // Start Temperature sensor reading task xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 2048, NULL, 10, NULL); @@ -96,7 +107,8 @@ void loop() { delay(50); if ((millis() - startTime) > 3000) { // If key pressed for more than 3secs, factory reset Zigbee and reboot - Serial.printf("Resetting Zigbee to factory settings, reboot.\n"); + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); + delay(1000); Zigbee.factoryReset(); } } diff --git a/libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino b/libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino index 565d9e64919..df02f891794 100644 --- a/libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino +++ b/libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino @@ -80,19 +80,22 @@ void setup() { Zigbee.setRebootOpenNetwork(180); // When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode - Zigbee.begin(ZIGBEE_COORDINATOR); - - Serial.println("Waiting for Temperature sensor to bound to the switch"); + if (!Zigbee.begin(ZIGBEE_COORDINATOR)) { + Serial.println("Zigbee failed to start!"); + Serial.println("Rebooting..."); + ESP.restart(); + } - //Wait for switch to bound to a light: - while (!zbThermostat.isBound()) { + Serial.println("Waiting for Temperature sensor to bound to the thermostat"); + while (!zbThermostat.bound()) { Serial.printf("."); delay(500); } + Serial.println(); + // Get temperature sensor configuration zbThermostat.getSensorSettings(); - Serial.println(); } void loop() { diff --git a/libraries/Zigbee/keywords.txt b/libraries/Zigbee/keywords.txt index 53ce5fffe2a..1a8b7ae85ba 100644 --- a/libraries/Zigbee/keywords.txt +++ b/libraries/Zigbee/keywords.txt @@ -30,14 +30,18 @@ zigbee_scan_result_t KEYWORD1 ####################################### # ZigbeeCore -isStarted KEYWORD2 +begin KEYWORD2 +started KEYWORD2 +connected KEYWORD2 +getRole KEYWORD2 addEndpoint KEYWORD2 setRadioConfig KEYWORD2 -setHostConfig KEYWORD2 getRadioConfig KEYWORD2 +setHostConfig KEYWORD2 getHostConfig KEYWORD2 setPrimaryChannelMask KEYWORD2 setRebootOpenNetwork KEYWORD2 +openNetwork KEYWORD2 scanNetworks KEYWORD2 scanComplete KEYWORD2 getScanResult KEYWORD2 @@ -45,21 +49,33 @@ scanDelete KEYWORD2 factoryReset KEYWORD2 # Common ZigbeeEP +setEpConfig KEYWORD2 setVersion KEYWORD2 -setManufacturerAndModel KEYWORD2 -is_bound KEYWORD2 +getEndpoint KEYWORD2 printBoundDevices KEYWORD2 +getBoundDevices KEYWORD2 +bound KEYWORD2 allowMultipleBinding KEYWORD2 +setManufacturerAndModel KEYWORD2 +setPowerSource KEYWORD2 +setBatteryPercentage KEYWORD2 +reportBatteryPercentage KEYWORD2 +readManufacturer KEYWORD2 +readModel KEYWORD2 +onIdentify KEYWORD2 # ZigbeeLight + ZigbeeColorDimmableLight -setOnOff KEYWORD2 -sceneControl KEYWORD2 -setOnOffTime KEYWORD2 -setOffWaitTime KEYWORD2 -setLevel KEYWORD2 -setColor KEYWORD2 -setColorSaturation KEYWORD2 -setColorHue KEYWORD2 +onLightChange KEYWORD2 +restoreLight KEYWORD2 +setLight KEYWORD2 +setLightState KEYWORD2 +setLightLevel KEYWORD2 +setLightColor KEYWORD2 +getLightState KEYWORD2 +getLightLevel KEYWORD2 +getLightRed KEYWORD2 +getLightGreen KEYWORD2 +getLightBlue KEYWORD2 # ZigbeeSwitch + ZigbeeColorDimmerSwitch lightToggle KEYWORD2 @@ -70,22 +86,23 @@ lightOnWithTimedOff KEYWORD2 lightOnWithSceneRecall KEYWORD2 setLightLevel KEYWORD2 setLightColor KEYWORD2 -setLightColorSaturation KEYWORD2 -setLightColorHue KEYWORD2 -# ZigbeeTempSensor +# ZigbeeTempSensor + humidity setTemperature KEYWORD2 setMinMaxValue KEYWORD2 setTolerance KEYWORD2 setReporting KEYWORD2 reportTemperature KEYWORD2 +addHumiditySensor KEYWORD2 +setHumidity KEYWORD2 +setHumidityReporting KEYWORD2 +reportHumidity KEYWORD2 # ZigbeeThermostat -temperatureRead KEYWORD2 -temperatureMin KEYWORD2 -temperatureMax KEYWORD2 -temperatureTolerance KEYWORD2 +onTempRecieve KEYWORD2 +onConfigRecieve KEYWORD2 getTemperature KEYWORD2 +getSensorSettings KEYWORD2 setTemperatureReporting KEYWORD2 ####################################### diff --git a/libraries/Zigbee/src/ZigbeeCore.cpp b/libraries/Zigbee/src/ZigbeeCore.cpp index dd84e2a63e4..38ef9b169d5 100644 --- a/libraries/Zigbee/src/ZigbeeCore.cpp +++ b/libraries/Zigbee/src/ZigbeeCore.cpp @@ -6,6 +6,8 @@ #include "ZigbeeHandlers.cpp" #include "Arduino.h" +#define ZB_INIT_TIMEOUT 10000 // 10 seconds + extern "C" void zb_set_ed_node_descriptor(bool power_src, bool rx_on_when_idle, bool alloc_addr); static bool edBatteryPowered = false; @@ -17,6 +19,13 @@ ZigbeeCore::ZigbeeCore() { _open_network = 0; _scan_status = ZB_SCAN_FAILED; _started = false; + _connected = false; + if (!lock) { + lock = xSemaphoreCreateBinary(); + if (lock == NULL) { + log_e("Semaphore creation failed"); + } + } } ZigbeeCore::~ZigbeeCore() {} @@ -25,10 +34,14 @@ static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id, bool ZigbeeCore::begin(esp_zb_cfg_t *role_cfg, bool erase_nvs) { if (!zigbeeInit(role_cfg, erase_nvs)) { + log_e("ZigbeeCore begin failed"); return false; } _role = (zigbee_role_t)role_cfg->esp_zb_role; - return true; + if (xSemaphoreTake(lock, ZB_INIT_TIMEOUT) != pdTRUE) { + log_e("ZigbeeCore begin timeout"); + } + return started(); } bool ZigbeeCore::begin(zigbee_role_t role, bool erase_nvs) { @@ -57,7 +70,10 @@ bool ZigbeeCore::begin(zigbee_role_t role, bool erase_nvs) { } default: log_e("Invalid Zigbee Role"); return false; } - return status; + if (!status || xSemaphoreTake(lock, ZB_INIT_TIMEOUT) != pdTRUE) { + log_e("ZigbeeCore begin failed or timeout"); + } + return started(); } void ZigbeeCore::addEndpoint(ZigbeeEP *ep) { @@ -167,7 +183,7 @@ void ZigbeeCore::setRebootOpenNetwork(uint8_t time) { } void ZigbeeCore::openNetwork(uint8_t time) { - if (isStarted()) { + if (started()) { log_v("Opening network for joining for %d seconds", time); esp_zb_bdb_open_network(time); } @@ -203,21 +219,24 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) { } else { log_i("Start network steering"); esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING); + Zigbee._started = true; + xSemaphoreGive(Zigbee.lock); } - //----------------- - } else { log_i("Device rebooted"); Zigbee._started = true; + xSemaphoreGive(Zigbee.lock); if ((zigbee_role_t)Zigbee.getRole() == ZIGBEE_COORDINATOR && Zigbee._open_network > 0) { log_i("Opening network for joining for %d seconds", Zigbee._open_network); esp_zb_bdb_open_network(Zigbee._open_network); + } else { + Zigbee._connected = true; } } } else { /* commissioning failed */ log_e("Failed to initialize Zigbee stack (status: %s)", esp_err_to_name(err_status)); - esp_restart(); + xSemaphoreGive(Zigbee.lock); } break; case ESP_ZB_BDB_SIGNAL_FORMATION: // Coordinator @@ -243,6 +262,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) { log_i("Network steering started"); } Zigbee._started = true; + xSemaphoreGive(Zigbee.lock); } else { if (err_status == ESP_OK) { esp_zb_ieee_addr_t extended_pan_id; @@ -252,7 +272,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) { 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], extended_pan_id[0], esp_zb_get_pan_id(), esp_zb_get_current_channel(), esp_zb_get_short_address() ); - Zigbee._started = true; + Zigbee._connected = true; } else { log_i("Network steering was not successful (status: %s)", esp_err_to_name(err_status)); esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_STEERING, 1000); @@ -281,7 +301,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) { // for each endpoint in the list call the findEndpoint function if not bounded or allowed to bind multiple devices for (std::list::iterator it = Zigbee.ep_objects.begin(); it != Zigbee.ep_objects.end(); ++it) { - if (!(*it)->isBound() || (*it)->epAllowMultipleBinding()) { + if (!(*it)->bound() || (*it)->epAllowMultipleBinding()) { (*it)->findEndpoint(&cmd_req); } } @@ -335,7 +355,7 @@ void ZigbeeCore::scanCompleteCallback(esp_zb_zdp_status_t zdo_status, uint8_t co } void ZigbeeCore::scanNetworks(u_int32_t channel_mask, u_int8_t scan_duration) { - if (!isStarted()) { + if (!started()) { log_e("Zigbee stack is not started, cannot scan networks"); return; } diff --git a/libraries/Zigbee/src/ZigbeeCore.h b/libraries/Zigbee/src/ZigbeeCore.h index 08ff059dd51..e068d74430e 100644 --- a/libraries/Zigbee/src/ZigbeeCore.h +++ b/libraries/Zigbee/src/ZigbeeCore.h @@ -70,9 +70,11 @@ class ZigbeeCore { esp_zb_ep_list_t *_zb_ep_list; zigbee_role_t _role; bool _started; + bool _connected; uint8_t _open_network; zigbee_scan_result_t *_scan_result; + SemaphoreHandle_t lock; bool zigbeeInit(esp_zb_cfg_t *zb_cfg, bool erase_nvs); static void scanCompleteCallback(esp_zb_zdp_status_t zdo_status, uint8_t count, esp_zb_network_descriptor_t *nwk_descriptor); @@ -88,9 +90,12 @@ class ZigbeeCore { bool begin(esp_zb_cfg_t *role_cfg, bool erase_nvs = false); // bool end(); - bool isStarted() { + bool started() { return _started; } + bool connected() { + return _connected; + } zigbee_role_t getRole() { return _role; } diff --git a/libraries/Zigbee/src/ZigbeeEP.cpp b/libraries/Zigbee/src/ZigbeeEP.cpp index cd77b99996b..0a947d3ab9e 100644 --- a/libraries/Zigbee/src/ZigbeeEP.cpp +++ b/libraries/Zigbee/src/ZigbeeEP.cpp @@ -19,14 +19,12 @@ ZigbeeEP::ZigbeeEP(uint8_t endpoint) { _ep_config.endpoint = 0; _cluster_list = nullptr; _on_identify = nullptr; -#if !CONFIG_DISABLE_HAL_LOCKS if (!lock) { lock = xSemaphoreCreateBinary(); if (lock == NULL) { log_e("Semaphore creation failed"); } } -#endif } ZigbeeEP::~ZigbeeEP() {} diff --git a/libraries/Zigbee/src/ZigbeeEP.h b/libraries/Zigbee/src/ZigbeeEP.h index 1c0c2d983ab..21ed7706d31 100644 --- a/libraries/Zigbee/src/ZigbeeEP.h +++ b/libraries/Zigbee/src/ZigbeeEP.h @@ -73,7 +73,7 @@ class ZigbeeEP { return _bound_devices; } - static bool isBound() { + static bool bound() { return _is_bound; } static void allowMultipleBinding(bool bind) { diff --git a/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.h b/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.h index eb854b6c919..9b623d2f8ef 100644 --- a/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.h +++ b/libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.h @@ -39,14 +39,6 @@ class ZigbeeColorDimmerSwitch : public ZigbeeEP { void setLightColor(uint8_t red, uint8_t green, uint8_t blue, uint16_t group_addr); void setLightColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t endpoint, uint16_t short_addr); - void setLightColorSaturation(uint8_t value); - void setLightColorSaturation(uint8_t value, uint16_t group_addr); - void setLightColorSaturation(uint8_t value, uint8_t endpoint, uint16_t short_addr); - - void setLightColorHue(uint8_t value); - void setLightColorHue(uint8_t value, uint16_t group_addr); - void setLightColorHue(uint8_t value, uint8_t endpoint, uint16_t short_addr); - private: // save instance of the class in order to use it in static functions static ZigbeeColorDimmerSwitch *_instance;