32
32
33
33
#include " Zigbee.h"
34
34
35
+ #define USE_GLOBAL_ON_RESPONSE_CALLBACK 1 // Set to 0 to use local callback specified directly for the endpoint.
36
+
35
37
/* Zigbee temperature + humidity sensor configuration */
36
38
#define TEMP_SENSOR_ENDPOINT_NUMBER 10
37
39
@@ -46,6 +48,31 @@ ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);
46
48
uint8_t dataToSend = 2 ; // Temperature and humidity values are reported in same endpoint, so 2 values are reported
47
49
bool resend = false ;
48
50
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
+
49
76
/* *********************** Temp sensor *****************************/
50
77
void meausureAndSleep () {
51
78
// Measure temperature sensor value
@@ -85,16 +112,6 @@ void meausureAndSleep() {
85
112
esp_deep_sleep_start ();
86
113
}
87
114
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
- }
98
115
/* ******************** Arduino functions **************************/
99
116
void setup () {
100
117
Serial.begin (115200 );
@@ -121,8 +138,15 @@ void setup() {
121
138
// Add humidity cluster to the temperature sensor device with min, max and tolerance values
122
139
zbTempSensor.addHumiditySensor (0 , 100 , 1 );
123
140
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
125
148
zbTempSensor.onDefaultResponse (onResponse);
149
+ #endif
126
150
127
151
// Add endpoint to Zigbee Core
128
152
Zigbee.addEndpoint (&zbTempSensor);
0 commit comments