Skip to content

feat(zigbee): Add Fan Control endpoint support #11559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
libraries/Zigbee/src/ep/ZigbeeElectricalMeasurement.cpp
libraries/Zigbee/src/ep/ZigbeeBinary.cpp
libraries/Zigbee/src/ep/ZigbeePowerOutlet.cpp
libraries/Zigbee/src/ep/ZigbeeFanControl.cpp
)

set(ARDUINO_LIBRARY_BLE_SRCS
Expand Down
83 changes: 83 additions & 0 deletions libraries/Zigbee/examples/Zigbee_Fan_Control/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Arduino-ESP32 Zigbee Fan Control Example

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.

# Supported Targets

Currently, this example supports the following targets.

| Supported Targets | ESP32-C6 | ESP32-H2 |
| ----------------- | -------- | -------- |

## Fan Control Functions

1. Initialize a Zigbee fan control device.
2. Control fan modes (OFF, LOW, MEDIUM, HIGH, ON).
3. Respond to fan control commands from the Zigbee network.

## Hardware Required

* ESP32-H2 or ESP32-C6 development board
* A USB cable for power supply and programming
* RGB LED for visual feedback (built-in on most development boards)

### Configure the Project

In this example the RGB LED is used to indicate the current fan control mode.
The LED colors represent different fan modes:
- OFF: No light
- LOW: Blue
- MEDIUM: Yellow
- HIGH: Red
- ON: White

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).

#### 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 ZCZR (coordinator/router)`
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee ZCZR 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)
129 changes: 129 additions & 0 deletions libraries/Zigbee/examples/Zigbee_Fan_Control/Zigbee_Fan_Control.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2025 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 simple Zigbee fan control.
*
* The example demonstrates how to use Zigbee library to create a router device fan control.
* The fan control is a Zigbee router 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 Jan Procházka (https://github.com/P-R-O-C-H-Y/)
*/

#ifndef ZIGBEE_MODE_ZCZR
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
#endif

#include "Zigbee.h"

/* Zigbee light bulb configuration */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Zigbee light bulb configuration */
/* Zigbee fan configuration */

#define ZIGBEE_FAN_CONTROL_ENDPOINT 1

#ifdef RGB_BUILTIN
uint8_t led = RGB_BUILTIN; // To demonstrate the current fan control mode
#else
uint8_t led = 2;
#endif

uint8_t button = BOOT_PIN;

ZigbeeFanControl zbFanControl = ZigbeeFanControl(ZIGBEE_FAN_CONTROL_ENDPOINT);

/********************* fan control callback function **************************/
void setFan(ZigbeeFanMode mode) {
switch (mode) {
case FAN_MODE_OFF:
rgbLedWrite(led, 0, 0, 0); // Off
Serial.println("Fan mode: OFF");
break;
case FAN_MODE_LOW:
rgbLedWrite(led, 0, 0, 255); // Blue
Serial.println("Fan mode: LOW");
break;
case FAN_MODE_MEDIUM:
rgbLedWrite(led, 255, 255, 0); // Yellow
Serial.println("Fan mode: MEDIUM");
break;
case FAN_MODE_HIGH:
rgbLedWrite(led, 255, 0, 0); // Red
Serial.println("Fan mode: HIGH");
break;
case FAN_MODE_ON:
rgbLedWrite(led, 255, 255, 255); // White
Serial.println("Fan mode: ON");
break;
default: log_e("Unhandled fan mode: %d", mode); break;
}
}

/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);

// Init LED that will be used to indicate the current fan control mode
rgbLedWrite(led, 0, 0, 0);

// Init button for factory reset
pinMode(button, INPUT_PULLUP);

//Optional: set Zigbee device name and model
zbFanControl.setManufacturerAndModel("Espressif", "ZBFanControl");

// Set the fan mode sequence to LOW_MED_HIGH
zbFanControl.setFanModeSequence(FAN_MODE_SEQUENCE_LOW_MED_HIGH);

// Set callback function for fan mode change
zbFanControl.onFanModeChange(setFan);

//Add endpoint to Zigbee Core
Serial.println("Adding ZigbeeFanControl endpoint to Zigbee Core");
Zigbee.addEndpoint(&zbFanControl);

// When all EPs are registered, start Zigbee in ROUTER mode
if (!Zigbee.begin(ZIGBEE_ROUTER)) {
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();
}
}
}
delay(100);
}
6 changes: 6 additions & 0 deletions libraries/Zigbee/examples/Zigbee_Fan_Control/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"fqbn_append": "PartitionScheme=zigbee_zczr,ZigbeeMode=zczr",
"requires": [
"CONFIG_ZB_ENABLED=y"
]
}
60 changes: 48 additions & 12 deletions libraries/Zigbee/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ Zigbee KEYWORD1
ZigbeeEP KEYWORD1

# Endpoint Classes
ZigbeeLight KEYWORD1
ZigbeeSwitch KEYWORD1
ZigbeeColorDimmableLight KEYWORD1
ZigbeeColorDimmerSwitch KEYWORD1
ZigbeeTempSensor KEYWORD1
ZigbeeThermostat KEYWORD1
ZigbeeFlowSensor KEYWORD1
ZigbeePressureSensor KEYWORD1
ZigbeeOccupancySensor KEYWORD1
ZigbeeAnalog KEYWORD1
ZigbeeBinary KEYWORD1
ZigbeeCarbonDioxideSensor KEYWORD1
ZigbeeColorDimmableLight KEYWORD1
ZigbeeColorDimmerSwitch KEYWORD1
ZigbeeContactSwitch KEYWORD1
ZigbeeDimableLight KEYWORD1
ZigbeeDoorWindowHandle KEYWORD1
ZigbeeElectricalMeasurement KEYWORD1
ZigbeeFanControl KEYWORD1
ZigbeeFlowSensor KEYWORD1
ZigbeeGateway KEYWORD1
ZigbeeIlluminanceSensor KEYWORD1
ZigbeeLight KEYWORD1
ZigbeeOccupancySensor KEYWORD1
ZigbeePM25Sensor KEYWORD1
ZigbeePowerOutlet KEYWORD1
ZigbeePressureSensor KEYWORD1
ZigbeeRangeExtender KEYWORD1
ZigbeeSwitch KEYWORD1
ZigbeeTempSensor KEYWORD1
ZigbeeThermostat KEYWORD1
ZigbeeVibrationSensor KEYWORD1
ZigbeeWindowCovering KEYWORD1
ZigbeeIlluminanceSensor KEYWORD1
ZigbeePowerOutlet KEYWORD1
ZigbeeWindSpeedSensor KEYWORD1

# Other
zigbee_role_t KEYWORD1
Expand All @@ -39,6 +45,8 @@ zb_device_params_t KEYWORD1
zigbee_scan_result_t KEYWORD1
zb_power_source_t KEYWORD1
ZigbeeWindowCoveringType KEYWORD1
ZigbeeFanMode KEYWORD1
ZigbeeFanModeSequence KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down Expand Up @@ -73,13 +81,21 @@ printBoundDevices KEYWORD2
getBoundDevices KEYWORD2
bound KEYWORD2
allowMultipleBinding KEYWORD2
setManualBinding KEYWORD2
setManufacturerAndModel KEYWORD2
setPowerSource KEYWORD2
setBatteryPercentage KEYWORD2
reportBatteryPercentage KEYWORD2
readManufacturer KEYWORD2
readModel KEYWORD2
onIdentify KEYWORD2
addTimeCluster KEYWORD2
setTime KEYWORD2
setTimezone KEYWORD2
getTime KEYWORD2
getTimezone KEYWORD2
addOTAClient KEYWORD2
clearBoundDevices KEYWORD2

# ZigbeeLight + ZigbeeColorDimmableLight
onLightChange KEYWORD2
Expand Down Expand Up @@ -171,7 +187,7 @@ setTilted KEYWORD2
# ZigbeeVibrationSensor
setVibration KEYWORD2

ZigbeeWindowCovering
# ZigbeeWindowCovering
onOpen KEYWORD2
onClose KEYWORD2
onGoToLiftPercentage KEYWORD2
Expand All @@ -186,6 +202,26 @@ setConfigStatus KEYWORD2
setMode KEYWORD2
setLimits KEYWORD2

# ZigbeeBinary
addBinaryInput KEYWORD2
addBinaryOutput KEYWORD2
onBinaryOutputChange KEYWORD2
setBinaryInput KEYWORD2
setBinaryOutput KEYWORD2
getBinaryOutput KEYWORD2
reportBinaryInput KEYWORD2
reportBinaryOutput KEYWORD2
setBinaryInputApplication KEYWORD2
setBinaryInputDescription KEYWORD2
setBinaryOutputApplication KEYWORD2
setBinaryOutputDescription KEYWORD2

# ZigbeeFanControl
setFanModeSequence KEYWORD2
getFanMode KEYWORD2
getFanModeSequence KEYWORD2
onFanModeChange KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
Expand Down
1 change: 1 addition & 0 deletions libraries/Zigbee/src/Zigbee.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ep/ZigbeeLight.h"
//// Controllers
#include "ep/ZigbeeThermostat.h"
#include "ep/ZigbeeFanControl.h"
////Outlets
#include "ep/ZigbeePowerOutlet.h"
//// Sensors
Expand Down
Loading
Loading