Skip to content

Commit 5b9eff3

Browse files
committed
feat(zigbee): Add multistate endpoint support
1 parent 6a5839a commit 5b9eff3

File tree

8 files changed

+892
-0
lines changed

8 files changed

+892
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
306306
libraries/Zigbee/src/ep/ZigbeeBinary.cpp
307307
libraries/Zigbee/src/ep/ZigbeePowerOutlet.cpp
308308
libraries/Zigbee/src/ep/ZigbeeFanControl.cpp
309+
libraries/Zigbee/src/ep/ZigbeeMultistate.cpp
309310
)
310311

311312
set(ARDUINO_LIBRARY_BLE_SRCS
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Arduino-ESP32 Zigbee Multistate Input Output Example
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) multistate input/output 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+
## Multistate Device Functions
13+
14+
This example demonstrates two different multistate devices:
15+
16+
1. **Standard Multistate Device** (`zbMultistateDevice`): Uses predefined application states from the Zigbee specification
17+
- Application Type 0: Fan states (Off, On, Auto)
18+
- Application Type 7: Light states (High, Normal, Low)
19+
20+
2. **Custom Multistate Device** (`zbMultistateDeviceCustom`): Uses user-defined custom states
21+
- Custom fan states: Off, On, UltraSlow, Slow, Fast, SuperFast
22+
23+
* After this board first starts up, it will be configured as two multistate devices with different state configurations.
24+
* By clicking the button (BOOT) on this board, the devices will cycle through their respective states and report the changes to the network.
25+
26+
## Hardware Required
27+
28+
* A USB cable for power supply and programming
29+
30+
### Configure the Project
31+
32+
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).
33+
34+
The example creates two multistate devices:
35+
- **Endpoint 1**: Standard multistate device using predefined Zigbee application types
36+
- **Endpoint 2**: Custom multistate device using user-defined states
37+
38+
You can modify the state names and configurations by changing the following variables:
39+
- `multistate_custom_state_names[]`: Array of custom state names
40+
- Application type and lenght macros: `ZB_MULTISTATE_APPLICATION_TYPE_X_STATE_NAMES`,
41+
`ZB_MULTISTATE_APPLICATION_TYPE_X_NUM_STATES`, `ZB_MULTISTATE_APPLICATION_TYPE_X_INDEX`
42+
- Device descriptions and application types in the setup() function
43+
44+
#### Using Arduino IDE
45+
46+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
47+
48+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
49+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
50+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
51+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
52+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
53+
54+
## Troubleshooting
55+
56+
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.
57+
You can do the following:
58+
59+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
60+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
61+
62+
By default, the coordinator network is closed after rebooting or flashing new firmware.
63+
To open the network you have 2 options:
64+
65+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
66+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
67+
68+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
69+
70+
* **LED not blinking:** Check the wiring connection and the IO selection.
71+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
72+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
73+
74+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
75+
76+
## Contribute
77+
78+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
79+
80+
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!
81+
82+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
83+
84+
## Resources
85+
86+
* Official ESP32 Forum: [Link](https://esp32.com)
87+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
88+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
89+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
90+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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 multistate input / output device.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device multistate device.
19+
* In the example, we have two multistate devices:
20+
* - zbMultistateDevice: uses defined application states from Zigbee specification
21+
* - zbMultistateDeviceCustom: uses custom application states (user defined)
22+
*
23+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
24+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
25+
*
26+
* Please check the README.md for instructions and more detailed description.
27+
*
28+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
29+
* Modified by Pat Clay
30+
*/
31+
32+
#ifndef ZIGBEE_MODE_ZCZR
33+
#error "Zigbee coordinator/router device mode is not selected in Tools->Zigbee mode"
34+
#endif
35+
36+
#include "Zigbee.h"
37+
38+
/* Zigbee multistate device configuration */
39+
#define MULTISTATE_DEVICE_ENDPOINT_NUMBER 1
40+
41+
uint8_t button = BOOT_PIN;
42+
43+
// zbMultistateDevice will use defined application states
44+
ZigbeeMultistate zbMultistateDevice = ZigbeeMultistate(MULTISTATE_DEVICE_ENDPOINT_NUMBER);
45+
46+
// zbMultistateDeviceCustom will use custom application states (user defined)
47+
ZigbeeMultistate zbMultistateDeviceCustom = ZigbeeMultistate(MULTISTATE_DEVICE_ENDPOINT_NUMBER + 1);
48+
49+
const char* multistate_custom_state_names[6] = {"Off", "On", "UltraSlow", "Slow", "Fast", "SuperFast"};
50+
51+
void onStateChange(uint16_t state) {
52+
// print the state
53+
Serial.printf("Received state change: %d\r\n", state);
54+
// print the state name using the stored state names
55+
const char* const* state_names = zbMultistateDevice.getMultistateOutputStateNames();
56+
if (state_names && state < zbMultistateDevice.getMultistateOutputStateNamesLength()) {
57+
Serial.printf("State name: %s\r\n", state_names[state]);
58+
}
59+
// print state index of possible options
60+
Serial.printf("State index: %d / %d\r\n", state, zbMultistateDevice.getMultistateOutputStateNamesLength() - 1);
61+
}
62+
63+
void onStateChangeCustom(uint16_t state) {
64+
// print the state
65+
Serial.printf("Received state change: %d\r\n", state);
66+
// print the state name using the stored state names
67+
const char* const* state_names = zbMultistateDeviceCustom.getMultistateOutputStateNames();
68+
if (state_names && state < zbMultistateDeviceCustom.getMultistateOutputStateNamesLength()) {
69+
Serial.printf("State name: %s\r\n", state_names[state]);
70+
}
71+
// print state index of possible options
72+
Serial.printf("State index: %d / %d\r\n", state, zbMultistateDevice.getMultistateOutputStateNamesLength() - 1);
73+
74+
Serial.print("Changing to fan mode to: ");
75+
switch (state) {
76+
case 0:
77+
Serial.println("Off");
78+
break;
79+
case 1:
80+
Serial.println("On");
81+
break;
82+
case 2:
83+
Serial.println("Slow");
84+
break;
85+
case 3:
86+
Serial.println("Medium");
87+
break;
88+
case 4:
89+
Serial.println("Fast");
90+
break;
91+
default:
92+
Serial.println("Invalid state");
93+
break;
94+
}
95+
}
96+
97+
void setup() {
98+
Serial.begin(115200);
99+
100+
// Init button switch
101+
pinMode(button, INPUT_PULLUP);
102+
103+
// Optional: set Zigbee device name and model
104+
zbMultistateDevice.setManufacturerAndModel("Espressif", "ZigbeeMultistateDevice");
105+
106+
// Set up analog input
107+
zbMultistateDevice.addMultistateInput();
108+
zbMultistateDevice.setMultistateInputApplication(ZB_MULTISTATE_APPLICATION_TYPE_0_INDEX);
109+
zbMultistateDevice.setMultistateInputDescription("Fan (on/off/auto)");
110+
zbMultistateDevice.setMultistateInputStates(ZB_MULTISTATE_APPLICATION_TYPE_0_STATE_NAMES, ZB_MULTISTATE_APPLICATION_TYPE_0_NUM_STATES);
111+
112+
// Set up analog output
113+
zbMultistateDevice.addMultistateOutput();
114+
zbMultistateDevice.setMultistateOutputApplication(ZB_MULTISTATE_APPLICATION_TYPE_7_INDEX);
115+
zbMultistateDevice.setMultistateOutputDescription("Light (high/normal/low)");
116+
zbMultistateDevice.setMultistateOutputStates(ZB_MULTISTATE_APPLICATION_TYPE_7_STATE_NAMES, ZB_MULTISTATE_APPLICATION_TYPE_7_NUM_STATES);
117+
118+
// Set up custom output
119+
zbMultistateDeviceCustom.addMultistateOutput();
120+
zbMultistateDeviceCustom.setMultistateOutputApplication(ZB_MULTISTATE_APPLICATION_TYPE_OTHER_INDEX);
121+
zbMultistateDeviceCustom.setMultistateOutputDescription("Fan (on/off/slow/medium/fast)");
122+
zbMultistateDeviceCustom.setMultistateOutputStates(multistate_custom_state_names, 5);
123+
124+
// Set callback function for multistate output change
125+
zbMultistateDevice.onMultistateOutputChange(onStateChange);
126+
zbMultistateDeviceCustom.onMultistateOutputChange(onStateChangeCustom);
127+
128+
// Add endpoints to Zigbee Core
129+
Zigbee.addEndpoint(&zbMultistateDevice);
130+
Zigbee.addEndpoint(&zbMultistateDeviceCustom);
131+
132+
Serial.println("Starting Zigbee...");
133+
// When all EPs are registered, start Zigbee in Router Device mode
134+
if (!Zigbee.begin(ZIGBEE_ROUTER)) {
135+
Serial.println("Zigbee failed to start!");
136+
Serial.println("Rebooting...");
137+
ESP.restart();
138+
} else {
139+
Serial.println("Zigbee started successfully!");
140+
}
141+
Serial.println("Connecting to network");
142+
while (!Zigbee.connected()) {
143+
Serial.print(".");
144+
delay(100);
145+
}
146+
Serial.println("Connected");
147+
}
148+
149+
void loop() {
150+
static uint32_t timeCounter = 0;
151+
152+
// Checking button for factory reset and reporting
153+
if (digitalRead(button) == LOW) { // Push button pressed
154+
// Key debounce handling
155+
delay(100);
156+
int startTime = millis();
157+
while (digitalRead(button) == LOW) {
158+
delay(50);
159+
if ((millis() - startTime) > 3000) {
160+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
161+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
162+
delay(1000);
163+
Zigbee.factoryReset();
164+
}
165+
}
166+
// For demonstration purposes, increment the multistate output/input value by 1
167+
if (zbMultistateDevice.getMultistateOutput() < zbMultistateDevice.getMultistateOutputStateNamesLength() - 1) {
168+
zbMultistateDevice.setMultistateOutput(zbMultistateDevice.getMultistateOutput() + 1);
169+
zbMultistateDevice.reportMultistateOutput();
170+
zbMultistateDevice.setMultistateInput(zbMultistateDevice.getMultistateOutput());
171+
zbMultistateDevice.reportMultistateInput();
172+
} else {
173+
zbMultistateDevice.setMultistateOutput(0);
174+
zbMultistateDevice.reportMultistateOutput();
175+
zbMultistateDevice.setMultistateInput(zbMultistateDevice.getMultistateOutput());
176+
zbMultistateDevice.reportMultistateInput();
177+
}
178+
179+
if (zbMultistateDeviceCustom.getMultistateOutput() < zbMultistateDeviceCustom.getMultistateOutputStateNamesLength() - 1) {
180+
zbMultistateDeviceCustom.setMultistateOutput(zbMultistateDeviceCustom.getMultistateOutput() + 1);
181+
zbMultistateDeviceCustom.reportMultistateOutput();
182+
} else {
183+
zbMultistateDeviceCustom.setMultistateOutput(0);
184+
zbMultistateDeviceCustom.reportMultistateOutput();
185+
}
186+
}
187+
delay(100);
188+
}
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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ZigbeeFlowSensor KEYWORD1
2626
ZigbeeGateway KEYWORD1
2727
ZigbeeIlluminanceSensor KEYWORD1
2828
ZigbeeLight KEYWORD1
29+
ZigbeeMultistate KEYWORD1
2930
ZigbeeOccupancySensor KEYWORD1
3031
ZigbeePM25Sensor KEYWORD1
3132
ZigbeePowerOutlet KEYWORD1
@@ -222,6 +223,27 @@ getFanMode KEYWORD2
222223
getFanModeSequence KEYWORD2
223224
onFanModeChange KEYWORD2
224225

226+
# ZigbeeMultistate
227+
addMultistateInput KEYWORD2
228+
addMultistateOutput KEYWORD2
229+
onMultistateOutputChange KEYWORD2
230+
setMultistateInput KEYWORD2
231+
getMultistateInput KEYWORD2
232+
setMultistateOutput KEYWORD2
233+
getMultistateOutput KEYWORD2
234+
reportMultistateInput KEYWORD2
235+
reportMultistateOutput KEYWORD2
236+
setMultistateInputApplication KEYWORD2
237+
setMultistateInputDescription KEYWORD2
238+
setMultistateInputStates KEYWORD2
239+
setMultistateOutputApplication KEYWORD2
240+
setMultistateOutputDescription KEYWORD2
241+
setMultistateOutputStates KEYWORD2
242+
getMultistateInputStateNames KEYWORD2
243+
getMultistateInputStateNamesLength KEYWORD2
244+
getMultistateOutputStateNames KEYWORD2
245+
getMultistateOutputStateNamesLength KEYWORD2
246+
225247
#######################################
226248
# Constants (LITERAL1)
227249
#######################################
@@ -236,3 +258,42 @@ ZIGBEE_DEFAULT_RADIO_CONFIG LITERAL1
236258
ZIGBEE_DEFAULT_UART_RCP_RADIO_CONFIG LITERAL1
237259
ZIGBEE_DEFAULT_HOST_CONFIG LITERAL1
238260
ZB_ARRAY_LENGHT LITERAL1
261+
262+
# ZigbeeMultistate
263+
ZB_MULTISTATE_APPLICATION_TYPE_0_INDEX LITERAL1
264+
ZB_MULTISTATE_APPLICATION_TYPE_0_NUM_STATES LITERAL1
265+
ZB_MULTISTATE_APPLICATION_TYPE_0_STATE_NAMES LITERAL1
266+
ZB_MULTISTATE_APPLICATION_TYPE_1_INDEX LITERAL1
267+
ZB_MULTISTATE_APPLICATION_TYPE_1_NUM_STATES LITERAL1
268+
ZB_MULTISTATE_APPLICATION_TYPE_1_STATE_NAMES LITERAL1
269+
ZB_MULTISTATE_APPLICATION_TYPE_2_INDEX LITERAL1
270+
ZB_MULTISTATE_APPLICATION_TYPE_2_NUM_STATES LITERAL1
271+
ZB_MULTISTATE_APPLICATION_TYPE_2_STATE_NAMES LITERAL1
272+
ZB_MULTISTATE_APPLICATION_TYPE_3_INDEX LITERAL1
273+
ZB_MULTISTATE_APPLICATION_TYPE_3_NUM_STATES LITERAL1
274+
ZB_MULTISTATE_APPLICATION_TYPE_3_STATE_NAMES LITERAL1
275+
ZB_MULTISTATE_APPLICATION_TYPE_4_INDEX LITERAL1
276+
ZB_MULTISTATE_APPLICATION_TYPE_4_NUM_STATES LITERAL1
277+
ZB_MULTISTATE_APPLICATION_TYPE_4_STATE_NAMES LITERAL1
278+
ZB_MULTISTATE_APPLICATION_TYPE_5_INDEX LITERAL1
279+
ZB_MULTISTATE_APPLICATION_TYPE_5_NUM_STATES LITERAL1
280+
ZB_MULTISTATE_APPLICATION_TYPE_5_STATE_NAMES LITERAL1
281+
ZB_MULTISTATE_APPLICATION_TYPE_6_INDEX LITERAL1
282+
ZB_MULTISTATE_APPLICATION_TYPE_6_NUM_STATES LITERAL1
283+
ZB_MULTISTATE_APPLICATION_TYPE_6_STATE_NAMES LITERAL1
284+
ZB_MULTISTATE_APPLICATION_TYPE_7_INDEX LITERAL1
285+
ZB_MULTISTATE_APPLICATION_TYPE_7_NUM_STATES LITERAL1
286+
ZB_MULTISTATE_APPLICATION_TYPE_7_STATE_NAMES LITERAL1
287+
ZB_MULTISTATE_APPLICATION_TYPE_8_INDEX LITERAL1
288+
ZB_MULTISTATE_APPLICATION_TYPE_8_NUM_STATES LITERAL1
289+
ZB_MULTISTATE_APPLICATION_TYPE_8_STATE_NAMES LITERAL1
290+
ZB_MULTISTATE_APPLICATION_TYPE_9_INDEX LITERAL1
291+
ZB_MULTISTATE_APPLICATION_TYPE_9_NUM_STATES LITERAL1
292+
ZB_MULTISTATE_APPLICATION_TYPE_9_STATE_NAMES LITERAL1
293+
ZB_MULTISTATE_APPLICATION_TYPE_10_INDEX LITERAL1
294+
ZB_MULTISTATE_APPLICATION_TYPE_10_NUM_STATES LITERAL1
295+
ZB_MULTISTATE_APPLICATION_TYPE_10_STATE_NAMES LITERAL1
296+
ZB_MULTISTATE_APPLICATION_TYPE_11_INDEX LITERAL1
297+
ZB_MULTISTATE_APPLICATION_TYPE_11_NUM_STATES LITERAL1
298+
ZB_MULTISTATE_APPLICATION_TYPE_11_STATE_NAMES LITERAL1
299+
ZB_MULTISTATE_APPLICATION_TYPE_OTHER_INDEX LITERAL1

libraries/Zigbee/src/Zigbee.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "ep/ZigbeeElectricalMeasurement.h"
2929
#include "ep/ZigbeeFlowSensor.h"
3030
#include "ep/ZigbeeIlluminanceSensor.h"
31+
#include "ep/ZigbeeMultistate.h"
3132
#include "ep/ZigbeeOccupancySensor.h"
3233
#include "ep/ZigbeePM25Sensor.h"
3334
#include "ep/ZigbeePressureSensor.h"

0 commit comments

Comments
 (0)