Skip to content

feat(matter): adds new Temperature Sensor Matter Endpoint #10698

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 15 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ set(ARDUINO_LIBRARY_Matter_SRCS
libraries/Matter/src/MatterEndpoints/MatterColorLight.cpp
libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.cpp
libraries/Matter/src/MatterEndpoints/MatterFan.cpp
libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp
libraries/Matter/src/Matter.cpp)

set(ARDUINO_LIBRARY_PPP_SRCS
Expand Down Expand Up @@ -401,3 +402,4 @@ endif()
if(NOT CONFIG_ARDUINO_SELECTIVE_COMPILATION OR CONFIG_ARDUINO_SELECTIVE_WiFiProv)
maybe_add_component(espressif__network_provisioning)
endif()

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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 the smallest 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 <Matter.h>
#include <WiFi.h>

// List of Matter Endpoints for this Node
// Celcius Temperature Sensor Endpoint


MatterTemperatureSensor CelciusTempSensor;

// 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

// Simulate a temperature sensor - add your prefered temperature sensor library code here

float getTemperature() {
static float CelciusTempHWSensor = -10.0;

// it will increase from -10C to 10C in 0.5C steps to simulate a temperature sensor
CelciusTempHWSensor = CelciusTempHWSensor + 0.5;
if (CelciusTempHWSensor > 10) {
CelciusTempHWSensor = -10;
}

return CelciusTempHWSensor;
}

void setup() {
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 temperature sensor measurement
// Simulated Sensor - it shall print -25C first and then move to the -10C to 10C range
CelciusTempSensor.begin(-25.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 Temperature 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() {
Serial.printf("Current Temperature is %.02fC\r\n", (float)CelciusTempSensor);
// update the temperature sensor value every 5 seconds
// Matter phone APP shall display the temperature change

delay(5000);
CelciusTempSensor = getTemperature();
}
7 changes: 7 additions & 0 deletions libraries/Matter/examples/MatterTemperatureSensor/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fqbn_append": "PartitionScheme=huge_app",
"requires": [
"CONFIG_SOC_WIFI_SUPPORTED=y",
"CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y"
]
}
6 changes: 6 additions & 0 deletions libraries/Matter/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ MatterEndPoint KEYWORD1
MatterFan KEYWORD1
FanMode_t KEYWORD1
FanModeSequence_t KEYWORD1
MatterTemperatureSensor KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down Expand Up @@ -62,6 +63,10 @@ setMode KEYWORD2
getMode KEYWORD2
onChangeMode KEYWORD2
onChangeSpeedPercent KEYWORD2
setRawTemperature KEYWORD2
getRawTemperature KEYWORD2
setTemperatureCelsius KEYWORD2
getTemperatureCelsius KEYWORD2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those should also be updated

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


#######################################
# Constants (LITERAL1)
Expand All @@ -88,3 +93,4 @@ FAN_MODE_SEQ_OFF_LOW_MED_HIGH_AUTO LITERAL1
FAN_MODE_SEQ_OFF_LOW_HIGH_AUTO LITERAL1
FAN_MODE_SEQ_OFF_HIGH_AUTO LITERAL1
FAN_MODE_SEQ_OFF_HIGH LITERAL1

2 changes: 2 additions & 0 deletions libraries/Matter/src/Matter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <MatterEndpoints/MatterColorLight.h>
#include <MatterEndpoints/MatterEnhancedColorLight.h>
#include <MatterEndpoints/MatterFan.h>
#include <MatterEndpoints/MatterTemperatureSensor.h>

using namespace esp_matter;

Expand Down Expand Up @@ -58,6 +59,7 @@ class ArduinoMatter {
friend class MatterColorLight;
friend class MatterEnhancedColorLight;
friend class MatterFan;
friend class MatterTemperatureSensor;

protected:
static void _init();
Expand Down
100 changes: 100 additions & 0 deletions libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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 <sdkconfig.h>
#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL

#include <Matter.h>
#include <app/server/Server.h>
#include <MatterEndpoints/MatterTemperatureSensor.h>

using namespace esp_matter;
using namespace esp_matter::endpoint;
using namespace chip::app::Clusters;

bool MatterTemperatureSensor::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 Temperature Sensor device has not begun.");
return false;
}

log_d("Temperature Sensor Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);
return ret;
}

MatterTemperatureSensor::MatterTemperatureSensor() {}

MatterTemperatureSensor::~MatterTemperatureSensor() {
end();
}

bool MatterTemperatureSensor::begin(int16_t _rawTemperature) {
ArduinoMatter::_init();

temperature_sensor::config_t temperature_sensor_config;
temperature_sensor_config.temperature_measurement.measured_value = _rawTemperature;
temperature_sensor_config.temperature_measurement.min_measured_value = nullptr;
temperature_sensor_config.temperature_measurement.max_measured_value = nullptr;

// endpoint handles can be used to add/modify clusters.
endpoint_t *endpoint = temperature_sensor::create(node::get(), &temperature_sensor_config, ENDPOINT_FLAG_NONE, (void *)this);
if (endpoint == nullptr) {
log_e("Failed to create Temperature Sensor endpoint");
return false;
}
rawTemperature = _rawTemperature;
setEndPointId(endpoint::get_id(endpoint));
log_i("Temperature Sensor created with endpoint_id %d", getEndPointId());
started = true;
return true;
}

void MatterTemperatureSensor::end() {
started = false;
}

bool MatterTemperatureSensor::setRawTemperature(int16_t _rawTemperature) {
if (!started) {
log_e("Matter Temperature Sensor device has not begun.");
return false;
}

// avoid processing the a "no-change"
if (rawTemperature == _rawTemperature) {
return true;
}

esp_matter_attr_val_t temperatureVal = esp_matter_invalid(NULL);

if (!getAttributeVal(TemperatureMeasurement::Id, TemperatureMeasurement::Attributes::MeasuredValue::Id, &temperatureVal)) {
log_e("Failed to get Temperature Sensor Attribute.");
return false;
}
if (temperatureVal.val.i16 != _rawTemperature) {
temperatureVal.val.i16 = _rawTemperature;
bool ret;
ret = updateAttributeVal(TemperatureMeasurement::Id, TemperatureMeasurement::Attributes::MeasuredValue::Id, &temperatureVal);
if (!ret) {
log_e("Failed to update Fan Speed Percent Attribute.");
return false;
}
rawTemperature = _rawTemperature;
}
log_v("Temperature Sensor set to %.02f Celcius Degrees", (float)_rawTemperature / 100.00);

return true;
}

#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */
63 changes: 63 additions & 0 deletions libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 <sdkconfig.h>
#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL

#include <Matter.h>
#include <MatterEndPoint.h>

class MatterTemperatureSensor : public MatterEndPoint {
public:
MatterTemperatureSensor();
~MatterTemperatureSensor();
// default initial raw temperature
virtual bool begin(int16_t _rawTemperature = 0);
bool begin(double temperatureCelcius) {
int16_t rawValue = static_cast<int16_t>(temperatureCelcius * 100.0f);
return begin(rawValue);
}
// this will just stop processing Temperature Sensor Matter events
void end();

// set the reported raw temperature
bool setRawTemperature(int16_t _rawTemperature);
bool setTemperatureCelsius(double temperatureCelcius) {
int16_t rawValue = static_cast<int16_t>(temperatureCelcius * 100.0f);
return setRawTemperature(rawValue);
}
int16_t getRawTemperature() { // returns the reported raw temperature
return rawTemperature;
}
double getTemperatureCelsius() { // returns the reported temperature in Celcius
return (double)rawTemperature / 100.0;
}
void operator=(double temperatureCelcius) { // sets the reported temperature in Celcius
int16_t rawValue = static_cast<int16_t>(temperatureCelcius * 100.0f);
setRawTemperature(rawValue);
}
operator double() { // returns the reported temperature in Celcius
return (double) getTemperatureCelsius();
}

// 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);
// User Callback for whenever the Light state is changed by the Matter Controller

protected:
bool started = false;
int16_t rawTemperature = 0; // default initial reported temperature
};
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */
Loading