Skip to content

Commit d014e3e

Browse files
authored
feat(matter): new lambda example
1 parent 5793b6e commit d014e3e

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 create 6 on-off light endpoint 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 <Preferences.h>
33+
#if !CONFIG_ENABLE_CHIPOBLE
34+
// if the device can be commissioned using BLE, WiFi is not used - save flash space
35+
#include <WiFi.h>
36+
// CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network
37+
// WiFi is manually set and started
38+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
39+
const char *password = "your-password"; // Change this to your WiFi password
40+
#endif
41+
42+
//number of On-Off Lights:
43+
const uint8_t MAX_LIGHT_NUMBER = 6;
44+
45+
// array of OnOffLight endpoints
46+
MatterOnOffLight OnOffLight[MAX_LIGHT_NUMBER];
47+
48+
// all pins, one for each on-off light
49+
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
50+
51+
// friendly OnOffLights names used for printing a message in the callback
52+
const char *lightName[MAX_LIGHT_NUMBER] = {
53+
"Room 1",
54+
"Room 2",
55+
"Room 3",
56+
"Room 4",
57+
"Room 5",
58+
"Room 6",
59+
};
60+
61+
// simple setup() function
62+
void setup() {
63+
Serial.begin(115200); // callback will just print a message in the console
64+
65+
// CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network
66+
#if !CONFIG_ENABLE_CHIPOBLE
67+
// We start by connecting to a WiFi network
68+
Serial.print("Connecting to ");
69+
Serial.println(ssid);
70+
// Manually connect to WiFi
71+
WiFi.begin(ssid, password);
72+
// Wait for connection
73+
while (WiFi.status() != WL_CONNECTED) {
74+
delay(500);
75+
Serial.print(".");
76+
}
77+
Serial.println("\r\nWiFi connected");
78+
Serial.println("IP address: ");
79+
Serial.println(WiFi.localIP());
80+
delay(500);
81+
#endif
82+
83+
// setup all the OnOff Light endpoint and their lambda callback functions
84+
for (uint8_t i = 0; i < MAX_LIGHT_NUMBER; i++) {
85+
pinMode( lightPins[i], OUTPUT); // set the GPIO function
86+
OnOffLight[i].begin(false); // off
87+
88+
// inline lambda function using capture array index -> it will just print a message in the console
89+
OnOffLight[i].onChangeOnOff([i](bool state) -> bool {
90+
// Display message with the specific light name and details
91+
Serial.printf("Matter App Control: '%s' (OnOffLight[%d], Endpoint %d, GPIO %d) changed to: %s\r\n",
92+
lightName[i], i, OnOffLight[i].getEndPointId(),
93+
lightPins[i], state ? "ON" : "OFF");
94+
95+
return true;
96+
});
97+
}
98+
// last step, starting Matter Stack
99+
Matter.begin();
100+
}
101+
102+
void loop() {
103+
// Check Matter Plugin Commissioning state, which may change during execution of loop()
104+
if (!Matter.isDeviceCommissioned()) {
105+
Serial.println("");
106+
Serial.println("Matter Node is not commissioned yet.");
107+
Serial.println("Initiate the device discovery in your Matter environment.");
108+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
109+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
110+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
111+
// waits for Matter Plugin Commissioning.
112+
uint32_t timeCount = 0;
113+
while (!Matter.isDeviceCommissioned()) {
114+
delay(100);
115+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
116+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
117+
}
118+
}
119+
} else {
120+
if (!Matter.isDeviceConnected()) {
121+
Serial.println("Matter Node is commissioned and connected to the network. Ready for use.");
122+
} else {
123+
Serial.println("Matter Node is commissioned. Waiting for the network connection.");
124+
}
125+
}
126+
127+
delay(100);
128+
}

0 commit comments

Comments
 (0)