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/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index fb8444484dd..34a2660e3a3 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -33,8 +33,8 @@ #include "hal/gpio_hal.h" #include "esp_rom_gpio.h" -static int s_uart_debug_nr = 0; // UART number for debug output -#define REF_TICK_BAUDRATE_LIMIT 250000 // this is maximum UART badrate using REF_TICK as clock +static int s_uart_debug_nr = 0; // UART number for debug output +#define REF_TICK_BAUDRATE_LIMIT 250000 // this is maximum UART badrate using REF_TICK as clock struct uart_struct_t { diff --git a/libraries/AsyncUDP/keywords.txt b/libraries/AsyncUDP/keywords.txt index 67c0b97a715..1401ea66c72 100644 --- a/libraries/AsyncUDP/keywords.txt +++ b/libraries/AsyncUDP/keywords.txt @@ -8,6 +8,7 @@ AsyncUDP KEYWORD1 AsyncUDPPacket KEYWORD1 +AsyncUDPMessage KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -19,6 +20,21 @@ listen KEYWORD2 listenMulticast KEYWORD2 close KEYWORD2 write KEYWORD2 +space KEYWORD2 +flush KEYWORD2 +isBroadcast KEYWORD2 +isMulticast KEYWORD2 +isIPv6 KEYWORD2 +interface KEYWORD2 +localIPv6 KEYWORD2 +remoteIPv6 KEYWORD2 +remoteMac KEYWORD2 +send KEYWORD2 +peek KEYWORD2 +available KEYWORD2 +writeTo KEYWORD2 +broadcastTo KEYWORD2 +sendTo KEYWORD2 broadcast KEYWORD2 onPacket KEYWORD2 data KEYWORD2 @@ -27,7 +43,17 @@ localIP KEYWORD2 localPort KEYWORD2 remoteIP KEYWORD2 remotePort KEYWORD2 +listenIP KEYWORD2 +listenIPv6 KEYWORD2 +lastErr KEYWORD2 +_s_recv KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### + +TCPIP_ADAPTER_IF_STA LITERAL1 +TCPIP_ADAPTER_IF_STA LITERAL1 +TCPIP_ADAPTER_IF_AP LITERAL1 +TCPIP_ADAPTER_IF_ETH LITERAL1 +TCPIP_ADAPTER_IF_PPP LITERAL1 diff --git a/libraries/Matter/examples/MatterMinimum/MatterMinimum.ino b/libraries/Matter/examples/MatterMinimum/MatterMinimum.ino new file mode 100644 index 00000000000..719c91db23b --- /dev/null +++ b/libraries/Matter/examples/MatterMinimum/MatterMinimum.ino @@ -0,0 +1,81 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * This example is the smallest code that will create a Matter Device which can be + * commissioned and controlled from a Matter Environment APP. + * It controls a GPIO that could be attached to a LED for visualization. + * Additionally the ESP32 will send debug messages indicating the Matter activity. + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. + */ + +// Matter Manager +#include +#include + +// List of Matter Endpoints for this Node +// Single On/Off Light Endpoint - at least one per node +MatterOnOffLight OnOffLight; + +// Light GPIO that can be controlled by Matter APP +#ifdef LED_BUILTIN +const uint8_t ledPin = LED_BUILTIN; +#else +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN +#endif + +// Matter Protocol Endpoint (On/OFF Light) Callback +bool matterCB(bool state) { + digitalWrite(ledPin, state ? HIGH : LOW); + // This callback must return the success state to Matter core + return true; +} + +// WiFi is manually set and started +const char *ssid = "your-ssid"; // Change this to your WiFi SSID +const char *password = "your-password"; // Change this to your WiFi password + +void setup() { + // Initialize the LED GPIO + pinMode(ledPin, OUTPUT); + + WiFi.enableIPv6(true); + // Manually connect to WiFi + WiFi.begin(ssid, password); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + } + + // Initialize at least one Matter EndPoint + OnOffLight.begin(); + + // Associate a callback to the Matter Controller + OnOffLight.onChange(matterCB); + + // Matter beginning - Last step, after all EndPoints are initialized + Matter.begin(); + + if (!Matter.isDeviceCommissioned()) { + log_i("Matter Node is not commissioned yet."); + log_i("Initiate the device discovery in your Matter environment."); + log_i("Commission it to your Matter hub with the manual pairing code or QR code"); + log_i("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + log_i("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + } +} + +void loop() { + delay(500); +} diff --git a/libraries/Matter/examples/MatterMinimum/ci.json b/libraries/Matter/examples/MatterMinimum/ci.json new file mode 100644 index 00000000000..556a8a9ee6b --- /dev/null +++ b/libraries/Matter/examples/MatterMinimum/ci.json @@ -0,0 +1,7 @@ +{ + "fqbn_append": "PartitionScheme=huge_app", + "requires": [ + "CONFIG_SOC_WIFI_SUPPORTED=y", + "CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y" + ] +} diff --git a/libraries/Update/src/Update.h b/libraries/Update/src/Update.h index 5e9323fa138..67189da2f02 100644 --- a/libraries/Update/src/Update.h +++ b/libraries/Update/src/Update.h @@ -7,7 +7,9 @@ #ifndef ESP32UPDATER_H #define ESP32UPDATER_H +#ifndef UPDATE_NOCRYPT #define UPDATE_NOCRYPT +#endif #include #include @@ -145,11 +147,13 @@ class UpdateClass { sets the expected MD5 for the firmware (hexString) If calc_post_decryption is true, the update library will calculate the MD5 after the decryption, if false the calculation occurs before the decryption */ - bool setMD5(const char *expected_md5 + bool setMD5( + const char *expected_md5 #ifndef UPDATE_NOCRYPT -, bool calc_post_decryption = true + , + bool calc_post_decryption = true #endif /* #ifdef UPDATE_NOCRYPT */ -); + ); /* returns the MD5 String of the successfully ended firmware diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index 6f4d3c08210..3b0c517431d 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -73,13 +73,15 @@ bool UpdateClass::_enablePartition(const esp_partition_t *partition) { UpdateClass::UpdateClass() : _error(0), #ifndef UPDATE_NOCRYPT -_cryptKey(0), _cryptBuffer(0), + _cryptKey(0), _cryptBuffer(0), #endif /* UPDATE_NOCRYPT */ -_buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), _command(U_FLASH), _partition(NULL) + _buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), _command(U_FLASH), _partition(NULL) #ifndef UPDATE_NOCRYPT -, _cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf) + , + _cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf) #endif /* UPDATE_NOCRYPT */ -{} +{ +} UpdateClass &UpdateClass::onProgress(THandlerFunction_Progress fn) { _progress_callback = fn; @@ -378,7 +380,7 @@ bool UpdateClass::_writeBuffer() { return false; } } - #endif /* UPDATE_NOCRYPT */ +#endif /* UPDATE_NOCRYPT */ //first bytes of new firmware uint8_t skip = 0; if (!_progress && _command == U_FLASH) { @@ -476,9 +478,11 @@ bool UpdateClass::_verifyEnd() { return false; } -bool UpdateClass::setMD5(const char *expected_md5 +bool UpdateClass::setMD5( + const char *expected_md5 #ifndef UPDATE_NOCRYPT -,bool calc_post_decryption + , + bool calc_post_decryption #endif /* UPDATE_NOCRYPT */ ) { if (strlen(expected_md5) != 32) { 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;