Skip to content

feat(matter_examples): apply boot button change to all examples #10702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
#endif

// set your board USER BUTTON pin here
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
#ifdef BOOT_PIN
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
#else
const uint8_t buttonPin = 0; // Set your button pin here.
#warning "Do not forget to set the USER BUTTON pin"
#endif

// WiFi is manually set and started
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,42 @@

// List of Matter Endpoints for this Node
// There will be 3 On/Off Light Endpoints in the same Node
MatterOnOffLight OnOffLight1;
MatterOnOffLight OnOffLight2;
MatterOnOffLight OnOffLight3;
MatterOnOffLight Light1;
MatterDimmableLight Light2;
MatterColorLight Light3;

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

// set your board USER BUTTON pin here - USED to decommission the Matter Node
#ifdef BOOT_PIN
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
#else
const uint8_t buttonPin = 0; // Set your button pin here.
#warning "Do not forget to set the USER BUTTON pin"
#endif

// Matter Protocol Endpoint Callback for each Light Accessory
bool setLightOnOff1(bool state) {
Serial.printf("CB-Light1 changed state to: %s\r\n", state ? "ON" : "OFF");
Serial.printf("Light1 changed state to: %s\r\n", state ? "ON" : "OFF");
return true;
}

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

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

void setup() {
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
pinMode(buttonPin, INPUT_PULLUP);

Serial.begin(115200);
while (!Serial) {
delay(100);
Expand All @@ -60,23 +71,29 @@ void setup() {
delay(500);
Serial.print(".");
}
Serial.println("\r\nWiFi connected");
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);

// Initialize all 3 Matter EndPoints
OnOffLight1.begin();
OnOffLight2.begin();
OnOffLight3.begin();
OnOffLight1.onChange(setLightOnOff1);
OnOffLight2.onChange(setLightOnOff2);
OnOffLight3.onChange(setLightOnOff3);
Light1.begin();
Light2.begin();
Light3.begin();
Light1.onChangeOnOff(setLightOnOff1);
Light2.onChangeOnOff(setLightOnOff2);
Light3.onChangeOnOff(setLightOnOff3);

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

// Button control
uint32_t button_time_stamp = 0; // debouncing control
bool button_state = false; // false = released | true = pressed
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light

void loop() {
// Check Matter Light Commissioning state
if (!Matter.isDeviceCommissioned()) {
Expand All @@ -97,10 +114,29 @@ void loop() {
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
}

//displays the Light state every 3 seconds
//displays the Light state every 5 seconds
Serial.println("======================");
Serial.printf("Matter Light #1 is %s\r\n", OnOffLight1.getOnOff() ? "ON" : "OFF");
Serial.printf("Matter Light #2 is %s\r\n", OnOffLight2.getOnOff() ? "ON" : "OFF");
Serial.printf("Matter Light #3 is %s\r\n", OnOffLight3.getOnOff() ? "ON" : "OFF");
delay(3000);
Serial.printf("Matter Light #1 is %s\r\n", Light1.getOnOff() ? "ON" : "OFF");
Serial.printf("Matter Light #2 is %s\r\n", Light2.getOnOff() ? "ON" : "OFF");
Serial.printf("Matter Light #3 is %s\r\n", Light3.getOnOff() ? "ON" : "OFF");

// Check if the button has been pressed
if (digitalRead(buttonPin) == LOW && !button_state) {
// deals with button debouncing
button_time_stamp = millis(); // record the time while the button is pressed.
button_state = true; // pressed.
}

// Onboard User Button is used to decommission matter fabric
uint32_t time_diff = millis() - button_time_stamp;
if (button_state && time_diff > decommissioningTimeout && digitalRead(buttonPin) == HIGH) {
button_state = false; // released
// Factory reset is triggered if the button is pressed longer than 10 seconds
if (time_diff > decommissioningTimeout) {
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
Matter.decommission();
}
}

delay(5000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
#endif

// set your board USER BUTTON pin here
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
#ifdef BOOT_PIN
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
#else
const uint8_t buttonPin = 0; // Set your button pin here.
#warning "Do not forget to set the USER BUTTON pin"
#endif

// WiFi is manually set and started
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
#endif

// set your board USER BUTTON pin here
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
#ifdef BOOT_PIN
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
#else
const uint8_t buttonPin = 0; // Set your button pin here.
#warning "Do not forget to set the USER BUTTON pin"
#endif

// WiFi is manually set and started
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
Expand Down
12 changes: 9 additions & 3 deletions libraries/Matter/examples/MatterFan/MatterFan.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
// Fan Endpoint - On/Off control + Speed Percent Control + Fan Modes
MatterFan Fan;

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


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

void setup() {
// Initialize the USER BUTTON (Boot button) GPIO that will toggle the Fan (On/Off)
// Initialize the USER BUTTON (Boot button) GPIO that will toggle the Fan (On/Off) and decommission the Matter Node
pinMode(buttonPin, INPUT_PULLUP);
// Initialize the Analog Pin A0 used to read input voltage and to set the Fan speed accordingly
pinMode(analogPin, INPUT);
Expand Down
35 changes: 34 additions & 1 deletion libraries/Matter/examples/MatterMinimum/MatterMinimum.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ const uint8_t ledPin = LED_BUILTIN;
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
#endif

// set your board USER BUTTON pin here
#ifdef BOOT_PIN
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
#else
const uint8_t buttonPin = 0; // Set your button pin here.
#warning "Do not forget to set the USER BUTTON pin"
#endif

// Matter Protocol Endpoint (On/OFF Light) Callback
bool matterCB(bool state) {
digitalWrite(ledPin, state ? HIGH : LOW);
Expand All @@ -47,6 +55,8 @@ const char *ssid = "your-ssid"; // Change this to your WiFi SSID
const char *password = "your-password"; // Change this to your WiFi password

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

Expand Down Expand Up @@ -76,6 +86,29 @@ void setup() {
}
}

// Button control - decommision the Matter Node
uint32_t button_time_stamp = 0; // debouncing control
bool button_state = false; // false = released | true = pressed
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission

void loop() {
delay(500);
// Check if the button has been pressed
if (digitalRead(buttonPin) == LOW && !button_state) {
// deals with button debouncing
button_time_stamp = millis(); // record the time while the button is pressed.
button_state = true; // pressed.
}

// Onboard User Button is used to decommission matter node
uint32_t time_diff = millis() - button_time_stamp;
if (button_state && time_diff > decommissioningTimeout && digitalRead(buttonPin) == HIGH) {
button_state = false; // released
// Factory reset is triggered if the button is pressed longer than 10 seconds
if (time_diff > decommissioningTimeout) {
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
Matter.decommission();
}
}

delay(5000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
#endif

// set your board USER BUTTON pin here
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
#ifdef BOOT_PIN
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
#else
const uint8_t buttonPin = 0; // Set your button pin here.
#warning "Do not forget to set the USER BUTTON pin"
#endif

// WiFi is manually set and started
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
MatterGenericSwitch SmartButton;

// set your board USER BUTTON pin here
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
#ifdef BOOT_PIN
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
#else
const uint8_t buttonPin = 0; // Set your button pin here.
#warning "Do not forget to set the USER BUTTON pin"
#endif

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

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

Serial.begin(115200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
#endif

// set your board USER BUTTON pin here
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
#ifdef BOOT_PIN
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
#else
const uint8_t buttonPin = 0; // Set your button pin here.
#warning "Do not forget to set the USER BUTTON pin"
#endif

// WiFi is manually set and started
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
// Matter Temperature Sensor Endpoint
MatterTemperatureSensor SimulatedTemperatureSensor;

// set your board USER BUTTON pin here - decommissioning button
#ifdef BOOT_PIN
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
#else
const uint8_t buttonPin = 0; // Set your button pin here.
#warning "Do not forget to set the USER BUTTON pin"
#endif

// WiFi is manually set and started
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
const char *password = "your-password"; // Change this to your WiFi password
Expand All @@ -47,6 +55,9 @@ float getSimulatedTemperature() {
}

void setup() {
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
pinMode(buttonPin, INPUT_PULLUP);

Serial.begin(115200);

// Manually connect to WiFi
Expand Down Expand Up @@ -85,10 +96,34 @@ void setup() {
}
}

// Button control - decommision the Matter Node
uint32_t button_time_stamp = 0; // debouncing control
bool button_state = false; // false = released | true = pressed
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission

void loop() {
Serial.printf("Current Temperature is %.02f <Temperature Units>\r\n", SimulatedTemperatureSensor.getTemperature());

// Check if the button has been pressed
if (digitalRead(buttonPin) == LOW && !button_state) {
// deals with button debouncing
button_time_stamp = millis(); // record the time while the button is pressed.
button_state = true; // pressed.
}

// Onboard User Button is used to decommission matter node
uint32_t time_diff = millis() - button_time_stamp;
if (button_state && time_diff > decommissioningTimeout && digitalRead(buttonPin) == HIGH) {
button_state = false; // released
// Factory reset is triggered if the button is pressed longer than 10 seconds
if (time_diff > decommissioningTimeout) {
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
Matter.decommission();
}
}

// update the temperature sensor value every 5 seconds
// Matter APP shall display the updated temperature
delay(5000);
SimulatedTemperatureSensor.setTemperature(getSimulatedTemperature());
// Matter APP shall display the updated temperature
SimulatedTemperatureSensor.setTemperature(getSimulatedTemperature());
}
Loading
Loading