Skip to content

Commit 19fb1c0

Browse files
committed
fix c2 build / example matter
1 parent 756d03e commit 19fb1c0

16 files changed

+325
-973
lines changed
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: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,20 @@
1-
| Supported Targets | ESP32-C3 | ESP32-C6 |
2-
| ----------------- | -------- | -------- |
3-
4-
5-
# Managed Component Light
6-
7-
This example is configured by default to work with the ESP32-C6, which has the RGB LED GPIO set as pin 8 and the BOOT button on GPIO 9.
8-
9-
This example creates a Color Temperature Light device using the esp_matter component automatically downloaded from the [Espressif Component Registry](https://components.espressif.com/). See the [docs](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html) for more information about matter.
10-
11-
The code is based on the Arduino API and uses Arduino as an IDF Component.
12-
13-
## How to use it
14-
15-
Once the device runs for the first time, it must be commissioned to the Matter Fabric of the available Matter Environment.
16-
Possible Matter Environments are:
17-
- Amazon Alexa
18-
- Google Home Assistant (*)
19-
- Apple Home
20-
- Open Source Home Assistant
21-
22-
(*) Google Home Assistant requires the user to set up a Matter Light using the [Google Home Developer Console](https://developers.home.google.com/codelabs/matter-device#2). It is necessary to create a Matter Light device with VID = 0xFFF1 and PID = 0x8000. Otherwise, the Light won't show up in the GHA APP. This action is necessary because the Firmware uses Testing credentials and Google requires the user to create the testing device before using it.
23-
24-
There is no QR Code to be used when the Smartphone APP wants to add the Matter Device.
25-
Please enter the code manually: `34970112332`
26-
27-
The devboard has a built-in LED that will be used as the Matter Light.
28-
The default setting of the code uses pin 8 for the ESP32-C6,
29-
Please change it in `main/matter_accessory_driver.h` or in the `sdkconfig.defaults` file.
30-
31-
## LED Status and Factory Mode
32-
33-
The WS2812b built-in LED will turn purple as soon as the device is flashed and runs for the first time.
34-
The purple color indicates that the Matter Accessory has not been commissioned yet.
35-
After using a Matter provider Smartphone APP to add a Matter device to your Home Application, it may turn orange to indicate that it has no WiFi connection.
36-
37-
Once it connects to the WiFi network, the LED will turn white to indicate that Matter is working and the device is connected to the Matter Environment.
38-
Please note that Matter over WiFi using an ESP32 device will connect to a 2.4GHz WiFi SSID, therefore the Commissioner APP Smartphone shall be connected to this SSID.
39-
40-
The Matter and WiFi configuration will be stored in NVS to ensure that it will connect to the Matter Fabric and WiFi Network again once it is reset.
41-
42-
The Matter Smartphone APP will control the light state (ON/OFF), temperature (Warm/Cold White), and brightness.
43-
44-
## On Board Light toggle button
45-
46-
The built-in BOOT button will toggle On/Off and replicate the new state to the Matter Environment, making it visible in the Matter Smartphone APP as well.
47-
48-
## Returning to the Factory State
49-
50-
Holding the BOOT button pressed for more than 10 seconds and then releasing it will erase all Matter and WiFi configuration, forcing it to reset to factory state. After that, the device needs to be commissioned again. Previous setups done in the Smartphone APP won't work again; therefore, the virtual device shall be removed from the APP.
51-
52-
## Building the Application using WiFi and Matter
53-
54-
This example has been tested with Arduino Core 3.2.0. It should work with newer versions too.
55-
56-
There is a configuration file for these SoCs: esp32c3, esp32c6.
57-
Those are the tested devices that have a WS2812 RGB LED and can run BLE, WiFi and Matter.
58-
59-
In case it is necessary to change the Button Pin or the REG LED Pin, please use the `menuconfig` and change the Menu Option `Light Matter Accessory`
60-
61-
## Using OpenThread with Matter
62-
63-
This is possible with the ESP32-C6.
64-
It is necessary to have a Thread Border Router in the Matter Environment. Check your Matter hardware provider.
65-
1+
# Arduino ESP-Matter over WiFi (Thread) example using ESP32-C6
2+
This is an Arduino as IDF Project to build an ESP-Matter over WiFi or Thread RGB Light using ESP32-C6 and ESP-Matter Arduino API \
3+
4+
Example from [ESP32 Matter Library examples](https://github.com/espressif/arduino-esp32/tree/master/libraries/Matter/examples)
5+
6+
The `main/idf_component.yml` file holds the ESP-Matter component.\
7+
8+
# Instructions:
9+
10+
1- Compile with `pio run`
11+
12+
2- Try to add the Matter RGB light to your Matter environment.
13+
14+
# Thread variant:
15+
16+
1- Rename `sdkconfig.defaults` to `sdkconfig.defaults.wifi` and `sdkconfig.defaults.thread` to `sdkconfig.defaults`
17+
18+
2- Compile with `pio run`
19+
20+
3- Try to add the Matter RGB light to your Matter environment.
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# This file was automatically generated for projects
2-
# without default 'CMakeLists.txt' file.
1+
idf_component_register(SRC_DIRS "."
2+
INCLUDE_DIRS ".")
33

4-
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/main/*.*)
5-
6-
idf_component_register(SRCS ${app_sources})
7-
8-
#set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
9-
#target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H")

examples/espidf-arduino-matter-light/main/Kconfig.projbuild

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
#include <Preferences.h>
18+
19+
// List of Matter Endpoints for this Node
20+
// Color Light Endpoint
21+
MatterEnhancedColorLight EnhancedColorLight;
22+
23+
// It will use HSV color to control all Matter Attribute Changes
24+
HsvColor_t currentHSVColor = {0, 0, 0};
25+
26+
// it will keep last OnOff & HSV Color state stored, using Preferences
27+
Preferences matterPref;
28+
const char *onOffPrefKey = "OnOff";
29+
const char *hsvColorPrefKey = "HSV";
30+
31+
// set your board RGB LED pin here
32+
#ifdef RGB_BUILTIN
33+
const uint8_t ledPin = RGB_BUILTIN;
34+
#else
35+
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
36+
#warning "Do not forget to set the RGB LED pin"
37+
#endif
38+
39+
// set your board USER BUTTON pin here
40+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
41+
42+
// Button control
43+
uint32_t button_time_stamp = 0; // debouncing control
44+
bool button_state = false; // false = released | true = pressed
45+
const uint32_t debounceTime = 250; // button debouncing time (ms)
46+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
47+
48+
// Set the RGB LED Light based on the current state of the Enhanced Color Light
49+
bool setLightState(bool state, espHsvColor_t colorHSV, uint8_t brightness, uint16_t temperature_Mireds) {
50+
51+
if (state) {
52+
#ifdef RGB_BUILTIN
53+
// currentHSVColor keeps final color result
54+
espRgbColor_t rgbColor = espHsvColorToRgbColor(currentHSVColor);
55+
// set the RGB LED
56+
rgbLedWrite(ledPin, rgbColor.r, rgbColor.g, rgbColor.b);
57+
#else
58+
// No Color RGB LED, just use the HSV value (brightness) to control the LED
59+
analogWrite(ledPin, colorHSV.v);
60+
#endif
61+
} else {
62+
#ifndef RGB_BUILTIN
63+
// after analogWrite(), it is necessary to set the GPIO to digital mode first
64+
pinMode(ledPin, OUTPUT);
65+
#endif
66+
digitalWrite(ledPin, LOW);
67+
}
68+
// store last HSV Color and OnOff state for when the Light is restarted / power goes off
69+
matterPref.putBool(onOffPrefKey, state);
70+
matterPref.putUInt(hsvColorPrefKey, currentHSVColor.h << 16 | currentHSVColor.s << 8 | currentHSVColor.v);
71+
// This callback must return the success state to Matter core
72+
return true;
73+
}
74+
75+
void setup() {
76+
// Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch
77+
pinMode(buttonPin, INPUT_PULLUP);
78+
// Initialize the LED (light) GPIO and Matter End Point
79+
pinMode(ledPin, OUTPUT);
80+
81+
Serial.begin(115200);
82+
83+
// Initialize Matter EndPoint
84+
matterPref.begin("MatterPrefs", false);
85+
// default OnOff state is ON if not stored before
86+
bool lastOnOffState = matterPref.getBool(onOffPrefKey, true);
87+
// default HSV color is (21, 216, 25) - Warm White Color at 10% intensity
88+
uint32_t prefHsvColor = matterPref.getUInt(hsvColorPrefKey, 21 << 16 | 216 << 8 | 25);
89+
currentHSVColor = {uint8_t(prefHsvColor >> 16), uint8_t(prefHsvColor >> 8), uint8_t(prefHsvColor)};
90+
EnhancedColorLight.begin(lastOnOffState, currentHSVColor);
91+
// set the callback function to handle the Light state change
92+
EnhancedColorLight.onChange(setLightState);
93+
94+
// lambda functions are used to set the attribute change callbacks
95+
EnhancedColorLight.onChangeOnOff([](bool state) {
96+
Serial.printf("Light OnOff changed to %s\r\n", state ? "ON" : "OFF");
97+
return true;
98+
});
99+
EnhancedColorLight.onChangeColorTemperature([](uint16_t colorTemperature) {
100+
Serial.printf("Light Color Temperature changed to %d\r\n", colorTemperature);
101+
// get correspondent Hue and Saturation of the color temperature
102+
HsvColor_t hsvTemperature = espRgbColorToHsvColor(espCTToRgbColor(colorTemperature));
103+
// keep previous the brightness and just change the Hue and Saturation
104+
currentHSVColor.h = hsvTemperature.h;
105+
currentHSVColor.s = hsvTemperature.s;
106+
return true;
107+
});
108+
EnhancedColorLight.onChangeBrightness([](uint8_t brightness) {
109+
Serial.printf("Light brightness changed to %d\r\n", brightness);
110+
// change current brightness (HSV value)
111+
currentHSVColor.v = brightness;
112+
return true;
113+
});
114+
EnhancedColorLight.onChangeColorHSV([](HsvColor_t hsvColor) {
115+
Serial.printf("Light HSV Color changed to (%d,%d,%d)\r\n", hsvColor.h, hsvColor.s, hsvColor.v);
116+
// keep the current brightness and just change Hue and Saturation
117+
currentHSVColor.h = hsvColor.h;
118+
currentHSVColor.s = hsvColor.s;
119+
return true;
120+
});
121+
122+
// Matter beginning - Last step, after all EndPoints are initialized
123+
Matter.begin();
124+
// This may be a restart of a already commissioned Matter accessory
125+
if (Matter.isDeviceCommissioned()) {
126+
Serial.println("Matter Node is commissioned and connected to the network. Ready for use.");
127+
Serial.printf(
128+
"Initial state: %s | RGB Color: (%d,%d,%d) \r\n", EnhancedColorLight ? "ON" : "OFF", EnhancedColorLight.getColorRGB().r,
129+
EnhancedColorLight.getColorRGB().g, EnhancedColorLight.getColorRGB().b
130+
);
131+
// configure the Light based on initial on-off state and its color
132+
EnhancedColorLight.updateAccessory();
133+
}
134+
}
135+
136+
void loop() {
137+
// Check Matter Light Commissioning state, which may change during execution of loop()
138+
if (!Matter.isDeviceCommissioned()) {
139+
Serial.println("");
140+
Serial.println("Matter Node is not commissioned yet.");
141+
Serial.println("Initiate the device discovery in your Matter environment.");
142+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
143+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
144+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
145+
// waits for Matter Light Commissioning.
146+
uint32_t timeCount = 0;
147+
while (!Matter.isDeviceCommissioned()) {
148+
delay(100);
149+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
150+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
151+
}
152+
}
153+
Serial.printf(
154+
"Initial state: %s | RGB Color: (%d,%d,%d) \r\n", EnhancedColorLight ? "ON" : "OFF", EnhancedColorLight.getColorRGB().r,
155+
EnhancedColorLight.getColorRGB().g, EnhancedColorLight.getColorRGB().b
156+
);
157+
// configure the Light based on initial on-off state and its color
158+
EnhancedColorLight.updateAccessory();
159+
Serial.println("Matter Node is commissioned and connected to the network. Ready for use.");
160+
}
161+
162+
// A button is also used to control the light
163+
// Check if the button has been pressed
164+
if (digitalRead(buttonPin) == LOW && !button_state) {
165+
// deals with button debouncing
166+
button_time_stamp = millis(); // record the time while the button is pressed.
167+
button_state = true; // pressed.
168+
}
169+
170+
// Onboard User Button is used as a Light toggle switch or to decommission it
171+
uint32_t time_diff = millis() - button_time_stamp;
172+
if (button_state && time_diff > debounceTime && digitalRead(buttonPin) == HIGH) {
173+
button_state = false; // released
174+
// Toggle button is released - toggle the light
175+
Serial.println("User button released. Toggling Light!");
176+
EnhancedColorLight.toggle(); // Matter Controller also can see the change
177+
}
178+
179+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
180+
if (button_state && time_diff > decommissioningTimeout) {
181+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
182+
EnhancedColorLight = false; // turn the light off
183+
Matter.decommission();
184+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
185+
}
186+
}

0 commit comments

Comments
 (0)