32
32
33
33
#include " Zigbee.h"
34
34
35
- #define LED_PIN RGB_BUILTIN
36
- #define BUTTON_PIN 9 // ESP32-C6/H2 Boot button
35
+ /* Zigbee light bulb configuration */
37
36
#define ZIGBEE_LIGHT_ENDPOINT 10
37
+ uint8_t led = RGB_BUILTIN;
38
+ uint8_t button = BOOT_PIN;
38
39
39
40
ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);
40
41
41
42
/* ******************** RGB LED functions **************************/
42
43
void setLED (bool value) {
43
- digitalWrite (LED_PIN , value);
44
+ digitalWrite (led , value);
44
45
}
45
46
46
47
/* ******************** Arduino functions **************************/
47
48
void setup () {
49
+ Serial.begin (115200 );
50
+
48
51
// 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);
51
54
52
55
// Init button for factory reset
53
- pinMode (BUTTON_PIN , INPUT_PULLUP);
56
+ pinMode (button , INPUT_PULLUP);
54
57
55
58
// Optional: set Zigbee device name and model
56
59
zbLight.setManufacturerAndModel (" Espressif" , " ZBLightBulb" );
@@ -59,28 +62,40 @@ void setup() {
59
62
zbLight.onLightChange (setLED);
60
63
61
64
// Add endpoint to Zigbee Core
62
- log_d (" Adding ZigbeeLight endpoint to Zigbee Core" );
65
+ Serial. println (" Adding ZigbeeLight endpoint to Zigbee Core" );
63
66
Zigbee.addEndpoint (&zbLight);
64
67
65
68
// 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 ();
68
80
}
69
81
70
82
void loop () {
71
83
// Checking button for factory reset
72
- if (digitalRead (BUTTON_PIN ) == LOW) { // Push button pressed
84
+ if (digitalRead (button ) == LOW) { // Push button pressed
73
85
// Key debounce handling
74
86
delay (100 );
75
87
int startTime = millis ();
76
- while (digitalRead (BUTTON_PIN ) == LOW) {
88
+ while (digitalRead (button ) == LOW) {
77
89
delay (50 );
78
90
if ((millis () - startTime) > 3000 ) {
79
91
// 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 );
81
94
Zigbee.factoryReset ();
82
95
}
83
96
}
97
+ // Toggle light by pressing the button
98
+ zbLight.setLight (!zbLight.getLightState ());
84
99
}
85
100
delay (100 );
86
101
}
0 commit comments