From 178af05079730648a1bfd98e6bb89eced68e1efb Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 10 Dec 2024 19:51:52 -0300 Subject: [PATCH 1/3] feat(matter): adds a new matter endpoint for pressure sensor --- CMakeLists.txt | 1 + .../MatterPressureSensor.ino | 131 ++++++++++++++++++ .../examples/MatterPressureSensor/ci.json | 7 + libraries/Matter/keywords.txt | 3 + libraries/Matter/src/Matter.h | 2 + .../MatterEndpoints/MatterPressureSensor.cpp | 99 +++++++++++++ .../MatterEndpoints/MatterPressureSensor.h | 62 +++++++++ 7 files changed, 305 insertions(+) create mode 100644 libraries/Matter/examples/MatterPressureSensor/MatterPressureSensor.ino create mode 100644 libraries/Matter/examples/MatterPressureSensor/ci.json create mode 100644 libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp create mode 100644 libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ed9ae23f83f..ec465a4182b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,6 +177,7 @@ set(ARDUINO_LIBRARY_Matter_SRCS libraries/Matter/src/MatterEndpoints/MatterFan.cpp libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.cpp + libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp libraries/Matter/src/Matter.cpp) set(ARDUINO_LIBRARY_PPP_SRCS diff --git a/libraries/Matter/examples/MatterPressureSensor/MatterPressureSensor.ino b/libraries/Matter/examples/MatterPressureSensor/MatterPressureSensor.ino new file mode 100644 index 00000000000..b1708f85d85 --- /dev/null +++ b/libraries/Matter/examples/MatterPressureSensor/MatterPressureSensor.ino @@ -0,0 +1,131 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * This example is an example code that will create a Matter Device which can be + * commissioned and controlled from a Matter Environment APP. + * Additionally the ESP32 will send debug messages indicating the Matter activity. + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. + */ + +// Matter Manager +#include +#include + +// List of Matter Endpoints for this Node +// Matter Pressure Sensor Endpoint +MatterPressureSensor SimulatedPressureSensor; + +// set your board USER BUTTON pin here - decommissioning button +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. + +// 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 + +// 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 = 5000; // keep the button pressed for 5s, or longer, to decommission + +// Simulate a pressure sensor - add your preferred pressure sensor library code here +float getSimulatedPressure() { + // The Endpoint implementation keeps an uint16_t as internal value information, + // which stores data in hPa (pressure measurement unit) + static float simulatedPressureHWSensor = 950; + + // it will increase from 950 to 1100 hPa in steps of 10 hPa to simulate a pressure sensor + simulatedPressureHWSensor = simulatedPressureHWSensor + 10; + if (simulatedPressureHWSensor > 1100) { + simulatedPressureHWSensor = 950; + } + + return simulatedPressureHWSensor; +} + +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 + WiFi.begin(ssid, password); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + + // set initial pressure sensor measurement + // Simulated Sensor - it shall initially print 900hPa and then move to the 950 to 1100 hPa as pressure range + SimulatedPressureSensor.begin(900.00); + + // Matter beginning - Last step, after all EndPoints are initialized + Matter.begin(); + + // Check Matter Accessory Commissioning state, which may change during execution of loop() + if (!Matter.isDeviceCommissioned()) { + Serial.println(""); + Serial.println("Matter Node is not commissioned yet."); + Serial.println("Initiate the device discovery in your Matter environment."); + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + // waits for Matter Pressure Sensor Commissioning. + uint32_t timeCount = 0; + while (!Matter.isDeviceCommissioned()) { + delay(100); + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); + } + } + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); + } +} + +void loop() { + static uint32_t timeCounter = 0; + + // Print the current pressure value every 5s + if (!(timeCounter++ % 10)) { // delaying for 500ms x 10 = 5s + // Print the current pressure value + Serial.printf("Current Pressure is %.02fhPa\r\n", SimulatedPressureSensor.getPressure()); + // Update Pressure from the (Simulated) Hardware Sensor + // Matter APP shall display the updated pressure percent + SimulatedPressureSensor.setPressure(getSimulatedPressure()); + } + + // 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. + } + + if (digitalRead(buttonPin) == HIGH && button_state) { + button_state = false; // released + } + + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node + uint32_t time_diff = millis() - button_time_stamp; + if (button_state && time_diff > decommissioningTimeout) { + // Factory reset is triggered if the button is pressed longer than 10 seconds + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); + Matter.decommission(); + } + + delay(500); +} diff --git a/libraries/Matter/examples/MatterPressureSensor/ci.json b/libraries/Matter/examples/MatterPressureSensor/ci.json new file mode 100644 index 00000000000..556a8a9ee6b --- /dev/null +++ b/libraries/Matter/examples/MatterPressureSensor/ci.json @@ -0,0 +1,7 @@ +{ + "fqbn_append": "PartitionScheme=huge_app", + "requires": [ + "CONFIG_SOC_WIFI_SUPPORTED=y", + "CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y" + ] +} diff --git a/libraries/Matter/keywords.txt b/libraries/Matter/keywords.txt index c54b040d94b..287b8789a86 100644 --- a/libraries/Matter/keywords.txt +++ b/libraries/Matter/keywords.txt @@ -20,6 +20,7 @@ FanMode_t KEYWORD1 FanModeSequence_t KEYWORD1 MatterTemperatureSensor KEYWORD1 MatterHumiditySensor KEYWORD1 +MatterPressureSensor KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -68,6 +69,8 @@ setTemperature KEYWORD2 getTemperature KEYWORD2 setHumidity KEYWORD2 getHumidity KEYWORD2 +setPressure KEYWORD2 +getPressure KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/libraries/Matter/src/Matter.h b/libraries/Matter/src/Matter.h index 4b7804d61df..ebde7cdf589 100644 --- a/libraries/Matter/src/Matter.h +++ b/libraries/Matter/src/Matter.h @@ -28,6 +28,7 @@ #include #include #include +#include using namespace esp_matter; @@ -62,6 +63,7 @@ class ArduinoMatter { friend class MatterFan; friend class MatterTemperatureSensor; friend class MatterHumiditySensor; + friend class MatterPressureSensor; protected: static void _init(); diff --git a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp new file mode 100644 index 00000000000..6b54cf8af3a --- /dev/null +++ b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp @@ -0,0 +1,99 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL + +#include +#include + +using namespace esp_matter; +using namespace esp_matter::endpoint; +using namespace chip::app::Clusters; + +bool MatterPressureSensor::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) { + bool ret = true; + if (!started) { + log_e("Matter Pressure Sensor device has not begun."); + return false; + } + + log_d("Pressure Sensor Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32); + return ret; +} + +MatterPressureSensor::MatterPressureSensor() {} + +MatterPressureSensor::~MatterPressureSensor() { + end(); +} + +bool MatterPressureSensor::begin(int16_t _rawPressure) { + ArduinoMatter::_init(); + + pressure_sensor::config_t pressure_sensor_config; + pressure_sensor_config.pressure_measurement.pressure_measured_value = _rawPressure; + pressure_sensor_config.pressure_measurement.pressure_min_measured_value = nullptr; + pressure_sensor_config.pressure_measurement.pressure_max_measured_value = nullptr; + + // endpoint handles can be used to add/modify clusters + endpoint_t *endpoint = pressure_sensor::create(node::get(), &pressure_sensor_config, ENDPOINT_FLAG_NONE, (void *)this); + if (endpoint == nullptr) { + log_e("Failed to create Pressure Sensor endpoint"); + return false; + } + rawPressure = _rawPressure; + setEndPointId(endpoint::get_id(endpoint)); + log_i("Pressure Sensor created with endpoint_id %d", getEndPointId()); + started = true; + return true; +} + +void MatterPressureSensor::end() { + started = false; +} + +bool MatterPressureSensor::setRawPressure(int16_t _rawPressure) { + if (!started) { + log_e("Matter Pressure Sensor device has not begun."); + return false; + } + + // avoid processing the a "no-change" + if (rawPressure == _rawPressure) { + return true; + } + + esp_matter_attr_val_t pressureVal = esp_matter_invalid(NULL); + + if (!getAttributeVal(PressureMeasurement::Id, PressureMeasurement::Attributes::MeasuredValue::Id, &pressureVal)) { + log_e("Failed to get Pressure Sensor Attribute."); + return false; + } + if (pressureVal.val.i16 != _rawPressure) { + pressureVal.val.i16 = _rawPressure; + bool ret; + ret = updateAttributeVal(PressureMeasurement::Id, PressureMeasurement::Attributes::MeasuredValue::Id, &pressureVal); + if (!ret) { + log_e("Failed to update Fan Speed Percent Attribute."); + return false; + } + rawPressure = _rawPressure; + } + log_v("Pressure Sensor set to %.02f Degrees", (float)_rawPressure / 100.00); + + return true; +} + +#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h new file mode 100644 index 00000000000..9fdd90c6ebe --- /dev/null +++ b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h @@ -0,0 +1,62 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once +#include +#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL + +#include +#include + +class MatterPressureSensor : public MatterEndPoint { +public: + MatterPressureSensor(); + ~MatterPressureSensor(); + // begin Matter Pressure Sensor endpoint with initial float pressure + bool begin(double pressure = 0.00) { + return begin(static_cast(pressure)); + } + // this will stop processing Pressure Sensor Matter events + void end(); + + // set the reported raw pressure in hPa + bool setPressure(double pressure) { + int16_t rawValue = static_cast(pressure); + return setRawPressure(rawValue); + } + // returns the reported float pressure in hPa + double getPressure() { + return (double)rawPressure; + } + // double conversion operator + void operator=(double pressure) { + setPressure(pressure); + } + // double conversion operator + operator double() { + return (double)getPressure(); + } + + // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. + bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + +protected: + bool started = false; + // implementation keeps pressure in hPa + int16_t rawPressure = 0; + // internal function to set the raw pressure value (Matter Cluster) + bool setRawPressure(int16_t _rawPressure); + bool begin(int16_t _rawPressure); +}; +#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ From 1f9eb74337a453a9fba4d3b2120a14270a6d5725 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 11 Dec 2024 00:25:43 -0300 Subject: [PATCH 2/3] fix(matter): fix a bad error message - left over --- libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp index 6b54cf8af3a..a157469c980 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp @@ -86,7 +86,7 @@ bool MatterPressureSensor::setRawPressure(int16_t _rawPressure) { bool ret; ret = updateAttributeVal(PressureMeasurement::Id, PressureMeasurement::Attributes::MeasuredValue::Id, &pressureVal); if (!ret) { - log_e("Failed to update Fan Speed Percent Attribute."); + log_e("Failed to update Pressure Sensor Measurement Attribute."); return false; } rawPressure = _rawPressure; From 56a9b1d123fc7c8ea08703818bb68dbeb5536b27 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:25:13 +0000 Subject: [PATCH 3/3] ci(pre-commit): Apply automatic fixes --- .../MatterPressureSensor.ino | 262 +++++++++--------- 1 file changed, 131 insertions(+), 131 deletions(-) diff --git a/libraries/Matter/examples/MatterPressureSensor/MatterPressureSensor.ino b/libraries/Matter/examples/MatterPressureSensor/MatterPressureSensor.ino index b1708f85d85..aa6b28f199f 100644 --- a/libraries/Matter/examples/MatterPressureSensor/MatterPressureSensor.ino +++ b/libraries/Matter/examples/MatterPressureSensor/MatterPressureSensor.ino @@ -1,131 +1,131 @@ -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/* - * This example is an example code that will create a Matter Device which can be - * commissioned and controlled from a Matter Environment APP. - * Additionally the ESP32 will send debug messages indicating the Matter activity. - * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. - */ - -// Matter Manager -#include -#include - -// List of Matter Endpoints for this Node -// Matter Pressure Sensor Endpoint -MatterPressureSensor SimulatedPressureSensor; - -// set your board USER BUTTON pin here - decommissioning button -const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. - -// 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 - -// 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 = 5000; // keep the button pressed for 5s, or longer, to decommission - -// Simulate a pressure sensor - add your preferred pressure sensor library code here -float getSimulatedPressure() { - // The Endpoint implementation keeps an uint16_t as internal value information, - // which stores data in hPa (pressure measurement unit) - static float simulatedPressureHWSensor = 950; - - // it will increase from 950 to 1100 hPa in steps of 10 hPa to simulate a pressure sensor - simulatedPressureHWSensor = simulatedPressureHWSensor + 10; - if (simulatedPressureHWSensor > 1100) { - simulatedPressureHWSensor = 950; - } - - return simulatedPressureHWSensor; -} - -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 - WiFi.begin(ssid, password); - // Wait for connection - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(); - - // set initial pressure sensor measurement - // Simulated Sensor - it shall initially print 900hPa and then move to the 950 to 1100 hPa as pressure range - SimulatedPressureSensor.begin(900.00); - - // Matter beginning - Last step, after all EndPoints are initialized - Matter.begin(); - - // Check Matter Accessory Commissioning state, which may change during execution of loop() - if (!Matter.isDeviceCommissioned()) { - Serial.println(""); - Serial.println("Matter Node is not commissioned yet."); - Serial.println("Initiate the device discovery in your Matter environment."); - Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); - Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); - Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); - // waits for Matter Pressure Sensor Commissioning. - uint32_t timeCount = 0; - while (!Matter.isDeviceCommissioned()) { - delay(100); - if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec - Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); - } - } - Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); - } -} - -void loop() { - static uint32_t timeCounter = 0; - - // Print the current pressure value every 5s - if (!(timeCounter++ % 10)) { // delaying for 500ms x 10 = 5s - // Print the current pressure value - Serial.printf("Current Pressure is %.02fhPa\r\n", SimulatedPressureSensor.getPressure()); - // Update Pressure from the (Simulated) Hardware Sensor - // Matter APP shall display the updated pressure percent - SimulatedPressureSensor.setPressure(getSimulatedPressure()); - } - - // 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. - } - - if (digitalRead(buttonPin) == HIGH && button_state) { - button_state = false; // released - } - - // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node - uint32_t time_diff = millis() - button_time_stamp; - if (button_state && time_diff > decommissioningTimeout) { - // Factory reset is triggered if the button is pressed longer than 10 seconds - Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); - Matter.decommission(); - } - - delay(500); -} +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * This example is an example code that will create a Matter Device which can be + * commissioned and controlled from a Matter Environment APP. + * Additionally the ESP32 will send debug messages indicating the Matter activity. + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. + */ + +// Matter Manager +#include +#include + +// List of Matter Endpoints for this Node +// Matter Pressure Sensor Endpoint +MatterPressureSensor SimulatedPressureSensor; + +// set your board USER BUTTON pin here - decommissioning button +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. + +// 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 + +// 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 = 5000; // keep the button pressed for 5s, or longer, to decommission + +// Simulate a pressure sensor - add your preferred pressure sensor library code here +float getSimulatedPressure() { + // The Endpoint implementation keeps an uint16_t as internal value information, + // which stores data in hPa (pressure measurement unit) + static float simulatedPressureHWSensor = 950; + + // it will increase from 950 to 1100 hPa in steps of 10 hPa to simulate a pressure sensor + simulatedPressureHWSensor = simulatedPressureHWSensor + 10; + if (simulatedPressureHWSensor > 1100) { + simulatedPressureHWSensor = 950; + } + + return simulatedPressureHWSensor; +} + +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 + WiFi.begin(ssid, password); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + + // set initial pressure sensor measurement + // Simulated Sensor - it shall initially print 900hPa and then move to the 950 to 1100 hPa as pressure range + SimulatedPressureSensor.begin(900.00); + + // Matter beginning - Last step, after all EndPoints are initialized + Matter.begin(); + + // Check Matter Accessory Commissioning state, which may change during execution of loop() + if (!Matter.isDeviceCommissioned()) { + Serial.println(""); + Serial.println("Matter Node is not commissioned yet."); + Serial.println("Initiate the device discovery in your Matter environment."); + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + // waits for Matter Pressure Sensor Commissioning. + uint32_t timeCount = 0; + while (!Matter.isDeviceCommissioned()) { + delay(100); + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); + } + } + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); + } +} + +void loop() { + static uint32_t timeCounter = 0; + + // Print the current pressure value every 5s + if (!(timeCounter++ % 10)) { // delaying for 500ms x 10 = 5s + // Print the current pressure value + Serial.printf("Current Pressure is %.02fhPa\r\n", SimulatedPressureSensor.getPressure()); + // Update Pressure from the (Simulated) Hardware Sensor + // Matter APP shall display the updated pressure percent + SimulatedPressureSensor.setPressure(getSimulatedPressure()); + } + + // 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. + } + + if (digitalRead(buttonPin) == HIGH && button_state) { + button_state = false; // released + } + + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node + uint32_t time_diff = millis() - button_time_stamp; + if (button_state && time_diff > decommissioningTimeout) { + // Factory reset is triggered if the button is pressed longer than 10 seconds + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); + Matter.decommission(); + } + + delay(500); +}