Skip to content

Commit 2ae28cc

Browse files
committed
upd
1 parent 803f4a5 commit 2ae28cc

File tree

72 files changed

+7461
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+7461
-2
lines changed
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: 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+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
// On/Off Light Endpoint
21+
MatterOnOffLight OnOffLight;
22+
23+
// WiFi is manually set and started
24+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
25+
const char *password = "your-password"; // Change this to your WiFi password
26+
27+
void setup() {
28+
Serial.begin(115200);
29+
30+
// We start by connecting to a WiFi network
31+
Serial.print("Connecting to ");
32+
Serial.println(ssid);
33+
// Manually connect to WiFi
34+
WiFi.begin(ssid, password);
35+
// Wait for connection
36+
while (WiFi.status() != WL_CONNECTED) {
37+
delay(500);
38+
Serial.print(".");
39+
}
40+
Serial.println("\r\nWiFi connected");
41+
Serial.println("IP address: ");
42+
Serial.println(WiFi.localIP());
43+
delay(500);
44+
45+
// Initialize at least one Matter EndPoint
46+
OnOffLight.begin();
47+
48+
// Matter beginning - Last step, after all EndPoints are initialized
49+
Matter.begin();
50+
}
51+
52+
void loop() {
53+
// Check Matter Commissioning state
54+
if (!Matter.isDeviceCommissioned()) {
55+
Serial.println("");
56+
Serial.println("Matter Node is not commissioned yet.");
57+
Serial.println("Initiate the device discovery in your Matter environment.");
58+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
59+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
60+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
61+
// waits for Matter Light Commissioning.
62+
while (!Matter.isDeviceCommissioned()) {
63+
delay(5000);
64+
Serial.println("Matter Fabric not commissioned yet. Waiting for commissioning.");
65+
}
66+
}
67+
Serial.println("Matter Node is commissioned and connected to Wi-Fi.");
68+
Serial.println("====> Decommissioning in 30 seconds. <====");
69+
delay(30000);
70+
Matter.decommission();
71+
Serial.println("Matter Node is decommissioned. Commsssioning widget shall start over.");
72+
}
Lines 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)