Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6bc3ce6

Browse filesBrowse files
feat(matter): New Matter Endpoint (#10628)
* feat(matter): add new endpoint - color temperature light --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent ff613b3 commit 6bc3ce6
Copy full SHA for 6bc3ce6

File tree

13 files changed

+829
-5
lines changed
Filter options

13 files changed

+829
-5
lines changed

‎CMakeLists.txt

Copy file name to clipboardExpand all lines: CMakeLists.txt
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ set(ARDUINO_LIBRARY_OpenThread_SRCS
169169
set(ARDUINO_LIBRARY_Matter_SRCS
170170
libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp
171171
libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp
172+
libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp
173+
libraries/Matter/src/MatterUtil/ColorFormat.cpp
172174
libraries/Matter/src/Matter.cpp)
173175

174176
set(ARDUINO_LIBRARY_PPP_SRCS

‎libraries/Matter/examples/MatterDimmableLight/MatterDimmableLight.ino

Copy file name to clipboardExpand all lines: libraries/Matter/examples/MatterDimmableLight/MatterDimmableLight.ino
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ MatterDimmableLight DimmableLight;
2323

2424
// it will keep last OnOff & Brightness state stored, using Preferences
2525
Preferences matterPref;
26-
const char *onOffPrefKey = "OnOffState";
27-
const char *brightnessPrefKey = "BrightnessState";
26+
const char *onOffPrefKey = "OnOff";
27+
const char *brightnessPrefKey = "Brightness";
2828

2929
// set your board RGB LED pin here
3030
#ifdef RGB_BUILTIN

‎libraries/Matter/examples/MatterOnOffLight/MatterOnOffLight.ino

Copy file name to clipboardExpand all lines: libraries/Matter/examples/MatterOnOffLight/MatterOnOffLight.ino
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ MatterOnOffLight OnOffLight;
2323

2424
// it will keep last OnOff state stored, using Preferences
2525
Preferences matterPref;
26-
const char *onOffPrefKey = "OnOffState";
26+
const char *onOffPrefKey = "OnOff";
2727

2828
// set your board LED pin here
2929
#ifdef LED_BUILTIN
+196Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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 Temperature CW/WW Light Endpoint
22+
MatterColorTemperatureLight CW_WW_Light;
23+
24+
// it will keep last OnOff & Brightness state stored, using Preferences
25+
Preferences matterPref;
26+
const char *onOffPrefKey = "OnOff";
27+
const char *brightnessPrefKey = "Brightness";
28+
const char *temperaturePrefKey = "Temperature";
29+
30+
// set your board RGB LED pin here
31+
#ifdef RGB_BUILTIN
32+
const uint8_t ledPin = RGB_BUILTIN;
33+
#else
34+
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
35+
#warning "Do not forget to set the RGB LED pin"
36+
#endif
37+
38+
// set your board USER BUTTON pin here
39+
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
40+
41+
// WiFi is manually set and started
42+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
43+
const char *password = "your-password"; // Change this to your WiFi password
44+
45+
// Set the RGB LED Light based on the current state of the Color Temperature Light
46+
bool setLightState(bool state, uint8_t brightness, uint16_t temperature_Mireds) {
47+
48+
if (state) {
49+
#ifdef RGB_BUILTIN
50+
CtColor_t ct = {temperature_Mireds};
51+
RgbColor_t rgb_ct = CTToRgb(ct);
52+
// simple intensity correction
53+
float brightnessPercent = (float)brightness / MatterColorTemperatureLight::MAX_BRIGHTNESS;
54+
rgb_ct.r = brightnessPercent * rgb_ct.r;
55+
rgb_ct.g = brightnessPercent * rgb_ct.g;
56+
rgb_ct.b = brightnessPercent * rgb_ct.b;
57+
// set the RGB LED
58+
rgbLedWrite(ledPin, rgb_ct.r, rgb_ct.g, rgb_ct.b);
59+
#else
60+
// No Color RGB LED, just use the brightness to control the LED
61+
analogWrite(ledPin, brightness);
62+
#endif
63+
} else {
64+
digitalWrite(ledPin, LOW);
65+
}
66+
// store last Brightness and OnOff state for when the Light is restarted / power goes off
67+
matterPref.putUChar(brightnessPrefKey, brightness);
68+
matterPref.putBool(onOffPrefKey, state);
69+
matterPref.putUShort(temperaturePrefKey, temperature_Mireds);
70+
// This callback must return the success state to Matter core
71+
return true;
72+
}
73+
74+
void setup() {
75+
// Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch
76+
pinMode(buttonPin, INPUT_PULLUP);
77+
// Initialize the LED (light) GPIO and Matter End Point
78+
pinMode(ledPin, OUTPUT);
79+
80+
Serial.begin(115200);
81+
while (!Serial) {
82+
delay(100);
83+
}
84+
85+
// We start by connecting to a WiFi network
86+
Serial.print("Connecting to ");
87+
Serial.println(ssid);
88+
// enable IPv6
89+
WiFi.enableIPv6(true);
90+
// Manually connect to WiFi
91+
WiFi.begin(ssid, password);
92+
// Wait for connection
93+
while (WiFi.status() != WL_CONNECTED) {
94+
delay(500);
95+
Serial.print(".");
96+
}
97+
Serial.println("\r\nWiFi connected");
98+
Serial.println("IP address: ");
99+
Serial.println(WiFi.localIP());
100+
delay(500);
101+
102+
// Initialize Matter EndPoint
103+
matterPref.begin("MatterPrefs", false);
104+
// default OnOff state is ON if not stored before
105+
bool lastOnOffState = matterPref.getBool(onOffPrefKey, true);
106+
// default brightness ~= 6% (15/255)
107+
uint8_t lastBrightness = matterPref.getUChar(brightnessPrefKey, 15);
108+
// default temperature ~= 454 Mireds (Warm White)
109+
uint16_t lastTemperature = matterPref.getUShort(temperaturePrefKey, MatterColorTemperatureLight::WARM_WHITE_COLOR_TEMPERATURE);
110+
CW_WW_Light.begin(lastOnOffState, lastBrightness, lastTemperature);
111+
// set the callback function to handle the Light state change
112+
CW_WW_Light.onChange(setLightState);
113+
114+
// lambda functions are used to set the attribute change callbacks
115+
CW_WW_Light.onChangeOnOff([](bool state) {
116+
Serial.printf("Light OnOff changed to %s\r\n", state ? "ON" : "OFF");
117+
return true;
118+
});
119+
CW_WW_Light.onChangeBrightness([](uint8_t level) {
120+
Serial.printf("Light Brightness changed to %d\r\n", level);
121+
return true;
122+
});
123+
CW_WW_Light.onChangeColorTemperature([](uint16_t temperature) {
124+
Serial.printf("Light Color Temperature changed to %d\r\n", temperature);
125+
return true;
126+
});
127+
128+
// Matter beginning - Last step, after all EndPoints are initialized
129+
Matter.begin();
130+
// This may be a restart of a already commissioned Matter accessory
131+
if (Matter.isDeviceCommissioned()) {
132+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
133+
Serial.printf(
134+
"Initial state: %s | brightness: %d | Color Temperature: %d mireds \r\n", CW_WW_Light ? "ON" : "OFF", CW_WW_Light.getBrightness(),
135+
CW_WW_Light.getColorTemperature()
136+
);
137+
// configure the Light based on initial on-off state and brightness
138+
CW_WW_Light.updateAccessory();
139+
}
140+
}
141+
// Button control
142+
uint32_t button_time_stamp = 0; // debouncing control
143+
bool button_state = false; // false = released | true = pressed
144+
const uint32_t debouceTime = 250; // button debouncing time (ms)
145+
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
146+
147+
void loop() {
148+
// Check Matter Light Commissioning state, which may change during execution of loop()
149+
if (!Matter.isDeviceCommissioned()) {
150+
Serial.println("");
151+
Serial.println("Matter Node is not commissioned yet.");
152+
Serial.println("Initiate the device discovery in your Matter environment.");
153+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
154+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
155+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
156+
// waits for Matter Light Commissioning.
157+
uint32_t timeCount = 0;
158+
while (!Matter.isDeviceCommissioned()) {
159+
delay(100);
160+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
161+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
162+
}
163+
}
164+
Serial.printf(
165+
"Initial state: %s | brightness: %d | Color Temperature: %d mireds \r\n", CW_WW_Light ? "ON" : "OFF", CW_WW_Light.getBrightness(),
166+
CW_WW_Light.getColorTemperature()
167+
);
168+
// configure the Light based on initial on-off state and brightness
169+
CW_WW_Light.updateAccessory();
170+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
171+
}
172+
173+
// A button is also used to control the light
174+
// Check if the button has been pressed
175+
if (digitalRead(buttonPin) == LOW && !button_state) {
176+
// deals with button debouncing
177+
button_time_stamp = millis(); // record the time while the button is pressed.
178+
button_state = true; // pressed.
179+
}
180+
181+
// Onboard User Button is used as a Light toggle switch or to decommission it
182+
uint32_t time_diff = millis() - button_time_stamp;
183+
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
184+
button_state = false; // released
185+
// Toggle button is released - toggle the light
186+
Serial.println("User button released. Toggling Light!");
187+
CW_WW_Light.toggle(); // Matter Controller also can see the change
188+
189+
// Factory reset is triggered if the button is pressed longer than 10 seconds
190+
if (time_diff > decommissioningTimeout) {
191+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
192+
CW_WW_Light = false; // turn the light off
193+
Matter.decommission();
194+
}
195+
}
196+
}
+7Lines changed: 7 additions & 0 deletions
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+
"CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y"
6+
]
7+
}

‎libraries/Matter/keywords.txt

Copy file name to clipboardExpand all lines: libraries/Matter/keywords.txt
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ Matter KEYWORD1
1010
ArduinoMatter KEYWORD1
1111
MatterOnOffLight KEYWORD1
1212
MatterDimmableLight KEYWORD1
13+
MatterColorTemperatureLight KEYWORD1
1314
MatterEndPoint KEYWORD1
15+
CtColor_t KEYWORD1
16+
XyColor_t KEYWORD1
17+
HsvColor_t KEYWORD1
18+
RgbColor_t KEYWORD1
1419

1520
#######################################
1621
# Methods and Functions (KEYWORD2)
@@ -30,12 +35,28 @@ setOnOff KEYWORD2
3035
getOnOff KEYWORD2
3136
setBrightness KEYWORD2
3237
getBrightness KEYWORD2
38+
setColorTemperature KEYWORD2
39+
getColorTemperature KEYWORD2
3340
toggle KEYWORD2
3441
updateAccessory KEYWORD2
3542
onChange KEYWORD2
3643
onChangeOnOff KEYWORD2
3744
onChangeBrightness KEYWORD2
45+
onChangeColorTemperature KEYWORD2
46+
XYToRgb KEYWORD2
47+
HsvToRgb KEYWORD2
48+
CTToRgb KEYWORD2
49+
RgbToHsv KEYWORD2
3850

3951
#######################################
4052
# Constants (LITERAL1)
4153
#######################################
54+
55+
MAX_BRIGHTNESS LITERAL1
56+
MAX_COLOR_TEMPERATURE LITERAL1
57+
MIN_COLOR_TEMPERATURE LITERAL1
58+
COOL_WHITE_COLOR_TEMPERATURE LITERAL1
59+
DAYLIGHT_WHITE_COLOR_TEMPERATURE LITERAL1
60+
WHITE_COLOR_TEMPERATURE LITERAL1
61+
SOFT_WHITE_COLOR_TEMPERATURE LITERAL1
62+
WARM_WHITE_COLOR_TEMPERATURE LITERAL1

‎libraries/Matter/src/Matter.h

Copy file name to clipboardExpand all lines: libraries/Matter/src/Matter.h
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
#include <Arduino.h>
2020
#include <esp_matter.h>
21+
#include <MatterUtil/ColorFormat.h>
2122
#include <MatterEndpoints/MatterOnOffLight.h>
2223
#include <MatterEndpoints/MatterDimmableLight.h>
24+
#include <MatterEndpoints/MatterColorTemperatureLight.h>
2325

2426
using namespace esp_matter;
2527

@@ -47,6 +49,7 @@ class ArduinoMatter {
4749
// list of Matter EndPoints Friend Classes
4850
friend class MatterOnOffLight;
4951
friend class MatterDimmableLight;
52+
friend class MatterColorTemperatureLight;
5053

5154
protected:
5255
static void _init();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.