33
33
34
34
#include " Zigbee.h"
35
35
36
+ /* Zigbee switch configuration */
36
37
#define SWITCH_ENDPOINT_NUMBER 5
37
38
38
- /* Switch configuration */
39
- #define GPIO_INPUT_IO_TOGGLE_SWITCH 9
39
+ #define GPIO_INPUT_IO_TOGGLE_SWITCH BOOT_PIN
40
40
#define PAIR_SIZE (TYPE_STR_PAIR ) (sizeof (TYPE_STR_PAIR) / sizeof (TYPE_STR_PAIR[0 ]))
41
41
42
42
typedef enum {
@@ -70,6 +70,7 @@ ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
70
70
static void onZbButton (SwitchData *button_func_pair) {
71
71
if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
72
72
// Send toggle command to the light
73
+ Serial.println (" Toggling light" );
73
74
zbSwitch.lightToggle ();
74
75
}
75
76
}
@@ -93,11 +94,7 @@ static void enableGpioInterrupt(bool enabled) {
93
94
94
95
/* ******************** Arduino functions **************************/
95
96
void setup () {
96
-
97
97
Serial.begin (115200 );
98
- while (!Serial) {
99
- delay (10 );
100
- }
101
98
102
99
// Optional: set Zigbee device name and model
103
100
zbSwitch.setManufacturerAndModel (" Espressif" , " ZigbeeSwitch" );
@@ -106,7 +103,7 @@ void setup() {
106
103
zbSwitch.allowMultipleBinding (true );
107
104
108
105
// Add endpoint to Zigbee Core
109
- log_d (" Adding ZigbeeSwitch endpoint to Zigbee Core" );
106
+ Serial. println (" Adding ZigbeeSwitch endpoint to Zigbee Core" );
110
107
Zigbee.addEndpoint (&zbSwitch);
111
108
112
109
// Open network for 180 seconds after boot
@@ -118,34 +115,36 @@ void setup() {
118
115
/* create a queue to handle gpio event from isr */
119
116
gpio_evt_queue = xQueueCreate (10 , sizeof (SwitchData));
120
117
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 ( );
123
120
}
124
121
attachInterruptArg (buttonFunctionPair[i].pin , onGpioInterrupt, (void *)(buttonFunctionPair + i), FALLING);
125
122
}
126
123
127
124
// 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
+ }
130
130
131
131
Serial.println (" Waiting for Light to bound to the switch" );
132
132
// Wait for switch to bound to a light:
133
- while (!zbSwitch.isBound ()) {
133
+ while (!zbSwitch.bound ()) {
134
134
Serial.printf (" ." );
135
135
delay (500 );
136
136
}
137
137
138
- // Optional: read manufacturer and model name from the bound light
138
+ // Optional: List all bound devices and read manufacturer and model name
139
139
std::list<zb_device_params_t *> boundLights = zbSwitch.getBoundDevices ();
140
- // List all bound lights
141
140
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 );
143
142
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 ]
146
145
);
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 ));
149
148
}
150
149
151
150
Serial.println ();
@@ -188,6 +187,6 @@ void loop() {
188
187
static uint32_t lastPrint = 0 ;
189
188
if (millis () - lastPrint > 10000 ) {
190
189
lastPrint = millis ();
191
- zbSwitch.printBoundDevices ();
190
+ zbSwitch.printBoundDevices (Serial );
192
191
}
193
192
}
0 commit comments