@@ -66,6 +66,8 @@ static SwitchData buttonFunctionPair[] = {{GPIO_INPUT_IO_TOGGLE_SWITCH, SWITCH_O
66
66
67
67
ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
68
68
69
+ static bool light_state = false ;
70
+
69
71
/* ******************** Zigbee functions **************************/
70
72
static void onZbButton (SwitchData *button_func_pair) {
71
73
if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
@@ -75,6 +77,33 @@ static void onZbButton(SwitchData *button_func_pair) {
75
77
}
76
78
}
77
79
80
+ static void onLightStateChange (bool state) {
81
+ if (state != light_state) {
82
+ light_state = state;
83
+ Serial.printf (" Light state changed to %d\r\n " , state);
84
+ }
85
+ }
86
+
87
+ /* ******************** Periodic task ***************************/
88
+ void periodicTask (void *arg) {
89
+ while (true ) {
90
+ // print the bound lights every 10 seconds
91
+ static uint32_t lastPrint = 0 ;
92
+ if (millis () - lastPrint > 10000 ) {
93
+ lastPrint = millis ();
94
+ zbSwitch.printBoundDevices (Serial);
95
+ }
96
+
97
+ // Poll light state every second
98
+ static uint32_t lastPoll = 0 ;
99
+ if (millis () - lastPoll > 1000 ) {
100
+ lastPoll = millis ();
101
+ zbSwitch.getLightState ();
102
+ }
103
+ vTaskDelay (1000 / portTICK_PERIOD_MS);
104
+ }
105
+ }
106
+
78
107
/* ******************** GPIO functions **************************/
79
108
static QueueHandle_t gpio_evt_queue = NULL ;
80
109
@@ -102,6 +131,8 @@ void setup() {
102
131
// Optional to allow multiple light to bind to the switch
103
132
zbSwitch.allowMultipleBinding (true );
104
133
134
+ zbSwitch.onLightStateChange (onLightStateChange);
135
+
105
136
// Add endpoint to Zigbee Core
106
137
Serial.println (" Adding ZigbeeSwitch endpoint to Zigbee Core" );
107
138
Zigbee.addEndpoint (&zbSwitch);
@@ -154,6 +185,8 @@ void setup() {
154
185
}
155
186
156
187
Serial.println ();
188
+
189
+ xTaskCreate (periodicTask, " periodicTask" , 1024 * 4 , NULL , 10 , NULL );
157
190
}
158
191
159
192
void loop () {
@@ -188,11 +221,4 @@ void loop() {
188
221
}
189
222
vTaskDelay (10 / portTICK_PERIOD_MS);
190
223
}
191
-
192
- // print the bound lights every 10 seconds
193
- static uint32_t lastPrint = 0 ;
194
- if (millis () - lastPrint > 10000 ) {
195
- lastPrint = millis ();
196
- zbSwitch.printBoundDevices (Serial);
197
- }
198
224
}
0 commit comments