Skip to content

Commit cc3800d

Browse files
authored
Update Zigbee_On_Off_Switch.ino
1 parent a45b0a4 commit cc3800d

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333

3434
#include "Zigbee.h"
3535

36+
/* Zigbee switch configuration */
3637
#define SWITCH_ENDPOINT_NUMBER 5
3738

38-
/* Switch configuration */
39-
#define GPIO_INPUT_IO_TOGGLE_SWITCH 9
39+
#define GPIO_INPUT_IO_TOGGLE_SWITCH BOOT_PIN
4040
#define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0]))
4141

4242
typedef enum {
@@ -70,6 +70,7 @@ ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
7070
static void onZbButton(SwitchData *button_func_pair) {
7171
if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
7272
// Send toggle command to the light
73+
Serial.println("Toggling light");
7374
zbSwitch.lightToggle();
7475
}
7576
}
@@ -93,11 +94,7 @@ static void enableGpioInterrupt(bool enabled) {
9394

9495
/********************* Arduino functions **************************/
9596
void setup() {
96-
9797
Serial.begin(115200);
98-
while (!Serial) {
99-
delay(10);
100-
}
10198

10299
//Optional: set Zigbee device name and model
103100
zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch");
@@ -106,7 +103,7 @@ void setup() {
106103
zbSwitch.allowMultipleBinding(true);
107104

108105
//Add endpoint to Zigbee Core
109-
log_d("Adding ZigbeeSwitch endpoint to Zigbee Core");
106+
Serial.println("Adding ZigbeeSwitch endpoint to Zigbee Core");
110107
Zigbee.addEndpoint(&zbSwitch);
111108

112109
//Open network for 180 seconds after boot
@@ -118,34 +115,36 @@ void setup() {
118115
/* create a queue to handle gpio event from isr */
119116
gpio_evt_queue = xQueueCreate(10, sizeof(SwitchData));
120117
if (gpio_evt_queue == 0) {
121-
log_e("Queue was not created and must not be used");
122-
while (1);
118+
Serial.println("Queue creating failed, rebooting...");
119+
ESP.restart();
123120
}
124121
attachInterruptArg(buttonFunctionPair[i].pin, onGpioInterrupt, (void *)(buttonFunctionPair + i), FALLING);
125122
}
126123

127124
// When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode
128-
log_d("Calling Zigbee.begin()");
129-
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+
}
130130

131131
Serial.println("Waiting for Light to bound to the switch");
132132
//Wait for switch to bound to a light:
133-
while (!zbSwitch.isBound()) {
133+
while (!zbSwitch.bound()) {
134134
Serial.printf(".");
135135
delay(500);
136136
}
137137

138-
// Optional: read manufacturer and model name from the bound light
138+
// Optional: List all bound devices and read manufacturer and model name
139139
std::list<zb_device_params_t *> boundLights = zbSwitch.getBoundDevices();
140-
//List all bound lights
141140
for (const auto &device : boundLights) {
142-
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);
143142
Serial.printf(
144-
"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],
145-
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]
146145
);
147-
Serial.printf("Light manufacturer: %s", zbSwitch.readManufacturer(device->endpoint, device->short_addr));
148-
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));
149148
}
150149

151150
Serial.println();
@@ -188,6 +187,6 @@ void loop() {
188187
static uint32_t lastPrint = 0;
189188
if (millis() - lastPrint > 10000) {
190189
lastPrint = millis();
191-
zbSwitch.printBoundDevices();
190+
zbSwitch.printBoundDevices(Serial);
192191
}
193192
}

0 commit comments

Comments
 (0)