Skip to content

Commit 4939690

Browse files
committed
feat(zigbee): Extend swithc with get attribute methods
1 parent 9d44061 commit 4939690

File tree

5 files changed

+526
-8
lines changed

5 files changed

+526
-8
lines changed

libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ static SwitchData buttonFunctionPair[] = {{GPIO_INPUT_IO_TOGGLE_SWITCH, SWITCH_O
6666

6767
ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
6868

69+
static bool light_state = false;
70+
6971
/********************* Zigbee functions **************************/
7072
static void onZbButton(SwitchData *button_func_pair) {
7173
if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
@@ -75,6 +77,33 @@ static void onZbButton(SwitchData *button_func_pair) {
7577
}
7678
}
7779

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+
78107
/********************* GPIO functions **************************/
79108
static QueueHandle_t gpio_evt_queue = NULL;
80109

@@ -102,6 +131,8 @@ void setup() {
102131
//Optional to allow multiple light to bind to the switch
103132
zbSwitch.allowMultipleBinding(true);
104133

134+
zbSwitch.onLightStateChange(onLightStateChange);
135+
105136
//Add endpoint to Zigbee Core
106137
Serial.println("Adding ZigbeeSwitch endpoint to Zigbee Core");
107138
Zigbee.addEndpoint(&zbSwitch);
@@ -154,6 +185,8 @@ void setup() {
154185
}
155186

156187
Serial.println();
188+
189+
xTaskCreate(periodicTask, "periodicTask", 1024 * 4, NULL, 10, NULL);
157190
}
158191

159192
void loop() {
@@ -188,11 +221,4 @@ void loop() {
188221
}
189222
vTaskDelay(10 / portTICK_PERIOD_MS);
190223
}
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-
}
198224
}

0 commit comments

Comments
 (0)