-
Notifications
You must be signed in to change notification settings - Fork 54
Open
Description
Hi, I am trying to get current readings from a CT clamp with in-built zigbee. At the moment, I am unable to correctly read RMS data from the CT. I am not sure if this has something to do with the CT and my ESP32 C6 not correctly communicating what endpoint to transfer on. Cc @xieqinan
My logs and code are below.
I (35746) ZB_COORD: Received Zigbee signal: 0x30, status: ESP_OK
I (35746) ZB_COORD: Device update signal received
I (35756) ZB_COORD: Received Zigbee signal: 0x2F, status: ESP_OK
I (35756) ZB_COORD: Device announcement signal 0x2F received
I (35766) ZB_COORD: New device joined: 0x9a25
I (35766) ZB_COORD: Device 0x9a25 already known, skipping
I (41496) ZB_COORD: Periodic read from device 0: 0x9a25
E (41496) ZB_COORD: Failed to send read attribute command: ERROR (0x1)
E (41496) ZB_COORD: Request details: device=0x9a25, dst_ep=1, src_ep=1, cluster=0x0b04, attr=0x0508
I (43306) ZB_COORD: Received Zigbee signal: 0x12, status: ESP_OK
I (43306) ZB_COORD: Unhandled signal: 0x12, status: ESP_OK
ZB_TRACE_LOG[0]: common/zb_address.c:1138 zb_address_delete ref 57
I (43316) ZB_COORD: Received Zigbee signal: 0x30, status: ESP_OK
I (43316) ZB_COORD: Device update signal received
I (43316) ZB_COORD: Received Zigbee signal: 0x2F, status: ESP_OK
I (43326) ZB_COORD: Device announcement signal 0x2F received
I (43326) ZB_COORD: New device joined: 0x9a25
I (43336) ZB_COORD: Device 0x9a25 already known, skipping
I (47876) ZB_COORD: Received Zigbee signal: 0x32, status: ESP_OK
I (47876) ZB_COORD: Unhandled signal: 0x32, status: ESP_OK
I (50866) ZB_COORD: Received Zigbee signal: 0x12, status: ESP_OK
I (50866) ZB_COORD: Unhandled signal: 0x12, status: ESP_OK
ZB_TRACE_LOG[0]: common/zb_address.c:1138 zb_address_delete ref 57
I (50876) ZB_COORD: Received Zigbee signal: 0x30, status: ESP_OK
I (50876) ZB_COORD: Device update signal received
I (50886) ZB_COORD: Received Zigbee signal: 0x2F, status: ESP_OK
I (50886) ZB_COORD: Device announcement signal 0x2F received
I (50896) ZB_COORD: New device joined: 0x9a25
I (50896) ZB_COORD: Device 0x9a25 already known, skipping
I (52496) ZB_COORD: Periodic read from device 0: 0x9a25
E (52496) ZB_COORD: Failed to send read attribute command: ERROR (0x2)
E (52496) ZB_COORD: Request details: device=0x9a25, dst_ep=1, src_ep=1, cluster=0x0b04, attr=0x0508
I (58436) ZB_COORD: Received Zigbee signal: 0x12, status: ESP_OK
I (58436) ZB_COORD: Unhandled signal: 0x12, status: ESP_OK
ZB_TRACE_LOG[0]: common/zb_address.c:1138 zb_address_delete ref 57
I (58446) ZB_COORD: Received Zigbee signal: 0x30, status: ESP_OK
I (58446) ZB_COORD: Device update signal received
I (58446) ZB_COORD: Received Zigbee signal: 0x2F, status: ESP_OK
I (58456) ZB_COORD: Device announcement signal 0x2F received
I (58456) ZB_COORD: New device joined: 0x9a25
I (58466) ZB_COORD: Device 0x9a25 already known, skipping
static void read_ct_clamp_data(uint16_t device_addr, uint8_t endpoint)
{
// Create the attribute ID array
static uint16_t attr_id = ESP_ZB_ZCL_ATTR_ELECTRICAL_MEASUREMENT_RMS_CURRENT_ID;
// Create the read attribute command structure
esp_zb_zcl_read_attr_cmd_t read_req;
read_req.address_mode = ESP_ZB_APS_ADDR_MODE_16_ENDP_PRESENT;
read_req.zcl_basic_cmd.dst_addr_u.addr_short = device_addr;
read_req.zcl_basic_cmd.dst_endpoint = endpoint;
read_req.zcl_basic_cmd.src_endpoint = HA_ESP_COORDINATOR_ENDPOINT;
read_req.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ELECTRICAL_MEASUREMENT;
read_req.manuf_specific = 0;
read_req.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV;
read_req.dis_defalut_resp = 0;
read_req.manuf_code = 0;
read_req.attr_number = 1;
read_req.attr_field = &attr_id;
static void read_ct_clamp_data(uint16_t device_addr, uint8_t endpoint)
{
// Create the attribute ID array
static uint16_t attr_id = ESP_ZB_ZCL_ATTR_ELECTRICAL_MEASUREMENT_RMS_CURRENT_ID;
// Create the read attribute command structure
esp_zb_zcl_read_attr_cmd_t read_req;
read_req.address_mode = ESP_ZB_APS_ADDR_MODE_16_ENDP_PRESENT;
read_req.zcl_basic_cmd.dst_addr_u.addr_short = device_addr;
read_req.zcl_basic_cmd.dst_endpoint = endpoint;
read_req.zcl_basic_cmd.src_endpoint = HA_ESP_COORDINATOR_ENDPOINT;
read_req.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ELECTRICAL_MEASUREMENT;
read_req.manuf_specific = 0;
read_req.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV;
read_req.dis_defalut_resp = 0;
read_req.manuf_code = 0;
read_req.attr_number = 1;
read_req.attr_field = &attr_id;
// Send the read attribute command
esp_err_t err = esp_zb_zcl_read_attr_cmd_req(&read_req);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to send read attribute command: %s (0x%x)", esp_err_to_name(err), err);
ESP_LOGE(TAG, "Request details: device=0x%04x, dst_ep=%d, src_ep=%d, cluster=0x%04x, attr=0x%04x",
device_addr, endpoint, HA_ESP_COORDINATOR_ENDPOINT,
ESP_ZB_ZCL_CLUSTER_ID_ELECTRICAL_MEASUREMENT, attr_id);
}
else
{
ESP_LOGI(TAG, "Reading RMS current from device 0x%04x, endpoint %d", device_addr, endpoint);
}
}
static void handle_zcl_attr_response(esp_zb_zcl_cmd_read_attr_resp_message_t *message)
{
ESP_LOGI(TAG, "Received ZCL attribute response from 0x%04x",
message->info.src_address.u.short_addr);
// Process the attribute response
if (message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS)
{
ESP_LOGI(TAG, "Successfully read attribute from device");
// Parse the attribute data
esp_zb_zcl_read_attr_resp_variable_t *variable = message->variables;
while (variable)
{
ESP_LOGI(TAG, "Attribute ID: 0x%04x, Status: 0x%02x",
variable->attribute.id, variable->status);
if (variable->status == ESP_ZB_ZCL_STATUS_SUCCESS)
{
// Check if this is a manufacturer name attribute from Basic cluster
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_BASIC &&
variable->attribute.id == ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID)
{
char manufacturer[32] = {0};
memcpy(manufacturer, variable->attribute.data.value,
variable->attribute.data.size);
ESP_LOGI(TAG, "Device 0x%04hx manufacturer: %s",
message->info.src_address.u.short_addr, manufacturer);
// Check if it's your CT clamp (replace with actual manufacturer name)
if (strstr(manufacturer, "Tuya") != NULL)
{
ESP_LOGI(TAG, "Found CT clamp device!");
// Now try reading current data
read_ct_clamp_data(message->info.src_address.u.short_addr, message->info.src_endpoint);
// read_basic_info(message->info.src_address.u.short_addr, message->info.src_endpoint);
}
}
else
{
// Handle other attribute types as before
switch (variable->attribute.data.type)
{
case ESP_ZB_ZCL_ATTR_TYPE_U16:
ESP_LOGI(TAG, "Attribute value (U16): %u", *(uint16_t *)(variable->attribute.data.value));
break;
case ESP_ZB_ZCL_ATTR_TYPE_U32:
ESP_LOGI(TAG, "Attribute value (U32): %lu", *(uint32_t *)(variable->attribute.data.value));
break;
case ESP_ZB_ZCL_ATTR_TYPE_CHAR_STRING:
ESP_LOGI(TAG, "Attribute value (String): %.*s",
variable->attribute.data.size,
(char *)variable->attribute.data.value);
break;
default:
ESP_LOGI(TAG, "Attribute type: 0x%02x",
variable->attribute.data.type);
break;
}
}
}
variable = variable->next;
}
}
else
{
ESP_LOGW(TAG, "Failed to read attribute, status: 0x%02x", message->info.status);
}
}
Metadata
Metadata
Assignees
Labels
No labels