Skip to content

Commit baaff35

Browse files
committed
feat(zigbee): Add Fan Control endpoint support
1 parent 33c8438 commit baaff35

File tree

8 files changed

+387
-12
lines changed

8 files changed

+387
-12
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
305305
libraries/Zigbee/src/ep/ZigbeeElectricalMeasurement.cpp
306306
libraries/Zigbee/src/ep/ZigbeeBinary.cpp
307307
libraries/Zigbee/src/ep/ZigbeePowerOutlet.cpp
308+
libraries/Zigbee/src/ep/ZigbeeFanControl.cpp
308309
)
309310

310311
set(ARDUINO_LIBRARY_BLE_SRCS
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Arduino-ESP32 Zigbee Fan Control Example
2+
3+
This example demonstrates how to use the Zigbee library to create a router device fan control and use it as a Home Automation (HA) fan control device.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## Fan Control Functions
13+
14+
1. Initialize a Zigbee fan control device.
15+
2. Control fan modes (OFF, LOW, MEDIUM, HIGH, ON).
16+
3. Respond to fan control commands from the Zigbee network.
17+
18+
## Hardware Required
19+
20+
* ESP32-H2 or ESP32-C6 development board
21+
* A USB cable for power supply and programming
22+
* RGB LED for visual feedback (built-in on most development boards)
23+
24+
### Configure the Project
25+
26+
In this example the RGB LED is used to indicate the current fan control mode.
27+
The LED colors represent different fan modes:
28+
- OFF: No light
29+
- LOW: Blue
30+
- MEDIUM: Yellow
31+
- HIGH: Red
32+
- ON: White
33+
34+
Set the button GPIO by changing the `button` variable. By default, it's the pin `BOOT_PIN` (BOOT button on ESP32-C6 and ESP32-H2).
35+
36+
#### Using Arduino IDE
37+
38+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
39+
40+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
41+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ZCZR (coordinator/router)`
42+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee ZCZR 4MB with spiffs`
43+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
44+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
45+
46+
## Troubleshooting
47+
48+
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.
49+
You can do the following:
50+
51+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
52+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
53+
54+
By default, the coordinator network is closed after rebooting or flashing new firmware.
55+
To open the network you have 2 options:
56+
57+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
58+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
59+
60+
61+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
62+
63+
* **LED not blinking:** Check the wiring connection and the IO selection.
64+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
65+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
66+
67+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
68+
69+
## Contribute
70+
71+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
72+
73+
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!
74+
75+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
76+
77+
## Resources
78+
79+
* Official ESP32 Forum: [Link](https://esp32.com)
80+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
81+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
82+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
83+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 simple Zigbee fan control.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a router device fan control.
19+
* The fan control is a Zigbee router device, which is controlled by a Zigbee coordinator.
20+
*
21+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
22+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
23+
*
24+
* Please check the README.md for instructions and more detailed description.
25+
*
26+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
27+
*/
28+
29+
#ifndef ZIGBEE_MODE_ZCZR
30+
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
31+
#endif
32+
33+
#include "Zigbee.h"
34+
35+
/* Zigbee light bulb configuration */
36+
#define ZIGBEE_FAN_CONTROL_ENDPOINT 1
37+
uint8_t led = RGB_BUILTIN; // To demonstrate the current fan control mode
38+
uint8_t button = BOOT_PIN;
39+
40+
ZigbeeFanControl zbFanControl = ZigbeeFanControl(ZIGBEE_FAN_CONTROL_ENDPOINT);
41+
42+
/********************* fan control callback function **************************/
43+
void setFan(ZigbeeFanMode mode) {
44+
switch (mode) {
45+
case FAN_MODE_OFF:
46+
rgbLedWrite(led, 0, 0, 0); // Off
47+
Serial.println("Fan mode: OFF");
48+
break;
49+
case FAN_MODE_LOW:
50+
rgbLedWrite(led, 0, 0, 255); // Blue
51+
Serial.println("Fan mode: LOW");
52+
break;
53+
case FAN_MODE_MEDIUM:
54+
rgbLedWrite(led, 255, 255, 0); // Yellow
55+
Serial.println("Fan mode: MEDIUM");
56+
break;
57+
case FAN_MODE_HIGH:
58+
rgbLedWrite(led, 255, 0, 0); // Red
59+
Serial.println("Fan mode: HIGH");
60+
break;
61+
case FAN_MODE_ON:
62+
rgbLedWrite(led, 255, 255, 255); // White
63+
Serial.println("Fan mode: ON");
64+
break;
65+
default:
66+
log_e("Unhandled fan mode: %d", mode);
67+
break;
68+
}
69+
}
70+
71+
/********************* Arduino functions **************************/
72+
void setup() {
73+
Serial.begin(115200);
74+
75+
// Init LED that will be used to indicate the current fan control mode
76+
rgbLedWrite(led, 0, 0, 0);
77+
78+
// Init button for factory reset
79+
pinMode(button, INPUT_PULLUP);
80+
81+
//Optional: set Zigbee device name and model
82+
zbFanControl.setManufacturerAndModel("Espressif", "ZBFanControl");
83+
84+
// Set the fan mode sequence to LOW_MED_HIGH
85+
zbFanControl.setFanModeSequence(FAN_MODE_SEQUENCE_LOW_MED_HIGH);
86+
87+
// Set callback function for fan mode change
88+
zbFanControl.onFanModeChange(setFan);
89+
90+
//Add endpoint to Zigbee Core
91+
Serial.println("Adding ZigbeeFanControl endpoint to Zigbee Core");
92+
Zigbee.addEndpoint(&zbFanControl);
93+
94+
// When all EPs are registered, start Zigbee in ROUTER mode
95+
if (!Zigbee.begin(ZIGBEE_ROUTER)) {
96+
Serial.println("Zigbee failed to start!");
97+
Serial.println("Rebooting...");
98+
ESP.restart();
99+
}
100+
Serial.println("Connecting to network");
101+
while (!Zigbee.connected()) {
102+
Serial.print(".");
103+
delay(100);
104+
}
105+
Serial.println();
106+
}
107+
108+
void loop() {
109+
// Checking button for factory reset
110+
if (digitalRead(button) == LOW) { // Push button pressed
111+
// Key debounce handling
112+
delay(100);
113+
int startTime = millis();
114+
while (digitalRead(button) == LOW) {
115+
delay(50);
116+
if ((millis() - startTime) > 3000) {
117+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
118+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
119+
delay(1000);
120+
Zigbee.factoryReset();
121+
}
122+
}
123+
}
124+
delay(100);
125+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee_zczr,ZigbeeMode=zczr",
3+
"requires": [
4+
"CONFIG_ZB_ENABLED=y"
5+
]
6+
}

libraries/Zigbee/keywords.txt

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,31 @@ Zigbee KEYWORD1
1212
ZigbeeEP KEYWORD1
1313

1414
# Endpoint Classes
15-
ZigbeeLight KEYWORD1
16-
ZigbeeSwitch KEYWORD1
17-
ZigbeeColorDimmableLight KEYWORD1
18-
ZigbeeColorDimmerSwitch KEYWORD1
19-
ZigbeeTempSensor KEYWORD1
20-
ZigbeeThermostat KEYWORD1
21-
ZigbeeFlowSensor KEYWORD1
22-
ZigbeePressureSensor KEYWORD1
23-
ZigbeeOccupancySensor KEYWORD1
2415
ZigbeeAnalog KEYWORD1
16+
ZigbeeBinary KEYWORD1
2517
ZigbeeCarbonDioxideSensor KEYWORD1
18+
ZigbeeColorDimmableLight KEYWORD1
19+
ZigbeeColorDimmerSwitch KEYWORD1
2620
ZigbeeContactSwitch KEYWORD1
21+
ZigbeeDimableLight KEYWORD1
2722
ZigbeeDoorWindowHandle KEYWORD1
23+
ZigbeeElectricalMeasurement KEYWORD1
24+
ZigbeeFanControl KEYWORD1
25+
ZigbeeFlowSensor KEYWORD1
2826
ZigbeeGateway KEYWORD1
27+
ZigbeeIlluminanceSensor KEYWORD1
28+
ZigbeeLight KEYWORD1
29+
ZigbeeOccupancySensor KEYWORD1
30+
ZigbeePM25Sensor KEYWORD1
31+
ZigbeePowerOutlet KEYWORD1
32+
ZigbeePressureSensor KEYWORD1
2933
ZigbeeRangeExtender KEYWORD1
34+
ZigbeeSwitch KEYWORD1
35+
ZigbeeTempSensor KEYWORD1
36+
ZigbeeThermostat KEYWORD1
3037
ZigbeeVibrationSensor KEYWORD1
3138
ZigbeeWindowCovering KEYWORD1
32-
ZigbeeIlluminanceSensor KEYWORD1
33-
ZigbeePowerOutlet KEYWORD1
39+
ZigbeeWindSpeedSensor KEYWORD1
3440

3541
# Other
3642
zigbee_role_t KEYWORD1
@@ -39,6 +45,8 @@ zb_device_params_t KEYWORD1
3945
zigbee_scan_result_t KEYWORD1
4046
zb_power_source_t KEYWORD1
4147
ZigbeeWindowCoveringType KEYWORD1
48+
ZigbeeFanMode KEYWORD1
49+
ZigbeeFanModeSequence KEYWORD1
4250

4351
#######################################
4452
# Methods and Functions (KEYWORD2)
@@ -73,13 +81,21 @@ printBoundDevices KEYWORD2
7381
getBoundDevices KEYWORD2
7482
bound KEYWORD2
7583
allowMultipleBinding KEYWORD2
84+
setManualBinding KEYWORD2
7685
setManufacturerAndModel KEYWORD2
7786
setPowerSource KEYWORD2
7887
setBatteryPercentage KEYWORD2
7988
reportBatteryPercentage KEYWORD2
8089
readManufacturer KEYWORD2
8190
readModel KEYWORD2
8291
onIdentify KEYWORD2
92+
addTimeCluster KEYWORD2
93+
setTime KEYWORD2
94+
setTimezone KEYWORD2
95+
getTime KEYWORD2
96+
getTimezone KEYWORD2
97+
addOTAClient KEYWORD2
98+
clearBoundDevices KEYWORD2
8399

84100
# ZigbeeLight + ZigbeeColorDimmableLight
85101
onLightChange KEYWORD2
@@ -171,7 +187,7 @@ setTilted KEYWORD2
171187
# ZigbeeVibrationSensor
172188
setVibration KEYWORD2
173189

174-
ZigbeeWindowCovering
190+
# ZigbeeWindowCovering
175191
onOpen KEYWORD2
176192
onClose KEYWORD2
177193
onGoToLiftPercentage KEYWORD2
@@ -186,6 +202,26 @@ setConfigStatus KEYWORD2
186202
setMode KEYWORD2
187203
setLimits KEYWORD2
188204

205+
# ZigbeeBinary
206+
addBinaryInput KEYWORD2
207+
addBinaryOutput KEYWORD2
208+
onBinaryOutputChange KEYWORD2
209+
setBinaryInput KEYWORD2
210+
setBinaryOutput KEYWORD2
211+
getBinaryOutput KEYWORD2
212+
reportBinaryInput KEYWORD2
213+
reportBinaryOutput KEYWORD2
214+
setBinaryInputApplication KEYWORD2
215+
setBinaryInputDescription KEYWORD2
216+
setBinaryOutputApplication KEYWORD2
217+
setBinaryOutputDescription KEYWORD2
218+
219+
# ZigbeeFanControl
220+
setFanModeSequence KEYWORD2
221+
getFanMode KEYWORD2
222+
getFanModeSequence KEYWORD2
223+
onFanModeChange KEYWORD2
224+
189225
#######################################
190226
# Constants (LITERAL1)
191227
#######################################

libraries/Zigbee/src/Zigbee.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "ep/ZigbeeLight.h"
1717
//// Controllers
1818
#include "ep/ZigbeeThermostat.h"
19+
#include "ep/ZigbeeFanControl.h"
1920
////Outlets
2021
#include "ep/ZigbeePowerOutlet.h"
2122
//// Sensors

0 commit comments

Comments
 (0)