Description of Change
This pull request introduces improvements to theZigbeeThermostat EP, primarily by expanding support for humidity measurement and refactoring temperature configuration methods for clarity and consistency. The changes also ensure backward compatibility for deprecated methods.
Refactoring and Backward Compatibility
- Refactored temperature configuration and settings methods in
ZigbeeThermostat to useonTempConfigReceive andgetTemperatureSettings instead of the olderonConfigReceive andgetSensorSettings. Deprecated methods now act as aliases to the new ones to maintain backward compatibility. (libraries/Zigbee/src/ep/ZigbeeThermostat.h,libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino,libraries/Zigbee/keywords.txt)[1][2][3][4]
Humidity Measurement Feature
- Added comprehensive humidity measurement support to
ZigbeeThermostat, including new callback setters, getter and setter methods, configuration retrieval, and reporting functions. Also updated the keywords file to reflect these additions. (libraries/Zigbee/src/ep/ZigbeeThermostat.h,libraries/Zigbee/keywords.txt)[1][2]
Data Type Consistency
- Changed internal representation of humidity values from
int16_t touint16_t and updated conversion logic to multiply by 100 for precision, ensuring consistency with Zigbee cluster attribute requirements. (libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp)[1][2]
These changes make the codebase easier to maintain, extend functionality for humidity sensors, and ensure smooth migration for existing users of the temperature sensor API.
Test Scenarios
Tested using two ESP32-C6 with following examples:
Temp + Humidity sensor - ED:
// 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./** * @brief This example demonstrates Zigbee temperature and humidity sensor Sleepy device. * * The example demonstrates how to use Zigbee library to create an end device temperature and humidity sensor. * The sensor is a Zigbee end device, which is reporting data to the Zigbee network. * * Proper Zigbee mode must be selected in Tools->Zigbee mode * and also the correct partition scheme must be selected in Tools->Partition Scheme. * * Please check the README.md for instructions and more detailed description. * * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)*/#ifndef ZIGBEE_MODE_ED#error "Zigbee end device mode is not selected in Tools->Zigbee mode"#endif#include"Zigbee.h"/* Zigbee temperature + humidity sensor configuration*/#defineTEMP_SENSOR_ENDPOINT_NUMBER10uint8_t button = BOOT_PIN;ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);/********************* Arduino functions **************************/voidsetup() { Serial.begin(115200);// Init button switchpinMode(button, INPUT_PULLUP);// Optional: set Zigbee device name and model zbTempSensor.setManufacturerAndModel("Espressif","ZigbeeTempSensor");// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement) zbTempSensor.setMinMaxValue(10,50);// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C) zbTempSensor.setTolerance(1);// Add humidity cluster to the temperature sensor device with min, max and tolerance values//zbTempSensor.addHumiditySensor(0, 100, 1);// Add endpoint to Zigbee Core Zigbee.addEndpoint(&zbTempSensor);// When all EPs are registered, start Zigbee in End Device modeif (!Zigbee.begin()) { Serial.println("Zigbee failed to start!"); Serial.println("Rebooting..."); ESP.restart();// If Zigbee failed to start, reboot the device and try again } Serial.println("Connecting to network");while (!Zigbee.connected()) { Serial.print(".");delay(100); } Serial.println(); Serial.println("Successfully connected to Zigbee network");}voidloop() {// Checking button for factory resetif (digitalRead(button) == LOW) {// Push button pressed// Key debounce handlingdelay(100);int startTime =millis();while (digitalRead(button) == LOW) {delay(50);if ((millis() - startTime) >10000) {// If key pressed for more than 10secs, factory reset Zigbee and reboot Serial.println("Resetting Zigbee to factory and rebooting in 1s.");delay(1000);// Optional set reset in factoryReset to false, to not restart device after erasing nvram, but set it to endless sleep manually instead Zigbee.factoryReset(); } }// Report temperature and humidity values zbTempSensor.report();// reports temperature and humidity values (if humidity sensor is not added, only temperature is reported) Serial.printf("Reported temperature and humidity"); }// Meassure temperature sensor data every 2 secondsstaticuint32_t last_meassure =0;if (millis() - last_meassure >2000) { last_meassure =millis();// Measure temperature sensor valuefloat temperature =temperatureRead();// Use temperature value as humidity value to demonstrate both temperature and humidityfloat humidity = temperature;// Update temperature and humidity values in Temperature sensor EP zbTempSensor.setTemperature(temperature); zbTempSensor.setHumidity(humidity); Serial.printf("Meassured temperature: %.2f°C, Humidity: %.2f%%\r\n", temperature, humidity); }}Thermostat - Coordinator:
// 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./** * @brief This example demonstrates simple Zigbee thermostat. * * The example demonstrates how to use Zigbee library to get data from temperature * sensor end device and act as an thermostat. * The temperature sensor is a Zigbee end device, which is controlled by a Zigbee coordinator (thermostat). * * Proper Zigbee mode must be selected in Tools->Zigbee mode * and also the correct partition scheme must be selected in Tools->Partition Scheme. * * Please check the README.md for instructions and more detailed description. * * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)*/#ifndef ZIGBEE_MODE_ZCZR#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"#endif#include"Zigbee.h"/* Zigbee thermostat configuration*/#defineTHERMOSTAT_ENDPOINT_NUMBER1uint8_t button = BOOT_PIN;ZigbeeThermostat zbThermostat = ZigbeeThermostat(THERMOSTAT_ENDPOINT_NUMBER);// Save temperature sensor datafloat sensor_temp;float sensor_max_temp;float sensor_min_temp;float sensor_tolerance_temp;float sensor_hum;float sensor_max_hum;float sensor_min_hum;float sensor_tolerance_hum;/****************** Temperature sensor handling *******************/voidreceiveSensorTemp(float temperature) { Serial.printf("Temperature sensor value: %.2f°C\n", temperature); sensor_temp = temperature;}voidreceiveSensorHumidity(float humidity) { Serial.printf("Humidity sensor value: %.2f°C\n", humidity); sensor_hum = humidity;}voidreceiveTemperatureConfig(float min_temp,float max_temp,float tolerance) { Serial.printf("Temperature sensor config: min %.2f°C, max %.2f°C, tolerance %.2f°C\n", min_temp, max_temp, tolerance); sensor_min_temp = min_temp; sensor_max_temp = max_temp; sensor_tolerance_temp = tolerance;}voidreceiveHumidityConfig(float min_hum,float max_hum,float tolerance) { Serial.printf("Humidity sensor config: min %.2f°C, max %.2f°C, tolerance %.2f°C\n", min_hum, max_hum, tolerance); sensor_min_hum = min_hum; sensor_max_hum = max_hum; sensor_tolerance_hum = tolerance;}/********************* Arduino functions **************************/voidsetup() { Serial.begin(115200);// Init button switchpinMode(button, INPUT_PULLUP); zbThermostat.onTempReceive(receiveSensorTemp); zbThermostat.onHumidityReceive(receiveSensorHumidity);// Set callback function for receiving sensor configuration zbThermostat.onTempConfigReceive(receiveTemperatureConfig); zbThermostat.onHumidityConfigReceive(receiveHumidityConfig);//Optional: set Zigbee device name and model zbThermostat.setManufacturerAndModel("Espressif","ZigbeeThermostat");//Add endpoint to Zigbee Core Zigbee.addEndpoint(&zbThermostat);//Open network for 180 seconds after boot Zigbee.setRebootOpenNetwork(180);// When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR modeif (!Zigbee.begin(ZIGBEE_COORDINATOR)) { Serial.println("Zigbee failed to start!"); Serial.println("Rebooting..."); ESP.restart(); } Serial.println("Waiting for Temperature sensor to bound to the thermostat");while (!zbThermostat.bound()) { Serial.printf(".");delay(500); } Serial.println();// Get temperature sensor configuration for all bound sensors by endpoint number and address std::list<zb_device_params_t *> boundSensors = zbThermostat.getBoundDevices();for (constauto &device : boundSensors) { Serial.println("--------------------------------");if (device->short_addr ==0x0000 || device->short_addr ==0xFFFF) {//End devices never have 0x0000 short address or 0xFFFF group address Serial.printf("Device on endpoint %d, IEEE Address: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\r\n", device->endpoint, device->ieee_addr[7], device->ieee_addr[6], device->ieee_addr[5], device->ieee_addr[4], device->ieee_addr[3], device->ieee_addr[2], device->ieee_addr[1], device->ieee_addr[0] ); zbThermostat.getTemperatureSettings(device->endpoint, device->ieee_addr); zbThermostat.getHumiditySettings(device->endpoint, device->ieee_addr); }else { Serial.printf("Device on endpoint %d, short address: 0x%x\r\n", device->endpoint, device->short_addr); zbThermostat.getTemperatureSettings(device->endpoint, device->short_addr); zbThermostat.getHumiditySettings(device->endpoint, device->short_addr); } }}voidloop() {// Handle button switch in loop()if (digitalRead(button) == LOW) {// Push button pressed// Key debounce handlingwhile (digitalRead(button) == LOW) {delay(50); }// Set reporting interval for temperature sensor zbThermostat.setTemperatureReporting(0,10,2); zbThermostat.setHumidityReporting(0,10,2); zbThermostat.printBoundDevices(Serial); }// Print temperature sensor data each 10 secondsstaticuint32_t last_print =0;if (millis() - last_print >10000) { last_print =millis();int temp_percent = (int)((sensor_temp - sensor_min_temp) / (sensor_max_temp - sensor_min_temp) *100);int hum_percent = (int)((sensor_hum - sensor_min_hum) / (sensor_max_hum - sensor_min_hum) *100); Serial.printf("Loop temperature info: %.2f°C (%d %%)\n", sensor_temp, temp_percent); Serial.printf("Loop humidity info: %.2f°C (%d %%)\n", sensor_hum, hum_percent); }}Related links
Closes#11665
Uh oh!
There was an error while loading.Please reload this page.
Description of Change
This pull request introduces improvements to the
ZigbeeThermostatEP, primarily by expanding support for humidity measurement and refactoring temperature configuration methods for clarity and consistency. The changes also ensure backward compatibility for deprecated methods.Refactoring and Backward Compatibility
ZigbeeThermostatto useonTempConfigReceiveandgetTemperatureSettingsinstead of the olderonConfigReceiveandgetSensorSettings. Deprecated methods now act as aliases to the new ones to maintain backward compatibility. (libraries/Zigbee/src/ep/ZigbeeThermostat.h,libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino,libraries/Zigbee/keywords.txt)[1][2][3][4]Humidity Measurement Feature
ZigbeeThermostat, including new callback setters, getter and setter methods, configuration retrieval, and reporting functions. Also updated the keywords file to reflect these additions. (libraries/Zigbee/src/ep/ZigbeeThermostat.h,libraries/Zigbee/keywords.txt)[1][2]Data Type Consistency
int16_ttouint16_tand updated conversion logic to multiply by 100 for precision, ensuring consistency with Zigbee cluster attribute requirements. (libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp)[1][2]These changes make the codebase easier to maintain, extend functionality for humidity sensors, and ensure smooth migration for existing users of the temperature sensor API.
Test Scenarios
Tested using two ESP32-C6 with following examples:
Temp + Humidity sensor - ED:
Thermostat - Coordinator:
Related links
Closes#11665