Skip to content

Commit af84da6

Browse files
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>
1 parent 9d8df8b commit af84da6

File tree

7 files changed

+266
-1
lines changed

7 files changed

+266
-1
lines changed

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

+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
@@ -46,7 +47,7 @@ onChangeOnOff KEYWORD2
4647
onChangeBrightness KEYWORD2
4748
onChangeColorTemperature KEYWORD2
4849
onChangeColorHSV KEYWORD2
49-
50+
click KEYWORD2
5051

5152
#######################################
5253
# Constants (LITERAL1)

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>
@@ -49,6 +50,7 @@ class ArduinoMatter {
4950
static void decommission();
5051

5152
// list of Matter EndPoints Friend Classes
53+
friend class MatterGenericSwitch;
5254
friend class MatterOnOffLight;
5355
friend class MatterDimmableLight;
5456
friend class MatterColorTemperatureLight;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
MatterGenericSwitch::~MatterGenericSwitch() {
30+
end();
31+
}
32+
33+
bool MatterGenericSwitch::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) {
34+
if (!started) {
35+
log_e("Matter Generic Switch device has not begun.");
36+
return false;
37+
}
38+
39+
log_d("Generic Switch Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);
40+
return true;
41+
}
42+
43+
bool MatterGenericSwitch::begin() {
44+
ArduinoMatter::_init();
45+
generic_switch::config_t switch_config;
46+
47+
// endpoint handles can be used to add/modify clusters.
48+
endpoint_t *endpoint = generic_switch::create(node::get(), &switch_config, ENDPOINT_FLAG_NONE, (void *)this);
49+
if (endpoint == nullptr) {
50+
log_e("Failed to create Generic swtich endpoint");
51+
return false;
52+
}
53+
// Add group cluster to the switch endpoint
54+
cluster::groups::config_t groups_config;
55+
cluster::groups::create(endpoint, &groups_config, CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);
56+
57+
cluster_t *aCluster = cluster::get(endpoint, Descriptor::Id);
58+
esp_matter::cluster::descriptor::feature::taglist::add(aCluster);
59+
60+
cluster::fixed_label::config_t fl_config;
61+
cluster::fixed_label::create(endpoint, &fl_config, CLUSTER_FLAG_SERVER);
62+
63+
cluster::user_label::config_t ul_config;
64+
cluster::user_label::create(endpoint, &ul_config, CLUSTER_FLAG_SERVER);
65+
66+
aCluster = cluster::get(endpoint, Switch::Id);
67+
switch_cluster::feature::momentary_switch::add(aCluster);
68+
switch_cluster::event::create_initial_press(aCluster);
69+
70+
switch_cluster::feature::momentary_switch::add(aCluster);
71+
72+
switch_cluster::attribute::create_current_position(aCluster, 0);
73+
switch_cluster::attribute::create_number_of_positions(aCluster, 2);
74+
75+
setEndPointId(endpoint::get_id(endpoint));
76+
log_i("Generic Switch created with endpoint_id %d", getEndPointId());
77+
started = true;
78+
return true;
79+
}
80+
81+
void MatterGenericSwitch::end() {
82+
started = false;
83+
}
84+
85+
void MatterGenericSwitch::click() {
86+
if (!started) {
87+
log_e("Matter Generic Switch device has not begun.");
88+
return;
89+
}
90+
91+
int switch_endpoint_id = getEndPointId();
92+
uint8_t newPosition = 1;
93+
// Press moves Position from 0 (off) to 1 (on)
94+
chip::DeviceLayer::SystemLayer().ScheduleLambda([switch_endpoint_id, newPosition]() {
95+
// InitialPress event takes newPosition as event data
96+
switch_cluster::event::send_initial_press(switch_endpoint_id, newPosition);
97+
});
98+
}
99+
100+
#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)