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 136190d

Browse filesBrowse files
Jason2866SuGliderP-R-O-C-H-Yme-no-devpre-commit-ci-lite[bot]
authored
Merge 31 (#562)
* fix(zigbee): Increase timeout, commision again on failure + setScanDuration (espressif#10651) * fix(zigbee): Increase timeout, commision again on failure * fix(zigbee): Update library keywords * feat(Matter): add new MatterColorLight endpoint (espressif#10654) * feat(matter): adds Matter Color Light endpoint * Tasmota changes * optional Ethernet support (JL1101 driver added) * esp-modem only esp32, esp32s2 and esp32s3 * remove `OpenThread` * remove all BT BLE libraries * remove zigbee * remove SPIFFS * remove Client Secure * remove Provisioning * remove TfLite, Insights and Rainmaker * make GPIOViewer working see arendst/Tasmota@9696118 * remove FS log which is just littering * feat(matter): New example => Wifi Prov within Matter as alternative for wireless network provisioning (espressif#10658) * feat(matter): Arduino WiFi Prov example for Matter * feat(matter): Adds Matter Enhanced Color Light Endpoint (CW/WW/RGB) (espressif#10657) * feat(matter): created enhanced color light new matter endpoint and example * feat(matter): Adds a new Matter Endpoint: Generic Switch (smart button) (espressif#10662) * feat(matter): adds new matter generic switch endpoint * fix(matter): no need of arduino preferences here * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --------- Co-authored-by: Rodrigo Garcia <rodrigo.garcia@espressif.com> Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 7c4a64f commit 136190d
Copy full SHA for 136190d

File tree

Expand file treeCollapse file tree

13 files changed

+1101
-1
lines changed
Filter options
Expand file treeCollapse file tree

13 files changed

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

0 commit comments

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