Skip to content

Commit 9ecb6f3

Browse files
committed
feat(matter): improve example using preferences
1 parent 966ae0d commit 9ecb6f3

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

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

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// Matter Manager
22
#include <Matter.h>
33
#include <WiFi.h>
4+
#include <Preferences.h>
45

56
// List of Matter Endpoints for this Node
67
// On/Off Light Endpoint
78
#include <MatterOnOffLight.h>
89
MatterOnOffLight OnOffLight;
910

11+
// it will keep last OnOff state stored, using Preferences
12+
Preferences lastStatePref;
13+
1014
// set your board LED pin here
1115
#ifdef LED_BUILTIN
1216
const uint8_t ledPin = LED_BUILTIN;
@@ -26,19 +30,23 @@ bool setLightOnOff(bool state) {
2630
} else {
2731
digitalWrite(ledPin, LOW);
2832
}
33+
// store last OnOff state for when the Light is restarted / power goes off
34+
lastStatePref.putBool("lastOnOffState", state);
2935
// This callback must return the success state to Matter core
3036
return true;
3137
}
3238

3339
// WiFi is manually set and started
3440

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
3743

3844
void setup() {
3945
// Initializa the USER BUTTON (Boot button) GPIO that will act as a toggle switch
4046
pinMode(buttonPin, INPUT_PULLUP);
41-
47+
// Initialize the LED (light) GPIO and Matter End Point
48+
pinMode(ledPin, OUTPUT);
49+
4250
Serial.begin(115200);
4351
while (!Serial) {
4452
delay(100);
@@ -62,21 +70,28 @@ void setup() {
6270
delay(500);
6371

6472
// Initialize Matter EndPoint
65-
OnOffLight.begin(true);
73+
lastStatePref.begin("matterLight", false);
74+
bool lastOnOffState = lastStatePref.getBool("lastOnOffState", true);
75+
OnOffLight.begin(lastOnOffState);
6676
OnOffLight.onChangeOnOff(setLightOnOff);
6777

6878
// Matter begining - Last step, after all EndPoints are initialized
6979
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+
}
7086
}
71-
7287
// Button control
7388
uint32_t button_time_stamp = 0; // debouncing control
7489
bool button_state = false; // false = released | true = pressed
7590
const uint32_t debouceTime = 250; // button debouncing time (ms)
7691
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light
7792

7893
void loop() {
79-
// Check Matter Light Commissioning state
94+
// Check Matter Light Commissioning state, which may change during execution of loop()
8095
if (!Matter.isDeviceCommissioned()) {
8196
Serial.println("");
8297
Serial.println("Matter Node is not commissioned yet.");
@@ -92,8 +107,6 @@ void loop() {
92107
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
93108
}
94109
}
95-
// Initialize the LED (light) GPIO and Matter End Point
96-
pinMode(ledPin, OUTPUT);
97110
Serial.printf("Initial state: %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
98111
setLightOnOff(OnOffLight.getOnOff()); // configure the Light based on initial state
99112
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
@@ -122,4 +135,4 @@ void loop() {
122135
Matter.decommission();
123136
}
124137
}
125-
}
138+
}

0 commit comments

Comments
 (0)