Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(matter): initial commit with arduino matter lib #10467

Merged
merged 24 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1229e54
feat(matter): initial commit with arduino matter lib
SuGlider Oct 16, 2024
a9a6fc2
feat(matter): add matter library to cmakelists.txt
SuGlider Oct 16, 2024
713a1d6
fix(matter): add correct guard for ci
SuGlider Oct 16, 2024
3a185e9
fix(matter): using correct ci requirements in ci.json
SuGlider Oct 16, 2024
260d619
fix(matter): using correct ci requirements in header files
SuGlider Oct 16, 2024
b495cc3
fix(matter): using correct ci requirements header and examples
SuGlider Oct 16, 2024
d3b83d9
fix(typo): typo and commentaries
SuGlider Oct 16, 2024
2fbbca8
fix(typo): typo and commentaries
SuGlider Oct 16, 2024
d733d19
fix(typo): typo and commentaries
SuGlider Oct 16, 2024
d3f8703
fix(commentary): longer explanation
SuGlider Oct 16, 2024
695df24
feat(matter): api simplification with begin
SuGlider Oct 16, 2024
5960abe
feat(matter): testing flashmode=qio in CI
SuGlider Oct 16, 2024
9e74283
feat(matter): testing flashmode=qio in CI
SuGlider Oct 16, 2024
96672e6
fix(matter): changes CI FQBN
SuGlider Oct 16, 2024
1797133
fix(matte): include all fqbn in ci.json using qio
SuGlider Oct 16, 2024
bb43fbf
fix(matter): revert ci and guard changes
SuGlider Oct 17, 2024
9380713
fix(matter): typo and commentaties
SuGlider Oct 17, 2024
cb1759b
feat(matter): adds a light toggle switch button
SuGlider Oct 17, 2024
7865619
feat(matter): improved the button control
SuGlider Oct 18, 2024
a980226
feat(matter): using switch instead of if() for attibute change
SuGlider Oct 18, 2024
8d6575a
fix(matter): switch/case scope
SuGlider Oct 18, 2024
966ae0d
fix(matter): problems found after pressing reset
SuGlider Oct 18, 2024
9ecb6f3
feat(matter): improve example using preferences
SuGlider Oct 18, 2024
a6d1d20
fix(pre-commit): Fix and apply pre-commit hooks
lucasssvaz Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ set(ARDUINO_ALL_LIBRARIES
HTTPUpdate
Insights
LittleFS
Matter
NetBIOS
Network
OpenThread
Expand Down Expand Up @@ -164,6 +165,10 @@ set(ARDUINO_LIBRARY_OpenThread_SRCS
libraries/OpenThread/src/OThreadCLI.cpp
libraries/OpenThread/src/OThreadCLI_Util.cpp)

set(ARDUINO_LIBRARY_Matter_SRCS
libraries/Matter/src/MatterOnOffLight.cpp
libraries/Matter/src/Matter.cpp)

set(ARDUINO_LIBRARY_PPP_SRCS
libraries/PPP/src/PPP.cpp
libraries/PPP/src/ppp.c)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Matter Manager
#include <Matter.h>
#include <WiFi.h>

// List of Matter Endpoints for this Node
// On/Off Light Endpoint
#include <MatterOnOffLight.h>
MatterOnOffLight OnOffLight;

// WiFi is manually set and started

const char *ssid = "your-ssid"; // Change this to your WiFi SSID
const char *password = "your-password"; // Change this to your WiFi password

void setup() {
Serial.begin(115200);
while (!Serial) {
delay(100);
}

// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
// enable IPv6
WiFi.enableIPv6(true);
// Manually connect to WiFi
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\r\nWiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);

// Initialize at least one Matter EndPoint
OnOffLight.begin();

// Matter begining - Last step, after all EndPoints are initialized
Matter.begin();
}

void loop() {
// Check Matter Commissioning state
if (!Matter.isDeviceCommissioned()) {
Serial.println("");
Serial.println("Matter Node is not commissioned yet.");
Serial.println("Initiate the device discovery in your Matter environment.");
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
// waits for Matter Light Commissioning.
while (!Matter.isDeviceCommissioned()) {
delay(5000);
Serial.println("Matter Fabric not commissioned yet. Waiting for commissioning.");
}
}
Serial.println("Matter Node is commissioned and connected to Wi-Fi.");
Serial.println("====> Decommissioning in 30 seconds. <====");
delay(30000);
Matter.decommission();
Serial.println("Matter Node is decommissioned. Commsssioning widget shall start over.");
}
8 changes: 8 additions & 0 deletions libraries/Matter/examples/MatterCommissionTest/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"fqbn_append": "PartitionScheme=huge_app",
"requires": [
"CONFIG_SOC_WIFI_SUPPORTED=y",
"CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y"
]
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Matter Manager
#include <Matter.h>
#include <WiFi.h>

// List of Matter Endpoints for this Node
// There will be 3 On/Off Light Endpoints in the same Node
#include <MatterOnOffLight.h>
MatterOnOffLight OnOffLight1;
MatterOnOffLight OnOffLight2;
MatterOnOffLight OnOffLight3;

// Matter Protocol Endpoint Callback for each Light Accessory
bool setLightOnOff1(bool state) {
Serial.printf("CB-Light1 changed state to: %s\r\n", state ? "ON" : "OFF");
return true;
}

bool setLightOnOff2(bool state) {
Serial.printf("CB-Light2 changed state to: %s\r\n", state ? "ON" : "OFF");
return true;
}

bool setLightOnOff3(bool state) {
Serial.printf("CB-Light3 changed state to: %s\r\n", state ? "ON" : "OFF");
return true;
}

// WiFi is manually set and started

const char *ssid = "your-ssid"; // Change this to your WiFi SSID
const char *password = "your-password"; // Change this to your WiFi password

void setup() {
Serial.begin(115200);
while (!Serial) {
delay(100);
}

// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
// enable IPv6
WiFi.enableIPv6(true);
// Manually connect to WiFi
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\r\nWiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);

// Initialize all 3 Matter EndPoints
OnOffLight1.begin();
OnOffLight2.begin();
OnOffLight3.begin();
OnOffLight1.onChangeOnOff(setLightOnOff1);
OnOffLight2.onChangeOnOff(setLightOnOff2);
OnOffLight3.onChangeOnOff(setLightOnOff3);

// Matter begining - Last step, after all EndPoints are initialized
Matter.begin();
}

void loop() {
// Check Matter Light Commissioning state
if (!Matter.isDeviceCommissioned()) {
Serial.println("");
Serial.println("Matter Node is not commissioned yet.");
Serial.println("Initiate the device discovery in your Matter environment.");
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
// waits for Matter Light Commissioning.
uint32_t timeCount = 0;
while (!Matter.isDeviceCommissioned()) {
delay(100);
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
}
}
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
}

//displays the Light state every 3 seconds
Serial.println("======================");
Serial.printf("Matter Light #1 is %s\r\n", OnOffLight1.getOnOff() ? "ON" : "OFF");
Serial.printf("Matter Light #2 is %s\r\n", OnOffLight2.getOnOff() ? "ON" : "OFF");
Serial.printf("Matter Light #3 is %s\r\n", OnOffLight3.getOnOff() ? "ON" : "OFF");
delay(3000);
}
7 changes: 7 additions & 0 deletions libraries/Matter/examples/MatterComposedLights/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fqbn_append": "PartitionScheme=huge_app",
"requires": [
"CONFIG_SOC_WIFI_SUPPORTED=y",
"CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y"
]
}
124 changes: 124 additions & 0 deletions libraries/Matter/examples/MatterOnOffLight/MatterOnOffLight.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Matter Manager
#include <Matter.h>
#include <WiFi.h>

// List of Matter Endpoints for this Node
// On/Off Light Endpoint
#include <MatterOnOffLight.h>
MatterOnOffLight OnOffLight;

// set your board LED pin here
#ifdef LED_BUILTIN
const uint8_t ledPin = LED_BUILTIN;
#else
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
#warning "Do not forget to set the LED pin"
#endif

// set your board USER BUTTON pin here
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.

// Matter Protocol Endpoint Callback
bool setLightOnOff(bool state) {
Serial.printf("User Callback :: New Light State = %s\r\n", state ? "ON" : "OFF");
if (state) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// This callback must return the success state to Matter core
return true;
}

// WiFi is manually set and started

const char *ssid = "your-ssid"; // Change this to your WiFi SSID
const char *password = "your-password"; // Change this to your WiFi password

void setup() {
Serial.begin(115200);
while (!Serial) {
delay(100);
}

// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
// enable IPv6
WiFi.enableIPv6(true);
// Manually connect to WiFi
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\r\nWiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);

// Initialize Matter EndPoint
OnOffLight.begin(true);
OnOffLight.onChangeOnOff(setLightOnOff);

// Matter begining - Last step, after all EndPoints are initialized
Matter.begin();
}

// Button control
uint32_t button_time_stamp = 0; // debouncing control
bool button_state = false; // false = released | true = pressed
const uint32_t debouceTime = 250; // button debouncing time (ms)
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light

void loop() {
// Check Matter Light Commissioning state
if (!Matter.isDeviceCommissioned()) {
Serial.println("");
Serial.println("Matter Node is not commissioned yet.");
Serial.println("Initiate the device discovery in your Matter environment.");
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
// waits for Matter Light Commissioning.
uint32_t timeCount = 0;
while (!Matter.isDeviceCommissioned()) {
delay(100);
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
}
}
// Initializa the USER BUTTON (Boot button) GPIO that will act as a toggle switch
pinMode(buttonPin, INPUT_PULLUP);
// Initialize the LED (light) GPIO and Matter End Point
pinMode(ledPin, OUTPUT);
Serial.printf("Initial state: %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
setLightOnOff(OnOffLight.getOnOff()); // configure the Light based on initial state
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
}

// A button is also used to control the light
// Check if the button has been pressed
if (digitalRead(buttonPin) == LOW && !button_state) {
// deals with button debouncing
button_time_stamp = millis(); // record the time while the button is pressed.
button_state = true; // pressed.
}

// Onboard User Button is used as a Light toggle switch or to decommission it
uint32_t time_diff = millis() - button_time_stamp;
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
button_state = false; // released
// Toggle button is released - toggle the light
Serial.println("User button released. Toggling Light!");
OnOffLight.toggle(); // Matter Controller also can see the change

// Factory reset is triggered if the button is pressed longer than 10 seconds
if (time_diff > decommissioningTimeout) {
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
OnOffLight.setOnOff(false); // turn the light off
Matter.decommission();
}
}
}
7 changes: 7 additions & 0 deletions libraries/Matter/examples/MatterOnOffLight/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fqbn_append": "PartitionScheme=huge_app",
"requires": [
"CONFIG_SOC_WIFI_SUPPORTED=y",
"CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y"
]
}
35 changes: 35 additions & 0 deletions libraries/Matter/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#######################################
# Syntax Coloring Map For OpenThread
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

Matter KEYWORD1
MatterOnOffLight KEYWORD1
MatterEndPoint KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

begin KEYWORD2
end KEYWORD2
start KEYWORD2
getManualPairingCode KEYWORD2
getOnboardingQRCodeUrl KEYWORD2
isDeviceCommissioned KEYWORD2
isWiFiConnected KEYWORD2
isThreadConnected KEYWORD2
isDeviceConnected KEYWORD2
decommission KEYWORD2
attributeChangeCB KEYWORD2
setOnOff KEYWORD2
getOnOff KEYWORD2
toggle KEYWORD2
onChangeOnOff KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
9 changes: 9 additions & 0 deletions libraries/Matter/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=Matter
version=3.1.0
author=Rodrigo Garcia | GitHub @SuGlider
maintainer=Rodrigo Garcia <[email protected]>
sentence=Library for supporting Matter environment on ESP32.
paragraph=This library implements Matter accessories using WiFi network.
category=Communication
url=https://github.com/espressif/arduino-esp32/
architectures=esp32
Loading
Loading