Skip to content

Commit c40444a

Browse files
SuGliderlucasssvaz
andauthored
feat(matter): initial commit with arduino matter lib (#10467)
* feat(matter): initial commit with arduino matter lib * feat(matter): add matter library to cmakelists.txt * fix(matter): add correct guard for ci * fix(matter): using correct ci requirements in ci.json * fix(matter): using correct ci requirements in header files * fix(matter): using correct ci requirements header and examples * fix(typo): typo and commentaries * fix(typo): typo and commentaries * fix(typo): typo and commentaries * fix(commentary): longer explanation * feat(matter): api simplification with begin * feat(matter): testing flashmode=qio in CI * feat(matter): testing flashmode=qio in CI * fix(matter): changes CI FQBN * fix(matte): include all fqbn in ci.json using qio * fix(matter): revert ci and guard changes * fix(matter): typo and commentaties * feat(matter): adds a light toggle switch button * feat(matter): improved the button control * feat(matter): using switch instead of if() for attibute change * fix(matter): switch/case scope * fix(matter): problems found after pressing reset * feat(matter): improve example using preferences * fix(pre-commit): Fix and apply pre-commit hooks --------- Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com>
1 parent f668557 commit c40444a

15 files changed

+735
-0
lines changed

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ repos:
2626
- id: trailing-whitespace
2727
args: [--markdown-linebreak-ext=md]
2828
- id: pretty-format-json
29+
stages: [manual]
2930
args: [--autofix]
3031
types_or: [json]
3132
exclude: |

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ set(ARDUINO_ALL_LIBRARIES
9494
HTTPUpdate
9595
Insights
9696
LittleFS
97+
Matter
9798
NetBIOS
9899
Network
99100
OpenThread
@@ -165,6 +166,10 @@ set(ARDUINO_LIBRARY_OpenThread_SRCS
165166
libraries/OpenThread/src/OThreadCLI.cpp
166167
libraries/OpenThread/src/OThreadCLI_Util.cpp)
167168

169+
set(ARDUINO_LIBRARY_Matter_SRCS
170+
libraries/Matter/src/MatterOnOffLight.cpp
171+
libraries/Matter/src/Matter.cpp)
172+
168173
set(ARDUINO_LIBRARY_PPP_SRCS
169174
libraries/PPP/src/PPP.cpp
170175
libraries/PPP/src/ppp.c)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Matter Manager
2+
#include <Matter.h>
3+
#include <WiFi.h>
4+
5+
// List of Matter Endpoints for this Node
6+
// On/Off Light Endpoint
7+
#include <MatterOnOffLight.h>
8+
MatterOnOffLight OnOffLight;
9+
10+
// WiFi is manually set and started
11+
12+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
13+
const char *password = "your-password"; // Change this to your WiFi password
14+
15+
void setup() {
16+
Serial.begin(115200);
17+
while (!Serial) {
18+
delay(100);
19+
}
20+
21+
// We start by connecting to a WiFi network
22+
Serial.print("Connecting to ");
23+
Serial.println(ssid);
24+
// enable IPv6
25+
WiFi.enableIPv6(true);
26+
// Manually connect to WiFi
27+
WiFi.begin(ssid, password);
28+
// Wait for connection
29+
while (WiFi.status() != WL_CONNECTED) {
30+
delay(500);
31+
Serial.print(".");
32+
}
33+
Serial.println("\r\nWiFi connected");
34+
Serial.println("IP address: ");
35+
Serial.println(WiFi.localIP());
36+
delay(500);
37+
38+
// Initialize at least one Matter EndPoint
39+
OnOffLight.begin();
40+
41+
// Matter beginning - Last step, after all EndPoints are initialized
42+
Matter.begin();
43+
}
44+
45+
void loop() {
46+
// Check Matter Commissioning state
47+
if (!Matter.isDeviceCommissioned()) {
48+
Serial.println("");
49+
Serial.println("Matter Node is not commissioned yet.");
50+
Serial.println("Initiate the device discovery in your Matter environment.");
51+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
52+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
53+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
54+
// waits for Matter Light Commissioning.
55+
while (!Matter.isDeviceCommissioned()) {
56+
delay(5000);
57+
Serial.println("Matter Fabric not commissioned yet. Waiting for commissioning.");
58+
}
59+
}
60+
Serial.println("Matter Node is commissioned and connected to Wi-Fi.");
61+
Serial.println("====> Decommissioning in 30 seconds. <====");
62+
delay(30000);
63+
Matter.decommission();
64+
Serial.println("Matter Node is decommissioned. Commsssioning widget shall start over.");
65+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Matter Manager
2+
#include <Matter.h>
3+
#include <WiFi.h>
4+
5+
// List of Matter Endpoints for this Node
6+
// There will be 3 On/Off Light Endpoints in the same Node
7+
#include <MatterOnOffLight.h>
8+
MatterOnOffLight OnOffLight1;
9+
MatterOnOffLight OnOffLight2;
10+
MatterOnOffLight OnOffLight3;
11+
12+
// Matter Protocol Endpoint Callback for each Light Accessory
13+
bool setLightOnOff1(bool state) {
14+
Serial.printf("CB-Light1 changed state to: %s\r\n", state ? "ON" : "OFF");
15+
return true;
16+
}
17+
18+
bool setLightOnOff2(bool state) {
19+
Serial.printf("CB-Light2 changed state to: %s\r\n", state ? "ON" : "OFF");
20+
return true;
21+
}
22+
23+
bool setLightOnOff3(bool state) {
24+
Serial.printf("CB-Light3 changed state to: %s\r\n", state ? "ON" : "OFF");
25+
return true;
26+
}
27+
28+
// WiFi is manually set and started
29+
30+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
31+
const char *password = "your-password"; // Change this to your WiFi password
32+
33+
void setup() {
34+
Serial.begin(115200);
35+
while (!Serial) {
36+
delay(100);
37+
}
38+
39+
// We start by connecting to a WiFi network
40+
Serial.print("Connecting to ");
41+
Serial.println(ssid);
42+
// enable IPv6
43+
WiFi.enableIPv6(true);
44+
// Manually connect to WiFi
45+
WiFi.begin(ssid, password);
46+
// Wait for connection
47+
while (WiFi.status() != WL_CONNECTED) {
48+
delay(500);
49+
Serial.print(".");
50+
}
51+
Serial.println("\r\nWiFi connected");
52+
Serial.println("IP address: ");
53+
Serial.println(WiFi.localIP());
54+
delay(500);
55+
56+
// Initialize all 3 Matter EndPoints
57+
OnOffLight1.begin();
58+
OnOffLight2.begin();
59+
OnOffLight3.begin();
60+
OnOffLight1.onChangeOnOff(setLightOnOff1);
61+
OnOffLight2.onChangeOnOff(setLightOnOff2);
62+
OnOffLight3.onChangeOnOff(setLightOnOff3);
63+
64+
// Matter beginning - Last step, after all EndPoints are initialized
65+
Matter.begin();
66+
}
67+
68+
void loop() {
69+
// Check Matter Light Commissioning state
70+
if (!Matter.isDeviceCommissioned()) {
71+
Serial.println("");
72+
Serial.println("Matter Node is not commissioned yet.");
73+
Serial.println("Initiate the device discovery in your Matter environment.");
74+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
75+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
76+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
77+
// waits for Matter Light Commissioning.
78+
uint32_t timeCount = 0;
79+
while (!Matter.isDeviceCommissioned()) {
80+
delay(100);
81+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
82+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
83+
}
84+
}
85+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
86+
}
87+
88+
//displays the Light state every 3 seconds
89+
Serial.println("======================");
90+
Serial.printf("Matter Light #1 is %s\r\n", OnOffLight1.getOnOff() ? "ON" : "OFF");
91+
Serial.printf("Matter Light #2 is %s\r\n", OnOffLight2.getOnOff() ? "ON" : "OFF");
92+
Serial.printf("Matter Light #3 is %s\r\n", OnOffLight3.getOnOff() ? "ON" : "OFF");
93+
delay(3000);
94+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Matter Manager
2+
#include <Matter.h>
3+
#include <WiFi.h>
4+
#include <Preferences.h>
5+
6+
// List of Matter Endpoints for this Node
7+
// On/Off Light Endpoint
8+
#include <MatterOnOffLight.h>
9+
MatterOnOffLight OnOffLight;
10+
11+
// it will keep last OnOff state stored, using Preferences
12+
Preferences lastStatePref;
13+
14+
// set your board LED pin here
15+
#ifdef LED_BUILTIN
16+
const uint8_t ledPin = LED_BUILTIN;
17+
#else
18+
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
19+
#warning "Do not forget to set the LED pin"
20+
#endif
21+
22+
// set your board USER BUTTON pin here
23+
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
24+
25+
// Matter Protocol Endpoint Callback
26+
bool setLightOnOff(bool state) {
27+
Serial.printf("User Callback :: New Light State = %s\r\n", state ? "ON" : "OFF");
28+
if (state) {
29+
digitalWrite(ledPin, HIGH);
30+
} else {
31+
digitalWrite(ledPin, LOW);
32+
}
33+
// store last OnOff state for when the Light is restarted / power goes off
34+
lastStatePref.putBool("lastOnOffState", state);
35+
// This callback must return the success state to Matter core
36+
return true;
37+
}
38+
39+
// WiFi is manually set and started
40+
41+
const char *ssid = "Apartment B15"; // Change this to your WiFi SSID
42+
const char *password = "flat-pony-body"; // Change this to your WiFi password
43+
44+
void setup() {
45+
// Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch
46+
pinMode(buttonPin, INPUT_PULLUP);
47+
// Initialize the LED (light) GPIO and Matter End Point
48+
pinMode(ledPin, OUTPUT);
49+
50+
Serial.begin(115200);
51+
while (!Serial) {
52+
delay(100);
53+
}
54+
55+
// We start by connecting to a WiFi network
56+
Serial.print("Connecting to ");
57+
Serial.println(ssid);
58+
// enable IPv6
59+
WiFi.enableIPv6(true);
60+
// Manually connect to WiFi
61+
WiFi.begin(ssid, password);
62+
// Wait for connection
63+
while (WiFi.status() != WL_CONNECTED) {
64+
delay(500);
65+
Serial.print(".");
66+
}
67+
Serial.println("\r\nWiFi connected");
68+
Serial.println("IP address: ");
69+
Serial.println(WiFi.localIP());
70+
delay(500);
71+
72+
// Initialize Matter EndPoint
73+
lastStatePref.begin("matterLight", false);
74+
bool lastOnOffState = lastStatePref.getBool("lastOnOffState", true);
75+
OnOffLight.begin(lastOnOffState);
76+
OnOffLight.onChangeOnOff(setLightOnOff);
77+
78+
// Matter beginning - Last step, after all EndPoints are initialized
79+
Matter.begin();
80+
// This may be a restart of a already commissioned Matter accessory
81+
if (Matter.isDeviceCommissioned()) {
82+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
83+
Serial.printf("Initial state: %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
84+
setLightOnOff(OnOffLight.getOnOff()); // configure the Light based on initial state
85+
}
86+
}
87+
// Button control
88+
uint32_t button_time_stamp = 0; // debouncing control
89+
bool button_state = false; // false = released | true = pressed
90+
const uint32_t debouceTime = 250; // button debouncing time (ms)
91+
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
92+
93+
void loop() {
94+
// Check Matter Light Commissioning state, which may change during execution of loop()
95+
if (!Matter.isDeviceCommissioned()) {
96+
Serial.println("");
97+
Serial.println("Matter Node is not commissioned yet.");
98+
Serial.println("Initiate the device discovery in your Matter environment.");
99+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
100+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
101+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
102+
// waits for Matter Light Commissioning.
103+
uint32_t timeCount = 0;
104+
while (!Matter.isDeviceCommissioned()) {
105+
delay(100);
106+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
107+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
108+
}
109+
}
110+
Serial.printf("Initial state: %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
111+
setLightOnOff(OnOffLight.getOnOff()); // configure the Light based on initial state
112+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
113+
}
114+
115+
// A button is also used to control the light
116+
// Check if the button has been pressed
117+
if (digitalRead(buttonPin) == LOW && !button_state) {
118+
// deals with button debouncing
119+
button_time_stamp = millis(); // record the time while the button is pressed.
120+
button_state = true; // pressed.
121+
}
122+
123+
// Onboard User Button is used as a Light toggle switch or to decommission it
124+
uint32_t time_diff = millis() - button_time_stamp;
125+
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
126+
button_state = false; // released
127+
// Toggle button is released - toggle the light
128+
Serial.println("User button released. Toggling Light!");
129+
OnOffLight.toggle(); // Matter Controller also can see the change
130+
131+
// Factory reset is triggered if the button is pressed longer than 10 seconds
132+
if (time_diff > decommissioningTimeout) {
133+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
134+
OnOffLight.setOnOff(false); // turn the light off
135+
Matter.decommission();
136+
}
137+
}
138+
}
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

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#######################################
2+
# Syntax Coloring Map For OpenThread
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Matter KEYWORD1
10+
MatterOnOffLight KEYWORD1
11+
MatterEndPoint KEYWORD1
12+
13+
#######################################
14+
# Methods and Functions (KEYWORD2)
15+
#######################################
16+
17+
begin KEYWORD2
18+
end KEYWORD2
19+
start KEYWORD2
20+
getManualPairingCode KEYWORD2
21+
getOnboardingQRCodeUrl KEYWORD2
22+
isDeviceCommissioned KEYWORD2
23+
isWiFiConnected KEYWORD2
24+
isThreadConnected KEYWORD2
25+
isDeviceConnected KEYWORD2
26+
decommission KEYWORD2
27+
attributeChangeCB KEYWORD2
28+
setOnOff KEYWORD2
29+
getOnOff KEYWORD2
30+
toggle KEYWORD2
31+
onChangeOnOff KEYWORD2
32+
33+
#######################################
34+
# Constants (LITERAL1)
35+
#######################################

libraries/Matter/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Matter
2+
version=3.1.0
3+
author=Rodrigo Garcia | GitHub @SuGlider
4+
maintainer=Rodrigo Garcia <Rodrigo.Garcia@espressif.com>
5+
sentence=Library for supporting Matter environment on ESP32.
6+
paragraph=This library implements Matter accessories using WiFi network.
7+
category=Communication
8+
url=https://github.com/espressif/arduino-esp32/
9+
architectures=esp32

0 commit comments

Comments
 (0)