18
18
19
19
// List of Matter Endpoints for this Node
20
20
// 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 ;
24
24
25
25
// WiFi is manually set and started
26
26
const char *ssid = " your-ssid" ; // Change this to your WiFi SSID
27
27
const char *password = " your-password" ; // Change this to your WiFi password
28
28
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
+
29
37
// Matter Protocol Endpoint Callback for each Light Accessory
30
38
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" );
32
40
return true ;
33
41
}
34
42
35
43
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" );
37
45
return true ;
38
46
}
39
47
40
48
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" );
42
50
return true ;
43
51
}
44
52
45
53
void setup () {
54
+ // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
55
+ pinMode (buttonPin, INPUT_PULLUP);
56
+
46
57
Serial.begin (115200 );
47
58
while (!Serial) {
48
59
delay (100 );
@@ -60,23 +71,29 @@ void setup() {
60
71
delay (500 );
61
72
Serial.print (" ." );
62
73
}
63
- Serial.println (" \r\n WiFi connected" );
74
+ Serial.println ();
75
+ Serial.println (" WiFi connected" );
64
76
Serial.println (" IP address: " );
65
77
Serial.println (WiFi.localIP ());
66
78
delay (500 );
67
79
68
80
// 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);
75
87
76
88
// Matter beginning - Last step, after all EndPoints are initialized
77
89
Matter.begin ();
78
90
}
79
91
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
+
80
97
void loop () {
81
98
// Check Matter Light Commissioning state
82
99
if (!Matter.isDeviceCommissioned ()) {
@@ -97,10 +114,29 @@ void loop() {
97
114
Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
98
115
}
99
116
100
- // displays the Light state every 3 seconds
117
+ // displays the Light state every 5 seconds
101
118
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 );
106
142
}
0 commit comments