3232
3333#include " Zigbee.h"
3434
35- #define LED_PIN RGB_BUILTIN
36- #define BUTTON_PIN 9 // ESP32-C6/H2 Boot button
35+ /* Zigbee light bulb configuration */
3736#define ZIGBEE_LIGHT_ENDPOINT 10
37+ uint8_t led = RGB_BUILTIN;
38+ uint8_t button = BOOT_PIN;
3839
3940ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);
4041
4142/* ******************** RGB LED functions **************************/
4243void setLED (bool value) {
43- digitalWrite (LED_PIN , value);
44+ digitalWrite (led , value);
4445}
4546
4647/* ******************** Arduino functions **************************/
4748void setup () {
49+ Serial.begin (115200 );
50+
4851 // Init LED and turn it OFF (if LED_PIN == RGB_BUILTIN, the rgbLedWrite() will be used under the hood)
49- pinMode (LED_PIN , OUTPUT);
50- digitalWrite (LED_PIN , LOW);
52+ pinMode (led , OUTPUT);
53+ digitalWrite (led , LOW);
5154
5255 // Init button for factory reset
53- pinMode (BUTTON_PIN , INPUT_PULLUP);
56+ pinMode (button , INPUT_PULLUP);
5457
5558 // Optional: set Zigbee device name and model
5659 zbLight.setManufacturerAndModel (" Espressif" , " ZBLightBulb" );
@@ -59,28 +62,40 @@ void setup() {
5962 zbLight.onLightChange (setLED);
6063
6164 // Add endpoint to Zigbee Core
62- log_d (" Adding ZigbeeLight endpoint to Zigbee Core" );
65+ Serial. println (" Adding ZigbeeLight endpoint to Zigbee Core" );
6366 Zigbee.addEndpoint (&zbLight);
6467
6568 // When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
66- log_d (" Calling Zigbee.begin()" );
67- Zigbee.begin ();
69+ if (!Zigbee.begin ()) {
70+ Serial.println (" Zigbee failed to start!" );
71+ Serial.println (" Rebooting..." );
72+ ESP.restart ();
73+ }
74+ Serial.println (" Connecting to network" );
75+ while (!Zigbee.connected ()) {
76+ Serial.print (" ." );
77+ delay (100 );
78+ }
79+ Serial.println ();
6880}
6981
7082void loop () {
7183 // Checking button for factory reset
72- if (digitalRead (BUTTON_PIN ) == LOW) { // Push button pressed
84+ if (digitalRead (button ) == LOW) { // Push button pressed
7385 // Key debounce handling
7486 delay (100 );
7587 int startTime = millis ();
76- while (digitalRead (BUTTON_PIN ) == LOW) {
88+ while (digitalRead (button ) == LOW) {
7789 delay (50 );
7890 if ((millis () - startTime) > 3000 ) {
7991 // If key pressed for more than 3secs, factory reset Zigbee and reboot
80- Serial.printf (" Resetting Zigbee to factory settings, reboot.\n " );
92+ Serial.println (" Resetting Zigbee to factory and rebooting in 1s." );
93+ delay (1000 );
8194 Zigbee.factoryReset ();
8295 }
8396 }
97+ // Toggle light by pressing the button
98+ zbLight.setLight (!zbLight.getLightState ());
8499 }
85100 delay (100 );
86101}
0 commit comments