Skip to content

feat(zigbee): Add support for Binary input EP + Analog EP extension #11339

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 @@ -301,6 +301,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
libraries/Zigbee/src/ep/ZigbeeWindSpeedSensor.cpp
libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp
libraries/Zigbee/src/ep/ZigbeePM25Sensor.cpp
libraries/Zigbee/src/ep/ZigbeeBinary.cpp
)

set(ARDUINO_LIBRARY_BLE_SRCS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ uint8_t analogPin = A0;
uint8_t button = BOOT_PIN;

ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER);
ZigbeeAnalog zbAnalogTemp = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER + 1);
ZigbeeAnalog zbAnalogFan = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER + 2);
ZigbeeAnalog zbAnalogPercent = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER + 3);

void onAnalogOutputChange(float analog_output) {
Serial.printf("Received analog output change: %.1f\r\n", analog_output);
Expand All @@ -57,15 +60,43 @@ void setup() {
// Optional: set Zigbee device name and model
zbAnalogDevice.setManufacturerAndModel("Espressif", "ZigbeeAnalogDevice");

// Add analog clusters to Zigbee Analog according your needs
// Set up analog input
zbAnalogDevice.addAnalogInput();
zbAnalogDevice.setAnalogInputApplication(ESP_ZB_ZCL_AI_POWER_IN_WATTS_CONSUMPTION);
zbAnalogDevice.setAnalogInputDescription("Power Consumption (Watts)");
zbAnalogDevice.setAnalogInputResolution(0.01);

// Set up analog output
zbAnalogDevice.addAnalogOutput();
zbAnalogDevice.setAnalogOutputApplication(ESP_ZB_ZCL_AI_RPM_OTHER);
zbAnalogDevice.setAnalogOutputDescription("Fan Speed (RPM)");

// If analog output cluster is added, set callback function for analog output change
zbAnalogDevice.onAnalogOutputChange(onAnalogOutputChange);

// Set up analog input
zbAnalogTemp.addAnalogInput();
zbAnalogTemp.setAnalogInputApplication(ESP_ZB_ZCL_AI_TEMPERATURE_OTHER);
zbAnalogTemp.setAnalogInputDescription("Temperature");
zbAnalogTemp.setAnalogInputResolution(0.1);

// Set up analog input
zbAnalogFan.addAnalogInput();
zbAnalogFan.setAnalogInputApplication(ESP_ZB_ZCL_AI_RPM_OTHER);
zbAnalogFan.setAnalogInputDescription("RPM");
zbAnalogFan.setAnalogInputResolution(1);

// Set up analog input
zbAnalogPercent.addAnalogInput();
zbAnalogPercent.setAnalogInputApplication(ESP_ZB_ZCL_AI_PERCENTAGE_OTHER);
zbAnalogPercent.setAnalogInputDescription("Percentage");
zbAnalogPercent.setAnalogInputResolution(0.01);

// Add endpoints to Zigbee Core
Zigbee.addEndpoint(&zbAnalogDevice);
Zigbee.addEndpoint(&zbAnalogTemp);
Zigbee.addEndpoint(&zbAnalogFan);
Zigbee.addEndpoint(&zbAnalogPercent);

Serial.println("Starting Zigbee...");
// When all EPs are registered, start Zigbee in End Device mode
Expand Down Expand Up @@ -95,9 +126,15 @@ void loop() {
float analog = (float)analogRead(analogPin);
Serial.printf("Updating analog input to %.1f\r\n", analog);
zbAnalogDevice.setAnalogInput(analog);
zbAnalogTemp.setAnalogInput(analog/100);
zbAnalogFan.setAnalogInput(analog);
zbAnalogPercent.setAnalogInput(analog/10);

// Analog input supports reporting
zbAnalogDevice.reportAnalogInput();
zbAnalogTemp.reportAnalogInput();
zbAnalogFan.reportAnalogInput();
zbAnalogPercent.reportAnalogInput();
}

// Checking button for factory reset and reporting
Expand Down
75 changes: 75 additions & 0 deletions libraries/Zigbee/examples/Zigbee_Binary_Input/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Arduino-ESP32 Zigbee Binary Input Example

This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) binary input device with two different applications: HVAC fan status and security zone armed status.

# Supported Targets

Currently, this example supports the following targets.

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

## Binary Input Functions

* The example implements two binary inputs:
- HVAC Fan Status: Reports the current state of a fan
- Security Zone Armed: Reports the armed state of a security zone
* By clicking the button (BOOT) on this board, it will toggle both binary inputs and immediately send a report of their states to the network.
* Holding the button for more than 3 seconds will trigger a factory reset of the Zigbee device.

## Hardware Required

* A USB cable for power supply and programming

### Configure the Project

The example uses the following default pins:
* Button: `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 ED (end device)`
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 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)
112 changes: 112 additions & 0 deletions libraries/Zigbee/examples/Zigbee_Binary_Input/Zigbee_Binary_Input.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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 Zigbee binary input device.
*
* The example demonstrates how to use Zigbee library to create an end device binary sensor device.
*
* 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_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#endif

#include "Zigbee.h"

/* Zigbee binary sensor device configuration */
#define BINARY_DEVICE_ENDPOINT_NUMBER 1

uint8_t binaryPin = A0;
uint8_t button = BOOT_PIN;

ZigbeeBinary zbBinaryFan = ZigbeeBinary(BINARY_DEVICE_ENDPOINT_NUMBER);
ZigbeeBinary zbBinaryZone = ZigbeeBinary(BINARY_DEVICE_ENDPOINT_NUMBER + 1);

bool binaryStatus = false;

void setup() {
Serial.begin(115200);
Serial.println("Starting...");

// Init button switch
pinMode(button, INPUT_PULLUP);

// Set analog resolution to 10 bits
analogReadResolution(10);

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

// Set up binary fan status input (HVAC)
zbBinaryFan.addBinaryInput();
zbBinaryFan.setBinaryInputApplication(BINARY_INPUT_APPLICATION_TYPE_HVAC_FAN_STATUS);
zbBinaryFan.setBinaryInputDescription("Fan Status");

// Set up binary zone armed input (Security)
zbBinaryZone.addBinaryInput();
zbBinaryZone.setBinaryInputApplication(BINARY_INPUT_APPLICATION_TYPE_SECURITY_ZONE_ARMED);
zbBinaryZone.setBinaryInputDescription("Zone Armed");

// Add endpoints to Zigbee Core
Zigbee.addEndpoint(&zbBinaryFan);
Zigbee.addEndpoint(&zbBinaryZone);

Serial.println("Starting Zigbee...");
// When all EPs are registered, start Zigbee in End Device mode
if (!Zigbee.begin()) {
Serial.println("Zigbee failed to start!");
Serial.println("Rebooting...");
ESP.restart();
} else {
Serial.println("Zigbee started successfully!");
}
Serial.println("Connecting to network");
while (!Zigbee.connected()) {
Serial.print(".");
delay(100);
}
Serial.println("Connected");
}

void loop() {
// Checking button for factory reset and reporting
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();
}
}
// Toggle binary input
binaryStatus = !binaryStatus;
zbBinaryFan.setBinaryInput(binaryStatus);
zbBinaryZone.setBinaryInput(binaryStatus);
zbBinaryFan.reportBinaryInput();
zbBinaryZone.reportBinaryInput();
}
delay(100);
}
7 changes: 7 additions & 0 deletions libraries/Zigbee/examples/Zigbee_Binary_Input/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
"requires": [
"CONFIG_SOC_IEEE802154_SUPPORTED=y",
"CONFIG_ZB_ENABLED=y"
]
}
1 change: 1 addition & 0 deletions libraries/Zigbee/src/Zigbee.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "ep/ZigbeeThermostat.h"
//// Sensors
#include "ep/ZigbeeAnalog.h"
#include "ep/ZigbeeBinary.h"
#include "ep/ZigbeeCarbonDioxideSensor.h"
#include "ep/ZigbeeContactSwitch.h"
#include "ep/ZigbeeDoorWindowHandle.h"
Expand Down
Loading
Loading