Skip to content

Commit 36a9b3c

Browse files
authored
Update Zigbee_On_Off_Switch.ino
1 parent 817a1d5 commit 36a9b3c

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

examples/arduino-zigbee-switch/src/Zigbee_On_Off_Switch.ino

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
3232
#endif
3333

34-
#include "ZigbeeCore.h"
35-
#include "ep/ZigbeeSwitch.h"
34+
#include "Zigbee.h"
3635

36+
/* Zigbee switch configuration */
3737
#define SWITCH_ENDPOINT_NUMBER 5
3838

39-
/* Switch configuration */
40-
#define GPIO_INPUT_IO_TOGGLE_SWITCH 9
39+
#define GPIO_INPUT_IO_TOGGLE_SWITCH BOOT_PIN
4140
#define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0]))
4241

4342
typedef enum {
@@ -71,6 +70,7 @@ ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
7170
static void onZbButton(SwitchData *button_func_pair) {
7271
if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
7372
// Send toggle command to the light
73+
Serial.println("Toggling light");
7474
zbSwitch.lightToggle();
7575
}
7676
}
@@ -94,11 +94,7 @@ static void enableGpioInterrupt(bool enabled) {
9494

9595
/********************* Arduino functions **************************/
9696
void setup() {
97-
9897
Serial.begin(115200);
99-
while (!Serial) {
100-
delay(10);
101-
}
10298

10399
//Optional: set Zigbee device name and model
104100
zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch");
@@ -107,7 +103,7 @@ void setup() {
107103
zbSwitch.allowMultipleBinding(true);
108104

109105
//Add endpoint to Zigbee Core
110-
log_d("Adding ZigbeeSwitch endpoint to Zigbee Core");
106+
Serial.println("Adding ZigbeeSwitch endpoint to Zigbee Core");
111107
Zigbee.addEndpoint(&zbSwitch);
112108

113109
//Open network for 180 seconds after boot
@@ -119,34 +115,36 @@ void setup() {
119115
/* create a queue to handle gpio event from isr */
120116
gpio_evt_queue = xQueueCreate(10, sizeof(SwitchData));
121117
if (gpio_evt_queue == 0) {
122-
log_e("Queue was not created and must not be used");
123-
while (1);
118+
Serial.println("Queue creating failed, rebooting...");
119+
ESP.restart();
124120
}
125121
attachInterruptArg(buttonFunctionPair[i].pin, onGpioInterrupt, (void *)(buttonFunctionPair + i), FALLING);
126122
}
127123

128124
// When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode
129-
log_d("Calling Zigbee.begin()");
130-
Zigbee.begin(ZIGBEE_COORDINATOR);
125+
if (!Zigbee.begin(ZIGBEE_COORDINATOR)) {
126+
Serial.println("Zigbee failed to start!");
127+
Serial.println("Rebooting...");
128+
ESP.restart();
129+
}
131130

132131
Serial.println("Waiting for Light to bound to the switch");
133132
//Wait for switch to bound to a light:
134-
while (!zbSwitch.isBound()) {
133+
while (!zbSwitch.bound()) {
135134
Serial.printf(".");
136135
delay(500);
137136
}
138137

139-
// Optional: read manufacturer and model name from the bound light
138+
// Optional: List all bound devices and read manufacturer and model name
140139
std::list<zb_device_params_t *> boundLights = zbSwitch.getBoundDevices();
141-
//List all bound lights
142140
for (const auto &device : boundLights) {
143-
Serial.printf("Device on endpoint %d, short address: 0x%x\n", device->endpoint, device->short_addr);
141+
Serial.printf("Device on endpoint %d, short address: 0x%x\r\n", device->endpoint, device->short_addr);
144142
Serial.printf(
145-
"IEEE Address: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", device->ieee_addr[0], device->ieee_addr[1], device->ieee_addr[2], device->ieee_addr[3],
146-
device->ieee_addr[4], device->ieee_addr[5], device->ieee_addr[6], device->ieee_addr[7]
143+
"IEEE Address: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\r\n", device->ieee_addr[7], device->ieee_addr[6], device->ieee_addr[5], device->ieee_addr[4],
144+
device->ieee_addr[3], device->ieee_addr[2], device->ieee_addr[1], device->ieee_addr[0]
147145
);
148-
Serial.printf("Light manufacturer: %s", zbSwitch.readManufacturer(device->endpoint, device->short_addr));
149-
Serial.printf("Light model: %s", zbSwitch.readModel(device->endpoint, device->short_addr));
146+
Serial.printf("Light manufacturer: %s\r\n", zbSwitch.readManufacturer(device->endpoint, device->short_addr, device->ieee_addr));
147+
Serial.printf("Light model: %s\r\n", zbSwitch.readModel(device->endpoint, device->short_addr, device->ieee_addr));
150148
}
151149

152150
Serial.println();
@@ -189,6 +187,6 @@ void loop() {
189187
static uint32_t lastPrint = 0;
190188
if (millis() - lastPrint > 10000) {
191189
lastPrint = millis();
192-
zbSwitch.printBoundDevices();
190+
zbSwitch.printBoundDevices(Serial);
193191
}
194192
}

0 commit comments

Comments
 (0)