Skip to content

Commit 9a783a5

Browse files
pwclayP-R-O-C-H-Yme-no-devpre-commit-ci-lite[bot]
authored
feat(zigbee): Add Analog endpoint device (input, output, value clusters) (#10950)
* add analog sensor modules * feat(zigbee): Add analog value, input and output support * fix(zigbee): add missing functiong to header file * fix(zigbee): Update log messages * ci(pre-commit): Apply automatic fixes * fix(example): Fix comment typo --------- Co-authored-by: Jan Procházka <[email protected]> Co-authored-by: Me No Dev <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent cd95e40 commit 9a783a5

File tree

8 files changed

+401
-1
lines changed

8 files changed

+401
-1
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
295295
libraries/Zigbee/src/ep/ZigbeeDoorWindowHandle.cpp
296296
libraries/Zigbee/src/ep/ZigbeeWindowCovering.cpp
297297
libraries/Zigbee/src/ep/ZigbeeVibrationSensor.cpp
298+
libraries/Zigbee/src/ep/ZigbeeAnalog.cpp
298299
libraries/Zigbee/src/ep/ZigbeeRangeExtender.cpp
299300
)
300301

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Arduino-ESP32 Zigbee Analog Input Output Example
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) analog 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+
## Analog Sensor Functions
13+
14+
* After this board first starts up, it would be configured locally to report an analog input on change or every 30 seconds.
15+
* By clicking the button (BOOT) on this board, this board will immediately send a report of the current measured value to the network.
16+
17+
## Hardware Required
18+
19+
* A USB cable for power supply and programming
20+
21+
### Configure the Project
22+
23+
Set the ADC GPIO by changing the `analogPin` variable. By default, it's the pin `A0`.
24+
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).
25+
26+
#### Using Arduino IDE
27+
28+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
29+
30+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
31+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
32+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
33+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
34+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
35+
36+
## Troubleshooting
37+
38+
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.
39+
You can do the following:
40+
41+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
42+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
43+
44+
By default, the coordinator network is closed after rebooting or flashing new firmware.
45+
To open the network you have 2 options:
46+
47+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
48+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
49+
50+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
51+
52+
* **LED not blinking:** Check the wiring connection and the IO selection.
53+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
54+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
55+
56+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
57+
58+
## Contribute
59+
60+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
61+
62+
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!
63+
64+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
65+
66+
## Resources
67+
68+
* Official ESP32 Forum: [Link](https://esp32.com)
69+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
70+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
71+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
72+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 ANALOG_DEVICE_ENDPOINT_NUMBER 1
37+
38+
uint8_t analogPin = A0;
39+
uint8_t button = BOOT_PIN;
40+
41+
ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER);
42+
43+
void onAnalogOutputChange(float analog_output) {
44+
Serial.printf("Received analog output change: %.1f\r\n", analog_output);
45+
}
46+
47+
void setup() {
48+
Serial.begin(115200);
49+
Serial.println("Starting...");
50+
51+
// Init button switch
52+
pinMode(button, INPUT_PULLUP);
53+
54+
// Set analog resolution to 10 bits
55+
analogReadResolution(10);
56+
57+
// Optional: set Zigbee device name and model
58+
zbAnalogDevice.setManufacturerAndModel("Espressif", "ZigbeeAnalogDevice");
59+
60+
// Add analog clusters to Zigbee Analog according your needs
61+
zbAnalogDevice.addAnalogInput();
62+
zbAnalogDevice.addAnalogOutput();
63+
64+
// If analog output cluster is added, set callback function for analog output change
65+
zbAnalogDevice.onAnalogOutputChange(onAnalogOutputChange);
66+
67+
// Add endpoints to Zigbee Core
68+
Zigbee.addEndpoint(&zbAnalogDevice);
69+
70+
Serial.println("Starting Zigbee...");
71+
// When all EPs are registered, start Zigbee in End Device mode
72+
if (!Zigbee.begin()) {
73+
Serial.println("Zigbee failed to start!");
74+
Serial.println("Rebooting...");
75+
ESP.restart();
76+
} else {
77+
Serial.println("Zigbee started successfully!");
78+
}
79+
Serial.println("Connecting to network");
80+
while (!Zigbee.connected()) {
81+
Serial.print(".");
82+
delay(100);
83+
}
84+
Serial.println("Connected");
85+
86+
// Optional: Add reporting for analog input
87+
zbAnalogDevice.setAnalogInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10
88+
}
89+
90+
void loop() {
91+
static uint32_t timeCounter = 0;
92+
93+
// Read ADC value and update the analog value every 2s
94+
if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s
95+
float analog = (float)analogRead(analogPin);
96+
Serial.printf("Updating analog input to %.1f\r\n", analog);
97+
zbAnalogDevice.setAnalogInput(analog);
98+
99+
// Analog input supports reporting
100+
zbAnalogDevice.reportAnalogInput();
101+
}
102+
103+
// Checking button for factory reset and reporting
104+
if (digitalRead(button) == LOW) { // Push button pressed
105+
// Key debounce handling
106+
delay(100);
107+
int startTime = millis();
108+
while (digitalRead(button) == LOW) {
109+
delay(50);
110+
if ((millis() - startTime) > 3000) {
111+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
112+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
113+
delay(1000);
114+
Zigbee.factoryReset();
115+
}
116+
}
117+
}
118+
delay(100);
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
5+
]
6+
}

libraries/Zigbee/src/Zigbee.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "ep/ZigbeeTempSensor.h"
1616
#include "ep/ZigbeeThermostat.h"
1717
#include "ep/ZigbeePressureSensor.h"
18+
#include "ep/ZigbeeAnalog.h"
1819
#include "ep/ZigbeeFlowSensor.h"
1920
#include "ep/ZigbeeOccupancySensor.h"
2021
#include "ep/ZigbeeCarbonDioxideSensor.h"
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include "ZigbeeAnalog.h"
2+
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
3+
4+
esp_zb_cluster_list_t *zigbee_analog_clusters_create(zigbee_analog_cfg_t *analog_sensor) {
5+
esp_zb_basic_cluster_cfg_t *basic_cfg = analog_sensor ? &(analog_sensor->basic_cfg) : NULL;
6+
esp_zb_identify_cluster_cfg_t *identify_cfg = analog_sensor ? &(analog_sensor->identify_cfg) : NULL;
7+
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
8+
esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
9+
esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
10+
return cluster_list;
11+
}
12+
13+
ZigbeeAnalog::ZigbeeAnalog(uint8_t endpoint) : ZigbeeEP(endpoint) {
14+
_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID;
15+
16+
//Create custom analog sensor configuration
17+
zigbee_analog_cfg_t analog_cfg = ZIGBEE_DEFAULT_ANALOG_CONFIG();
18+
_cluster_list = zigbee_analog_clusters_create(&analog_cfg);
19+
20+
_ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID, .app_device_version = 0};
21+
}
22+
23+
void ZigbeeAnalog::addAnalogValue() {
24+
esp_zb_cluster_list_add_analog_value_cluster(_cluster_list, esp_zb_analog_value_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
25+
_analog_clusters |= ANALOG_VALUE;
26+
}
27+
28+
void ZigbeeAnalog::addAnalogInput() {
29+
esp_zb_cluster_list_add_analog_input_cluster(_cluster_list, esp_zb_analog_input_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
30+
_analog_clusters |= ANALOG_INPUT;
31+
}
32+
33+
void ZigbeeAnalog::addAnalogOutput() {
34+
esp_zb_cluster_list_add_analog_output_cluster(_cluster_list, esp_zb_analog_output_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
35+
_analog_clusters |= ANALOG_OUTPUT;
36+
}
37+
38+
//set attribute method -> method overridden in child class
39+
void ZigbeeAnalog::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) {
40+
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT) {
41+
if (message->attribute.id == ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_SINGLE) {
42+
float analog_output = *(float *)message->attribute.data.value;
43+
analogOutputChanged(analog_output);
44+
} else {
45+
log_w("Received message ignored. Attribute ID: %d not supported for Analog Output", message->attribute.id);
46+
}
47+
} else {
48+
log_w("Received message ignored. Cluster ID: %d not supported for Analog endpoint", message->info.cluster);
49+
}
50+
}
51+
52+
void ZigbeeAnalog::analogOutputChanged(float analog_output) {
53+
if (_on_analog_output_change) {
54+
_on_analog_output_change(analog_output);
55+
} else {
56+
log_w("No callback function set for analog output change");
57+
}
58+
}
59+
60+
void ZigbeeAnalog::setAnalogValue(float analog) {
61+
if (!(_analog_clusters & ANALOG_VALUE)) {
62+
log_e("Analog Value cluster not added");
63+
return;
64+
}
65+
// float zb_analog = analog;
66+
log_d("Setting analog value to %.1f", analog);
67+
esp_zb_lock_acquire(portMAX_DELAY);
68+
esp_zb_zcl_set_attribute_val(
69+
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID, &analog, false
70+
);
71+
esp_zb_lock_release();
72+
}
73+
74+
void ZigbeeAnalog::setAnalogInput(float analog) {
75+
if (!(_analog_clusters & ANALOG_INPUT)) {
76+
log_e("Analog Input cluster not added");
77+
return;
78+
}
79+
// float zb_analog = analog;
80+
log_d("Setting analog input to %.1f", analog);
81+
esp_zb_lock_acquire(portMAX_DELAY);
82+
esp_zb_zcl_set_attribute_val(
83+
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_INPUT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_INPUT_PRESENT_VALUE_ID, &analog, false
84+
);
85+
esp_zb_lock_release();
86+
}
87+
88+
void ZigbeeAnalog::reportAnalogInput() {
89+
/* Send report attributes command */
90+
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
91+
report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
92+
report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_ANALOG_INPUT_PRESENT_VALUE_ID;
93+
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI;
94+
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_INPUT;
95+
report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint;
96+
97+
esp_zb_lock_acquire(portMAX_DELAY);
98+
esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd);
99+
esp_zb_lock_release();
100+
log_v("Analog Input report sent");
101+
}
102+
103+
void ZigbeeAnalog::setAnalogInputReporting(uint16_t min_interval, uint16_t max_interval, float delta) {
104+
esp_zb_zcl_reporting_info_t reporting_info;
105+
memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t));
106+
reporting_info.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV;
107+
reporting_info.ep = _endpoint;
108+
reporting_info.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT;
109+
reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE;
110+
reporting_info.attr_id = ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID;
111+
reporting_info.u.send_info.min_interval = min_interval;
112+
reporting_info.u.send_info.max_interval = max_interval;
113+
reporting_info.u.send_info.def_min_interval = min_interval;
114+
reporting_info.u.send_info.def_max_interval = max_interval;
115+
reporting_info.u.send_info.delta.s32 = delta;
116+
reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID;
117+
reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC;
118+
119+
esp_zb_lock_acquire(portMAX_DELAY);
120+
esp_zb_zcl_update_reporting_info(&reporting_info);
121+
esp_zb_lock_release();
122+
}
123+
124+
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

0 commit comments

Comments
 (0)