Skip to content

Commit 1229e54

Browse files
committed
feat(matter): initial commit with arduino matter lib
1 parent 3733c87 commit 1229e54

File tree

13 files changed

+698
-0
lines changed

13 files changed

+698
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <Arduino.h>
2+
#include <WiFi.h>
3+
4+
// Matter Manager
5+
#include <Matter.h>
6+
7+
// List of Matter Endpoints for this Node
8+
// On/Off Light Endpoint
9+
#include <MatterOnOffLight.h>
10+
MatterOnOffLight OnOffLight;
11+
12+
// WiFi is manually set and stated
13+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
14+
const char *password = "your-password"; // Change this to your WiFi password
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
while (!Serial) {
19+
delay(100);
20+
}
21+
22+
// We start by connecting to a WiFi network
23+
Serial.print("Connecting to ");
24+
Serial.println(ssid);
25+
// enable IPv6
26+
WiFi.enableIPv6(true);
27+
// Manually connect to WiFi
28+
WiFi.begin(ssid, password);
29+
// Wait for connection
30+
while (WiFi.status() != WL_CONNECTED) {
31+
delay(500);
32+
Serial.print(".");
33+
}
34+
Serial.println("\r\nWiFi connected");
35+
Serial.println("IP address: ");
36+
Serial.println(WiFi.localIP());
37+
delay(500);
38+
39+
// Initialize Matter Node
40+
Matter.begin();
41+
// Initialize Matter EndPoint
42+
OnOffLight.begin();
43+
// Matter start
44+
Matter.start();
45+
}
46+
47+
void loop() {
48+
// Check Matter Commissioning state
49+
if (!Matter.isDeviceCommissioned()) {
50+
Serial.println("");
51+
Serial.println("Matter Node is not commissioned yet.");
52+
Serial.println("Initiate the device discovery in your Matter environment.");
53+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
54+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
55+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
56+
// waits for Matter Light Commissioning.
57+
while (!Matter.isDeviceCommissioned()) {
58+
delay(5000);
59+
Serial.println("Matter Fabric not commissioned yet. Waiting for commissioning.");
60+
}
61+
}
62+
Serial.println("Matter Node is commissioned and connected to Wi-Fi.");
63+
Serial.println("====> Decommissioning in 30 seconds. <====");
64+
delay(30000);
65+
Matter.decommission();
66+
Serial.println("Matter Node is decommissioned. Commsssioning widget shall start over.");
67+
}
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+
]
6+
}
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <Arduino.h>
2+
#include <WiFi.h>
3+
4+
// Matter Manager
5+
#include <Matter.h>
6+
7+
// List of Matter Endpoints for this Node
8+
// There will be 3 On/Off Light Endpoints in the same Node
9+
#include <MatterOnOffLight.h>
10+
MatterOnOffLight OnOffLight1;
11+
MatterOnOffLight OnOffLight2;
12+
MatterOnOffLight OnOffLight3;
13+
14+
// Matter Protocol Endpoint Callback for each Light Accessory
15+
bool setLightOnOff1(bool state) {
16+
Serial.printf("CB-Light1 changed state to: %s\r\n", state ? "ON" : "OFF");
17+
return true;
18+
}
19+
20+
bool setLightOnOff2(bool state) {
21+
Serial.printf("CB-Light2 changed state to: %s\r\n", state ? "ON" : "OFF");
22+
return true;
23+
}
24+
25+
bool setLightOnOff3(bool state) {
26+
Serial.printf("CB-Light3 changed state to: %s\r\n", state ? "ON" : "OFF");
27+
return true;
28+
}
29+
30+
// WiFi is manually set and stated
31+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
32+
const char *password = "your-password"; // Change this to your WiFi password
33+
34+
void setup() {
35+
Serial.begin(115200);
36+
while (!Serial) {
37+
delay(100);
38+
}
39+
40+
// We start by connecting to a WiFi network
41+
Serial.print("Connecting to ");
42+
Serial.println(ssid);
43+
// enable IPv6
44+
WiFi.enableIPv6(true);
45+
// Manually connect to WiFi
46+
WiFi.begin(ssid, password);
47+
// Wait for connection
48+
while (WiFi.status() != WL_CONNECTED) {
49+
delay(500);
50+
Serial.print(".");
51+
}
52+
Serial.println("\r\nWiFi connected");
53+
Serial.println("IP address: ");
54+
Serial.println(WiFi.localIP());
55+
delay(500);
56+
57+
// Initialize Matter Node
58+
Matter.begin();
59+
60+
// Initialize all 3 Matter EndPoints
61+
OnOffLight1.begin();
62+
OnOffLight2.begin();
63+
OnOffLight3.begin();
64+
OnOffLight1.onChangeOnOff(setLightOnOff1);
65+
OnOffLight2.onChangeOnOff(setLightOnOff2);
66+
OnOffLight3.onChangeOnOff(setLightOnOff3);
67+
68+
// Matter start - Last step, after all EndPoints are initialized
69+
Matter.start();
70+
}
71+
72+
void loop() {
73+
// Check Matter Light Commissioning state
74+
if (!Matter.isDeviceCommissioned()) {
75+
Serial.println("");
76+
Serial.println("Matter Node is not commissioned yet.");
77+
Serial.println("Initiate the device discovery in your Matter environment.");
78+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
79+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
80+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
81+
// waits for Matter Light Commissioning.
82+
uint32_t timeCount = 0;
83+
while (!Matter.isDeviceCommissioned()) {
84+
delay(100);
85+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
86+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
87+
}
88+
}
89+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
90+
}
91+
92+
//displays the Light state every 3 seconds
93+
Serial.println("======================");
94+
Serial.printf("Matter Light #1 is %s\r\n", OnOffLight1.getOnOff() ? "ON" : "OFF");
95+
Serial.printf("Matter Light #2 is %s\r\n", OnOffLight2.getOnOff() ? "ON" : "OFF");
96+
Serial.printf("Matter Light #3 is %s\r\n", OnOffLight3.getOnOff() ? "ON" : "OFF");
97+
delay(3000);
98+
}
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+
]
6+
}
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <Arduino.h>
2+
#include <WiFi.h>
3+
4+
// Matter Manager
5+
#include <Matter.h>
6+
7+
// List of Matter Endpoints for this Node
8+
// On/Off Light Endpoint
9+
#include <MatterOnOffLight.h>
10+
MatterOnOffLight OnOffLight;
11+
12+
// set your board LED pin here
13+
#ifdef LED_BUILTIN
14+
const uint8_t ledPin = LED_BUILTIN;
15+
#else
16+
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
17+
#warning "Do not forget to set the LED pin"
18+
#endif
19+
20+
// Matter Protocol Endpoint Callback
21+
bool setLightOnOff(bool state) {
22+
Serial.printf("User Callback :: New Light State = %s\r\n", state ? "ON" : "OFF");
23+
if (state) {
24+
digitalWrite(ledPin, HIGH);
25+
} else {
26+
digitalWrite(ledPin, LOW);
27+
}
28+
// This callback must return the success state to Matter core
29+
return true;
30+
}
31+
32+
// WiFi is manually set and stated
33+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
34+
const char *password = "your-password"; // Change this to your WiFi password
35+
36+
void setup() {
37+
Serial.begin(115200);
38+
while (!Serial) {
39+
delay(100);
40+
}
41+
42+
// We start by connecting to a WiFi network
43+
Serial.print("Connecting to ");
44+
Serial.println(ssid);
45+
// enable IPv6
46+
WiFi.enableIPv6(true);
47+
// Manually connect to WiFi
48+
WiFi.begin(ssid, password);
49+
// Wait for connection
50+
while (WiFi.status() != WL_CONNECTED) {
51+
delay(500);
52+
Serial.print(".");
53+
}
54+
Serial.println("\r\nWiFi connected");
55+
Serial.println("IP address: ");
56+
Serial.println(WiFi.localIP());
57+
delay(500);
58+
59+
// Initialize Matter Node
60+
Matter.begin();
61+
62+
// Initialize Matter EndPoint
63+
OnOffLight.begin(true);
64+
OnOffLight.onChangeOnOff(setLightOnOff);
65+
66+
// Matter start - Last step, after all EndPoints are initialized
67+
Matter.start();
68+
}
69+
70+
uint32_t lastMillis = millis();
71+
const uint32_t toggle_interval = 15000; // light will toggle every 15 seconds
72+
void loop() {
73+
// Check Matter Light Commissioning state
74+
if (!Matter.isDeviceCommissioned()) {
75+
Serial.println("");
76+
Serial.println("Matter Node is not commissioned yet.");
77+
Serial.println("Initiate the device discovery in your Matter environment.");
78+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
79+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
80+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
81+
// waits for Matter Light Commissioning.
82+
uint32_t timeCount = 0;
83+
while (!Matter.isDeviceCommissioned()) {
84+
delay(100);
85+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
86+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
87+
}
88+
}
89+
// Initialize the LED (light) GPIO and Matter End Point
90+
pinMode(ledPin, OUTPUT);
91+
Serial.printf("Initial state: %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
92+
setLightOnOff(OnOffLight.getOnOff()); // configure the Light based on initial state
93+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
94+
delay(10000);
95+
}
96+
97+
//displays the Light state every 3 seconds
98+
Serial.printf("Matter Light is %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
99+
delay(3000);
100+
if (millis() - lastMillis > toggle_interval) {
101+
Serial.println("Toggling Light!");
102+
lastMillis = millis();
103+
OnOffLight.toggle(); // Matter Controller also can see the change
104+
}
105+
}
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+
]
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 <[email protected]>
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)