Skip to content

Commit 926c043

Browse files
authored
Merge pull request #10727 from FaBjE/feature/zigbeeDimmableLight
Zigbee: Add dimmable light endpoint class
2 parents 3c3ff48 + 4f863d5 commit 926c043

File tree

7 files changed

+413
-0
lines changed

7 files changed

+413
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
276276
libraries/Zigbee/src/ZigbeeHandlers.cpp
277277
libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.cpp
278278
libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp
279+
libraries/Zigbee/src/ep/ZigbeeDimmableLight.cpp
279280
libraries/Zigbee/src/ep/ZigbeeLight.cpp
280281
libraries/Zigbee/src/ep/ZigbeeSwitch.cpp
281282
libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Arduino-ESP32 Zigbee Dimmable Light Example
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) dimmable light.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## Hardware Required
13+
14+
* A USB cable for power supply and programming
15+
* Board (ESP32-H2 or ESP32-C6) as Zigbee end device and upload the Zigbee_Dimmable_Light example
16+
* Zigbee network / coordinator (Other board with switch examples or Zigbee2mqtt or ZigbeeHomeAssistant like application)
17+
18+
### Configure the Project
19+
20+
Set the LED GPIO by changing the `LED_PIN` definition. By default, the LED_PIN is `RGB_BUILTIN`.
21+
22+
#### Using Arduino IDE
23+
24+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
25+
26+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
27+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
28+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
29+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
30+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
31+
32+
## Troubleshooting
33+
34+
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.
35+
You can do the following:
36+
37+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
38+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
39+
40+
By default, the coordinator network is closed after rebooting or flashing new firmware.
41+
To open the network you have 2 options:
42+
43+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
44+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
45+
46+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
47+
48+
* **LED not blinking:** Check the wiring connection and the IO selection.
49+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
50+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
51+
52+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
53+
54+
## Contribute
55+
56+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
57+
58+
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!
59+
60+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
61+
62+
## Resources
63+
64+
* Official ESP32 Forum: [Link](https://esp32.com)
65+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
66+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
67+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
68+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @brief This example demonstrates Zigbee Dimmable light bulb.
17+
*
18+
* The example demonstrates how to use Zigbee library to create an end device with
19+
* dimmable light end point.
20+
* The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator.
21+
*
22+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
23+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
24+
*
25+
* Please check the README.md for instructions and more detailed description.
26+
*
27+
* Created by [FaBjE](https://github.com/FaBjE) based on examples by [Jan Procházka](https://github.com/P-R-O-C-H-Y/)
28+
*/
29+
30+
#ifndef ZIGBEE_MODE_ED
31+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
32+
#endif
33+
34+
#include "Zigbee.h"
35+
36+
/* Zigbee dimmable light configuration */
37+
#define ZIGBEE_LIGHT_ENDPOINT 10
38+
uint8_t led = RGB_BUILTIN;
39+
uint8_t button = BOOT_PIN;
40+
41+
ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
42+
43+
/********************* RGB LED functions **************************/
44+
void setLight(bool state, uint8_t level) {
45+
if (!state) {
46+
rgbLedWrite(led, 0, 0, 0);
47+
return;
48+
}
49+
rgbLedWrite(led, level, level, level);
50+
}
51+
52+
// Create a task on identify call to handle the identify function
53+
void identify(uint16_t time) {
54+
static uint8_t blink = 1;
55+
log_d("Identify called for %d seconds", time);
56+
if (time == 0) {
57+
// If identify time is 0, stop blinking and restore light as it was used for identify
58+
zbDimmableLight.restoreLight();
59+
return;
60+
}
61+
rgbLedWrite(led, 255 * blink, 255 * blink, 255 * blink);
62+
blink = !blink;
63+
}
64+
65+
/********************* Arduino functions **************************/
66+
void setup() {
67+
Serial.begin(115200);
68+
69+
// Init RMT and leave light OFF
70+
rgbLedWrite(led, 0, 0, 0);
71+
72+
// Init button for factory reset
73+
pinMode(button, INPUT_PULLUP);
74+
75+
// Set callback function for light change
76+
zbDimmableLight.onLightChange(setLight);
77+
78+
// Optional: Set callback function for device identify
79+
zbDimmableLight.onIdentify(identify);
80+
81+
// Optional: Set Zigbee device name and model
82+
zbDimmableLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
83+
84+
// Add endpoint to Zigbee Core
85+
Serial.println("Adding ZigbeeLight endpoint to Zigbee Core");
86+
Zigbee.addEndpoint(&zbDimmableLight);
87+
88+
// When all EPs are registered, start Zigbee in End Device mode
89+
if (!Zigbee.begin()) {
90+
Serial.println("Zigbee failed to start!");
91+
Serial.println("Rebooting...");
92+
ESP.restart();
93+
}
94+
Serial.println("Connecting to network");
95+
while (!Zigbee.connected()) {
96+
Serial.print(".");
97+
delay(100);
98+
}
99+
Serial.println();
100+
}
101+
102+
void loop() {
103+
// Checking button for factory reset
104+
if (digitalRead(button) == LOW) { // Push button pressed
105+
// Key debounce handling
106+
delay(100);
107+
int startTime = millis();
108+
while (digitalRead(button) == LOW) {
109+
delay(50);
110+
if ((millis() - startTime) > 3000) {
111+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
112+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
113+
delay(1000);
114+
Zigbee.factoryReset();
115+
}
116+
}
117+
// Increase blightness by 50 every time the button is pressed
118+
zbDimmableLight.setLightLevel(zbDimmableLight.getLightLevel() + 50);
119+
}
120+
delay(100);
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
5+
]
6+
}

libraries/Zigbee/src/Zigbee.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// Endpoints
1010
#include "ep/ZigbeeLight.h"
1111
#include "ep/ZigbeeSwitch.h"
12+
#include "ep/ZigbeeDimmableLight.h"
1213
#include "ep/ZigbeeColorDimmableLight.h"
1314
#include "ep/ZigbeeColorDimmerSwitch.h"
1415
#include "ep/ZigbeeTempSensor.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
#include "ZigbeeDimmableLight.h"
3+
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
4+
5+
#include "esp_zigbee_cluster.h"
6+
7+
ZigbeeDimmableLight::ZigbeeDimmableLight(uint8_t endpoint) : ZigbeeEP(endpoint) {
8+
_device_id = ESP_ZB_HA_DIMMABLE_LIGHT_DEVICE_ID;
9+
10+
zigbee_dimmable_light_cfg_t light_cfg = ZIGBEE_DEFAULT_DIMMABLE_LIGHT_CONFIG();
11+
_cluster_list = zigbee_dimmable_light_clusters_create(&light_cfg);
12+
13+
_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};
14+
15+
// set default values
16+
_current_state = false;
17+
_current_level = 255;
18+
}
19+
20+
// set attribute method -> method overridden in child class
21+
void ZigbeeDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) {
22+
// check the data and call right method
23+
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF) {
24+
if (message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_BOOL) {
25+
if (_current_state != *(bool *)message->attribute.data.value) {
26+
_current_state = *(bool *)message->attribute.data.value;
27+
lightChanged();
28+
}
29+
return;
30+
} else {
31+
log_w("Received message ignored. Attribute ID: %d not supported for On/Off Light", message->attribute.id);
32+
}
33+
} else if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL) {
34+
if (message->attribute.id == ESP_ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U8) {
35+
if (_current_level != *(uint8_t *)message->attribute.data.value) {
36+
_current_level = *(uint8_t *)message->attribute.data.value;
37+
lightChanged();
38+
}
39+
return;
40+
} else {
41+
log_w("Received message ignored. Attribute ID: %d not supported for Level Control", message->attribute.id);
42+
// TODO: implement more attributes -> includes/zcl/esp_zigbee_zcl_level.h
43+
}
44+
} else {
45+
log_w("Received message ignored. Cluster ID: %d not supported for dimmable Light", message->info.cluster);
46+
}
47+
}
48+
49+
void ZigbeeDimmableLight::lightChanged() {
50+
if (_on_light_change) {
51+
_on_light_change(_current_state, _current_level);
52+
}
53+
}
54+
55+
void ZigbeeDimmableLight::setLight(bool state, uint8_t level) {
56+
// Update all attributes
57+
_current_state = state;
58+
_current_level = level;
59+
lightChanged();
60+
61+
log_v("Updating on/off light state to %d", state);
62+
/* Update light clusters */
63+
esp_zb_lock_acquire(portMAX_DELAY);
64+
// set on/off state
65+
esp_zb_zcl_set_attribute_val(
66+
_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
67+
);
68+
// set level
69+
esp_zb_zcl_set_attribute_val(
70+
_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
71+
);
72+
esp_zb_lock_release();
73+
}
74+
75+
void ZigbeeDimmableLight::setLightState(bool state) {
76+
setLight(state, _current_level);
77+
}
78+
79+
void ZigbeeDimmableLight::setLightLevel(uint8_t level) {
80+
setLight(_current_state, level);
81+
}
82+
83+
esp_zb_cluster_list_t *ZigbeeDimmableLight::zigbee_dimmable_light_clusters_create(zigbee_dimmable_light_cfg_t *light_cfg) {
84+
esp_zb_attribute_list_t *esp_zb_basic_cluster = esp_zb_basic_cluster_create(&light_cfg->basic_cfg);
85+
esp_zb_attribute_list_t *esp_zb_identify_cluster = esp_zb_identify_cluster_create(&light_cfg->identify_cfg);
86+
esp_zb_attribute_list_t *esp_zb_groups_cluster = esp_zb_groups_cluster_create(&light_cfg->groups_cfg);
87+
esp_zb_attribute_list_t *esp_zb_scenes_cluster = esp_zb_scenes_cluster_create(&light_cfg->scenes_cfg);
88+
esp_zb_attribute_list_t *esp_zb_on_off_cluster = esp_zb_on_off_cluster_create(&light_cfg->on_off_cfg);
89+
esp_zb_attribute_list_t *esp_zb_level_cluster = esp_zb_level_cluster_create(&light_cfg->level_cfg);
90+
91+
// ------------------------------ Create cluster list ------------------------------
92+
esp_zb_cluster_list_t *esp_zb_cluster_list = esp_zb_zcl_cluster_list_create();
93+
esp_zb_cluster_list_add_basic_cluster(esp_zb_cluster_list, esp_zb_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
94+
esp_zb_cluster_list_add_identify_cluster(esp_zb_cluster_list, esp_zb_identify_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
95+
esp_zb_cluster_list_add_groups_cluster(esp_zb_cluster_list, esp_zb_groups_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
96+
esp_zb_cluster_list_add_scenes_cluster(esp_zb_cluster_list, esp_zb_scenes_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
97+
esp_zb_cluster_list_add_on_off_cluster(esp_zb_cluster_list, esp_zb_on_off_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
98+
esp_zb_cluster_list_add_level_cluster(esp_zb_cluster_list, esp_zb_level_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
99+
100+
return esp_zb_cluster_list;
101+
}
102+
103+
#endif // SOC_IEEE802154_SUPPORTED

0 commit comments

Comments
 (0)