Skip to content

Commit 78a489e

Browse files
Add example
1 parent fa081b8 commit 78a489e

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Arduino-ESP32 Zigbee Temperature Dimmable Light Example
2+
3+
This example shows how to configure a Zigbee end device and use it as a Home Automation (HA) temperature dimmable light (white tunable).
4+
5+
## Supported Targets
6+
7+
| Supported Targets | ESP32-C6 | ESP32-H2 |
8+
| ----------------- | -------- | -------- |
9+
10+
## Hardware Required
11+
12+
* A USB cable for power supply and programming
13+
14+
### Configure the Project
15+
16+
Set the LED GPIO by changing the `led` definition. By default, the LED is `RGB_BUILTIN`.
17+
18+
#### Using Arduino IDE
19+
20+
* Select the correct board: `Tools -> Board`.
21+
* Select Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
22+
* Select Partition Scheme: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
23+
* Select the COM port: `Tools -> Port: xxx` where `xxx` is the detected COM port.
24+
* Optional: Set debug level to verbose: `Tools -> Core Debug Level: Verbose`.
25+
26+
## Troubleshooting
27+
28+
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.
29+
30+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
31+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
32+
33+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
34+
35+
* **LED not blinking:** Check the wiring connection and the IO selection.
36+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
37+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
38+
39+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
40+
41+
## Contribute
42+
43+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
44+
45+
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!
46+
47+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
48+
49+
## Resources
50+
51+
* Official ESP32 Forum: [Link](https://esp32.com)
52+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
53+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
54+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
55+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2025 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 Temperature Dimmable light bulb.
17+
*
18+
* The example demonstrates how to use Zigbee library to create an end device with
19+
* temperature 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 Hannes Beckmann
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 ZIGBEE_TEMP_LIGHT_ENDPOINT 11
37+
uint8_t led = RGB_BUILTIN;
38+
uint8_t button = BOOT_PIN;
39+
40+
ZigbeeTemperatureDimmableLight zbTempLight = ZigbeeTemperatureDimmableLight(ZIGBEE_TEMP_LIGHT_ENDPOINT);
41+
42+
/********************* LED functions **************************/
43+
uint16_t kelvinToMireds(uint16_t kelvin) {
44+
return 1000000 / kelvin;
45+
}
46+
47+
uint16_t miredsToKelvin(uint16_t mireds) {
48+
return 1000000 / mireds;
49+
}
50+
51+
void setTempLight(bool state, uint8_t level, uint16_t mireds) {
52+
if (!state) {
53+
rgbLedWrite(led, 0, 0, 0);
54+
return;
55+
}
56+
float brightness = (float)level / 255;
57+
// Convert mireds to color temperature (K) and map to white/yellow
58+
uint16_t kelvin = miredsToKelvin(mireds);
59+
uint8_t warm = constrain(map(kelvin, 2000, 6500, 255, 0), 0, 255);
60+
uint8_t cold = constrain(map(kelvin, 2000, 6500, 0, 255), 0, 255);
61+
rgbLedWrite(led, warm * brightness, warm * brightness, cold * brightness);
62+
}
63+
64+
void identify(uint16_t time) {
65+
static uint8_t blink = 1;
66+
log_d("Identify called for %d seconds", time);
67+
if (time == 0) {
68+
zbTempLight.restoreLight();
69+
return;
70+
}
71+
rgbLedWrite(led, 255 * blink, 255 * blink, 255 * blink);
72+
blink = !blink;
73+
}
74+
75+
void setup() {
76+
Serial.begin(115200);
77+
rgbLedWrite(led, 0, 0, 0);
78+
pinMode(button, INPUT_PULLUP);
79+
zbTempLight.onLightChange(setTempLight);
80+
zbTempLight.onIdentify(identify);
81+
zbTempLight.setManufacturerAndModel("Espressif", "ZBTempLightBulb");
82+
// High Kelvin -> Low Mireds: Min and Max is switched
83+
zbTempLight.setMinMaxTemperature(kelvinToMireds(6500), kelvinToMireds(2000));
84+
Serial.println("Adding ZigbeeTemperatureDimmableLight endpoint to Zigbee Core");
85+
Zigbee.addEndpoint(&zbTempLight);
86+
if (!Zigbee.begin()) {
87+
Serial.println("Zigbee failed to start!");
88+
Serial.println("Rebooting...");
89+
ESP.restart();
90+
}
91+
Serial.println("Connecting to network");
92+
while (!Zigbee.connected()) {
93+
Serial.print(".");
94+
delay(100);
95+
}
96+
Serial.println();
97+
}
98+
99+
void loop() {
100+
if (digitalRead(button) == LOW) {
101+
delay(100);
102+
int startTime = millis();
103+
while (digitalRead(button) == LOW) {
104+
delay(50);
105+
if ((millis() - startTime) > 3000) {
106+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
107+
delay(1000);
108+
Zigbee.factoryReset();
109+
}
110+
}
111+
zbTempLight.setLightLevel(zbTempLight.getLightLevel() + 50);
112+
}
113+
delay(100);
114+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fqbn_append: PartitionScheme=zigbee,ZigbeeMode=ed
2+
3+
requires:
4+
- CONFIG_SOC_IEEE802154_SUPPORTED=y
5+
- CONFIG_ZB_ENABLED=y

0 commit comments

Comments
 (0)