1
1
// Matter Manager
2
2
#include < Matter.h>
3
3
#include < WiFi.h>
4
+ #include < Preferences.h>
4
5
5
6
// List of Matter Endpoints for this Node
6
7
// On/Off Light Endpoint
7
8
#include < MatterOnOffLight.h>
8
9
MatterOnOffLight OnOffLight;
9
10
11
+ // it will keep last OnOff state stored, using Preferences
12
+ Preferences lastStatePref;
13
+
10
14
// set your board LED pin here
11
15
#ifdef LED_BUILTIN
12
16
const uint8_t ledPin = LED_BUILTIN;
@@ -26,19 +30,23 @@ bool setLightOnOff(bool state) {
26
30
} else {
27
31
digitalWrite (ledPin, LOW);
28
32
}
33
+ // store last OnOff state for when the Light is restarted / power goes off
34
+ lastStatePref.putBool (" lastOnOffState" , state);
29
35
// This callback must return the success state to Matter core
30
36
return true ;
31
37
}
32
38
33
39
// WiFi is manually set and started
34
40
35
- const char *ssid = " your-ssid " ; // Change this to your WiFi SSID
36
- const char *password = " your-password " ; // Change this to your WiFi password
41
+ const char *ssid = " Apartment B15 " ; // Change this to your WiFi SSID
42
+ const char *password = " flat-pony-body " ; // Change this to your WiFi password
37
43
38
44
void setup () {
39
45
// Initializa the USER BUTTON (Boot button) GPIO that will act as a toggle switch
40
46
pinMode (buttonPin, INPUT_PULLUP);
41
-
47
+ // Initialize the LED (light) GPIO and Matter End Point
48
+ pinMode (ledPin, OUTPUT);
49
+
42
50
Serial.begin (115200 );
43
51
while (!Serial) {
44
52
delay (100 );
@@ -62,21 +70,28 @@ void setup() {
62
70
delay (500 );
63
71
64
72
// Initialize Matter EndPoint
65
- OnOffLight.begin (true );
73
+ lastStatePref.begin (" matterLight" , false );
74
+ bool lastOnOffState = lastStatePref.getBool (" lastOnOffState" , true );
75
+ OnOffLight.begin (lastOnOffState);
66
76
OnOffLight.onChangeOnOff (setLightOnOff);
67
77
68
78
// Matter begining - Last step, after all EndPoints are initialized
69
79
Matter.begin ();
80
+ // This may be a restart of a already commissioned Matter accessory
81
+ if (Matter.isDeviceCommissioned ()) {
82
+ Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
83
+ Serial.printf (" Initial state: %s\r\n " , OnOffLight.getOnOff () ? " ON" : " OFF" );
84
+ setLightOnOff (OnOffLight.getOnOff ()); // configure the Light based on initial state
85
+ }
70
86
}
71
-
72
87
// Button control
73
88
uint32_t button_time_stamp = 0 ; // debouncing control
74
89
bool button_state = false ; // false = released | true = pressed
75
90
const uint32_t debouceTime = 250 ; // button debouncing time (ms)
76
91
const uint32_t decommissioningTimeout = 10000 ; // keep the button pressed for 10s to decommission the light
77
92
78
93
void loop () {
79
- // Check Matter Light Commissioning state
94
+ // Check Matter Light Commissioning state, which may change during execution of loop()
80
95
if (!Matter.isDeviceCommissioned ()) {
81
96
Serial.println (" " );
82
97
Serial.println (" Matter Node is not commissioned yet." );
@@ -92,8 +107,6 @@ void loop() {
92
107
Serial.println (" Matter Node not commissioned yet. Waiting for commissioning." );
93
108
}
94
109
}
95
- // Initialize the LED (light) GPIO and Matter End Point
96
- pinMode (ledPin, OUTPUT);
97
110
Serial.printf (" Initial state: %s\r\n " , OnOffLight.getOnOff () ? " ON" : " OFF" );
98
111
setLightOnOff (OnOffLight.getOnOff ()); // configure the Light based on initial state
99
112
Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
@@ -122,4 +135,4 @@ void loop() {
122
135
Matter.decommission ();
123
136
}
124
137
}
125
- }
138
+ }
0 commit comments