Skip to content

Commit 6fd3dd5

Browse files
committed
add matter example
1 parent 3121f45 commit 6fd3dd5

File tree

7 files changed

+300
-1
lines changed

7 files changed

+300
-1
lines changed

.github/workflows/examples.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ jobs:
2222
- "examples/arduino-zigbee-switch"
2323
- "examples/tasmota"
2424
- "examples/espidf-arduino-h2zero-BLE_scan"
25-
- "examples/espidf-arduino-matter-light"
25+
#- "examples/espidf-arduino-matter-light"
26+
- "examples/arduino-matter-light"
2627
- "examples/espidf-arduino-blink"
2728
- "examples/espidf-arduino-littlefs"
2829
- "examples/espidf-blink"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter, extra scripting
4+
; Upload options: custom port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
;
7+
; Please visit documentation for the other options and examples
8+
; http://docs.platformio.org/page/projectconf.html
9+
10+
11+
[env:esp32-c6-devkitc-1]
12+
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
13+
framework = arduino
14+
board_build.partitions = huge_app.csv
15+
build_flags = -DCHIP_HAVE_CONFIG_H
16+
board = esp32-c6-devkitc-1
17+
monitor_speed = 115200
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Copyright 2024 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+
#include <WiFi.h>
18+
#include <Preferences.h>
19+
20+
// List of Matter Endpoints for this Node
21+
// Color Light Endpoint
22+
MatterColorLight ColorLight;
23+
24+
// WiFi is manually set and started
25+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
26+
const char *password = "your-password"; // Change this to your WiFi password
27+
28+
// it will keep last OnOff & HSV Color state stored, using Preferences
29+
Preferences matterPref;
30+
const char *onOffPrefKey = "OnOff";
31+
const char *hsvColorPrefKey = "HSV";
32+
33+
// set your board RGB LED pin here
34+
#ifdef RGB_BUILTIN
35+
const uint8_t ledPin = RGB_BUILTIN;
36+
#else
37+
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
38+
#warning "Do not forget to set the RGB LED pin"
39+
#endif
40+
41+
// set your board USER BUTTON pin here
42+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
43+
44+
// Button control
45+
uint32_t button_time_stamp = 0; // debouncing control
46+
bool button_state = false; // false = released | true = pressed
47+
const uint32_t debouceTime = 250; // button debouncing time (ms)
48+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
49+
50+
// Set the RGB LED Light based on the current state of the Color Light
51+
bool setLightState(bool state, espHsvColor_t colorHSV) {
52+
53+
if (state) {
54+
#ifdef RGB_BUILTIN
55+
espRgbColor_t rgbColor = espHsvColorToRgbColor(colorHSV);
56+
// set the RGB LED
57+
rgbLedWrite(ledPin, rgbColor.r, rgbColor.g, rgbColor.b);
58+
#else
59+
// No Color RGB LED, just use the HSV value (brightness) to control the LED
60+
analogWrite(ledPin, colorHSV.v);
61+
#endif
62+
} else {
63+
digitalWrite(ledPin, LOW);
64+
}
65+
// store last HSV Color and OnOff state for when the Light is restarted / power goes off
66+
matterPref.putBool(onOffPrefKey, state);
67+
matterPref.putUInt(hsvColorPrefKey, colorHSV.h << 16 | colorHSV.s << 8 | colorHSV.v);
68+
// This callback must return the success state to Matter core
69+
return true;
70+
}
71+
72+
void setup() {
73+
// Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch
74+
pinMode(buttonPin, INPUT_PULLUP);
75+
// Initialize the LED (light) GPIO and Matter End Point
76+
pinMode(ledPin, OUTPUT);
77+
78+
Serial.begin(115200);
79+
80+
// We start by connecting to a WiFi network
81+
Serial.print("Connecting to ");
82+
Serial.println(ssid);
83+
// Manually connect to WiFi
84+
WiFi.begin(ssid, password);
85+
// Wait for connection
86+
while (WiFi.status() != WL_CONNECTED) {
87+
delay(500);
88+
Serial.print(".");
89+
}
90+
Serial.println("\r\nWiFi connected");
91+
Serial.println("IP address: ");
92+
Serial.println(WiFi.localIP());
93+
delay(500);
94+
95+
// Initialize Matter EndPoint
96+
matterPref.begin("MatterPrefs", false);
97+
// default OnOff state is ON if not stored before
98+
bool lastOnOffState = matterPref.getBool(onOffPrefKey, true);
99+
// default HSV color is blue HSV(169, 254, 254)
100+
uint32_t prefHsvColor = matterPref.getUInt(hsvColorPrefKey, 169 << 16 | 254 << 8 | 254);
101+
espHsvColor_t lastHsvColor = {uint8_t(prefHsvColor >> 16), uint8_t(prefHsvColor >> 8), uint8_t(prefHsvColor)};
102+
ColorLight.begin(lastOnOffState, lastHsvColor);
103+
// set the callback function to handle the Light state change
104+
ColorLight.onChange(setLightState);
105+
106+
// lambda functions are used to set the attribute change callbacks
107+
ColorLight.onChangeOnOff([](bool state) {
108+
Serial.printf("Light OnOff changed to %s\r\n", state ? "ON" : "OFF");
109+
return true;
110+
});
111+
ColorLight.onChangeColorHSV([](HsvColor_t hsvColor) {
112+
Serial.printf("Light HSV Color changed to (%d,%d,%d)\r\n", hsvColor.h, hsvColor.s, hsvColor.v);
113+
return true;
114+
});
115+
116+
// Matter beginning - Last step, after all EndPoints are initialized
117+
Matter.begin();
118+
// This may be a restart of a already commissioned Matter accessory
119+
if (Matter.isDeviceCommissioned()) {
120+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
121+
Serial.printf(
122+
"Initial state: %s | RGB Color: (%d,%d,%d) \r\n", ColorLight ? "ON" : "OFF", ColorLight.getColorRGB().r, ColorLight.getColorRGB().g,
123+
ColorLight.getColorRGB().b
124+
);
125+
// configure the Light based on initial on-off state and its color
126+
ColorLight.updateAccessory();
127+
}
128+
}
129+
130+
void loop() {
131+
// Check Matter Light Commissioning state, which may change during execution of loop()
132+
if (!Matter.isDeviceCommissioned()) {
133+
Serial.println("");
134+
Serial.println("Matter Node is not commissioned yet.");
135+
Serial.println("Initiate the device discovery in your Matter environment.");
136+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
137+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
138+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
139+
// waits for Matter Light Commissioning.
140+
uint32_t timeCount = 0;
141+
while (!Matter.isDeviceCommissioned()) {
142+
delay(100);
143+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
144+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
145+
}
146+
}
147+
Serial.printf(
148+
"Initial state: %s | RGB Color: (%d,%d,%d) \r\n", ColorLight ? "ON" : "OFF", ColorLight.getColorRGB().r, ColorLight.getColorRGB().g,
149+
ColorLight.getColorRGB().b
150+
);
151+
// configure the Light based on initial on-off state and its color
152+
ColorLight.updateAccessory();
153+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
154+
}
155+
156+
// A button is also used to control the light
157+
// Check if the button has been pressed
158+
if (digitalRead(buttonPin) == LOW && !button_state) {
159+
// deals with button debouncing
160+
button_time_stamp = millis(); // record the time while the button is pressed.
161+
button_state = true; // pressed.
162+
}
163+
164+
// Onboard User Button is used as a Light toggle switch or to decommission it
165+
uint32_t time_diff = millis() - button_time_stamp;
166+
if (digitalRead(buttonPin) == HIGH && button_state && time_diff > debouceTime) {
167+
// Toggle button is released - toggle the light
168+
Serial.println("User button released. Toggling Light!");
169+
ColorLight.toggle(); // Matter Controller also can see the change
170+
button_state = false; // released
171+
}
172+
173+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
174+
if (button_state && time_diff > decommissioningTimeout) {
175+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
176+
ColorLight = false; // turn the light off
177+
Matter.decommission();
178+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
179+
}
180+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PIO Unit Testing and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PIO Unit Testing:
11+
- https://docs.platformio.org/page/plus/unit-testing.html

0 commit comments

Comments
 (0)