Skip to content

Commit 627e36e

Browse files
authored
Merge branch 'master' into documentation/matter_docs
2 parents 0e28460 + 21ddb09 commit 627e36e

File tree

38 files changed

+4745
-15
lines changed

38 files changed

+4745
-15
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# The following lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
7+
#target_compile_options(espressif__esp_matter PUBLIC
8+
# -DCHIP_ADDRESS_RESOLVE_IMPL_INCLUDE_HEADER=<lib/address_resolve/AddressResolve_DefaultImpl.h>
9+
# -DCHIP_HAVE_CONFIG_H)
10+
#list(APPEND compile_definitions "CHIP_HAVE_CONFIG_H=1")
11+
#list(APPEND compile_definitions "CHIP_ADDRESS_RESOLVE_IMPL_INCLUDE_HEADER=<lib/address_resolve/AddressResolve_DefaultImpl.h>")
12+
13+
project(Matter_Thread_Light)
14+
15+
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++2a;-Os;-DCHIP_HAVE_CONFIG_H" APPEND)
16+
idf_build_set_property(C_COMPILE_OPTIONS "-Os" APPEND)
17+
# For RISCV chips, project_include.cmake sets -Wno-format, but does not clear various
18+
# flags that depend on -Wformat
19+
idf_build_set_property(COMPILE_OPTIONS "-Wno-format-nonliteral;-Wno-format-security" APPEND)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
| Supported Targets | ESP32-C5 | ESP32-C6 | ESP32-H2 |
2+
| ----------------- | -------- | -------- | -------- |
3+
4+
# Arduino ESP-Matter over Thread example using ESP32-C5, ESP32-C6 and ESP32-H2 (any SoC with Thread radio)
5+
This is an Arduino as IDF Project to build an ESP-Matter over Thread RGB Light using ESP32-C5/C6/H2 and ESP-Matter Arduino API \
6+
This example shall work with Arduino 3.3.2+ and also IDF 5.5.1+\
7+
It is necessary to make sure that the IDF version matches with the one used to release the Arduino Core version.\
8+
This can be done looking into release information in https://github.com/espressif/arduino-esp32/releases \
9+
10+
Any example from [ESP32 Matter Library examples](https://github.com/espressif/arduino-esp32/tree/master/libraries/Matter/examples)
11+
can be used to build the application.\
12+
Feel free to create your own Arduino Matter sketch!\
13+
Do not forget to rename the `sketch_file_name.ino` to `sketch_file_name.cpp` in `main` folder.
14+
15+
The `main/idf_component.yml` file holds the ESP-Matter component version and Arduino Core version.\
16+
Edit this file to set the target versions, if necessary.
17+
18+
# General Instructions:
19+
20+
1- Install the required IDF version into your computer. It can be done following the guide in
21+
https://docs.espressif.com/projects/esp-idf/en/stable/esp32c6/get-started/index.html
22+
23+
For Windows: https://docs.espressif.com/projects/esp-idf/en/stable/esp32c6/get-started/index.html \
24+
For Linux or macOS: https://docs.espressif.com/projects/esp-idf/en/stable/esp32c6/get-started/linux-macos-setup.html
25+
26+
2- Test IDF with `idf.py --version` to check if it is installed and configured correctly.
27+
28+
3- To create a ESP-IDF project from this example with the latest release of Arduino-esp32, you can simply run command:
29+
`idf.py create-project-from-example "espressif/arduino-esp32:Arduino_ESP_Matter_over_OpenThread"`
30+
ESP-IDF will download all dependencies needed from the component registry and setup the project for you.
31+
32+
4- Open an IDF terminal and execute `idf.py set-target esp32c6` (esp32c5 and esp32h2 are also possible targets)
33+
34+
5- Execute `idf.py -p <your COM or /dev/tty port connected to the ESP32-C6> flash monitor`
35+
36+
6- It will build, upload and show the UART0 output in the screen.
37+
38+
7- Try to add the new Matter device to your local Matter environment.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
targets:
2+
esp32s2: false
3+
esp32s3: false
4+
esp32c2: false
5+
esp32p4: false
6+
requires:
7+
- CONFIG_OPENTHREAD_ENABLED=y
8+
- CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y
9+
- CONFIG_MBEDTLS_HKDF_C=y
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRC_DIRS "."
2+
INCLUDE_DIRS ".")
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dependencies:
2+
espressif/esp_matter:
3+
version: ">=1.3.0"
4+
require: public
5+
espressif/arduino-esp32:
6+
version: ">=3.1.0"
7+
override_path: "../../../"
8+
pre_release: true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Name, Type, SubType, Offset, Size, Flags
2+
# Note: Firmware partition offset needs to be 64K aligned, initial 36K (9 sectors) are reserved for bootloader and partition table
3+
esp_secure_cert, 0x3F, ,0xd000, 0x2000, encrypted
4+
nvs, data, nvs, 0x10000, 0xC000,
5+
nvs_keys, data, nvs_keys,, 0x1000, encrypted
6+
otadata, data, ota, , 0x2000
7+
phy_init, data, phy, , 0x1000,
8+
ota_0, app, ota_0, 0x20000, 0x1E0000,
9+
ota_1, app, ota_1, 0x200000, 0x1E0000,
10+
fctry, data, nvs, 0x3E0000, 0x6000
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Arduino ESP32 settings
2+
CONFIG_BOOTLOADER_LOG_LEVEL_ERROR=y
3+
CONFIG_AUTOSTART_ARDUINO=y
4+
CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL_INFO=y
5+
CONFIG_FREERTOS_HZ=1000
6+
CONFIG_LOG_DEFAULT_LEVEL_ERROR=y
7+
8+
# Enables Arduino Selective Library Compilation: Arduino Matter + Preferences Library
9+
CONFIG_ARDUINO_SELECTIVE_COMPILATION=y
10+
CONFIG_ARDUINO_SELECTIVE_Preferences=y
11+
CONFIG_ARDUINO_SELECTIVE_Network=y
12+
CONFIG_ARDUINO_SELECTIVE_ESPmDNS=y
13+
CONFIG_ARDUINO_SELECTIVE_Matter=y
14+
15+
# Flash Configuration
16+
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
17+
CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR=y
18+
CONFIG_ESPTOOLPY_FLASHMODE="dio"
19+
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
20+
CONFIG_ESPTOOLPY_FLASHFREQ="80m"
21+
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
22+
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
23+
CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE=y
24+
CONFIG_ESPTOOLPY_BEFORE_RESET=y
25+
CONFIG_ESPTOOLPY_BEFORE="default_reset"
26+
CONFIG_ESPTOOLPY_AFTER_RESET=y
27+
CONFIG_ESPTOOLPY_AFTER="hard_reset"
28+
CONFIG_ESPTOOLPY_MONITOR_BAUD=115200
29+
30+
# libsodium
31+
CONFIG_LIBSODIUM_USE_MBEDTLS_SHA=y
32+
33+
# NIMBLE
34+
CONFIG_BT_ENABLED=y
35+
CONFIG_BT_NIMBLE_ENABLED=y
36+
CONFIG_BT_NIMBLE_EXT_ADV=n
37+
CONFIG_BT_NIMBLE_HCI_EVT_BUF_SIZE=70
38+
CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING=n
39+
40+
# Enable OpenThread
41+
CONFIG_OPENTHREAD_ENABLED=y
42+
CONFIG_OPENTHREAD_SRP_CLIENT=y
43+
CONFIG_OPENTHREAD_DNS_CLIENT=y
44+
CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC=n
45+
CONFIG_OPENTHREAD_LOG_LEVEL_NOTE=y
46+
CONFIG_OPENTHREAD_CLI=n
47+
48+
# Disable lwip ipv6 autoconfig
49+
CONFIG_LWIP_IPV6_AUTOCONFIG=n
50+
51+
# Use a custom partition table
52+
CONFIG_PARTITION_TABLE_CUSTOM=y
53+
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
54+
55+
# LwIP config for OpenThread
56+
CONFIG_LWIP_IPV6_NUM_ADDRESSES=8
57+
CONFIG_LWIP_MULTICAST_PING=y
58+
59+
# MDNS platform
60+
CONFIG_USE_MINIMAL_MDNS=n
61+
CONFIG_ENABLE_EXTENDED_DISCOVERY=y
62+
63+
# Disable STA and AP for ESP32C6 ==> no Wifi, thread only
64+
CONFIG_ENABLE_WIFI_STATION=n
65+
CONFIG_ENABLE_WIFI_AP=n
66+
67+
# Enable HKDF in mbedtls
68+
CONFIG_MBEDTLS_HKDF_C=y

0 commit comments

Comments
 (0)