From 0baa931a1daf1295f19e367dc2ebb7b9e1a568d6 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 7 Jul 2025 11:09:47 -0300 Subject: [PATCH 01/10] feat(matter): new matter example --- .../MatterLambdaSingleCallbackManyEPs.ino | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino diff --git a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino new file mode 100644 index 00000000000..4e2ce6483d5 --- /dev/null +++ b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino @@ -0,0 +1,114 @@ +// Copyright 2025 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. + +// Matter Manager +#include + +#if !CONFIG_ENABLE_CHIPOBLE +// if the device can be commissioned using BLE, WiFi is not used - save flash space +#include + +// CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network +// 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 +#endif + +//number of On-Off Lights: +const uint8_t MAX_LIGHT_NUMBER = 6; + +// array of OnOffLight endpoints +MatterOnOffLight OnOffLight[MAX_LIGHT_NUMBER]; + +// all pins, one for each on-off light +uint8_t lightPins[MAX_LIGHT_NUMBER] = { 2, 4, 6, 8, 10, 12 }; // must replace it by the real pin for the target SoC and application + +// friendly OnOffLights names used for printing a message in the callback +const char *lightName[MAX_LIGHT_NUMBER] = { + "Room 1", + "Room 2", + "Room 3", + "Room 4", + "Room 5", + "Room 6", +}; + +// simple setup() function +void setup() { + Serial.begin(115200); // callback will just print a message in the console + + // CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network +#if !CONFIG_ENABLE_CHIPOBLE + // We start by connecting to a WiFi network + Serial.print("Connecting to "); + Serial.println(ssid); + // Manually connect to WiFi + WiFi.begin(ssid, password); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("\r\nWiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + delay(500); +#endif + + // setup all the OnOff Light endpoint and their lambda callback functions + for (uint8_t i = 0; i < MAX_LIGHT_NUMBER; i++) { + pinMode( lightPins[i], OUTPUT); // set the GPIO function + OnOffLight[i].begin(false); // off + + // inline lambda function using capture array index -> it will just print a message in the console + OnOffLight[i].onChangeOnOff([i](bool state) -> bool { + // Display message with the specific light name and details + Serial.printf("Matter App Control: '%s' (OnOffLight[%d], Endpoint %d, GPIO %d) changed to: %s\r\n", + lightName[i], i, OnOffLight[i].getEndPointId(), + lightPins[i], state ? "ON" : "OFF"); + + return true; + }); + } + // last step, starting Matter Stack + Matter.begin(); +} + +void loop() { + // Check Matter Plugin Commissioning state, which may change during execution of loop() + if (!Matter.isDeviceCommissioned()) { + Serial.println(""); + Serial.println("Matter Node is not commissioned yet."); + Serial.println("Initiate the device discovery in your Matter environment."); + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + // waits for Matter Plugin Commissioning. + uint32_t timeCount = 0; + while (!Matter.isDeviceCommissioned()) { + delay(100); + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); + } + } + } else { + if (!Matter.isDeviceConnected()) { + Serial.println("Matter Node is commissioned and connected to the network. Ready for use."); + } else { + Serial.println("Matter Node is commissioned. Waiting for the network connection."); + } + } + + delay(100); +} From 939fd5e58d144a333e5a2d224669e4b23b01fb61 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 7 Jul 2025 11:17:17 -0300 Subject: [PATCH 02/10] feat(matter): create ci.json --- .../examples/MatterLambdaSingleCallbackManyEPs/ci.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/ci.json diff --git a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/ci.json b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/ci.json new file mode 100644 index 00000000000..556a8a9ee6b --- /dev/null +++ b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/ci.json @@ -0,0 +1,7 @@ +{ + "fqbn_append": "PartitionScheme=huge_app", + "requires": [ + "CONFIG_SOC_WIFI_SUPPORTED=y", + "CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y" + ] +} From 78a11e559e150839fe3b753efee91a1e3bb0de14 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 7 Jul 2025 11:19:02 -0300 Subject: [PATCH 03/10] feat(matter): limit it to WiFi only --- .../MatterLambdaSingleCallbackManyEPs.ino | 8 -------- 1 file changed, 8 deletions(-) diff --git a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino index 4e2ce6483d5..0ee2b716aa8 100644 --- a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino +++ b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino @@ -14,16 +14,11 @@ // Matter Manager #include - -#if !CONFIG_ENABLE_CHIPOBLE -// if the device can be commissioned using BLE, WiFi is not used - save flash space #include -// CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network // 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 -#endif //number of On-Off Lights: const uint8_t MAX_LIGHT_NUMBER = 6; @@ -48,8 +43,6 @@ const char *lightName[MAX_LIGHT_NUMBER] = { void setup() { Serial.begin(115200); // callback will just print a message in the console - // CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network -#if !CONFIG_ENABLE_CHIPOBLE // We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(ssid); @@ -64,7 +57,6 @@ void setup() { Serial.println("IP address: "); Serial.println(WiFi.localIP()); delay(500); -#endif // setup all the OnOff Light endpoint and their lambda callback functions for (uint8_t i = 0; i < MAX_LIGHT_NUMBER; i++) { From f397e9167053f380062a8bf0bb5044d1e6eab746 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 7 Jul 2025 11:23:32 -0300 Subject: [PATCH 04/10] feat(matter): adds basic documentation to the example --- .../MatterLambdaSingleCallbackManyEPs.ino | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino index 0ee2b716aa8..9c6b39e5692 100644 --- a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino +++ b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino @@ -12,6 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. +/* + This example create 6 on-off light endpoint that share the same onChangeOnOff() callback code. + It uses Lambda Function with an extra Lambda Capture information that links the Endpoint to its individual information. + After the Matter example is commissioned, the expected Serial output shall be similar to this: + +Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: OFF +Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: ON +Matter App Control: 'Room 5' (OnOffLight[4], Endpoint 5, GPIO 10) changed to: ON +Matter App Control: 'Room 2' (OnOffLight[1], Endpoint 2, GPIO 4) changed to: ON +Matter App Control: 'Room 4' (OnOffLight[3], Endpoint 4, GPIO 8) changed to: ON +Matter App Control: 'Room 6' (OnOffLight[5], Endpoint 6, GPIO 12) changed to: ON +Matter App Control: 'Room 3' (OnOffLight[2], Endpoint 3, GPIO 6) changed to: ON +Matter App Control: 'Room 5' (OnOffLight[4], Endpoint 5, GPIO 10) changed to: OFF +*/ + // Matter Manager #include #include From 118450653ac75fb14ec9f544331d0fb89ee68d1e Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Thu, 10 Jul 2025 16:09:21 -0300 Subject: [PATCH 05/10] feat(matter): update keywords.txt --- libraries/Matter/keywords.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libraries/Matter/keywords.txt b/libraries/Matter/keywords.txt index 68aaebb1d4d..edba06083bd 100644 --- a/libraries/Matter/keywords.txt +++ b/libraries/Matter/keywords.txt @@ -36,8 +36,10 @@ EndPointSpeedCB KEYWORD1 EndPointOnOffCB KEYWORD1 EndPointBrightnessCB KEYWORD1 EndPointRGBColorCB KEYWORD1 +EndPointIdentifyCB KEYWORD1 matterEvent_t KEYWORD1 matterEventCB KEYWORD1 +attrOperation_t KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -111,6 +113,10 @@ onChangeLocalTemperature KEYWORD2 onChangeCoolingSetpoint KEYWORD2 onChangeHeatingSetpoint KEYWORD2 onEvent KEYWORD2 +setEndPointId KEYWORD2 +getEndPointId KEYWORD2 +onIdentify KEYWORD2 +endpointIdentifyCB KEYWORD2 ####################################### # Constants (LITERAL1) From 7cbcf7f21d2790f7991bb735995bee9a2a38b69b Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Thu, 10 Jul 2025 18:56:15 -0300 Subject: [PATCH 06/10] fix(matter): commentary typo Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../MatterLambdaSingleCallbackManyEPs.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino index 9c6b39e5692..687e8c6e374 100644 --- a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino +++ b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino @@ -13,7 +13,7 @@ // limitations under the License. /* - This example create 6 on-off light endpoint that share the same onChangeOnOff() callback code. + This example creates 6 on-off light endpoints that share the same onChangeOnOff() callback code. It uses Lambda Function with an extra Lambda Capture information that links the Endpoint to its individual information. After the Matter example is commissioned, the expected Serial output shall be similar to this: From a10d3832c3289e7d29d65db2ffe690d4bc767655 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Thu, 10 Jul 2025 18:56:43 -0300 Subject: [PATCH 07/10] fix(matter): worng test logic Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../MatterLambdaSingleCallbackManyEPs.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino index 687e8c6e374..b7b5548188f 100644 --- a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino +++ b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino @@ -110,7 +110,7 @@ void loop() { } } } else { - if (!Matter.isDeviceConnected()) { + if (Matter.isDeviceConnected()) { Serial.println("Matter Node is commissioned and connected to the network. Ready for use."); } else { Serial.println("Matter Node is commissioned. Waiting for the network connection."); From 54ea63e1aae0ca27809ab642fe060934d005dd83 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Thu, 10 Jul 2025 23:48:59 -0300 Subject: [PATCH 08/10] feat(matter): adds a waiting time --- .../MatterLambdaSingleCallbackManyEPs.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino index b7b5548188f..dfb91860889 100644 --- a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino +++ b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino @@ -115,6 +115,8 @@ void loop() { } else { Serial.println("Matter Node is commissioned. Waiting for the network connection."); } + // wait 3 seconds for the network connection + delay(3000); } delay(100); From b71205394d3b5540e852f14b1acbe035c0cb2398 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Thu, 10 Jul 2025 23:59:29 -0300 Subject: [PATCH 09/10] fix(matter): wifi is always already connected --- .../MatterLambdaSingleCallbackManyEPs.ino | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino index dfb91860889..01639c1b18f 100644 --- a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino +++ b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino @@ -109,15 +109,8 @@ void loop() { Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); } } - } else { - if (Matter.isDeviceConnected()) { - Serial.println("Matter Node is commissioned and connected to the network. Ready for use."); - } else { - Serial.println("Matter Node is commissioned. Waiting for the network connection."); - } - // wait 3 seconds for the network connection - delay(3000); - } - - delay(100); + Serial.println("Matter Node is commissioned and connected to the WiFi network. Ready for use."); + } + + delay(500); } From db60c57cd67359c8530eb0458fbae71bdfdc2311 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 11 Jul 2025 13:21:33 +0000 Subject: [PATCH 10/10] ci(pre-commit): Apply automatic fixes --- .../MatterLambdaSingleCallbackManyEPs.ino | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino index 01639c1b18f..4992771d925 100644 --- a/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino +++ b/libraries/Matter/examples/MatterLambdaSingleCallbackManyEPs/MatterLambdaSingleCallbackManyEPs.ino @@ -42,16 +42,11 @@ const uint8_t MAX_LIGHT_NUMBER = 6; MatterOnOffLight OnOffLight[MAX_LIGHT_NUMBER]; // all pins, one for each on-off light -uint8_t lightPins[MAX_LIGHT_NUMBER] = { 2, 4, 6, 8, 10, 12 }; // must replace it by the real pin for the target SoC and application +uint8_t lightPins[MAX_LIGHT_NUMBER] = {2, 4, 6, 8, 10, 12}; // must replace it by the real pin for the target SoC and application // friendly OnOffLights names used for printing a message in the callback const char *lightName[MAX_LIGHT_NUMBER] = { - "Room 1", - "Room 2", - "Room 3", - "Room 4", - "Room 5", - "Room 6", + "Room 1", "Room 2", "Room 3", "Room 4", "Room 5", "Room 6", }; // simple setup() function @@ -75,15 +70,16 @@ void setup() { // setup all the OnOff Light endpoint and their lambda callback functions for (uint8_t i = 0; i < MAX_LIGHT_NUMBER; i++) { - pinMode( lightPins[i], OUTPUT); // set the GPIO function - OnOffLight[i].begin(false); // off + pinMode(lightPins[i], OUTPUT); // set the GPIO function + OnOffLight[i].begin(false); // off // inline lambda function using capture array index -> it will just print a message in the console OnOffLight[i].onChangeOnOff([i](bool state) -> bool { // Display message with the specific light name and details - Serial.printf("Matter App Control: '%s' (OnOffLight[%d], Endpoint %d, GPIO %d) changed to: %s\r\n", - lightName[i], i, OnOffLight[i].getEndPointId(), - lightPins[i], state ? "ON" : "OFF"); + Serial.printf( + "Matter App Control: '%s' (OnOffLight[%d], Endpoint %d, GPIO %d) changed to: %s\r\n", lightName[i], i, OnOffLight[i].getEndPointId(), lightPins[i], + state ? "ON" : "OFF" + ); return true; }); @@ -110,7 +106,7 @@ void loop() { } } Serial.println("Matter Node is commissioned and connected to the WiFi network. Ready for use."); - } - - delay(500); + } + + delay(500); }