Update Zigbee_On_Off_Switch.ino

This commit is contained in:
Jason2866
2024-12-16 21:48:40 +01:00
committed by GitHub
parent 817a1d5881
commit 36a9b3c1f8
@@ -31,13 +31,12 @@
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" #error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
#endif #endif
#include "ZigbeeCore.h" #include "Zigbee.h"
#include "ep/ZigbeeSwitch.h"
/* Zigbee switch configuration */
#define SWITCH_ENDPOINT_NUMBER 5 #define SWITCH_ENDPOINT_NUMBER 5
/* Switch configuration */ #define GPIO_INPUT_IO_TOGGLE_SWITCH BOOT_PIN
#define GPIO_INPUT_IO_TOGGLE_SWITCH 9
#define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0])) #define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0]))
typedef enum { typedef enum {
@@ -71,6 +70,7 @@ ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
static void onZbButton(SwitchData *button_func_pair) { static void onZbButton(SwitchData *button_func_pair) {
if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) { if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
// Send toggle command to the light // Send toggle command to the light
Serial.println("Toggling light");
zbSwitch.lightToggle(); zbSwitch.lightToggle();
} }
} }
@@ -94,11 +94,7 @@ static void enableGpioInterrupt(bool enabled) {
/********************* Arduino functions **************************/ /********************* Arduino functions **************************/
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
while (!Serial) {
delay(10);
}
//Optional: set Zigbee device name and model //Optional: set Zigbee device name and model
zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch"); zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch");
@@ -107,7 +103,7 @@ void setup() {
zbSwitch.allowMultipleBinding(true); zbSwitch.allowMultipleBinding(true);
//Add endpoint to Zigbee Core //Add endpoint to Zigbee Core
log_d("Adding ZigbeeSwitch endpoint to Zigbee Core"); Serial.println("Adding ZigbeeSwitch endpoint to Zigbee Core");
Zigbee.addEndpoint(&zbSwitch); Zigbee.addEndpoint(&zbSwitch);
//Open network for 180 seconds after boot //Open network for 180 seconds after boot
@@ -119,34 +115,36 @@ void setup() {
/* create a queue to handle gpio event from isr */ /* create a queue to handle gpio event from isr */
gpio_evt_queue = xQueueCreate(10, sizeof(SwitchData)); gpio_evt_queue = xQueueCreate(10, sizeof(SwitchData));
if (gpio_evt_queue == 0) { if (gpio_evt_queue == 0) {
log_e("Queue was not created and must not be used"); Serial.println("Queue creating failed, rebooting...");
while (1); ESP.restart();
} }
attachInterruptArg(buttonFunctionPair[i].pin, onGpioInterrupt, (void *)(buttonFunctionPair + i), FALLING); attachInterruptArg(buttonFunctionPair[i].pin, onGpioInterrupt, (void *)(buttonFunctionPair + i), FALLING);
} }
// When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode // When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode
log_d("Calling Zigbee.begin()"); if (!Zigbee.begin(ZIGBEE_COORDINATOR)) {
Zigbee.begin(ZIGBEE_COORDINATOR); Serial.println("Zigbee failed to start!");
Serial.println("Rebooting...");
ESP.restart();
}
Serial.println("Waiting for Light to bound to the switch"); Serial.println("Waiting for Light to bound to the switch");
//Wait for switch to bound to a light: //Wait for switch to bound to a light:
while (!zbSwitch.isBound()) { while (!zbSwitch.bound()) {
Serial.printf("."); Serial.printf(".");
delay(500); delay(500);
} }
// Optional: read manufacturer and model name from the bound light // Optional: List all bound devices and read manufacturer and model name
std::list<zb_device_params_t *> boundLights = zbSwitch.getBoundDevices(); std::list<zb_device_params_t *> boundLights = zbSwitch.getBoundDevices();
//List all bound lights
for (const auto &device : boundLights) { for (const auto &device : boundLights) {
Serial.printf("Device on endpoint %d, short address: 0x%x\n", device->endpoint, device->short_addr); Serial.printf("Device on endpoint %d, short address: 0x%x\r\n", device->endpoint, device->short_addr);
Serial.printf( Serial.printf(
"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], "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],
device->ieee_addr[4], device->ieee_addr[5], device->ieee_addr[6], device->ieee_addr[7] device->ieee_addr[3], device->ieee_addr[2], device->ieee_addr[1], device->ieee_addr[0]
); );
Serial.printf("Light manufacturer: %s", zbSwitch.readManufacturer(device->endpoint, device->short_addr)); Serial.printf("Light manufacturer: %s\r\n", zbSwitch.readManufacturer(device->endpoint, device->short_addr, device->ieee_addr));
Serial.printf("Light model: %s", zbSwitch.readModel(device->endpoint, device->short_addr)); Serial.printf("Light model: %s\r\n", zbSwitch.readModel(device->endpoint, device->short_addr, device->ieee_addr));
} }
Serial.println(); Serial.println();
@@ -189,6 +187,6 @@ void loop() {
static uint32_t lastPrint = 0; static uint32_t lastPrint = 0;
if (millis() - lastPrint > 10000) { if (millis() - lastPrint > 10000) {
lastPrint = millis(); lastPrint = millis();
zbSwitch.printBoundDevices(); zbSwitch.printBoundDevices(Serial);
} }
} }