Skip to content

Commit 4ee17de

Browse files
authored
feat(matter): new matter lambda function example (#11561)
Adds a new Matter Library example using lambda function to creat 6 endpoints using a single callback
1 parent 87b718a commit 4ee17de

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/*
16+
This example creates 6 on-off light endpoints that share the same onChangeOnOff() callback code.
17+
It uses Lambda Function with an extra Lambda Capture information that links the Endpoint to its individual information.
18+
After the Matter example is commissioned, the expected Serial output shall be similar to this:
19+
20+
Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: OFF
21+
Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: ON
22+
Matter App Control: 'Room 5' (OnOffLight[4], Endpoint 5, GPIO 10) changed to: ON
23+
Matter App Control: 'Room 2' (OnOffLight[1], Endpoint 2, GPIO 4) changed to: ON
24+
Matter App Control: 'Room 4' (OnOffLight[3], Endpoint 4, GPIO 8) changed to: ON
25+
Matter App Control: 'Room 6' (OnOffLight[5], Endpoint 6, GPIO 12) changed to: ON
26+
Matter App Control: 'Room 3' (OnOffLight[2], Endpoint 3, GPIO 6) changed to: ON
27+
Matter App Control: 'Room 5' (OnOffLight[4], Endpoint 5, GPIO 10) changed to: OFF
28+
*/
29+
30+
// Matter Manager
31+
#include <Matter.h>
32+
#include <WiFi.h>
33+
34+
// WiFi is manually set and started
35+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
36+
const char *password = "your-password"; // Change this to your WiFi password
37+
38+
//number of On-Off Lights:
39+
const uint8_t MAX_LIGHT_NUMBER = 6;
40+
41+
// array of OnOffLight endpoints
42+
MatterOnOffLight OnOffLight[MAX_LIGHT_NUMBER];
43+
44+
// all pins, one for each on-off light
45+
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
46+
47+
// friendly OnOffLights names used for printing a message in the callback
48+
const char *lightName[MAX_LIGHT_NUMBER] = {
49+
"Room 1", "Room 2", "Room 3", "Room 4", "Room 5", "Room 6",
50+
};
51+
52+
// simple setup() function
53+
void setup() {
54+
Serial.begin(115200); // callback will just print a message in the console
55+
56+
// We start by connecting to a WiFi network
57+
Serial.print("Connecting to ");
58+
Serial.println(ssid);
59+
// Manually connect to WiFi
60+
WiFi.begin(ssid, password);
61+
// Wait for connection
62+
while (WiFi.status() != WL_CONNECTED) {
63+
delay(500);
64+
Serial.print(".");
65+
}
66+
Serial.println("\r\nWiFi connected");
67+
Serial.println("IP address: ");
68+
Serial.println(WiFi.localIP());
69+
delay(500);
70+
71+
// setup all the OnOff Light endpoint and their lambda callback functions
72+
for (uint8_t i = 0; i < MAX_LIGHT_NUMBER; i++) {
73+
pinMode(lightPins[i], OUTPUT); // set the GPIO function
74+
OnOffLight[i].begin(false); // off
75+
76+
// inline lambda function using capture array index -> it will just print a message in the console
77+
OnOffLight[i].onChangeOnOff([i](bool state) -> bool {
78+
// Display message with the specific light name and details
79+
Serial.printf(
80+
"Matter App Control: '%s' (OnOffLight[%d], Endpoint %d, GPIO %d) changed to: %s\r\n", lightName[i], i, OnOffLight[i].getEndPointId(), lightPins[i],
81+
state ? "ON" : "OFF"
82+
);
83+
84+
return true;
85+
});
86+
}
87+
// last step, starting Matter Stack
88+
Matter.begin();
89+
}
90+
91+
void loop() {
92+
// Check Matter Plugin Commissioning state, which may change during execution of loop()
93+
if (!Matter.isDeviceCommissioned()) {
94+
Serial.println("");
95+
Serial.println("Matter Node is not commissioned yet.");
96+
Serial.println("Initiate the device discovery in your Matter environment.");
97+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
98+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
99+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
100+
// waits for Matter Plugin Commissioning.
101+
uint32_t timeCount = 0;
102+
while (!Matter.isDeviceCommissioned()) {
103+
delay(100);
104+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
105+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
106+
}
107+
}
108+
Serial.println("Matter Node is commissioned and connected to the WiFi network. Ready for use.");
109+
}
110+
111+
delay(500);
112+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"fqbn_append": "PartitionScheme=huge_app",
3+
"requires": [
4+
"CONFIG_SOC_WIFI_SUPPORTED=y",
5+
"CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y"
6+
]
7+
}

libraries/Matter/keywords.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ EndPointSpeedCB KEYWORD1
3636
EndPointOnOffCB KEYWORD1
3737
EndPointBrightnessCB KEYWORD1
3838
EndPointRGBColorCB KEYWORD1
39+
EndPointIdentifyCB KEYWORD1
3940
matterEvent_t KEYWORD1
4041
matterEventCB KEYWORD1
42+
attrOperation_t KEYWORD1
4143

4244
#######################################
4345
# Methods and Functions (KEYWORD2)
@@ -111,6 +113,10 @@ onChangeLocalTemperature KEYWORD2
111113
onChangeCoolingSetpoint KEYWORD2
112114
onChangeHeatingSetpoint KEYWORD2
113115
onEvent KEYWORD2
116+
setEndPointId KEYWORD2
117+
getEndPointId KEYWORD2
118+
onIdentify KEYWORD2
119+
endpointIdentifyCB KEYWORD2
114120

115121
#######################################
116122
# Constants (LITERAL1)

0 commit comments

Comments
 (0)