Skip to content

Commit 471dcb0

Browse files
SuGliderpre-commit-ci-lite[bot]me-no-dev
authored
Feat(matter) adds matter water freeze detector endpoint (espressif#12101)
* feat(matter): adds water freeze detector matter endpoint * fix(matter): mixed branches * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Me No Dev <[email protected]>
1 parent e540af2 commit 471dcb0

File tree

10 files changed

+648
-0
lines changed

10 files changed

+648
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ set(ARDUINO_LIBRARY_Matter_SRCS
193193
libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp
194194
libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.cpp
195195
libraries/Matter/src/MatterEndpoints/MatterContactSensor.cpp
196+
libraries/Matter/src/MatterEndpoints/MatterWaterFreezeDetector.cpp
196197
libraries/Matter/src/MatterEndpoints/MatterRainSensor.cpp
197198
libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp
198199
libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.cpp
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
###########################
2+
MatterWaterFreezeDetector
3+
###########################
4+
5+
About
6+
-----
7+
8+
The ``MatterWaterFreezeDetector`` class provides a water freeze detector endpoint for Matter networks. This endpoint implements the Matter water freeze detection standard for detecting water freeze conditions (detected/not detected states).
9+
10+
**Features:**
11+
* Water freeze detection state reporting (detected/not detected)
12+
* Simple boolean state
13+
* Read-only sensor (no control functionality)
14+
* Automatic state updates
15+
* Integration with Apple HomeKit, Amazon Alexa, and Google Home
16+
* Matter standard compliance
17+
18+
**Use Cases:**
19+
* Water pipe freeze monitoring
20+
* Outdoor water system protection
21+
* HVAC freeze detection
22+
* Smart home automation triggers
23+
* Preventative maintenance systems
24+
25+
API Reference
26+
-------------
27+
28+
Constructor
29+
***********
30+
31+
MatterWaterFreezeDetector
32+
^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
34+
Creates a new Matter water freeze detector endpoint.
35+
36+
.. code-block:: arduino
37+
38+
MatterWaterFreezeDetector();
39+
40+
Initialization
41+
**************
42+
43+
begin
44+
^^^^^
45+
46+
Initializes the Matter water freeze detector endpoint with an initial freeze detection state.
47+
48+
.. code-block:: arduino
49+
50+
bool begin(bool _freezeState = false);
51+
52+
* ``_freezeState`` - Initial water freeze detection state (``true`` = detected, ``false`` = not detected, default: ``false``)
53+
54+
This function will return ``true`` if successful, ``false`` otherwise.
55+
56+
end
57+
^^^
58+
59+
Stops processing Matter water freeze detector events.
60+
61+
.. code-block:: arduino
62+
63+
void end();
64+
65+
Water Freeze Detection State Control
66+
************************************
67+
68+
setFreeze
69+
^^^^^^^^^
70+
71+
Sets the water freeze detection state.
72+
73+
.. code-block:: arduino
74+
75+
bool setFreeze(bool _freezeState);
76+
77+
* ``_freezeState`` - Water freeze detection state (``true`` = detected, ``false`` = not detected)
78+
79+
This function will return ``true`` if successful, ``false`` otherwise.
80+
81+
getFreeze
82+
^^^^^^^^^
83+
84+
Gets the current water freeze detection state.
85+
86+
.. code-block:: arduino
87+
88+
bool getFreeze();
89+
90+
This function will return ``true`` if water freeze is detected, ``false`` if not detected.
91+
92+
Operators
93+
*********
94+
95+
bool operator
96+
^^^^^^^^^^^^^
97+
98+
Returns the current water freeze detection state.
99+
100+
.. code-block:: arduino
101+
102+
operator bool();
103+
104+
Example:
105+
106+
.. code-block:: arduino
107+
108+
if (myDetector) {
109+
Serial.println("Water freeze is detected");
110+
} else {
111+
Serial.println("Water freeze is not detected");
112+
}
113+
114+
Assignment operator
115+
^^^^^^^^^^^^^^^^^^^
116+
117+
Sets the water freeze detection state.
118+
119+
.. code-block:: arduino
120+
121+
void operator=(bool _freezeState);
122+
123+
Example:
124+
125+
.. code-block:: arduino
126+
127+
myDetector = true; // Set water freeze detection to detected
128+
myDetector = false; // Set water freeze detection to not detected
129+
130+
Example
131+
-------
132+
133+
Water Freeze Detector
134+
*********************
135+
136+
.. literalinclude:: ../../../libraries/Matter/examples/MatterWaterFreezeDetector/MatterWaterFreezeDetector.ino
137+
:language: arduino

docs/en/matter/matter.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ The library provides specialized endpoint classes for different device types. Ea
126126
* ``MatterHumiditySensor``: Humidity sensor (read-only)
127127
* ``MatterPressureSensor``: Pressure sensor (read-only)
128128
* ``MatterContactSensor``: Contact sensor (open/closed state)
129+
* ``MatterWaterFreezeDetector``: Water freeze detector (detected/not detected state)
129130
* ``MatterRainSensor``: Rain sensor (detected/not detected state)
130131
* ``MatterOccupancySensor``: Occupancy sensor (occupied/unoccupied state)
131132

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/*
16+
* This example is an example code that will create a Matter Device which can be
17+
* commissioned and controlled from a Matter Environment APP.
18+
* Additionally the ESP32 will send debug messages indicating the Matter activity.
19+
* Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages.
20+
*
21+
* The example will create a Matter Water Freeze Detector Device.
22+
* The Water Freeze Detector state can be toggled by pressing the onboard button.
23+
* The Water Freeze Detector state will be indicated by the onboard LED.
24+
* The Water Freeze Detector state will be simulated to change every 20 seconds.
25+
*
26+
* The onboard button can be kept pressed for 5 seconds to decommission the Matter Node.
27+
* The example will also show the manual commissioning code and QR code to be used in the Matter environment.
28+
*
29+
*/
30+
31+
// Matter Manager
32+
#include <Matter.h>
33+
#if !CONFIG_ENABLE_CHIPOBLE
34+
// if the device can be commissioned using BLE, WiFi is not used - save flash space
35+
#include <WiFi.h>
36+
#endif
37+
38+
// List of Matter Endpoints for this Node
39+
// Matter Water Freeze Detector Endpoint
40+
MatterWaterFreezeDetector WaterFreezeDetector;
41+
42+
// CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network
43+
#if !CONFIG_ENABLE_CHIPOBLE
44+
// WiFi is manually set and started
45+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
46+
const char *password = "your-password"; // Change this to your WiFi password
47+
#endif
48+
49+
// LED will be used to indicate the Water Freeze Detector state
50+
// set your board RGB LED pin here
51+
#ifdef RGB_BUILTIN
52+
const uint8_t ledPin = RGB_BUILTIN;
53+
#else
54+
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
55+
#warning "Do not forget to set the RGB LED pin"
56+
#endif
57+
58+
// set your board USER BUTTON pin here - decommissioning and Manual Water Freeze Detector toggle button
59+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
60+
61+
// Button control
62+
uint32_t button_time_stamp = 0; // debouncing control
63+
bool button_state = false; // false = released | true = pressed
64+
const uint32_t debouceTime = 250; // button debouncing time (ms)
65+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
66+
67+
void setup() {
68+
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
69+
// The button will also be used to manually toggle the Water Freeze Detector state
70+
pinMode(buttonPin, INPUT_PULLUP);
71+
// Initialize the LED (light) GPIO and Matter End Point
72+
pinMode(ledPin, OUTPUT);
73+
74+
Serial.begin(115200);
75+
76+
// CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network
77+
#if !CONFIG_ENABLE_CHIPOBLE
78+
// Manually connect to WiFi
79+
WiFi.begin(ssid, password);
80+
// Wait for connection
81+
while (WiFi.status() != WL_CONNECTED) {
82+
delay(500);
83+
Serial.print(".");
84+
}
85+
Serial.println();
86+
#endif
87+
88+
// set initial water freeze detector state as false (default)
89+
WaterFreezeDetector.begin();
90+
digitalWrite(ledPin, LOW); // LED OFF
91+
92+
// Matter beginning - Last step, after all EndPoints are initialized
93+
Matter.begin();
94+
95+
// Check Matter Accessory Commissioning state, which may change during execution of loop()
96+
if (!Matter.isDeviceCommissioned()) {
97+
Serial.println("");
98+
Serial.println("Matter Node is not commissioned yet.");
99+
Serial.println("Initiate the device discovery in your Matter environment.");
100+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
101+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
102+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
103+
// waits for Matter Water Freeze Detector Commissioning.
104+
uint32_t timeCount = 0;
105+
while (!Matter.isDeviceCommissioned()) {
106+
delay(100);
107+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
108+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
109+
}
110+
}
111+
Serial.println("Matter Node is commissioned and connected to the network. Ready for use.");
112+
}
113+
}
114+
115+
bool simulatedHWWaterFreezeDetector() {
116+
// Simulated Water Freeze Detector
117+
static bool freezeState = false;
118+
static uint32_t lastTime = 0;
119+
120+
// Simulate a Water Freeze Detector state change every 20 seconds
121+
if (millis() - lastTime > 20000) {
122+
freezeState = !freezeState;
123+
lastTime = millis();
124+
}
125+
return freezeState;
126+
}
127+
128+
void loop() {
129+
// Check if the button has been pressed
130+
if (digitalRead(buttonPin) == LOW && !button_state) {
131+
// deals with button debouncing
132+
button_time_stamp = millis(); // record the time while the button is pressed.
133+
button_state = true; // pressed.
134+
}
135+
136+
uint32_t time_diff = millis() - button_time_stamp;
137+
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
138+
button_state = false; // released
139+
// button is released - toggle Freeze State (Not Detected/Detected)
140+
WaterFreezeDetector.setFreeze(!WaterFreezeDetector.getFreeze()); // same as WaterFreezeDetector = !WaterFreezeDetector;
141+
Serial.printf("User button released. Setting the Water Freeze Detector to %s.\r\n", WaterFreezeDetector ? "Detected" : "Not Detected");
142+
// LED will indicate the Water Freeze Detector state
143+
if (WaterFreezeDetector) {
144+
digitalWrite(ledPin, HIGH); // LED ON
145+
} else {
146+
digitalWrite(ledPin, LOW); // LED OFF
147+
}
148+
}
149+
150+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
151+
if (button_state && time_diff > decommissioningTimeout) {
152+
Serial.println("Decommissioning Water Freeze Detector Matter Accessory. It shall be commissioned again.");
153+
Matter.decommission();
154+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
155+
}
156+
157+
// Simulated Water Freeze Detector
158+
WaterFreezeDetector.setFreeze(simulatedHWWaterFreezeDetector());
159+
160+
delay(50);
161+
}

0 commit comments

Comments
 (0)