Skip to content

Commit 8e59a1b

Browse files
authored
Merge pull request #10722 from SuGlider/matter_plugin
feat(matter): adds a new endpoint to Matter - On/Off Plugin (Power Relay)
2 parents 417c7ee + faed832 commit 8e59a1b

File tree

7 files changed

+345
-0
lines changed

7 files changed

+345
-0
lines changed

Diff for: CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ set(ARDUINO_LIBRARY_Matter_SRCS
180180
libraries/Matter/src/MatterEndpoints/MatterContactSensor.cpp
181181
libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp
182182
libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.cpp
183+
libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.cpp
183184
libraries/Matter/src/Matter.cpp)
184185

185186
set(ARDUINO_LIBRARY_PPP_SRCS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
// On/Off Plugin Endpoint
22+
MatterOnOffPlugin OnOffPlugin;
23+
24+
// it will keep last OnOff state stored, using Preferences
25+
Preferences matterPref;
26+
const char *onOffPrefKey = "OnOff";
27+
28+
// set your board Power Relay pin here - this example uses the built-in LED for easy visualization
29+
#ifdef LED_BUILTIN
30+
const uint8_t onoffPin = LED_BUILTIN;
31+
#else
32+
const uint8_t onoffPin = 2; // Set your pin here - usually a power relay
33+
#warning "Do not forget to set the Power Relay pin"
34+
#endif
35+
36+
// board USER BUTTON pin necessary for Decommissioning
37+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
38+
39+
// Button control
40+
uint32_t button_time_stamp = 0; // debouncing control
41+
bool button_state = false; // false = released | true = pressed
42+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
43+
44+
// WiFi is manually set and started
45+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
46+
const char *password = "your-password"; // Change this to your WiFi password
47+
48+
// Matter Protocol Endpoint Callback
49+
bool setPluginOnOff(bool state) {
50+
Serial.printf("User Callback :: New Plugin State = %s\r\n", state ? "ON" : "OFF");
51+
if (state) {
52+
digitalWrite(onoffPin, HIGH);
53+
} else {
54+
digitalWrite(onoffPin, LOW);
55+
}
56+
// store last OnOff state for when the Plugin is restarted / power goes off
57+
matterPref.putBool(onOffPrefKey, state);
58+
// This callback must return the success state to Matter core
59+
return true;
60+
}
61+
62+
void setup() {
63+
// Initialize the USER BUTTON
64+
pinMode(buttonPin, INPUT_PULLUP);
65+
// Initialize the Power Relay (plugin) GPIO
66+
pinMode(onoffPin, OUTPUT);
67+
68+
Serial.begin(115200);
69+
70+
// We start by connecting to a WiFi network
71+
Serial.print("Connecting to ");
72+
Serial.println(ssid);
73+
WiFi.begin(ssid, password);
74+
// Wait for connection
75+
while (WiFi.status() != WL_CONNECTED) {
76+
delay(500);
77+
Serial.print(".");
78+
}
79+
Serial.println("\r\nWiFi connected");
80+
Serial.println("IP address: ");
81+
Serial.println(WiFi.localIP());
82+
delay(500);
83+
84+
// Initialize Matter EndPoint
85+
matterPref.begin("MatterPrefs", false);
86+
bool lastOnOffState = matterPref.getBool(onOffPrefKey, false);
87+
OnOffPlugin.begin(lastOnOffState);
88+
OnOffPlugin.onChange(setPluginOnOff);
89+
90+
// Matter beginning - Last step, after all EndPoints are initialized
91+
Matter.begin();
92+
// This may be a restart of a already commissioned Matter accessory
93+
if (Matter.isDeviceCommissioned()) {
94+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
95+
Serial.printf("Initial state: %s\r\n", OnOffPlugin.getOnOff() ? "ON" : "OFF");
96+
OnOffPlugin.updateAccessory(); // configure the Plugin based on initial state
97+
}
98+
}
99+
100+
void loop() {
101+
// Check Matter Plugin Commissioning state, which may change during execution of loop()
102+
if (!Matter.isDeviceCommissioned()) {
103+
Serial.println("");
104+
Serial.println("Matter Node is not commissioned yet.");
105+
Serial.println("Initiate the device discovery in your Matter environment.");
106+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
107+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
108+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
109+
// waits for Matter Plugin Commissioning.
110+
uint32_t timeCount = 0;
111+
while (!Matter.isDeviceCommissioned()) {
112+
delay(100);
113+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
114+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
115+
}
116+
}
117+
Serial.printf("Initial state: %s\r\n", OnOffPlugin.getOnOff() ? "ON" : "OFF");
118+
OnOffPlugin.updateAccessory(); // configure the Plugin based on initial state
119+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
120+
}
121+
122+
// Check if the button has been pressed
123+
if (digitalRead(buttonPin) == LOW && !button_state) {
124+
// deals with button debouncing
125+
button_time_stamp = millis(); // record the time while the button is pressed.
126+
button_state = true; // pressed.
127+
}
128+
129+
// Onboard User Button is used to decommission the Matter Node
130+
if (button_state && digitalRead(buttonPin) == HIGH) {
131+
button_state = false; // released
132+
}
133+
134+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
135+
uint32_t time_diff = millis() - button_time_stamp;
136+
if (button_state && time_diff > decommissioningTimeout) {
137+
Serial.println("Decommissioning the Plugin Matter Accessory. It shall be commissioned again.");
138+
OnOffPlugin.setOnOff(false); // turn the plugin off
139+
Matter.decommission();
140+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
141+
}
142+
}

Diff for: libraries/Matter/examples/MatterOnOffPlugin/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

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ MatterHumiditySensor KEYWORD1
2323
MatterContactSensor KEYWORD1
2424
MatterPressureSensor KEYWORD1
2525
MatterOccupancySensor KEYWORD1
26+
MatterOnOffPlugin KEYWORD1
2627

2728
#######################################
2829
# Methods and Functions (KEYWORD2)

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

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <MatterEndpoints/MatterContactSensor.h>
3232
#include <MatterEndpoints/MatterPressureSensor.h>
3333
#include <MatterEndpoints/MatterOccupancySensor.h>
34+
#include <MatterEndpoints/MatterOnOffPlugin.h>
3435

3536
using namespace esp_matter;
3637

@@ -68,6 +69,7 @@ class ArduinoMatter {
6869
friend class MatterContactSensor;
6970
friend class MatterPressureSensor;
7071
friend class MatterOccupancySensor;
72+
friend class MatterOnOffPlugin;
7173

7274
protected:
7375
static void _init();
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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/MatterOnOffPlugin.h>
21+
22+
using namespace esp_matter;
23+
using namespace esp_matter::endpoint;
24+
using namespace chip::app::Clusters;
25+
26+
bool MatterOnOffPlugin::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) {
27+
bool ret = true;
28+
if (!started) {
29+
log_e("Matter On-Off Plugin device has not begun.");
30+
return false;
31+
}
32+
33+
log_d("OnOff Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);
34+
35+
if (endpoint_id == getEndPointId()) {
36+
log_d("OnOffPlugin state changed to %d", val->val.b);
37+
if (cluster_id == OnOff::Id) {
38+
if (attribute_id == OnOff::Attributes::OnOff::Id) {
39+
if (_onChangeOnOffCB != NULL) {
40+
ret &= _onChangeOnOffCB(val->val.b);
41+
}
42+
if (_onChangeCB != NULL) {
43+
ret &= _onChangeCB(val->val.b);
44+
}
45+
if (ret == true) {
46+
onOffState = val->val.b;
47+
}
48+
}
49+
}
50+
}
51+
return ret;
52+
}
53+
54+
MatterOnOffPlugin::MatterOnOffPlugin() {}
55+
56+
MatterOnOffPlugin::~MatterOnOffPlugin() {
57+
end();
58+
}
59+
60+
bool MatterOnOffPlugin::begin(bool initialState) {
61+
ArduinoMatter::_init();
62+
on_off_plugin_unit::config_t plugin_config;
63+
64+
plugin_config.on_off.on_off = initialState;
65+
plugin_config.on_off.lighting.start_up_on_off = nullptr;
66+
67+
// endpoint handles can be used to add/modify clusters.
68+
endpoint_t *endpoint = on_off_plugin_unit::create(node::get(), &plugin_config, ENDPOINT_FLAG_NONE, (void *)this);
69+
if (endpoint == nullptr) {
70+
log_e("Failed to create on-off plugin endpoint");
71+
return false;
72+
}
73+
onOffState = initialState;
74+
setEndPointId(endpoint::get_id(endpoint));
75+
log_i("On-Off Plugin created with endpoint_id %d", getEndPointId());
76+
started = true;
77+
return true;
78+
}
79+
80+
void MatterOnOffPlugin::end() {
81+
started = false;
82+
}
83+
84+
void MatterOnOffPlugin::updateAccessory() {
85+
if (_onChangeCB != NULL) {
86+
_onChangeCB(onOffState);
87+
}
88+
}
89+
90+
bool MatterOnOffPlugin::setOnOff(bool newState) {
91+
if (!started) {
92+
log_e("Matter On-Off Plugin device has not begun.");
93+
return false;
94+
}
95+
96+
// avoid processing the a "no-change"
97+
if (onOffState == newState) {
98+
return true;
99+
}
100+
101+
esp_matter_attr_val_t onoffVal = esp_matter_invalid(NULL);
102+
103+
if (!getAttributeVal(OnOff::Id, OnOff::Attributes::OnOff::Id, &onoffVal)) {
104+
log_e("Failed to get Pressure Sensor Attribute.");
105+
return false;
106+
}
107+
if (onoffVal.val.b != newState) {
108+
onoffVal.val.b = newState;
109+
bool ret;
110+
ret = updateAttributeVal(OnOff::Id, OnOff::Attributes::OnOff::Id, &onoffVal);
111+
if (!ret) {
112+
log_e("Failed to update Pressure Sensor Measurement Attribute.");
113+
return false;
114+
}
115+
onOffState = newState;
116+
}
117+
log_v("Plugin OnOff state set to %s", newState ? "ON" : "OFF");
118+
return true;
119+
}
120+
121+
bool MatterOnOffPlugin::getOnOff() {
122+
return onOffState;
123+
}
124+
125+
bool MatterOnOffPlugin::toggle() {
126+
return setOnOff(!onOffState);
127+
}
128+
129+
MatterOnOffPlugin::operator bool() {
130+
return getOnOff();
131+
}
132+
133+
void MatterOnOffPlugin::operator=(bool newState) {
134+
setOnOff(newState);
135+
}
136+
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
class MatterOnOffPlugin : public MatterEndPoint {
23+
public:
24+
MatterOnOffPlugin();
25+
~MatterOnOffPlugin();
26+
virtual bool begin(bool initialState = false); // default initial state is off
27+
void end(); // this will just stop processing Plugin Matter events
28+
29+
bool setOnOff(bool newState); // returns true if successful
30+
bool getOnOff(); // returns current plugin state
31+
bool toggle(); // returns true if successful
32+
33+
// used to update the state of the plugin using the current Matter Plugin internal state
34+
// It is necessary to set a user callback function using onChange() to handle the physical plugin state
35+
void updateAccessory();
36+
37+
operator bool(); // returns current plugin state
38+
void operator=(bool state); // turns plugin on or off
39+
// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.
40+
bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val);
41+
// User Callback for whenever the Plugin state is changed by the Matter Controller
42+
using EndPointCB = std::function<bool(bool)>;
43+
void onChange(EndPointCB onChangeCB) {
44+
_onChangeCB = onChangeCB;
45+
}
46+
void onChangeOnOff(EndPointCB onChangeCB) {
47+
_onChangeOnOffCB = onChangeCB;
48+
}
49+
50+
protected:
51+
bool started = false;
52+
bool onOffState = false; // default initial state is off, but it can be changed by begin(bool)
53+
EndPointCB _onChangeCB = NULL;
54+
EndPointCB _onChangeOnOffCB = NULL;
55+
};
56+
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */

0 commit comments

Comments
 (0)