Skip to content

Commit e540af2

Browse files
feat(matter): adds rain sensor matter endpoint (#12100)
* feat(matter): adds rain sensor matter endpoint * feat(matter): adds source code to CMakeLists.txt * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 7790eac commit e540af2

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/MatterRainSensor.cpp
196197
libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp
197198
libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.cpp
198199
libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.cpp

docs/en/matter/ep_rain_sensor.rst

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
###################
2+
MatterRainSensor
3+
###################
4+
5+
About
6+
-----
7+
8+
The ``MatterRainSensor`` class provides a rain sensor endpoint for Matter networks. This endpoint implements the Matter rain sensing standard for detecting rain presence (detected/not detected states).
9+
10+
**Features:**
11+
* Rain 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+
* Weather monitoring systems
20+
* Irrigation control systems
21+
* Outdoor sensor networks
22+
* Smart home automation triggers
23+
* Rain detection for automated systems
24+
25+
API Reference
26+
-------------
27+
28+
Constructor
29+
***********
30+
31+
MatterRainSensor
32+
^^^^^^^^^^^^^^^^
33+
34+
Creates a new Matter rain sensor endpoint.
35+
36+
.. code-block:: arduino
37+
38+
MatterRainSensor();
39+
40+
Initialization
41+
**************
42+
43+
begin
44+
^^^^^
45+
46+
Initializes the Matter rain sensor endpoint with an initial rain detection state.
47+
48+
.. code-block:: arduino
49+
50+
bool begin(bool _rainState = false);
51+
52+
* ``_rainState`` - Initial rain 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 rain sensor events.
60+
61+
.. code-block:: arduino
62+
63+
void end();
64+
65+
Rain Detection State Control
66+
****************************
67+
68+
setRain
69+
^^^^^^^
70+
71+
Sets the rain detection state.
72+
73+
.. code-block:: arduino
74+
75+
bool setRain(bool _rainState);
76+
77+
* ``_rainState`` - Rain detection state (``true`` = detected, ``false`` = not detected)
78+
79+
This function will return ``true`` if successful, ``false`` otherwise.
80+
81+
getRain
82+
^^^^^^^
83+
84+
Gets the current rain detection state.
85+
86+
.. code-block:: arduino
87+
88+
bool getRain();
89+
90+
This function will return ``true`` if rain is detected, ``false`` if not detected.
91+
92+
Operators
93+
*********
94+
95+
bool operator
96+
^^^^^^^^^^^^^
97+
98+
Returns the current rain detection state.
99+
100+
.. code-block:: arduino
101+
102+
operator bool();
103+
104+
Example:
105+
106+
.. code-block:: arduino
107+
108+
if (mySensor) {
109+
Serial.println("Rain is detected");
110+
} else {
111+
Serial.println("Rain is not detected");
112+
}
113+
114+
Assignment operator
115+
^^^^^^^^^^^^^^^^^^^
116+
117+
Sets the rain detection state.
118+
119+
.. code-block:: arduino
120+
121+
void operator=(bool _rainState);
122+
123+
Example:
124+
125+
.. code-block:: arduino
126+
127+
mySensor = true; // Set rain detection to detected
128+
mySensor = false; // Set rain detection to not detected
129+
130+
Example
131+
-------
132+
133+
Rain Sensor
134+
***********
135+
136+
.. literalinclude:: ../../../libraries/Matter/examples/MatterRainSensor/MatterRainSensor.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+
* ``MatterRainSensor``: Rain sensor (detected/not detected state)
129130
* ``MatterOccupancySensor``: Occupancy sensor (occupied/unoccupied state)
130131

131132
**Control Endpoints:**
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 Rain Sensor Device.
22+
* The Rain Sensor state can be toggled by pressing the onboard button.
23+
* The Rain Sensor state will be indicated by the onboard LED.
24+
* The Rain Sensor 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 Rain Sensor Endpoint
40+
MatterRainSensor RainSensor;
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 Rain Sensor 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 Rain Sensor 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 Rain Sensor 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 rain sensor state as false (default)
89+
RainSensor.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 Rain Sensor 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 simulatedHWRainSensor() {
116+
// Simulated Rain Sensor
117+
static bool rainState = false;
118+
static uint32_t lastTime = 0;
119+
120+
// Simulate a Rain Sensor state change every 20 seconds
121+
if (millis() - lastTime > 20000) {
122+
rainState = !rainState;
123+
lastTime = millis();
124+
}
125+
return rainState;
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 Rain State (Not Detected/Detected)
140+
RainSensor.setRain(!RainSensor.getRain()); // same as RainSensor = !RainSensor;
141+
Serial.printf("User button released. Setting the Rain Sensor to %s.\r\n", RainSensor ? "Detected" : "Not Detected");
142+
// LED will indicate the Rain Sensor state
143+
if (RainSensor) {
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 Rain Sensor 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 Rain Sensor
158+
RainSensor.setRain(simulatedHWRainSensor());
159+
160+
delay(50);
161+
}

0 commit comments

Comments
 (0)