-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat(matter): adds new Temperature Sensor Matter Endpoint #10698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
me-no-dev
merged 15 commits into
espressif:release/v3.1.x
from
SuGlider:matter_temp_sensor
Dec 9, 2024
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ae3ac31
feat(matter): adds new temperature sensor matter endpoint
SuGlider cbf1548
feat(matter): commentaries review and fixes
SuGlider 4130060
feat(matter): commentaries review and fixes
SuGlider 543c7f4
feat(matter): commentaries review and fixes
SuGlider 214efe7
feat(matter): commentaries review and fixes
SuGlider 336d99e
feat(matter): commentaries review and fixes
SuGlider ff79cac
feat(matter): commentaries review and fixes
SuGlider b8c43cc
feat(matter): general commentaries and code review
SuGlider 1f3b13b
feat(matter): keeping arduino style for local variables (lower case)
SuGlider 8c09c18
feat(matter): applies a generic temperature unit to the implementatio…
SuGlider 11ba0c4
fix(matter): fixed problem with begin(float) implementation
SuGlider 291b6fb
fix(matter): fixed begin(float) initiallization
SuGlider a9c783b
feat(matter): updated matter temperature keywords with new api
SuGlider d858783
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] dbcdf6c
fix(matter): fixed code spell ci errors in matter temperature sensor
SuGlider File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
libraries/Matter/examples/MatterTemperatureSensor/MatterTemperatureSensor.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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; | ||
SuGlider marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ MatterEndPoint KEYWORD1 | |
MatterFan KEYWORD1 | ||
FanMode_t KEYWORD1 | ||
FanModeSequence_t KEYWORD1 | ||
MatterTemperatureSensor KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
|
@@ -62,6 +63,10 @@ setMode KEYWORD2 | |
getMode KEYWORD2 | ||
onChangeMode KEYWORD2 | ||
onChangeSpeedPercent KEYWORD2 | ||
setRawTemperature KEYWORD2 | ||
getRawTemperature KEYWORD2 | ||
setTemperatureCelsius KEYWORD2 | ||
getTemperatureCelsius KEYWORD2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. those should also be updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
|
@@ -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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
me-no-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 */ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.