-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat(matter): new Matter Endpoint for Thermostat #10755
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 10 commits into
espressif:master
from
SuGlider:matter_thermostat_endpoint
Jan 7, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c484f14
feat(matter): add new matter endpoint for thermostat
SuGlider bcf884b
Merge branch 'master' into matter_thermostat_endpoint
SuGlider f1eca7d
fix(matter): not used variable from log_e() message
SuGlider 102dfc6
feat(matter): adds specifc type name for thermostat auto mode enabled
SuGlider 60d674b
Merge branch 'master' into matter_thermostat_endpoint
SuGlider f708865
fix(matter): suggested changes in pr review
SuGlider 1d4e150
feat(matter): added the whole list of thermostat operational modes
SuGlider 10b42ee
fix(matter): fixed type in thermostat operational modes
SuGlider 4cd8751
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] 6ec7207
fix(matter): typos caught by CI codespell
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
243 changes: 243 additions & 0 deletions
243
libraries/Matter/examples/MatterThermostat/MatterThermostat.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,243 @@ | ||
// 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 Thermostat Endpoint | ||
MatterThermostat SimulatedThermostat; | ||
|
||
// 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 | ||
|
||
// set your board USER BUTTON pin here - decommissioning button | ||
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. | ||
|
||
// Button control - decommision the Matter Node | ||
uint32_t button_time_stamp = 0; // debouncing control | ||
bool button_state = false; // false = released | true = pressed | ||
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission | ||
|
||
// Simulate a system that will activate heating/cooling in addition to a temperature sensor - add your preferred code here | ||
float getSimulatedTemperature(bool isHeating, bool isCooling) { | ||
// read sensor temperature and apply heating/cooling | ||
float simulatedTempHWSensor = SimulatedThermostat.getLocalTemperature(); | ||
|
||
if (isHeating) { | ||
// it will increase to simulate a heating system | ||
simulatedTempHWSensor = simulatedTempHWSensor + 0.5; | ||
} | ||
if (isCooling) { | ||
// it will decrease to simulate a colling system | ||
simulatedTempHWSensor = simulatedTempHWSensor - 0.5; | ||
} | ||
// otherwise, it will keep the temperature stable | ||
return simulatedTempHWSensor; | ||
} | ||
|
||
void setup() { | ||
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node | ||
pinMode(buttonPin, INPUT_PULLUP); | ||
|
||
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(); | ||
|
||
// Simulated Thermostat in COOLING and HEATING mode with Auto Mode to keep the temperature between setpoints | ||
// Auto Mode can only be used when the control sequence of operation is Cooling & Heating | ||
SimulatedThermostat.begin(MatterThermostat::THERMOSTAT_SEQ_OP_COOLING_HEATING, MatterThermostat::THERMOSTAT_AUTO_MODE_ENABLED); | ||
|
||
// 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 Thermostat 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."); | ||
|
||
// after commissioning, set initial thermostat parameters | ||
// start the thermostat in AUTO mode | ||
SimulatedThermostat.setMode(MatterThermostat::THERMOSTAT_MODE_AUTO); | ||
// cooling setpoint must be lower than heating setpoint by at least 2.5C (deadband), in auto mode | ||
SimulatedThermostat.setCoolingHeatingSetpoints(20.0, 23.00); // the target cooler and heating setpoint | ||
// set the local temperature sensor in Celsius | ||
SimulatedThermostat.setLocalTemperature(12.50); | ||
|
||
Serial.println(); | ||
Serial.printf( | ||
"Initial Setpoints are %.01fC to %.01fC with a minimum 2.5C difference\r\n", SimulatedThermostat.getHeatingSetpoint(), | ||
SimulatedThermostat.getCoolingSetpoint() | ||
); | ||
Serial.printf("Auto mode is ON. Initial Temperature of %.01fC \r\n", SimulatedThermostat.getLocalTemperature()); | ||
Serial.println("Local Temperature Sensor will be simulated every 10 seconds and changed by a simulated heater and cooler to move in between setpoints."); | ||
} | ||
} | ||
|
||
// This will simulate the thermostat control system (heating and cooling) | ||
// User can set a local temperature using the Serial input (type a number and press Enter) | ||
// New temperature can be an positive or negative temperature in Celsius, between -50C and 50C | ||
// Initial local temperature is 10C as defined in getSimulatedTemperature() function | ||
void readSerialForNewTemperature() { | ||
static String newTemperatureStr; | ||
|
||
while (Serial.available()) { | ||
char c = Serial.read(); | ||
if (c == '\n' || c == '\r') { | ||
if (newTemperatureStr.length() > 0) { | ||
// convert the string to a float value | ||
float newTemperature = newTemperatureStr.toFloat(); | ||
// check if the new temperature is valid | ||
if (newTemperature >= -50.0 && newTemperature <= 50.0) { | ||
// set the new temperature | ||
SimulatedThermostat.setLocalTemperature(newTemperature); | ||
Serial.printf("New Temperature is %.01fC\r\n", newTemperature); | ||
} else { | ||
Serial.println("Invalid Temperature value. Please type a number between -50 and 50"); | ||
} | ||
newTemperatureStr = ""; | ||
} | ||
} else { | ||
if (c == '+' || c == '-' || (c >= '0' && c <= '9') || c == '.') { | ||
newTemperatureStr += c; | ||
} else { | ||
Serial.println("Invalid character. Please type a number between -50 and 50"); | ||
newTemperatureStr = ""; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// loop will simulate the thermostat control system | ||
// User can set a local temperature using the Serial input (type a number and press Enter) | ||
// User can change the thermostat mode using the Matter APP (smartphone) | ||
// The loop will simulate a heating and cooling system and the associated local temperature change | ||
void loop() { | ||
static uint32_t timeCounter = 0; | ||
|
||
// Simulate the heating and cooling systems | ||
static bool isHeating = false; | ||
static bool isCooling = false; | ||
|
||
// check if a new temperature is typed in the Serial Monitor | ||
readSerialForNewTemperature(); | ||
|
||
// simulate thermostat with heating/cooling system and the associated local temperature change, every 10s | ||
if (!(timeCounter++ % 20)) { // delaying for 500ms x 20 = 10s | ||
float localTemperature = getSimulatedTemperature(isHeating, isCooling); | ||
// Print the current thermostat local temperature value | ||
Serial.printf("Current Local Temperature is %.01fC\r\n", localTemperature); | ||
SimulatedThermostat.setLocalTemperature(localTemperature); // publish the new temperature value | ||
|
||
// Simulate the thermostat control system - User has 4 modes: OFF, HEAT, COOL, AUTO | ||
switch (SimulatedThermostat.getMode()) { | ||
case MatterThermostat::THERMOSTAT_MODE_OFF: | ||
// turn off the heating and cooling systems | ||
isHeating = false; | ||
isCooling = false; | ||
break; | ||
case MatterThermostat::THERMOSTAT_MODE_AUTO: | ||
// User APP has set the thermostat to AUTO mode -- keeping the tempeature between both setpoints | ||
// check if the heating system should be turned on or off | ||
if (localTemperature < SimulatedThermostat.getHeatingSetpoint() + SimulatedThermostat.getDeadBand()) { | ||
// turn on the heating system and turn off the cooling system | ||
isHeating = true; | ||
isCooling = false; | ||
} | ||
if (localTemperature > SimulatedThermostat.getCoolingSetpoint() - SimulatedThermostat.getDeadBand()) { | ||
// turn off the heating system and turn on the cooling system | ||
isHeating = false; | ||
isCooling = true; | ||
} | ||
break; | ||
case MatterThermostat::THERMOSTAT_MODE_HEAT: | ||
// Simulate the heating system - User has turned the heating system ON | ||
isHeating = true; | ||
isCooling = false; // keep the cooling system off as it is in heating mode | ||
// when the heating system is in HEATING mode, it will be turned off as soon as the local temperature is above the setpoint | ||
if (localTemperature > SimulatedThermostat.getHeatingSetpoint()) { | ||
// turn off the heating system | ||
isHeating = false; | ||
} | ||
break; | ||
case MatterThermostat::THERMOSTAT_MODE_COOL: | ||
// Simulate the cooling system - User has turned the cooling system ON | ||
if (SimulatedThermostat.getMode() == MatterThermostat::THERMOSTAT_MODE_COOL) { | ||
isCooling = true; | ||
isHeating = false; // keep the heating system off as it is in cooling mode | ||
// when the cooling system is in COOLING mode, it will be turned off as soon as the local temperature is bellow the setpoint | ||
if (localTemperature < SimulatedThermostat.getCoolingSetpoint()) { | ||
// turn off the cooling system | ||
isCooling = false; | ||
} | ||
} | ||
break; | ||
default: log_e("Invalid Thermostat Mode %d", SimulatedThermostat.getMode()); | ||
} | ||
// Reporting Heating and Cooling status | ||
Serial.printf( | ||
"\tThermostat Mode: %s >>> Heater is %s -- Cooler is %s\r\n", MatterThermostat::getThermostatModeString(SimulatedThermostat.getMode()), | ||
isHeating ? "ON" : "OFF", isCooling ? "ON" : "OFF" | ||
); | ||
} | ||
// Check if the button has been pressed | ||
if (digitalRead(buttonPin) == LOW && !button_state) { | ||
// deals with button debouncing | ||
button_time_stamp = millis(); // record the time while the button is pressed. | ||
button_state = true; // pressed. | ||
} | ||
|
||
if (digitalRead(buttonPin) == HIGH && button_state) { | ||
button_state = false; // released | ||
} | ||
|
||
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node | ||
uint32_t time_diff = millis() - button_time_stamp; | ||
if (button_state && time_diff > decommissioningTimeout) { | ||
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); | ||
Matter.decommission();< 6D40 /span> | ||
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so | ||
} | ||
|
||
delay(500); | ||
} |
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
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
Oops, something went wrong.
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.