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
+ const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
31
+
32
+ // Button control
33
+ uint32_t button_time_stamp = 0 ; // debouncing control
34
+ bool button_state = false ; // false = released | true = pressed
35
+ const uint32_t decommissioningTimeout = 5000 ; // keep the button pressed for 5s, or longer, to decommission
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,24 +71,27 @@ 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
80
92
void loop () {
93
+ static uint32_t timeCounter = 0 ;
94
+
81
95
// Check Matter Light Commissioning state
82
96
if (!Matter.isDeviceCommissioned ()) {
83
97
Serial.println (" " );
@@ -97,10 +111,32 @@ void loop() {
97
111
Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
98
112
}
99
113
100
- // displays the Light state every 3 seconds
101
- 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 );
114
+ // displays the Light state every 5 seconds
115
+ if (!(timeCounter++ % 10 )) { // delaying for 500ms x 10 = 5s
116
+ Serial.println (" ======================" );
117
+ Serial.printf (" Matter Light #1 is %s\r\n " , Light1.getOnOff () ? " ON" : " OFF" );
118
+ Serial.printf (" Matter Light #2 is %s\r\n " , Light2.getOnOff () ? " ON" : " OFF" );
119
+ Serial.printf (" Matter Light #3 is %s\r\n " , Light3.getOnOff () ? " ON" : " OFF" );
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
+ if (digitalRead (buttonPin) == HIGH && button_state) {
130
+ button_state = false ; // released
131
+ }
132
+
133
+ // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
134
+ uint32_t time_diff = millis () - button_time_stamp;
135
+ if (button_state && time_diff > decommissioningTimeout) {
136
+ Serial.println (" Decommissioning the Light Matter Accessory. It shall be commissioned again." );
137
+ Matter.decommission ();
138
+ button_time_stamp = millis (); // avoid running decommissining again, reboot takes a second or so
139
+ }
140
+
141
+ delay (500 );
106
142
}
0 commit comments