3434#include " Zigbee.h"
3535
3636/* Zigbee thermostat configuration */
37- #define THERMOSTAT_ENDPOINT_NUMBER 5
37+ #define THERMOSTAT_ENDPOINT_NUMBER 1
38+ #define USE_RECEIVE_TEMP_WITH_SOURCE 1
3839uint8_t button = BOOT_PIN;
3940
4041ZigbeeThermostat zbThermostat = ZigbeeThermostat(THERMOSTAT_ENDPOINT_NUMBER);
@@ -48,13 +49,28 @@ float sensor_tolerance;
4849struct tm timeinfo = {}; // Time structure for Time cluster
4950
5051/* ***************** Temperature sensor handling *******************/
51- void recieveSensorTemp (float temperature) {
52+ #if USE_RECEIVE_TEMP_WITH_SOURCE == 0
53+ void receiveSensorTemp (float temperature) {
5254 Serial.printf (" Temperature sensor value: %.2f°C\n " , temperature);
5355 sensor_temp = temperature;
5456}
57+ #else
58+ void receiveSensorTempWithSource (float temperature, uint8_t src_endpoint, esp_zb_zcl_addr_t src_address) {
59+ if (src_address.addr_type == ESP_ZB_ZCL_ADDR_TYPE_SHORT) {
60+ Serial.printf (" Temperature sensor value: %.2f°C from endpoint %d, address 0x%04x\n " , temperature, src_endpoint, src_address.u .short_addr );
61+ } else {
62+ Serial.printf (
63+ " Temperature sensor value: %.2f°C from endpoint %d, address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n " , temperature, src_endpoint,
64+ src_address.u .ieee_addr [7 ], src_address.u .ieee_addr [6 ], src_address.u .ieee_addr [5 ], src_address.u .ieee_addr [4 ], src_address.u .ieee_addr [3 ],
65+ src_address.u .ieee_addr [2 ], src_address.u .ieee_addr [1 ], src_address.u .ieee_addr [0 ]
66+ );
67+ }
68+ sensor_temp = temperature;
69+ }
70+ #endif
5571
56- void recieveSensorConfig (float min_temp, float max_temp, float tolerance) {
57- Serial.printf (" Temperature sensor settings : min %.2f°C, max %.2f°C, tolerance %.2f°C\n " , min_temp, max_temp, tolerance);
72+ void receiveSensorConfig (float min_temp, float max_temp, float tolerance) {
73+ Serial.printf (" Temperature sensor config : min %.2f°C, max %.2f°C, tolerance %.2f°C\n " , min_temp, max_temp, tolerance);
5874 sensor_min_temp = min_temp;
5975 sensor_max_temp = max_temp;
6076 sensor_tolerance = tolerance;
@@ -66,9 +82,15 @@ void setup() {
6682 // Init button switch
6783 pinMode (button, INPUT_PULLUP);
6884
69- // Set callback functions for temperature and configuration receive
70- zbThermostat.onTempRecieve (recieveSensorTemp);
71- zbThermostat.onConfigRecieve (recieveSensorConfig);
85+ // Set callback function for receiving temperature from sensor - Use only one option
86+ #if USE_RECEIVE_TEMP_WITH_SOURCE == 0
87+ zbThermostat.onTempReceive (receiveSensorTemp); // If you bound only one sensor or you don't need to know the source of the temperature
88+ #else
89+ zbThermostat.onTempReceiveWithSource (receiveSensorTempWithSource);
90+ #endif
91+
92+ // Set callback function for receiving sensor configuration
93+ zbThermostat.onConfigReceive (receiveSensorConfig);
7294
7395 // Optional: set Zigbee device name and model
7496 zbThermostat.setManufacturerAndModel (" Espressif" , " ZigbeeThermostat" );
@@ -107,19 +129,30 @@ void setup() {
107129
108130 Serial.println ();
109131
110- // Get temperature sensor configuration
111- zbThermostat.getSensorSettings ();
132+ // Get temperature sensor configuration for all bound sensors by endpoint number and address
133+ std::list<zb_device_params_t *> boundSensors = zbThermostat.getBoundDevices ();
134+ for (const auto &device : boundSensors) {
135+ Serial.println (" --------------------------------" );
136+ if (device->short_addr == 0x0000 || device->short_addr == 0xFFFF ) { // End devices never have 0x0000 short address or 0xFFFF group address
137+ Serial.printf (
138+ " 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 ],
139+ device->ieee_addr [5 ], device->ieee_addr [4 ], device->ieee_addr [3 ], device->ieee_addr [2 ], device->ieee_addr [1 ], device->ieee_addr [0 ]
140+ );
141+ zbThermostat.getSensorSettings (device->endpoint , device->ieee_addr );
142+ } else {
143+ Serial.printf (" Device on endpoint %d, short address: 0x%x\r\n " , device->endpoint , device->short_addr );
144+ zbThermostat.getSensorSettings (device->endpoint , device->short_addr );
145+ }
146+ }
112147}
113148
114149void loop () {
115150 // Handle button switch in loop()
116151 if (digitalRead (button) == LOW) { // Push button pressed
117-
118152 // Key debounce handling
119153 while (digitalRead (button) == LOW) {
120154 delay (50 );
121155 }
122-
123156 // Set reporting interval for temperature sensor
124157 zbThermostat.setTemperatureReporting (0 , 10 , 2 );
125158 }
@@ -130,5 +163,6 @@ void loop() {
130163 last_print = millis ();
131164 int temp_percent = (int )((sensor_temp - sensor_min_temp) / (sensor_max_temp - sensor_min_temp) * 100 );
132165 Serial.printf (" Loop temperature info: %.2f°C (%d %%)\n " , sensor_temp, temp_percent);
166+ zbThermostat.printBoundDevices (Serial);
133167 }
134168}
0 commit comments