33
33
34
34
#include " Zigbee.h"
35
35
36
- #define LED_PIN RGB_BUILTIN
37
- #define BUTTON_PIN 9 // C6/H2 Boot button
36
+ /* Zigbee dimmable light configuration */
38
37
#define ZIGBEE_LIGHT_ENDPOINT 10
38
+ uint8_t led = RGB_BUILTIN;
39
+ uint8_t button = BOOT_PIN;
39
40
40
41
ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
41
42
42
- /* ******************** LED functions **************************/
43
+ /* ******************** RGB LED functions **************************/
43
44
void setLight (bool state, uint8_t level)
44
45
{
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);
46
52
}
47
53
48
54
// Create a task on identify call to handle the identify function
@@ -56,18 +62,20 @@ void identify(uint16_t time)
56
62
zbDimmableLight.restoreLight ();
57
63
return ;
58
64
}
59
- rgbLedWrite (LED_PIN , 255 * blink, 255 * blink, 255 * blink);
65
+ rgbLedWrite (led , 255 * blink, 255 * blink, 255 * blink);
60
66
blink = !blink;
61
67
}
62
68
63
69
/* ******************** Arduino functions **************************/
64
70
void setup ()
65
71
{
72
+ Serial.begin (115200 );
73
+
66
74
// Init RMT and leave light OFF
67
- rgbLedWrite (LED_PIN , 0 , 0 , 0 );
75
+ rgbLedWrite (led , 0 , 0 , 0 );
68
76
69
77
// Init button for factory reset
70
- pinMode (BUTTON_PIN , INPUT_PULLUP);
78
+ pinMode (button , INPUT_PULLUP);
71
79
72
80
// Set callback function for light change
73
81
zbDimmableLight.onLightChange (setLight);
@@ -79,32 +87,46 @@ void setup()
79
87
zbDimmableLight.setManufacturerAndModel (" Espressif" , " ZBLightBulb" );
80
88
81
89
// Add endpoint to Zigbee Core
82
- log_d (" Adding ZigbeeLight endpoint to Zigbee Core" );
90
+ Serial. println (" Adding ZigbeeLight endpoint to Zigbee Core" );
83
91
Zigbee.addEndpoint (&zbDimmableLight);
84
92
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 ();
88
107
}
89
108
90
109
void loop ()
91
110
{
92
111
// Checking button for factory reset
93
- if (digitalRead (BUTTON_PIN ) == LOW)
112
+ if (digitalRead (button ) == LOW)
94
113
{ // Push button pressed
95
114
// Key debounce handling
96
115
delay (100 );
97
116
int startTime = millis ();
98
- while (digitalRead (BUTTON_PIN ) == LOW)
117
+ while (digitalRead (button ) == LOW)
99
118
{
100
119
delay (50 );
101
120
if ((millis () - startTime) > 3000 )
102
121
{
103
122
// 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 );
105
125
Zigbee.factoryReset ();
106
126
}
107
127
}
128
+ // Increase blightness by 50 every time the button is pressed
129
+ zbDimmableLight.setLightLevel (zbDimmableLight.getLightLevel () + 50 );
108
130
}
109
131
delay (100 );
110
132
}
0 commit comments