Skip to content

Commit 61efcbc

Browse files
committed
Implement tank percentage estimation and update in water heater example
1 parent 3b7bacb commit 61efcbc

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

libraries/Matter/examples/matter_waterheater/matter_waterheater.ino

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@
2121

2222
MatterWaterHeater matter_water_heater;
2323

24+
const float COLD_WATER_TEMP = 20.0f; // Assumed incoming cold water temperature in Celsius
25+
26+
// Estimates the tank's remaining hot water level from the current and target temperatures,
27+
// assuming incoming cold water enters the tank at COLD_WATER_TEMP.
28+
void update_tank_percentage()
29+
{
30+
float current_temperature = matter_water_heater.get_local_temperature();
31+
float target_temperature = matter_water_heater.get_heating_setpoint();
32+
33+
int tank_percentage = (int)(((current_temperature - COLD_WATER_TEMP) / (target_temperature - COLD_WATER_TEMP)) * 100.0f);
34+
if (tank_percentage < 0) {
35+
tank_percentage = 0;
36+
}
37+
if (tank_percentage > 100) {
38+
tank_percentage = 100;
39+
}
40+
matter_water_heater.set_tank_percentage(tank_percentage);
41+
}
42+
2443
void setup()
2544
{
2645
pinMode(LED_BUILTIN, OUTPUT);
@@ -56,13 +75,14 @@ void setup()
5675
// Describe the physical tank - a single immersion element heating a 100 liter tank
5776
matter_water_heater.set_heater_types(MatterWaterHeater::heater_type_t::IMMERSION_ELEMENT_1);
5877
matter_water_heater.set_tank_volume(100);
59-
matter_water_heater.set_tank_percentage(100);
6078

6179
// Set the measured water temperature to a fixed value
6280
matter_water_heater.set_local_temperature(45.0f);
6381

6482
// Set the initial target water temperature
6583
matter_water_heater.set_heating_setpoint(55.0f);
84+
85+
update_tank_percentage();
6686
}
6787

6888
void loop()
@@ -73,6 +93,7 @@ void loop()
7393
if (setpoint_prev != setpoint) {
7494
Serial.printf("Water heater setpoint: %.01f C\n", matter_water_heater.get_heating_setpoint());
7595
setpoint_prev = setpoint;
96+
update_tank_percentage();
7697
}
7798

7899
// Print the current mode if it changes

0 commit comments

Comments
 (0)