-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10727 from FaBjE/feature/zigbeeDimmableLight
Zigbee: Add dimmable light endpoint class
- Loading branch information
Showing
7 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Arduino-ESP32 Zigbee Dimmable Light Example | ||
|
||
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) dimmable light. | ||
|
||
# Supported Targets | ||
|
||
Currently, this example supports the following targets. | ||
|
||
| Supported Targets | ESP32-C6 | ESP32-H2 | | ||
| ----------------- | -------- | -------- | | ||
|
||
## Hardware Required | ||
|
||
* A USB cable for power supply and programming | ||
* Board (ESP32-H2 or ESP32-C6) as Zigbee end device and upload the Zigbee_Dimmable_Light example | ||
* Zigbee network / coordinator (Other board with switch examples or Zigbee2mqtt or ZigbeeHomeAssistant like application) | ||
|
||
### Configure the Project | ||
|
||
Set the LED GPIO by changing the `LED_PIN` definition. By default, the LED_PIN is `RGB_BUILTIN`. | ||
|
||
#### Using Arduino IDE | ||
|
||
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits). | ||
|
||
* Before Compile/Verify, select the correct board: `Tools -> Board`. | ||
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)` | ||
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs` | ||
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port. | ||
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`. | ||
|
||
## Troubleshooting | ||
|
||
If the End device flashed with this example is not connecting to the coordinator, erase the flash of the End device before flashing the example to the board. It is recommended to do this if you re-flash the coordinator. | ||
You can do the following: | ||
|
||
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`. | ||
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack. | ||
|
||
By default, the coordinator network is closed after rebooting or flashing new firmware. | ||
To open the network you have 2 options: | ||
|
||
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`. | ||
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join. | ||
|
||
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source*** | ||
|
||
* **LED not blinking:** Check the wiring connection and the IO selection. | ||
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed. | ||
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation. | ||
|
||
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute). | ||
|
||
## Contribute | ||
|
||
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst) | ||
|
||
If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome! | ||
|
||
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else. | ||
|
||
## Resources | ||
|
||
* Official ESP32 Forum: [Link](https://esp32.com) | ||
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32) | ||
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf) | ||
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf) | ||
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com) |
121 changes: 121 additions & 0 deletions
121
libraries/Zigbee/examples/Zigbee_Dimmable_Light/Zigbee_Dimmable_Light.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @brief This example demonstrates Zigbee Dimmable light bulb. | ||
* | ||
* The example demonstrates how to use Zigbee library to create an end device with | ||
* dimmable light end point. | ||
* The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator. | ||
* | ||
* Proper Zigbee mode must be selected in Tools->Zigbee mode | ||
* and also the correct partition scheme must be selected in Tools->Partition Scheme. | ||
* | ||
* Please check the README.md for instructions and more detailed description. | ||
* | ||
* Created by [FaBjE](https://github.com/FaBjE) based on examples by [Jan Procházka](https://github.com/P-R-O-C-H-Y/) | ||
*/ | ||
|
||
#ifndef ZIGBEE_MODE_ED | ||
#error "Zigbee end device mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "Zigbee.h" | ||
|
||
/* Zigbee dimmable light configuration */ | ||
#define ZIGBEE_LIGHT_ENDPOINT 10 | ||
uint8_t led = RGB_BUILTIN; | ||
uint8_t button = BOOT_PIN; | ||
|
||
ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT); | ||
|
||
/********************* RGB LED functions **************************/ | ||
void setLight(bool state, uint8_t 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 | ||
void identify(uint16_t time) { | ||
static uint8_t blink = 1; | ||
log_d("Identify called for %d seconds", time); | ||
if (time == 0) { | ||
// If identify time is 0, stop blinking and restore light as it was used for identify | ||
zbDimmableLight.restoreLight(); | ||
return; | ||
} | ||
rgbLedWrite(led, 255 * blink, 255 * blink, 255 * blink); | ||
blink = !blink; | ||
} | ||
|
||
/********************* Arduino functions **************************/ | ||
void setup() { | ||
Serial.begin(115200); | ||
|
||
// Init RMT and leave light OFF | ||
rgbLedWrite(led, 0, 0, 0); | ||
|
||
// Init button for factory reset | ||
pinMode(button, INPUT_PULLUP); | ||
|
||
// Set callback function for light change | ||
zbDimmableLight.onLightChange(setLight); | ||
|
||
// Optional: Set callback function for device identify | ||
zbDimmableLight.onIdentify(identify); | ||
|
||
// Optional: Set Zigbee device name and model | ||
zbDimmableLight.setManufacturerAndModel("Espressif", "ZBLightBulb"); | ||
|
||
// Add endpoint to Zigbee Core | ||
Serial.println("Adding ZigbeeLight endpoint to Zigbee Core"); | ||
Zigbee.addEndpoint(&zbDimmableLight); | ||
|
||
// When all EPs are registered, start Zigbee in End Device mode | ||
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) == LOW) { // Push button pressed | ||
// Key debounce handling | ||
delay(100); | ||
int startTime = millis(); | ||
while (digitalRead(button) == LOW) { | ||
delay(50); | ||
if ((millis() - startTime) > 3000) { | ||
// If key pressed for more than 3secs, factory reset Zigbee and reboot | ||
Serial.println("Resetting Zigbee to factory and rebooting in 1s."); | ||
delay(1000); | ||
Zigbee.factoryReset(); | ||
} | ||
} | ||
// Increase blightness by 50 every time the button is pressed | ||
zbDimmableLight.setLightLevel(zbDimmableLight.getLightLevel() + 50); | ||
} | ||
delay(100); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed", | ||
"requires": [ | ||
"CONFIG_SOC_IEEE802154_SUPPORTED=y" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
|
||
#include "ZigbeeDimmableLight.h" | ||
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED | ||
|
||
#include "esp_zigbee_cluster.h" | ||
|
||
ZigbeeDimmableLight::ZigbeeDimmableLight(uint8_t endpoint) : ZigbeeEP(endpoint) { | ||
_device_id = ESP_ZB_HA_DIMMABLE_LIGHT_DEVICE_ID; | ||
|
||
zigbee_dimmable_light_cfg_t light_cfg = ZIGBEE_DEFAULT_DIMMABLE_LIGHT_CONFIG(); | ||
_cluster_list = zigbee_dimmable_light_clusters_create(&light_cfg); | ||
|
||
_ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_DIMMABLE_LIGHT_DEVICE_ID, .app_device_version = 0}; | ||
|
||
// set default values | ||
_current_state = false; | ||
_current_level = 255; | ||
} | ||
|
||
// set attribute method -> method overridden in child class | ||
void ZigbeeDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) { | ||
// check the data and call right method | ||
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF) { | ||
if (message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_BOOL) { | ||
if (_current_state != *(bool *)message->attribute.data.value) { | ||
_current_state = *(bool *)message->attribute.data.value; | ||
lightChanged(); | ||
} | ||
return; | ||
} else { | ||
log_w("Received message ignored. Attribute ID: %d not supported for On/Off Light", message->attribute.id); | ||
} | ||
} else if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL) { | ||
if (message->attribute.id == ESP_ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U8) { | ||
if (_current_level != *(uint8_t *)message->attribute.data.value) { | ||
_current_level = *(uint8_t *)message->attribute.data.value; | ||
lightChanged(); | ||
} | ||
return; | ||
} else { | ||
log_w("Received message ignored. Attribute ID: %d not supported for Level Control", message->attribute.id); | ||
// TODO: implement more attributes -> includes/zcl/esp_zigbee_zcl_level.h | ||
} | ||
} else { | ||
log_w("Received message ignored. Cluster ID: %d not supported for dimmable Light", message->info.cluster); | ||
} | ||
} | ||
|
||
void ZigbeeDimmableLight::lightChanged() { | ||
if (_on_light_change) { | ||
_on_light_change(_current_state, _current_level); | ||
} | ||
} | ||
|
||
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::zigbee_dimmable_light_clusters_create(zigbee_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_identify_cluster = esp_zb_identify_cluster_create(&light_cfg->identify_cfg); | ||
esp_zb_attribute_list_t *esp_zb_groups_cluster = esp_zb_groups_cluster_create(&light_cfg->groups_cfg); | ||
esp_zb_attribute_list_t *esp_zb_scenes_cluster = esp_zb_scenes_cluster_create(&light_cfg->scenes_cfg); | ||
esp_zb_attribute_list_t *esp_zb_on_off_cluster = esp_zb_on_off_cluster_create(&light_cfg->on_off_cfg); | ||
esp_zb_attribute_list_t *esp_zb_level_cluster = esp_zb_level_cluster_create(&light_cfg->level_cfg); | ||
|
||
// ------------------------------ Create cluster list ------------------------------ | ||
esp_zb_cluster_list_t *esp_zb_cluster_list = esp_zb_zcl_cluster_list_create(); | ||
esp_zb_cluster_list_add_basic_cluster(esp_zb_cluster_list, esp_zb_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); | ||
esp_zb_cluster_list_add_identify_cluster(esp_zb_cluster_list, esp_zb_identify_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); | ||
esp_zb_cluster_list_add_groups_cluster(esp_zb_cluster_list, esp_zb_groups_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); | ||
esp_zb_cluster_list_add_scenes_cluster(esp_zb_cluster_list, esp_zb_scenes_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); | ||
esp_zb_cluster_list_add_on_off_cluster(esp_zb_cluster_list, esp_zb_on_off_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); | ||
esp_zb_cluster_list_add_level_cluster(esp_zb_cluster_list, esp_zb_level_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); | ||
|
||
return esp_zb_cluster_list; | ||
} | ||
|
||
#endif // SOC_IEEE802154_SUPPORTED |
Oops, something went wrong.