Update Zigbee_On_Off_Light.ino
This commit is contained in:
@@ -30,28 +30,30 @@
|
||||
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
|
||||
#endif
|
||||
|
||||
#include "ZigbeeCore.h"
|
||||
#include "ep/ZigbeeLight.h"
|
||||
#include "Zigbee.h"
|
||||
|
||||
#define LED_PIN RGB_BUILTIN
|
||||
#define BUTTON_PIN 9 // ESP32-C6/H2 Boot button
|
||||
/* Zigbee light bulb configuration */
|
||||
#define ZIGBEE_LIGHT_ENDPOINT 10
|
||||
uint8_t led = RGB_BUILTIN;
|
||||
uint8_t button = BOOT_PIN;
|
||||
|
||||
ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);
|
||||
|
||||
/********************* RGB LED functions **************************/
|
||||
void setLED(bool value) {
|
||||
digitalWrite(LED_PIN, value);
|
||||
digitalWrite(led, value);
|
||||
}
|
||||
|
||||
/********************* Arduino functions **************************/
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
// Init LED and turn it OFF (if LED_PIN == RGB_BUILTIN, the rgbLedWrite() will be used under the hood)
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
pinMode(led, OUTPUT);
|
||||
digitalWrite(led, LOW);
|
||||
|
||||
// Init button for factory reset
|
||||
pinMode(BUTTON_PIN, INPUT);
|
||||
pinMode(button, INPUT_PULLUP);
|
||||
|
||||
//Optional: set Zigbee device name and model
|
||||
zbLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
|
||||
@@ -60,28 +62,40 @@ void setup() {
|
||||
zbLight.onLightChange(setLED);
|
||||
|
||||
//Add endpoint to Zigbee Core
|
||||
log_d("Adding ZigbeeLight endpoint to Zigbee Core");
|
||||
Serial.println("Adding ZigbeeLight endpoint to Zigbee Core");
|
||||
Zigbee.addEndpoint(&zbLight);
|
||||
|
||||
// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
|
||||
log_d("Calling Zigbee.begin()");
|
||||
Zigbee.begin();
|
||||
if (!Zigbee.begin()) {
|
||||
Serial.println("Zigbee failed to start!");
|
||||
Serial.println("Rebooting...");
|
||||
ESP.restart();
|
||||
}
|
||||
Serial.println("Connecting to network");
|
||||
while (!Zigbee.connected()) {
|
||||
Serial.print(".");
|
||||
delay(100);
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Checking button for factory reset
|
||||
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
|
||||
if (digitalRead(button) == LOW) { // Push button pressed
|
||||
// Key debounce handling
|
||||
delay(100);
|
||||
int startTime = millis();
|
||||
while (digitalRead(BUTTON_PIN) == LOW) {
|
||||
while (digitalRead(button) == LOW) {
|
||||
delay(50);
|
||||
if ((millis() - startTime) > 3000) {
|
||||
// If key pressed for more than 3secs, factory reset Zigbee and reboot
|
||||
Serial.printf("Resetting Zigbee to factory settings, reboot.\n");
|
||||
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
|
||||
delay(1000);
|
||||
Zigbee.factoryReset();
|
||||
}
|
||||
}
|
||||
// Toggle light by pressing the button
|
||||
zbLight.setLight(!zbLight.getLightState());
|
||||
}
|
||||
delay(100);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user