Skip to content

feat(zigbee): Add method to set/get/report analog output #11431

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
merged 4 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
* Modified by Pat Clay
*/

#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#ifndef ZIGBEE_MODE_ZCZR
#error "Zigbee coordinator/router device mode is not selected in Tools->Zigbee mode"
#endif

#include "Zigbee.h"
Expand Down Expand Up @@ -70,6 +70,7 @@ void setup() {
zbAnalogDevice.addAnalogOutput();
zbAnalogDevice.setAnalogOutputApplication(ESP_ZB_ZCL_AI_RPM_OTHER);
zbAnalogDevice.setAnalogOutputDescription("Fan Speed (RPM)");
zbAnalogDevice.setAnalogOutputResolution(1);

// If analog output cluster is added, set callback function for analog output change
zbAnalogDevice.onAnalogOutputChange(onAnalogOutputChange);
Expand Down Expand Up @@ -99,8 +100,8 @@ void setup() {
Zigbee.addEndpoint(&zbAnalogPercent);

Serial.println("Starting Zigbee...");
// When all EPs are registered, start Zigbee in End Device mode
if (!Zigbee.begin()) {
// When all EPs are registered, start Zigbee in Router Device mode
if (!Zigbee.begin(ZIGBEE_ROUTER)) {
Serial.println("Zigbee failed to start!");
Serial.println("Rebooting...");
ESP.restart();
Expand Down Expand Up @@ -151,6 +152,9 @@ void loop() {
Zigbee.factoryReset();
}
}
// For demonstration purposes, increment the analog output value by 100
zbAnalogDevice.setAnalogOutput(zbAnalogDevice.getAnalogOutput() + 100);
zbAnalogDevice.reportAnalogOutput();
}
delay(100);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
"fqbn_append": "PartitionScheme=zigbee_zczr,ZigbeeMode=zczr",
"requires": [
"CONFIG_SOC_IEEE802154_SUPPORTED=y",
"CONFIG_ZB_ENABLED=y"
]
}
5 changes: 3 additions & 2 deletions libraries/Zigbee/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,14 @@ setSensorType KEYWORD2
setCarbonDioxide KEYWORD2

# ZigbeeAnalog
addAnalogValue KEYWORD2
addAnalogInput KEYWORD2
addAnalogOutput KEYWORD2
onAnalogOutputChange KEYWORD2
setAnalogValue KEYWORD2
setAnalogInput KEYWORD2
setAnalogOutput KEYWORD2
getAnalogOutput KEYWORD2
reportAnalogInput KEYWORD2
reportAnalogOutput KEYWORD2
setAnalogInputReporting KEYWORD2

# ZigbeeCarbonDioxideSensor
Expand Down
49 changes: 45 additions & 4 deletions libraries/Zigbee/src/ep/ZigbeeAnalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ bool ZigbeeAnalog::setAnalogOutputApplication(uint32_t application_type) {
void ZigbeeAnalog::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) {
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT) {
if (message->attribute.id == ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_SINGLE) {
float analog_output = *(float *)message->attribute.data.value;
analogOutputChanged(analog_output);
_output_state = *(float *)message->attribute.data.value;
analogOutputChanged();
} else {
log_w("Received message ignored. Attribute ID: %d not supported for Analog Output", message->attribute.id);
}
Expand All @@ -139,9 +139,9 @@ void ZigbeeAnalog::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *mes
}
}

void ZigbeeAnalog::analogOutputChanged(float analog_output) {
void ZigbeeAnalog::analogOutputChanged() {
if (_on_analog_output_change) {
_on_analog_output_change(analog_output);
_on_analog_output_change(_output_state);
} else {
log_w("No callback function set for analog output change");
}
Expand All @@ -166,6 +166,26 @@ bool ZigbeeAnalog::setAnalogInput(float analog) {
return true;
}

bool ZigbeeAnalog::setAnalogOutput(float analog) {
esp_zb_zcl_status_t ret = ESP_ZB_ZCL_STATUS_SUCCESS;
_output_state = analog;
analogOutputChanged();

log_v("Updating analog output to %.2f", analog);
/* Update analog output */
esp_zb_lock_acquire(portMAX_DELAY);
ret = esp_zb_zcl_set_attribute_val(
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID, &_output_state, false
);
esp_zb_lock_release();

if (ret != ESP_ZB_ZCL_STATUS_SUCCESS) {
log_e("Failed to set analog output: 0x%x: %s", ret, esp_zb_zcl_status_to_name(ret));
return false;
}
return true;
}

bool ZigbeeAnalog::reportAnalogInput() {
/* Send report attributes command */
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
Expand All @@ -187,6 +207,27 @@ bool ZigbeeAnalog::reportAnalogInput() {
return true;
}

bool ZigbeeAnalog::reportAnalogOutput() {
/* Send report attributes command */
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID;
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI;
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT;
report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint;
report_attr_cmd.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC;

esp_zb_lock_acquire(portMAX_DELAY);
esp_err_t ret = esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd);
esp_zb_lock_release();
if (ret != ESP_OK) {
log_e("Failed to send Analog Output report: 0x%x: %s", ret, esp_err_to_name(ret));
return false;
}
log_v("Analog Output report sent");
return true;
}

bool ZigbeeAnalog::setAnalogInputReporting(uint16_t min_interval, uint16_t max_interval, float delta) {
esp_zb_zcl_reporting_info_t reporting_info;
memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t));
Expand Down
12 changes: 9 additions & 3 deletions libraries/Zigbee/src/ep/ZigbeeAnalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ class ZigbeeAnalog : public ZigbeeEP {
_on_analog_output_change = callback;
}

// Set the analog input value
// Set the Analog Input/Output value
bool setAnalogInput(float analog);
bool setAnalogOutput(float analog);

// Report Analog Input value
// Get the Analog Output value
float getAnalogOutput() { return _output_state; }

// Report Analog Input/Output
bool reportAnalogInput();
bool reportAnalogOutput();

// Set reporting for Analog Input
bool setAnalogInputReporting(uint16_t min_interval, uint16_t max_interval, float delta);
Expand All @@ -59,9 +64,10 @@ class ZigbeeAnalog : public ZigbeeEP {
void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override;

void (*_on_analog_output_change)(float);
void analogOutputChanged(float analog_output);
void analogOutputChanged();

uint8_t _analog_clusters;
float _output_state;
};

#endif // CONFIG_ZB_ENABLED
Loading