3030#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
3131#endif
3232
33- #include " ZigbeeCore.h"
34- #include " ep/ZigbeeLight.h"
33+ #include " Zigbee.h"
3534
36- #define LED_PIN RGB_BUILTIN
37- #define BUTTON_PIN 9 // ESP32-C6/H2 Boot button
35+ /* Zigbee light bulb configuration */
3836#define ZIGBEE_LIGHT_ENDPOINT 10
37+ uint8_t led = RGB_BUILTIN;
38+ uint8_t button = BOOT_PIN;
3939
4040ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);
4141
4242/* ******************** RGB LED functions **************************/
4343void setLED (bool value) {
44- digitalWrite (LED_PIN , value);
44+ digitalWrite (led , value);
4545}
4646
4747/* ******************** Arduino functions **************************/
4848void setup () {
49+ Serial.begin (115200 );
50+
4951 // Init LED and turn it OFF (if LED_PIN == RGB_BUILTIN, the rgbLedWrite() will be used under the hood)
50- pinMode (LED_PIN , OUTPUT);
51- digitalWrite (LED_PIN , LOW);
52+ pinMode (led , OUTPUT);
53+ digitalWrite (led , LOW);
5254
5355 // Init button for factory reset
54- pinMode (BUTTON_PIN, INPUT );
56+ pinMode (button, INPUT_PULLUP );
5557
5658 // Optional: set Zigbee device name and model
5759 zbLight.setManufacturerAndModel (" Espressif" , " ZBLightBulb" );
@@ -60,28 +62,40 @@ void setup() {
6062 zbLight.onLightChange (setLED);
6163
6264 // Add endpoint to Zigbee Core
63- log_d (" Adding ZigbeeLight endpoint to Zigbee Core" );
65+ Serial. println (" Adding ZigbeeLight endpoint to Zigbee Core" );
6466 Zigbee.addEndpoint (&zbLight);
6567
6668 // When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
67- log_d (" Calling Zigbee.begin()" );
68- 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 ();
6980}
7081
7182void loop () {
7283 // Checking button for factory reset
73- if (digitalRead (BUTTON_PIN ) == LOW) { // Push button pressed
84+ if (digitalRead (button ) == LOW) { // Push button pressed
7485 // Key debounce handling
7586 delay (100 );
7687 int startTime = millis ();
77- while (digitalRead (BUTTON_PIN ) == LOW) {
88+ while (digitalRead (button ) == LOW) {
7889 delay (50 );
7990 if ((millis () - startTime) > 3000 ) {
8091 // If key pressed for more than 3secs, factory reset Zigbee and reboot
81- Serial.printf (" Resetting Zigbee to factory settings, reboot.\n " );
92+ Serial.println (" Resetting Zigbee to factory and rebooting in 1s." );
93+ delay (1000 );
8294 Zigbee.factoryReset ();
8395 }
8496 }
97+ // Toggle light by pressing the button
98+ zbLight.setLight (!zbLight.getLightState ());
8599 }
86100 delay (100 );
87101}
0 commit comments