Skip to content

Commit e473560

Browse files
committed
feat(Zigbee): Add Zigbee Dimmable light example
Add example for a dimmable light. Based on a copy of color dimmable light example.
1 parent 2bee085 commit e473560

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed
+68
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,110 @@
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 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+
#define LED_PIN RGB_BUILTIN
37+
#define BUTTON_PIN 9 // C6/H2 Boot button
38+
#define ZIGBEE_LIGHT_ENDPOINT 10
39+
40+
ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
41+
42+
/********************* LED functions **************************/
43+
void setLight(bool state, uint8_t level)
44+
{
45+
rgbLedWrite(LED_PIN, level, level, level);
46+
}
47+
48+
// Create a task on identify call to handle the identify function
49+
void identify(uint16_t time)
50+
{
51+
static uint8_t blink = 1;
52+
log_d("Identify called for %d seconds", time);
53+
if (time == 0)
54+
{
55+
// If identify time is 0, stop blinking and restore light as it was used for identify
56+
zbDimmableLight.restoreLight();
57+
return;
58+
}
59+
rgbLedWrite(LED_PIN, 255 * blink, 255 * blink, 255 * blink);
60+
blink = !blink;
61+
}
62+
63+
/********************* Arduino functions **************************/
64+
void setup()
65+
{
66+
// Init RMT and leave light OFF
67+
rgbLedWrite(LED_PIN, 0, 0, 0);
68+
69+
// Init button for factory reset
70+
pinMode(BUTTON_PIN, INPUT_PULLUP);
71+
72+
// Set callback function for light change
73+
zbDimmableLight.onLightChange(setLight);
74+
75+
// Optional: Set callback function for device identify
76+
zbDimmableLight.onIdentify(identify);
77+
78+
// Optional: Set Zigbee device name and model
79+
zbDimmableLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
80+
81+
// Add endpoint to Zigbee Core
82+
log_d("Adding ZigbeeLight endpoint to Zigbee Core");
83+
Zigbee.addEndpoint(&zbDimmableLight);
84+
85+
// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
86+
log_d("Calling Zigbee.begin()");
87+
Zigbee.begin();
88+
}
89+
90+
void loop()
91+
{
92+
// Checking button for factory reset
93+
if (digitalRead(BUTTON_PIN) == LOW)
94+
{ // Push button pressed
95+
// Key debounce handling
96+
delay(100);
97+
int startTime = millis();
98+
while (digitalRead(BUTTON_PIN) == LOW)
99+
{
100+
delay(50);
101+
if ((millis() - startTime) > 3000)
102+
{
103+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
104+
Serial.printf("Resetting Zigbee to factory settings, reboot.\n");
105+
Zigbee.factoryReset();
106+
}
107+
}
108+
}
109+
delay(100);
110+
}
+6
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+
}

0 commit comments

Comments
 (0)