3232
3333#include " Zigbee.h"
3434
35+ #define USE_GLOBAL_ON_RESPONSE_CALLBACK 1 // Set to 0 to use local callback specified directly for the endpoint.
36+
3537/* Zigbee temperature + humidity sensor configuration */
3638#define TEMP_SENSOR_ENDPOINT_NUMBER 10
3739
@@ -46,6 +48,31 @@ ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);
4648uint8_t dataToSend = 2 ; // Temperature and humidity values are reported in same endpoint, so 2 values are reported
4749bool resend = false ;
4850
51+ /* *********************** Callbacks *****************************/
52+ #if USE_GLOBAL_ON_RESPONSE_CALLBACK
53+ void onGlobalResponse (zb_cmd_type_t command, esp_zb_zcl_status_t status, uint8_t endpoint, uint16_t cluster){
54+ Serial.printf (" Global response command: %d, status: %s, endpoint: %d, cluster: 0x%04x\r\n " , command, esp_zb_zcl_status_to_name (status), endpoint, cluster);
55+ if ((command == ZB_CMD_REPORT_ATTRIBUTE) && (endpoint == TEMP_SENSOR_ENDPOINT_NUMBER)){
56+ switch (status){
57+ case ESP_ZB_ZCL_STATUS_SUCCESS: dataToSend--; break ;
58+ case ESP_ZB_ZCL_STATUS_FAIL: resend = true ; break ;
59+ default : break ;// add more statuses like ESP_ZB_ZCL_STATUS_INVALID_VALUE, ESP_ZB_ZCL_STATUS_TIMEOUT etc.
60+ }
61+ }
62+ }
63+ #else
64+ void onResponse (zb_cmd_type_t command, esp_zb_zcl_status_t status){
65+ Serial.printf (" Response command: %d, status: %s\r\n " , command, esp_zb_zcl_status_to_name (status));
66+ if (command == ZB_CMD_REPORT_ATTRIBUTE){
67+ switch (status){
68+ case ESP_ZB_ZCL_STATUS_SUCCESS: dataToSend--; break ;
69+ case ESP_ZB_ZCL_STATUS_FAIL: resend = true ; break ;
70+ default : break ;// add more statuses like ESP_ZB_ZCL_STATUS_INVALID_VALUE, ESP_ZB_ZCL_STATUS_TIMEOUT etc.
71+ }
72+ }
73+ }
74+ #endif
75+
4976/* *********************** Temp sensor *****************************/
5077void meausureAndSleep () {
5178 // Measure temperature sensor value
@@ -85,16 +112,6 @@ void meausureAndSleep() {
85112 esp_deep_sleep_start ();
86113}
87114
88- void onResponse (zb_cmd_type_t command, esp_zb_zcl_status_t status){
89- Serial.printf (" Response status received %s\r\n " , zbTempSensor.esp_zb_zcl_status_to_name (status));
90- if (command == ZB_CMD_REPORT_ATTRIBUTE){
91- switch (status){
92- case ESP_ZB_ZCL_STATUS_SUCCESS: dataToSend--; break ;
93- case ESP_ZB_ZCL_STATUS_FAIL: resend = true ; break ;
94- default : break ;// add more statuses like ESP_ZB_ZCL_STATUS_INVALID_VALUE, ESP_ZB_ZCL_STATUS_TIMEOUT etc.
95- }
96- }
97- }
98115/* ******************** Arduino functions **************************/
99116void setup () {
100117 Serial.begin (115200 );
@@ -121,8 +138,15 @@ void setup() {
121138 // Add humidity cluster to the temperature sensor device with min, max and tolerance values
122139 zbTempSensor.addHumiditySensor (0 , 100 , 1 );
123140
124- // Set callback for default response to handle status of reported data
141+ // Set callback for default response to handle status of reported data, there are 2 options.
142+
143+ #if USE_GLOBAL_ON_RESPONSE_CALLBACK
144+ // Global callback for all endpoints with more params to determine the endpoint and cluster in the callback function.
145+ Zigbee.onGlobalDefaultResponse (onGlobalResponse);
146+ #else
147+ // Callback specified for endpoint
125148 zbTempSensor.onDefaultResponse (onResponse);
149+ #endif
126150
127151 // Add endpoint to Zigbee Core
128152 Zigbee.addEndpoint (&zbTempSensor);
0 commit comments