3333
3434#include " Zigbee.h"
3535
36- #define LED_PIN RGB_BUILTIN
37- #define BUTTON_PIN 9 // C6/H2 Boot button
36+ /* Zigbee dimmable light configuration */
3837#define ZIGBEE_LIGHT_ENDPOINT 10
38+ uint8_t led = RGB_BUILTIN;
39+ uint8_t button = BOOT_PIN;
3940
4041ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
4142
42- /* ******************** LED functions **************************/
43+ /* ******************** RGB LED functions **************************/
4344void setLight (bool state, uint8_t level)
4445{
45- rgbLedWrite (LED_PIN, level, level, level);
46+ if (!state)
47+ {
48+ rgbLedWrite (led, 0 , 0 , 0 );
49+ return ;
50+ }
51+ rgbLedWrite (led, level, level, level);
4652}
4753
4854// Create a task on identify call to handle the identify function
@@ -56,18 +62,20 @@ void identify(uint16_t time)
5662 zbDimmableLight.restoreLight ();
5763 return ;
5864 }
59- rgbLedWrite (LED_PIN , 255 * blink, 255 * blink, 255 * blink);
65+ rgbLedWrite (led , 255 * blink, 255 * blink, 255 * blink);
6066 blink = !blink;
6167}
6268
6369/* ******************** Arduino functions **************************/
6470void setup ()
6571{
72+ Serial.begin (115200 );
73+
6674 // Init RMT and leave light OFF
67- rgbLedWrite (LED_PIN , 0 , 0 , 0 );
75+ rgbLedWrite (led , 0 , 0 , 0 );
6876
6977 // Init button for factory reset
70- pinMode (BUTTON_PIN , INPUT_PULLUP);
78+ pinMode (button , INPUT_PULLUP);
7179
7280 // Set callback function for light change
7381 zbDimmableLight.onLightChange (setLight);
@@ -79,32 +87,46 @@ void setup()
7987 zbDimmableLight.setManufacturerAndModel (" Espressif" , " ZBLightBulb" );
8088
8189 // Add endpoint to Zigbee Core
82- log_d (" Adding ZigbeeLight endpoint to Zigbee Core" );
90+ Serial. println (" Adding ZigbeeLight endpoint to Zigbee Core" );
8391 Zigbee.addEndpoint (&zbDimmableLight);
8492
85- // When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
86- log_d (" Calling Zigbee.begin()" );
87- Zigbee.begin ();
93+ // When all EPs are registered, start Zigbee in End Device mode
94+ if (!Zigbee.begin ())
95+ {
96+ Serial.println (" Zigbee failed to start!" );
97+ Serial.println (" Rebooting..." );
98+ ESP.restart ();
99+ }
100+ Serial.println (" Connecting to network" );
101+ while (!Zigbee.connected ())
102+ {
103+ Serial.print (" ." );
104+ delay (100 );
105+ }
106+ Serial.println ();
88107}
89108
90109void loop ()
91110{
92111 // Checking button for factory reset
93- if (digitalRead (BUTTON_PIN ) == LOW)
112+ if (digitalRead (button ) == LOW)
94113 { // Push button pressed
95114 // Key debounce handling
96115 delay (100 );
97116 int startTime = millis ();
98- while (digitalRead (BUTTON_PIN ) == LOW)
117+ while (digitalRead (button ) == LOW)
99118 {
100119 delay (50 );
101120 if ((millis () - startTime) > 3000 )
102121 {
103122 // If key pressed for more than 3secs, factory reset Zigbee and reboot
104- Serial.printf (" Resetting Zigbee to factory settings, reboot.\n " );
123+ Serial.println (" Resetting Zigbee to factory and rebooting in 1s." );
124+ delay (1000 );
105125 Zigbee.factoryReset ();
106126 }
107127 }
128+ // Increase blightness by 50 every time the button is pressed
129+ zbDimmableLight.setLightLevel (zbDimmableLight.getLightLevel () + 50 );
108130 }
109131 delay (100 );
110132}
0 commit comments