-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
feat(matter): adds new Temperature Sensor Matter Endpoint #10698
Merged
me-no-dev
merged 15 commits into
espressif:release/v3.1.x
from
SuGlider:matter_temp_sensor
Dec 9, 2024
Merged
Changes from 12 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 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
94 changes: 94 additions & 0 deletions
94
libraries/Matter/examples/MatterTemperatureSensor/MatterTemperatureSensor.ino
This file contains 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,94 @@ | ||
// 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 an example 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 | ||
// Matter Temperature Sensor Endpoint | ||
MatterTemperatureSensor SimulatedTemperatureSensor; | ||
|
||
// 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 getSimulatedTemperature() { | ||
// The Endpoint implementation keeps an int16_t as internal value information, | ||
// which stores data in 1/100th of any temperature unit | ||
static float simulatedTempHWSensor = -10.0; | ||
|
||
// it will increase from -10C to 10C in 0.5C steps to simulate a temperature sensor | ||
simulatedTempHWSensor = simulatedTempHWSensor + 0.5; | ||
if (simulatedTempHWSensor > 10) { | ||
simulatedTempHWSensor = -10; | ||
} | ||
|
||
return simulatedTempHWSensor; | ||
} | ||
|
||
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 initially print -25 degrees and then move to the -10 to 10 range | ||
SimulatedTemperatureSensor.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 %.02f <Temperature Units>\r\n", SimulatedTemperatureSensor.getTemperature()); | ||
// update the temperature sensor value every 5 seconds | ||
// Matter APP shall display the updated temperature | ||
delay(5000); | ||
SimulatedTemperatureSensor.setTemperature(getSimulatedTemperature()); | ||
} |
This file contains 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 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
This file contains 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
99 changes: 99 additions & 0 deletions
99
libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp
This file contains 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,99 @@ | ||
// 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 <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 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(); | ||
// begin Matter Temperature Sensor endpoint with initial float temperature | ||
bool begin(double temperature = 0.00) { | ||
return begin(static_cast<int16_t>(temperature * 100.0f)); | ||
} | ||
// this will stop processing Temperature Sensor Matter events | ||
void end(); | ||
|
||
// set the reported raw temperature | ||
bool setTemperature(double temperature) { | ||
// stores up to 1/100th of a degree precision | ||
int16_t rawValue = static_cast<int16_t>(temperature * 100.0f); | ||
return setRawTemperature(rawValue); | ||
} | ||
// returns the reported float temperature with 1/100th of a degree precision | ||
double getTemperature() { | ||
return (double)rawTemperature / 100.0; | ||
} | ||
// double conversion operator | ||
void operator=(double temperature) { | ||
setTemperature(temperature); | ||
} | ||
// double conversion operator | ||
operator double() { | ||
return (double) getTemperature(); | ||
} | ||
|
||
// 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); | ||
|
||
protected: | ||
bool started = false; | ||
// implementation keeps temperature in 1/100th of a degree, any temperature unit | ||
int16_t rawTemperature = 0; | ||
// internal function to set the raw temperature value (Matter Cluster) | ||
bool setRawTemperature(int16_t _rawTemperature); | ||
bool begin(int16_t _rawTemperature); | ||
}; | ||
#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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done