Skip to content

Commit a8be5fb

Browse files
committed
feat(zigbee): Add binary input and analog extension
1 parent 543fad2 commit a8be5fb

File tree

10 files changed

+741
-3
lines changed

10 files changed

+741
-3
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
301301
libraries/Zigbee/src/ep/ZigbeeWindSpeedSensor.cpp
302302
libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp
303303
libraries/Zigbee/src/ep/ZigbeePM25Sensor.cpp
304+
libraries/Zigbee/src/ep/ZigbeeBinary.cpp
304305
)
305306

306307
set(ARDUINO_LIBRARY_BLE_SRCS

libraries/Zigbee/examples/Zigbee_Analog_Input_Output/Zigbee_Analog_Input_Output.ino

+38-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ uint8_t analogPin = A0;
3939
uint8_t button = BOOT_PIN;
4040

4141
ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER);
42+
ZigbeeAnalog zbAnalogTemp = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER + 1);
43+
ZigbeeAnalog zbAnalogFan = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER + 2);
44+
ZigbeeAnalog zbAnalogPercent = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER + 3);
4245

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

60-
// Add analog clusters to Zigbee Analog according your needs
63+
// Set up analog input
6164
zbAnalogDevice.addAnalogInput();
65+
zbAnalogDevice.setAnalogInputApplication(ESP_ZB_ZCL_AI_POWER_IN_WATTS_CONSUMPTION);
66+
zbAnalogDevice.setAnalogInputDescription("Power Consumption (Watts)");
67+
zbAnalogDevice.setAnalogInputResolution(0.01);
68+
69+
// Set up analog output
6270
zbAnalogDevice.addAnalogOutput();
71+
zbAnalogDevice.setAnalogOutputApplication(ESP_ZB_ZCL_AI_RPM_OTHER);
72+
zbAnalogDevice.setAnalogOutputDescription("Fan Speed (RPM)");
6373

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

77+
// Set up analog input
78+
zbAnalogTemp.addAnalogInput();
79+
zbAnalogTemp.setAnalogInputApplication(ESP_ZB_ZCL_AI_TEMPERATURE_OTHER);
80+
zbAnalogTemp.setAnalogInputDescription("Temperature");
81+
zbAnalogTemp.setAnalogInputResolution(0.1);
82+
83+
// Set up analog input
84+
zbAnalogFan.addAnalogInput();
85+
zbAnalogFan.setAnalogInputApplication(ESP_ZB_ZCL_AI_RPM_OTHER);
86+
zbAnalogFan.setAnalogInputDescription("RPM");
87+
zbAnalogFan.setAnalogInputResolution(1);
88+
89+
// Set up analog input
90+
zbAnalogPercent.addAnalogInput();
91+
zbAnalogPercent.setAnalogInputApplication(ESP_ZB_ZCL_AI_PERCENTAGE_OTHER);
92+
zbAnalogPercent.setAnalogInputDescription("Percentage");
93+
zbAnalogPercent.setAnalogInputResolution(0.01);
94+
6795
// Add endpoints to Zigbee Core
6896
Zigbee.addEndpoint(&zbAnalogDevice);
97+
Zigbee.addEndpoint(&zbAnalogTemp);
98+
Zigbee.addEndpoint(&zbAnalogFan);
99+
Zigbee.addEndpoint(&zbAnalogPercent);
69100

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

99133
// Analog input supports reporting
100134
zbAnalogDevice.reportAnalogInput();
135+
zbAnalogTemp.reportAnalogInput();
136+
zbAnalogFan.reportAnalogInput();
137+
zbAnalogPercent.reportAnalogInput();
101138
}
102139

103140
// Checking button for factory reset and reporting
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Arduino-ESP32 Zigbee Binary Input Example
2+
3+
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.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## Binary Input Functions
13+
14+
* The example implements two binary inputs:
15+
- HVAC Fan Status: Reports the current state of a fan
16+
- Security Zone Armed: Reports the armed state of a security zone
17+
* 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.
18+
* Holding the button for more than 3 seconds will trigger a factory reset of the Zigbee device.
19+
20+
## Hardware Required
21+
22+
* A USB cable for power supply and programming
23+
24+
### Configure the Project
25+
26+
The example uses the following default pins:
27+
* Button: `BOOT_PIN` (BOOT button on ESP32-C6 and ESP32-H2)
28+
29+
#### Using Arduino IDE
30+
31+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
32+
33+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
34+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
35+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
36+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
37+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
38+
39+
## Troubleshooting
40+
41+
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.
42+
You can do the following:
43+
44+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
45+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
46+
47+
By default, the coordinator network is closed after rebooting or flashing new firmware.
48+
To open the network you have 2 options:
49+
50+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
51+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
52+
53+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
54+
55+
* **LED not blinking:** Check the wiring connection and the IO selection.
56+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
57+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
58+
59+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
60+
61+
## Contribute
62+
63+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
64+
65+
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!
66+
67+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
68+
69+
## Resources
70+
71+
* Official ESP32 Forum: [Link](https://esp32.com)
72+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
73+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
74+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
75+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 analog input / output device.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device analog device.
19+
*
20+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
21+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
22+
*
23+
* Please check the README.md for instructions and more detailed description.
24+
*
25+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
26+
* Modified by Pat Clay
27+
*/
28+
29+
#ifndef ZIGBEE_MODE_ED
30+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
31+
#endif
32+
33+
#include "Zigbee.h"
34+
35+
/* Zigbee analog device configuration */
36+
#define BINARY_DEVICE_ENDPOINT_NUMBER 1
37+
38+
uint8_t binaryPin = A0;
39+
uint8_t button = BOOT_PIN;
40+
41+
ZigbeeBinary zbBinaryFan = ZigbeeBinary(BINARY_DEVICE_ENDPOINT_NUMBER);
42+
ZigbeeBinary zbBinaryZone = ZigbeeBinary(BINARY_DEVICE_ENDPOINT_NUMBER + 1);
43+
44+
bool binaryStatus = false;
45+
46+
void onAnalogOutputChange(float analog_output) {
47+
Serial.printf("Received analog output change: %.1f\r\n", analog_output);
48+
}
49+
50+
void setup() {
51+
Serial.begin(115200);
52+
Serial.println("Starting...");
53+
54+
// Init button switch
55+
pinMode(button, INPUT_PULLUP);
56+
57+
// Set analog resolution to 10 bits
58+
analogReadResolution(10);
59+
60+
// Optional: set Zigbee device name and model
61+
zbBinaryFan.setManufacturerAndModel("Espressif", "ZigbeeBinarySensor");
62+
63+
// Set up binary fan status input (HVAC)
64+
zbBinaryFan.addBinaryInput();
65+
zbBinaryFan.setBinaryInputApplication(BINARY_INPUT_APPLICATION_TYPE_HVAC_FAN_STATUS);
66+
zbBinaryFan.setBinaryInputDescription("Fan Status");
67+
68+
// Set up binary zone armed input (Security)
69+
zbBinaryZone.addBinaryInput();
70+
zbBinaryZone.setBinaryInputApplication(BINARY_INPUT_APPLICATION_TYPE_SECURITY_ZONE_ARMED);
71+
zbBinaryZone.setBinaryInputDescription("Zone Armed");
72+
73+
// Add endpoints to Zigbee Core
74+
Zigbee.addEndpoint(&zbBinaryFan);
75+
Zigbee.addEndpoint(&zbBinaryZone);
76+
77+
Serial.println("Starting Zigbee...");
78+
// When all EPs are registered, start Zigbee in End Device mode
79+
if (!Zigbee.begin()) {
80+
Serial.println("Zigbee failed to start!");
81+
Serial.println("Rebooting...");
82+
ESP.restart();
83+
} else {
84+
Serial.println("Zigbee started successfully!");
85+
}
86+
Serial.println("Connecting to network");
87+
while (!Zigbee.connected()) {
88+
Serial.print(".");
89+
delay(100);
90+
}
91+
Serial.println("Connected");
92+
93+
// // Optional: Add reporting for analog input
94+
// zbBinaryFan.setBinaryInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10
95+
// zbBinaryZone.setBinaryInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10
96+
}
97+
98+
void loop() {
99+
// Checking button for factory reset and reporting
100+
if (digitalRead(button) == LOW) { // Push button pressed
101+
// Key debounce handling
102+
delay(100);
103+
int startTime = millis();
104+
while (digitalRead(button) == LOW) {
105+
delay(50);
106+
if ((millis() - startTime) > 3000) {
107+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
108+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
109+
delay(1000);
110+
Zigbee.factoryReset();
111+
}
112+
}
113+
// Toggle binary input
114+
binaryStatus = !binaryStatus;
115+
zbBinaryFan.setBinaryInput(binaryStatus);
116+
zbBinaryZone.setBinaryInput(binaryStatus);
117+
zbBinaryFan.reportBinaryInput();
118+
zbBinaryZone.reportBinaryInput();
119+
}
120+
delay(100);
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y",
5+
"CONFIG_ZB_ENABLED=y"
6+
]
7+
}

libraries/Zigbee/src/Zigbee.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ep/ZigbeeThermostat.h"
1919
//// Sensors
2020
#include "ep/ZigbeeAnalog.h"
21+
#include "ep/ZigbeeBinary.h"
2122
#include "ep/ZigbeeCarbonDioxideSensor.h"
2223
#include "ep/ZigbeeContactSwitch.h"
2324
#include "ep/ZigbeeDoorWindowHandle.h"

0 commit comments

Comments
 (0)