Skip to content

Commit 0baa931

Browse files
authored
feat(matter): new matter example
1 parent b709a78 commit 0baa931

File tree

1 file changed

+114
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)