feat(Zigbee): Update Zigbee Dimmable light example to 3.1.x features
This commit is contained in:
parent
aa0c3da729
commit
538c57a994
3 changed files with 81 additions and 16 deletions
|
|
@ -33,16 +33,22 @@
|
||||||
|
|
||||||
#include "Zigbee.h"
|
#include "Zigbee.h"
|
||||||
|
|
||||||
#define LED_PIN RGB_BUILTIN
|
/* Zigbee dimmable light configuration */
|
||||||
#define BUTTON_PIN 9 // C6/H2 Boot button
|
|
||||||
#define ZIGBEE_LIGHT_ENDPOINT 10
|
#define ZIGBEE_LIGHT_ENDPOINT 10
|
||||||
|
uint8_t led = RGB_BUILTIN;
|
||||||
|
uint8_t button = BOOT_PIN;
|
||||||
|
|
||||||
ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
|
ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
|
||||||
|
|
||||||
/********************* LED functions **************************/
|
/********************* RGB LED functions **************************/
|
||||||
void setLight(bool state, uint8_t level)
|
void setLight(bool state, uint8_t level)
|
||||||
{
|
{
|
||||||
rgbLedWrite(LED_PIN, level, level, level);
|
if (!state)
|
||||||
|
{
|
||||||
|
rgbLedWrite(led, 0, 0, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
rgbLedWrite(led, level, level, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a task on identify call to handle the identify function
|
// Create a task on identify call to handle the identify function
|
||||||
|
|
@ -56,18 +62,20 @@ void identify(uint16_t time)
|
||||||
zbDimmableLight.restoreLight();
|
zbDimmableLight.restoreLight();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rgbLedWrite(LED_PIN, 255 * blink, 255 * blink, 255 * blink);
|
rgbLedWrite(led, 255 * blink, 255 * blink, 255 * blink);
|
||||||
blink = !blink;
|
blink = !blink;
|
||||||
}
|
}
|
||||||
|
|
||||||
/********************* Arduino functions **************************/
|
/********************* Arduino functions **************************/
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
// Init RMT and leave light OFF
|
// Init RMT and leave light OFF
|
||||||
rgbLedWrite(LED_PIN, 0, 0, 0);
|
rgbLedWrite(led, 0, 0, 0);
|
||||||
|
|
||||||
// Init button for factory reset
|
// Init button for factory reset
|
||||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
pinMode(button, INPUT_PULLUP);
|
||||||
|
|
||||||
// Set callback function for light change
|
// Set callback function for light change
|
||||||
zbDimmableLight.onLightChange(setLight);
|
zbDimmableLight.onLightChange(setLight);
|
||||||
|
|
@ -79,32 +87,46 @@ void setup()
|
||||||
zbDimmableLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
|
zbDimmableLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
|
||||||
|
|
||||||
// Add endpoint to Zigbee Core
|
// Add endpoint to Zigbee Core
|
||||||
log_d("Adding ZigbeeLight endpoint to Zigbee Core");
|
Serial.println("Adding ZigbeeLight endpoint to Zigbee Core");
|
||||||
Zigbee.addEndpoint(&zbDimmableLight);
|
Zigbee.addEndpoint(&zbDimmableLight);
|
||||||
|
|
||||||
// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
|
// When all EPs are registered, start Zigbee in End Device mode
|
||||||
log_d("Calling Zigbee.begin()");
|
if (!Zigbee.begin())
|
||||||
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()
|
void loop()
|
||||||
{
|
{
|
||||||
// Checking button for factory reset
|
// Checking button for factory reset
|
||||||
if (digitalRead(BUTTON_PIN) == LOW)
|
if (digitalRead(button) == LOW)
|
||||||
{ // Push button pressed
|
{ // Push button pressed
|
||||||
// Key debounce handling
|
// Key debounce handling
|
||||||
delay(100);
|
delay(100);
|
||||||
int startTime = millis();
|
int startTime = millis();
|
||||||
while (digitalRead(BUTTON_PIN) == LOW)
|
while (digitalRead(button) == LOW)
|
||||||
{
|
{
|
||||||
delay(50);
|
delay(50);
|
||||||
if ((millis() - startTime) > 3000)
|
if ((millis() - startTime) > 3000)
|
||||||
{
|
{
|
||||||
// If key pressed for more than 3secs, factory reset Zigbee and reboot
|
// 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();
|
Zigbee.factoryReset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Increase blightness by 50 every time the button is pressed
|
||||||
|
zbDimmableLight.setLightLevel(zbDimmableLight.getLightLevel() + 50);
|
||||||
}
|
}
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ void ZigbeeDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_message
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
log_w("Received message ignored. Cluster ID: %d not supported for Color dimmable Light", message->info.cluster);
|
log_w("Received message ignored. Cluster ID: %d not supported for dimmable Light", message->info.cluster);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,6 +69,35 @@ void ZigbeeDimmableLight::lightChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ZigbeeDimmableLight::setLight(bool state, uint8_t level)
|
||||||
|
{
|
||||||
|
// Update all attributes
|
||||||
|
_current_state = state;
|
||||||
|
_current_level = level;
|
||||||
|
lightChanged();
|
||||||
|
|
||||||
|
log_v("Updating on/off light state to %d", state);
|
||||||
|
/* Update light clusters */
|
||||||
|
esp_zb_lock_acquire(portMAX_DELAY);
|
||||||
|
// set on/off state
|
||||||
|
esp_zb_zcl_set_attribute_val(
|
||||||
|
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_ON_OFF, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID, &_current_state, false);
|
||||||
|
// set level
|
||||||
|
esp_zb_zcl_set_attribute_val(
|
||||||
|
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID, &_current_level, false);
|
||||||
|
esp_zb_lock_release();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ZigbeeDimmableLight::setLightState(bool state)
|
||||||
|
{
|
||||||
|
setLight(state, _current_level);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ZigbeeDimmableLight::setLightLevel(uint8_t level)
|
||||||
|
{
|
||||||
|
setLight(_current_state, level);
|
||||||
|
}
|
||||||
|
|
||||||
esp_zb_cluster_list_t *ZigbeeDimmableLight::esp_zb_dimmable_light_clusters_create(esp_zb_dimmable_light_cfg_t *light_cfg)
|
esp_zb_cluster_list_t *ZigbeeDimmableLight::esp_zb_dimmable_light_clusters_create(esp_zb_dimmable_light_cfg_t *light_cfg)
|
||||||
{
|
{
|
||||||
esp_zb_attribute_list_t *esp_zb_basic_cluster = esp_zb_basic_cluster_create(&light_cfg->basic_cfg);
|
esp_zb_attribute_list_t *esp_zb_basic_cluster = esp_zb_basic_cluster_create(&light_cfg->basic_cfg);
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "soc/soc_caps.h"
|
#include "soc/soc_caps.h"
|
||||||
#if SOC_IEEE802154_SUPPORTED
|
#include "sdkconfig.h"
|
||||||
|
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
|
||||||
|
|
||||||
#include "ZigbeeEP.h"
|
#include "ZigbeeEP.h"
|
||||||
#include "ha/esp_zigbee_ha_standard.h"
|
#include "ha/esp_zigbee_ha_standard.h"
|
||||||
|
|
@ -77,6 +78,19 @@ public:
|
||||||
lightChanged();
|
lightChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setLightState(bool state);
|
||||||
|
void setLightLevel(uint8_t level);
|
||||||
|
void setLight(bool state, uint8_t level);
|
||||||
|
|
||||||
|
bool getLightState()
|
||||||
|
{
|
||||||
|
return _current_state;
|
||||||
|
}
|
||||||
|
uint8_t getLightLevel()
|
||||||
|
{
|
||||||
|
return _current_level;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override;
|
void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue