-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Merged
me-no-dev
merged 5 commits into
espressif:master
from
P-R-O-C-H-Y:feat/zigbee-input-output-clusters
May 27, 2025
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a8be5fb
feat(zigbee): Add binary input and analog extension
P-R-O-C-H-Y 03fff97
Apply suggestions from code review
P-R-O-C-H-Y 6392dd3
feat(zigbee): Remove setBinaryInputReporting
P-R-O-C-H-Y 66bfbce
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] decf71b
Merge branch 'master' into feat/zigbee-input-output-clusters
me-no-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
121 changes: 121 additions & 0 deletions
121
libraries/Zigbee/examples/Zigbee_Binary_Input/Zigbee_Binary_Input.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// 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 analog input / output device. | ||
* | ||
* The example demonstrates how to use Zigbee library to create a end device analog device. | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* 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/) | ||
* Modified by Pat Clay | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
|
||
#ifndef ZIGBEE_MODE_ED | ||
#error "Zigbee end device mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "Zigbee.h" | ||
|
||
/* Zigbee analog device configuration */ | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#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 onAnalogOutputChange(float analog_output) { | ||
Serial.printf("Received analog output change: %.1f\r\n", analog_output); | ||
} | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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"); | ||
|
||
// // Optional: Add reporting for analog input | ||
// zbBinaryFan.setBinaryInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10 | ||
// zbBinaryZone.setBinaryInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10 | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.