Skip to content

Commit 8c09c18

Browse files
committed
feat(matter): applies a generic temperature unit to the implementation and example
1 parent 1f3b13b commit 8c09c18

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

libraries/Matter/examples/MatterTemperatureSensor/MatterTemperatureSensor.ino

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,26 @@
2424
#include <WiFi.h>
2525

2626
// List of Matter Endpoints for this Node
27-
// Celcius Temperature Sensor Endpoint
28-
MatterTemperatureSensor CelciusTempSensor;
27+
// Matter Temperature Sensor Endpoint
28+
MatterTemperatureSensor SimulatedTemperatureSensor;
2929

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

3434
// Simulate a temperature sensor - add your prefered temperature sensor library code here
35-
float getTemperature() {
36-
static float celciusTempHWSensor = -10.0;
35+
float getSimulatedTemperature() {
36+
// The Endpoint implementation keeps an int16_t as internal value information,
37+
// which stores data in 1/100th of any temperature unit
38+
static float simulatedTempHWSensor = -10.0;
3739

3840
// it will increase from -10C to 10C in 0.5C steps to simulate a temperature sensor
39-
celciusTempHWSensor = celciusTempHWSensor + 0.5;
40-
if (celciusTempHWSensor > 10) {
41-
celciusTempHWSensor = -10;
41+
simulatedTempHWSensor = simulatedTempHWSensor + 0.5;
42+
if (simulatedTempHWSensor > 10) {
43+
simulatedTempHWSensor = -10;
4244
}
4345

44-
return celciusTempHWSensor;
46+
return simulatedTempHWSensor;
4547
}
4648

4749
void setup() {
@@ -57,8 +59,8 @@ void setup() {
5759
Serial.println();
5860

5961
// set initial temperature sensor measurement
60-
// Simulated Sensor - it shall initially print -25C and then move to the -10C to 10C range
61-
CelciusTempSensor.begin(-25.00);
62+
// Simulated Sensor - it shall initially print -25 degrees and then move to the -10 to 10 range
63+
SimulatedTemperatureSensor.begin(-25.00);
6264

6365
// Matter beginning - Last step, after all EndPoints are initialized
6466
Matter.begin();
@@ -84,9 +86,9 @@ void setup() {
8486
}
8587

8688
void loop() {
87-
Serial.printf("Current Temperature is %.02fC\r\n", (float)CelciusTempSensor);
89+
Serial.printf("Current Temperature is %.02f <Temperature Units>\r\n", SimulatedTemperatureSensor.getTemperature());
8890
// update the temperature sensor value every 5 seconds
8991
// Matter APP shall display the updated temperature
9092
delay(5000);
91-
CelciusTempSensor = getTemperature();
93+
SimulatedTemperatureSensor.setTemperature(getSimulatedTemperature());
9294
}

libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,41 @@ class MatterTemperatureSensor : public MatterEndPoint {
2323
public:
2424
MatterTemperatureSensor();
2525
~MatterTemperatureSensor();
26-
// begin Matter Temperature Sensor endpoint
27-
virtual bool begin(int16_t _rawTemperature = 0);
28-
bool begin(double temperatureCelcius) {
29-
int16_t rawValue = static_cast<int16_t>(temperatureCelcius * 100.0f);
30-
return begin(rawValue);
26+
// begin Matter Temperature Sensor endpoint with initial float temperature
27+
bool begin(double temperature) {
28+
return setTemperature(temperature);
3129
}
3230
// this will stop processing Temperature Sensor Matter events
3331
void end();
3432

3533
// set the reported raw temperature
36-
bool setRawTemperature(int16_t _rawTemperature);
37-
bool setTemperatureCelsius(double temperatureCelcius) {
38-
int16_t rawValue = static_cast<int16_t>(temperatureCelcius * 100.0f);
34+
bool setTemperature(double temperature) {
35+
// stores up to 1/100th of a degree precision
36+
int16_t rawValue = static_cast<int16_t>(temperature * 100.0f);
3937
return setRawTemperature(rawValue);
4038
}
41-
// returns the reported raw temperature (in 1/100th of a degree)
42-
int16_t getRawTemperature() {
43-
return rawTemperature;
44-
}
45-
// returns the reported temperature in Celcius
46-
double getTemperatureCelsius() {
39+
// returns the reported float temperature with 1/100th of a degree precision
40+
double getTemperature() {
4741
return (double)rawTemperature / 100.0;
4842
}
49-
// sets the reported temperature in Celcius
50-
void operator=(double temperatureCelcius) {
51-
int16_t rawValue = static_cast<int16_t>(temperatureCelcius * 100.0f);
52-
setRawTemperature(rawValue);
43+
// double conversion operator
44+
void operator=(double temperature) {
45+
setTemperature(temperature);
5346
}
47+
// double conversion operator
5448
operator double() {
55-
return (double) getTemperatureCelsius();
49+
return (double) getTemperature();
5650
}
5751

5852
// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.
5953
bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val);
6054

6155
protected:
6256
bool started = false;
63-
int16_t rawTemperature = 0; // default initial reported temperature
57+
// implementation keeps temperature in 1/100th of a degree, any temperature unit
58+
int16_t rawTemperature = 0;
59+
// internal function to set the raw temperature value (Matter Cluster)
60+
bool setRawTemperature(int16_t _rawTemperature);
61+
bool begin(int16_t _rawTemperature);
6462
};
6563
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */

0 commit comments

Comments
 (0)