Skip to content

Commit dca8a90

Browse files
committed
feat(matter): adds new matter generic switch endpoint
1 parent 414e4f3 commit dca8a90

File tree

7 files changed

+268
-1
lines changed

7 files changed

+268
-1
lines changed

Diff for: CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ set(ARDUINO_LIBRARY_OpenThread_SRCS
168168
libraries/OpenThread/src/OThreadCLI_Util.cpp)
169169

170170
set(ARDUINO_LIBRARY_Matter_SRCS
171+
libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp
171172
libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp
172173
libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp
173174
libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
// Generic Switch Endpoint - works as a smart button with a single click
22+
MatterGenericSwitch SmartButton;
23+
24+
// set your board USER BUTTON pin here
25+
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
26+
27+
// WiFi is manually set and started
28+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
29+
const char *password = "your-password"; // Change this to your WiFi password
30+
31+
void setup() {
32+
// Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch
33+
pinMode(buttonPin, INPUT_PULLUP);
34+
35+
Serial.begin(115200);
36+
while (!Serial) {
37+
delay(100);
38+
}
39+
40+
// We start by connecting to a WiFi network
41+
Serial.print("Connecting to ");
42+
Serial.println(ssid);
43+
// enable IPv6
44+
WiFi.enableIPv6(true);
45+
// Manually connect to WiFi
46+
WiFi.begin(ssid, password);
47+
// Wait for connection
48+
while (WiFi.status() != WL_CONNECTED) {
49+
delay(500);
50+
Serial.print(".");
51+
}
52+
Serial.println("\r\nWiFi connected");
53+
Serial.println("IP address: ");
54+
Serial.println(WiFi.localIP());
55+
delay(500);
56+
57+
// Initialize the Matter EndPoint
58+
SmartButton.begin();
59+
60+
// Matter beginning - Last step, after all EndPoints are initialized
61+
Matter.begin();
62+
// This may be a restart of a already commissioned Matter accessory
63+
if (Matter.isDeviceCommissioned()) {
64+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
65+
}
66+
}
67+
// Button control
68+
uint32_t button_time_stamp = 0; // debouncing control
69+
bool button_state = false; // false = released | true = pressed
70+
const uint32_t debouceTime = 250; // button debouncing time (ms)
71+
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the Matter Fabric
72+
73+
void loop() {
74+
// Check Matter Accessory Commissioning state, which may change during execution of loop()
75+
if (!Matter.isDeviceCommissioned()) {
76+
Serial.println("");
77+
Serial.println("Matter Node is not commissioned yet.");
78+
Serial.println("Initiate the device discovery in your Matter environment.");
79+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
80+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
81+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
82+
// waits for Matter Generic Switch Commissioning.
83+
uint32_t timeCount = 0;
84+
while (!Matter.isDeviceCommissioned()) {
85+
delay(100);
86+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
87+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
88+
}
89+
}
90+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
91+
}
92+
93+
// A builtin button is used to trigger a command to the Matter Controller
94+
// Check if the button has been pressed
95+
if (digitalRead(buttonPin) == LOW && !button_state) {
96+
// deals with button debouncing
97+
button_time_stamp = millis(); // record the time while the button is pressed.
98+
button_state = true; // pressed.
99+
}
100+
101+
// Onboard User Button is used as a smart button or to decommission it
102+
uint32_t time_diff = millis() - button_time_stamp;
103+
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
104+
button_state = false; // released
105+
// builtin button is released - send a click event to the Matter Controller
106+
Serial.println("User button released. Sending Click to the Matter Controller!");
107+
// Matter Controller will receive an event and, if programmed, it will trigger an action
108+
SmartButton.click();
109+
110+
// Factory reset is triggered if the button is pressed longer than 10 seconds
111+
if (time_diff > decommissioningTimeout) {
112+
Serial.println("Decommissioning the Generic Switch Matter Accessory. It shall be commissioned again.");
113+
Matter.decommission();
114+
}
115+
}
116+
}

Diff for: libraries/Matter/examples/MatterSmartButon/ci.json

+7
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+
}

Diff for: libraries/Matter/keywords.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
Matter KEYWORD1
1010
ArduinoMatter KEYWORD1
11+
MatterGenericSwitch KEYWORD1
1112
MatterOnOffLight KEYWORD1
1213
MatterDimmableLight KEYWORD1
1314
MatterColorTemperatureLight KEYWORD1
@@ -45,7 +46,7 @@ onChangeOnOff KEYWORD2
4546
onChangeBrightness KEYWORD2
4647
onChangeColorTemperature KEYWORD2
4748
onChangeColorHSV KEYWORD2
48-
49+
click KEYWORD2
4950

5051
#######################################
5152
# Constants (LITERAL1)

Diff for: libraries/Matter/src/Matter.h

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <Arduino.h>
2020
#include <esp_matter.h>
2121
#include <ColorFormat.h>
22+
#include <MatterEndpoints/MatterGenericSwitch.h>
2223
#include <MatterEndpoints/MatterOnOffLight.h>
2324
#include <MatterEndpoints/MatterDimmableLight.h>
2425
#include <MatterEndpoints/MatterColorTemperatureLight.h>
@@ -48,6 +49,7 @@ class ArduinoMatter {
4849
static void decommission();
4950

5051
// list of Matter EndPoints Friend Classes
52+
friend class MatterGenericSwitch;
5153
friend class MatterOnOffLight;
5254
friend class MatterDimmableLight;
5355
friend class MatterColorTemperatureLight;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
#include <sdkconfig.h>
16+
#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL
17+
18+
#include <Matter.h>
19+
#include <app/server/Server.h>
20+
#include <MatterEndpoints/MatterGenericSwitch.h>
21+
22+
using namespace esp_matter;
23+
using namespace esp_matter::endpoint;
24+
using namespace esp_matter::cluster;
25+
using namespace chip::app::Clusters;
26+
27+
MatterGenericSwitch::MatterGenericSwitch() {
28+
}
29+
30+
MatterGenericSwitch::~MatterGenericSwitch() {
31+
end();
32+
}
33+
34+
bool MatterGenericSwitch::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) {
35+
if (!started) {
36+
log_e("Matter Generic Switch device has not begun.");
37+
return false;
38+
}
39+
40+
log_d("Generic Switch Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);
41+
return true;
42+
}
43+
44+
bool MatterGenericSwitch::begin() {
45+
ArduinoMatter::_init();
46+
generic_switch::config_t switch_config;
47+
48+
// endpoint handles can be used to add/modify clusters.
49+
endpoint_t *endpoint = generic_switch::create(node::get(), &switch_config, ENDPOINT_FLAG_NONE, (void *)this);
50+
if (endpoint == nullptr) {
51+
log_e("Failed to create Generic swtich endpoint");
52+
return false;
53+
}
54+
// Add group cluster to the switch endpoint
55+
cluster::groups::config_t groups_config;
56+
cluster::groups::create(endpoint, &groups_config, CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);
57+
58+
cluster_t* aCluster = cluster::get(endpoint,Descriptor::Id);
59+
esp_matter::cluster::descriptor::feature::taglist::add(aCluster);
60+
61+
cluster::fixed_label::config_t fl_config;
62+
cluster::fixed_label::create(endpoint, &fl_config, CLUSTER_FLAG_SERVER);
63+
64+
cluster::user_label::config_t ul_config;
65+
cluster::user_label::create(endpoint, &ul_config, CLUSTER_FLAG_SERVER);
66+
67+
aCluster = cluster::get(endpoint, Switch::Id);
68+
switch_cluster::feature::momentary_switch::add(aCluster);
69+
switch_cluster::event::create_initial_press(aCluster);
70+
71+
switch_cluster::feature::momentary_switch::add(aCluster);
72+
73+
switch_cluster::attribute::create_current_position(aCluster, 0);
74+
switch_cluster::attribute::create_number_of_positions(aCluster, 2);
75+
76+
setEndPointId(endpoint::get_id(endpoint));
77+
log_i("Generic Switch created with endpoint_id %d", getEndPointId());
78+
started = true;
79+
return true;
80+
}
81+
82+
void MatterGenericSwitch::end() {
83+
started = false;
84+
}
85+
86+
void MatterGenericSwitch::click() {
87+
if (!started) {
88+
log_e("Matter Generic Switch device has not begun.");
89+
return;
90+
}
91+
92+
int switch_endpoint_id = getEndPointId();
93+
uint8_t newPosition = 1;
94+
// Press moves Position from 0 (off) to 1 (on)
95+
chip::DeviceLayer::SystemLayer().ScheduleLambda([switch_endpoint_id, newPosition]() {
96+
// InitialPress event takes newPosition as event data
97+
switch_cluster::event::send_initial_press(switch_endpoint_id, newPosition);
98+
});
99+
}
100+
101+
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#pragma once
16+
#include <sdkconfig.h>
17+
#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL
18+
19+
#include <Matter.h>
20+
#include <MatterEndPoint.h>
21+
22+
// Matter Generic Switch Endpoint that works as a single click smart button
23+
class MatterGenericSwitch : public MatterEndPoint {
24+
public:
25+
MatterGenericSwitch();
26+
~MatterGenericSwitch();
27+
virtual bool begin();
28+
void end(); // this will just stop processing Matter events
29+
30+
// send a simple click event to the Matter Controller
31+
void click();
32+
33+
// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.
34+
bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val);
35+
36+
protected:
37+
bool started = false;
38+
};
39+
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */

0 commit comments

Comments
 (0)