Skip to content

Commit e9f1b92

Browse files
committed
feat(matter_examples): apply boot button change to all examples
1 parent 538efe3 commit e9f1b92

File tree

13 files changed

+206
-32
lines changed

13 files changed

+206
-32
lines changed

Diff for: libraries/Matter/examples/Matter_ColorLight/Matter_ColorLight.ino renamed to libraries/Matter/examples/MatterColorLight/MatterColorLight.ino

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
3535
#endif
3636

3737
// set your board USER BUTTON pin here
38-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
38+
#ifdef BOOT_PIN
39+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
40+
#else
41+
const uint8_t buttonPin = 0; // Set your button pin here.
42+
#warning "Do not forget to set the USER BUTTON pin"
43+
#endif
3944

4045
// WiFi is manually set and started
4146
const char *ssid = "your-ssid"; // Change this to your WiFi SSID

Diff for: libraries/Matter/examples/MatterComposedLights/MatterComposedLights.ino

+54-18
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,42 @@
1818

1919
// List of Matter Endpoints for this Node
2020
// There will be 3 On/Off Light Endpoints in the same Node
21-
MatterOnOffLight OnOffLight1;
22-
MatterOnOffLight OnOffLight2;
23-
MatterOnOffLight OnOffLight3;
21+
MatterOnOffLight Light1;
22+
MatterDimmableLight Light2;
23+
MatterColorLight Light3;
2424

2525
// WiFi is manually set and started
2626
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
2727
const char *password = "your-password"; // Change this to your WiFi password
2828

29+
// set your board USER BUTTON pin here - USED to decommission the Matter Node
30+
#ifdef BOOT_PIN
31+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
32+
#else
33+
const uint8_t buttonPin = 0; // Set your button pin here.
34+
#warning "Do not forget to set the USER BUTTON pin"
35+
#endif
36+
2937
// Matter Protocol Endpoint Callback for each Light Accessory
3038
bool setLightOnOff1(bool state) {
31-
Serial.printf("CB-Light1 changed state to: %s\r\n", state ? "ON" : "OFF");
39+
Serial.printf("Light1 changed state to: %s\r\n", state ? "ON" : "OFF");
3240
return true;
3341
}
3442

3543
bool setLightOnOff2(bool state) {
36-
Serial.printf("CB-Light2 changed state to: %s\r\n", state ? "ON" : "OFF");
44+
Serial.printf("Light2 changed state to: %s\r\n", state ? "ON" : "OFF");
3745
return true;
3846
}
3947

4048
bool setLightOnOff3(bool state) {
41-
Serial.printf("CB-Light3 changed state to: %s\r\n", state ? "ON" : "OFF");
49+
Serial.printf("Light3 changed state to: %s\r\n", state ? "ON" : "OFF");
4250
return true;
4351
}
4452

4553
void setup() {
54+
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
55+
pinMode(buttonPin, INPUT_PULLUP);
56+
4657
Serial.begin(115200);
4758
while (!Serial) {
4859
delay(100);
@@ -60,23 +71,29 @@ void setup() {
6071
delay(500);
6172
Serial.print(".");
6273
}
63-
Serial.println("\r\nWiFi connected");
74+
Serial.println();
75+
Serial.println("WiFi connected");
6476
Serial.println("IP address: ");
6577
Serial.println(WiFi.localIP());
6678
delay(500);
6779

6880
// Initialize all 3 Matter EndPoints
69-
OnOffLight1.begin();
70-
OnOffLight2.begin();
71-
OnOffLight3.begin();
72-
OnOffLight1.onChange(setLightOnOff1);
73-
OnOffLight2.onChange(setLightOnOff2);
74-
OnOffLight3.onChange(setLightOnOff3);
81+
Light1.begin();
82+
Light2.begin();
83+
Light3.begin();
84+
Light1.onChangeOnOff(setLightOnOff1);
85+
Light2.onChangeOnOff(setLightOnOff2);
86+
Light3.onChangeOnOff(setLightOnOff3);
7587

7688
// Matter beginning - Last step, after all EndPoints are initialized
7789
Matter.begin();
7890
}
7991

92+
// Button control
93+
uint32_t button_time_stamp = 0; // debouncing control
94+
bool button_state = false; // false = released | true = pressed
95+
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
96+
8097
void loop() {
8198
// Check Matter Light Commissioning state
8299
if (!Matter.isDeviceCommissioned()) {
@@ -97,10 +114,29 @@ void loop() {
97114
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
98115
}
99116

100-
//displays the Light state every 3 seconds
117+
//displays the Light state every 5 seconds
101118
Serial.println("======================");
102-
Serial.printf("Matter Light #1 is %s\r\n", OnOffLight1.getOnOff() ? "ON" : "OFF");
103-
Serial.printf("Matter Light #2 is %s\r\n", OnOffLight2.getOnOff() ? "ON" : "OFF");
104-
Serial.printf("Matter Light #3 is %s\r\n", OnOffLight3.getOnOff() ? "ON" : "OFF");
105-
delay(3000);
119+
Serial.printf("Matter Light #1 is %s\r\n", Light1.getOnOff() ? "ON" : "OFF");
120+
Serial.printf("Matter Light #2 is %s\r\n", Light2.getOnOff() ? "ON" : "OFF");
121+
Serial.printf("Matter Light #3 is %s\r\n", Light3.getOnOff() ? "ON" : "OFF");
122+
123+
// Check if the button has been pressed
124+
if (digitalRead(buttonPin) == LOW && !button_state) {
125+
// deals with button debouncing
126+
button_time_stamp = millis(); // record the time while the button is pressed.
127+
button_state = true; // pressed.
128+
}
129+
130+
// Onboard User Button is used to decommission matter fabric
131+
uint32_t time_diff = millis() - button_time_stamp;
132+
if (button_state && time_diff > decommissioningTimeout && digitalRead(buttonPin) == HIGH) {
133+
button_state = false; // released
134+
// Factory reset is triggered if the button is pressed longer than 10 seconds
135+
if (time_diff > decommissioningTimeout) {
136+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
137+
Matter.decommission();
138+
}
139+
}
140+
141+
delay(5000);
106142
}

Diff for: libraries/Matter/examples/MatterDimmableLight/MatterDimmableLight.ino

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
3535
#endif
3636

3737
// set your board USER BUTTON pin here
38-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
38+
#ifdef BOOT_PIN
39+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
40+
#else
41+
const uint8_t buttonPin = 0; // Set your button pin here.
42+
#warning "Do not forget to set the USER BUTTON pin"
43+
#endif
3944

4045
// WiFi is manually set and started
4146
const char *ssid = "your-ssid"; // Change this to your WiFi SSID

Diff for: libraries/Matter/examples/MatterEnhancedColorLight/MatterEnhancedColorLight.ino

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
3838
#endif
3939

4040
// set your board USER BUTTON pin here
41-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
41+
#ifdef BOOT_PIN
42+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
43+
#else
44+
const uint8_t buttonPin = 0; // Set your button pin here.
45+
#warning "Do not forget to set the USER BUTTON pin"
46+
#endif
4247

4348
// WiFi is manually set and started
4449
const char *ssid = "your-ssid"; // Change this to your WiFi SSID

Diff for: libraries/Matter/examples/MatterFan/MatterFan.ino

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020
// Fan Endpoint - On/Off control + Speed Percent Control + Fan Modes
2121
MatterFan Fan;
2222

23-
// set your board USER BUTTON pin here - used for toggling On/Off
24-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
23+
// set your board USER BUTTON pin here - used for toggling On/Off and decommission the Matter Node
24+
#ifdef BOOT_PIN
25+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
26+
#else
27+
const uint8_t buttonPin = 0; // Set your button pin here.
28+
#warning "Do not forget to set the USER BUTTON pin"
29+
#endif
30+
2531

2632
// set your board Analog Pin here - used for changing the Fan speed
2733
const uint8_t analogPin = A0; // Analog Pin depends on each board
@@ -56,7 +62,7 @@ void fanDCMotorDrive(bool fanState, uint8_t speedPercent) {
5662
}
5763

5864
void setup() {
59-
// Initialize the USER BUTTON (Boot button) GPIO that will toggle the Fan (On/Off)
65+
// Initialize the USER BUTTON (Boot button) GPIO that will toggle the Fan (On/Off) and decommission the Matter Node
6066
pinMode(buttonPin, INPUT_PULLUP);
6167
// Initialize the Analog Pin A0 used to read input voltage and to set the Fan speed accordingly
6268
pinMode(analogPin, INPUT);

Diff for: libraries/Matter/examples/MatterMinimum/MatterMinimum.ino

+34-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ const uint8_t ledPin = LED_BUILTIN;
3535
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
3636
#endif
3737

38+
// set your board USER BUTTON pin here
39+
#ifdef BOOT_PIN
40+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
41+
#else
42+
const uint8_t buttonPin = 0; // Set your button pin here.
43+
#warning "Do not forget to set the USER BUTTON pin"
44+
#endif
45+
3846
// Matter Protocol Endpoint (On/OFF Light) Callback
3947
bool matterCB(bool state) {
4048
digitalWrite(ledPin, state ? HIGH : LOW);
@@ -47,6 +55,8 @@ const char *ssid = "your-ssid"; // Change this to your WiFi SSID
4755
const char *password = "your-password"; // Change this to your WiFi password
4856

4957
void setup() {
58+
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
59+
pinMode(buttonPin, INPUT_PULLUP);
5060
// Initialize the LED GPIO
5161
pinMode(ledPin, OUTPUT);
5262

@@ -76,6 +86,29 @@ void setup() {
7686
}
7787
}
7888

89+
// Button control - decommision the Matter Node
90+
uint32_t button_time_stamp = 0; // debouncing control
91+
bool button_state = false; // false = released | true = pressed
92+
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission
93+
7994
void loop() {
80-
delay(500);
95+
// Check if the button has been pressed
96+
if (digitalRead(buttonPin) == LOW && !button_state) {
97+
// deals with button debouncing
98+
button_time_stamp = millis(); // record the time while the button is pressed.
99+
button_state = true; // pressed.
100+
}
101+
102+
// Onboard User Button is used to decommission matter node
103+
uint32_t time_diff = millis() - button_time_stamp;
104+
if (button_state && time_diff > decommissioningTimeout && digitalRead(buttonPin) == HIGH) {
105+
button_state = false; // released
106+
// Factory reset is triggered if the button is pressed longer than 10 seconds
107+
if (time_diff > decommissioningTimeout) {
108+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
109+
Matter.decommission();
110+
}
111+
}
112+
113+
delay(5000);
81114
}

Diff for: libraries/Matter/examples/MatterOnOffLight/MatterOnOffLight.ino

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
3434
#endif
3535

3636
// set your board USER BUTTON pin here
37-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
37+
#ifdef BOOT_PIN
38+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
39+
#else
40+
const uint8_t buttonPin = 0; // Set your button pin here.
41+
#warning "Do not forget to set the USER BUTTON pin"
42+
#endif
3843

3944
// WiFi is manually set and started
4045
const char *ssid = "your-ssid"; // Change this to your WiFi SSID

Diff for: libraries/Matter/examples/MatterSmartButon/MatterSmartButon.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
MatterGenericSwitch SmartButton;
2222

2323
// set your board USER BUTTON pin here
24-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
24+
#ifdef BOOT_PIN
25+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
26+
#else
27+
const uint8_t buttonPin = 0; // Set your button pin here.
28+
#warning "Do not forget to set the USER BUTTON pin"
29+
#endif
2530

2631
// WiFi is manually set and started
2732
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
2833
const char *password = "your-password"; // Change this to your WiFi password
2934

3035
void setup() {
31-
// Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch
36+
// Initialize the USER BUTTON (Boot button) GPIO that will act as a smart button or to decommission the Matter Node
3237
pinMode(buttonPin, INPUT_PULLUP);
3338

3439
Serial.begin(115200);

Diff for: libraries/Matter/examples/Matter_CW_WW_Light/Matter_CW_WW_Light.ino renamed to libraries/Matter/examples/MatterTemperatureLight/MatterTemperatureLight.ino

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
3636
#endif
3737

3838
// set your board USER BUTTON pin here
39-
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
39+
#ifdef BOOT_PIN
40+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
41+
#else
42+
const uint8_t buttonPin = 0; // Set your button pin here.
43+
#warning "Do not forget to set the USER BUTTON pin"
44+
#endif
4045

4146
// WiFi is manually set and started
4247
const char *ssid = "your-ssid"; // Change this to your WiFi SSID

Diff for: libraries/Matter/examples/MatterTemperatureSensor/MatterTemperatureSensor.ino

+37-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
// Matter Temperature Sensor Endpoint
2828
MatterTemperatureSensor SimulatedTemperatureSensor;
2929

30+
// set your board USER BUTTON pin here - decommissioning button
31+
#ifdef BOOT_PIN
32+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
33+
#else
34+
const uint8_t buttonPin = 0; // Set your button pin here.
35+
#warning "Do not forget to set the USER BUTTON pin"
36+
#endif
37+
3038
// WiFi is manually set and started
3139
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
3240
const char *password = "your-password"; // Change this to your WiFi password
@@ -47,6 +55,9 @@ float getSimulatedTemperature() {
4755
}
4856

4957
void setup() {
58+
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
59+
pinMode(buttonPin, INPUT_PULLUP);
60+
5061
Serial.begin(115200);
5162

5263
// Manually connect to WiFi
@@ -85,10 +96,34 @@ void setup() {
8596
}
8697
}
8798

99+
// Button control - decommision the Matter Node
100+
uint32_t button_time_stamp = 0; // debouncing control
101+
bool button_state = false; // false = released | true = pressed
102+
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission
103+
88104
void loop() {
89105
Serial.printf("Current Temperature is %.02f <Temperature Units>\r\n", SimulatedTemperatureSensor.getTemperature());
106+
107+
// Check if the button has been pressed
108+
if (digitalRead(buttonPin) == LOW && !button_state) {
109+
// deals with button debouncing
110+
button_time_stamp = millis(); // record the time while the button is pressed.
111+
button_state = true; // pressed.
112+
}
113+
114+
// Onboard User Button is used to decommission matter node
115+
uint32_t time_diff = millis() - button_time_stamp;
116+
if (button_state && time_diff > decommissioningTimeout && digitalRead(buttonPin) == HIGH) {
117+
button_state = false; // released
118+
// Factory reset is triggered if the button is pressed longer than 10 seconds
119+
if (time_diff > decommissioningTimeout) {
120+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
121+
Matter.decommission();
122+
}
123+
}
124+
90125
// update the temperature sensor value every 5 seconds
91-
// Matter APP shall display the updated temperature
92126
delay(5000);
93-
SimulatedTemperatureSensor.setTemperature(getSimulatedTemperature());
127+
// Matter APP shall display the updated temperature
128+
SimulatedTemperatureSensor.setTemperature(getSimulatedTemperature());
94129
}

0 commit comments

Comments
 (0)