Skip to content

Commit

Permalink
feature/zigbee2mqtt: Add ESP32C6.Light device
Browse files Browse the repository at this point in the history
For the specific details of ESP32C6.Light, you can go to #22 for discussion.

Signed-off-by: lbuque <1102390310@qq.com>
  • Loading branch information
lbuque committed Feb 10, 2023
1 parent d6e9c88 commit fc7602f
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 4 deletions.
93 changes: 93 additions & 0 deletions examples/zigbee2mqtt/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,99 @@ void lilygoLightReport(uint64_t u64IeeeAddr, uint8_t u8OnOff) {
}


void espressifLightAdd(uint64_t u64IeeeAddr) {
char ieeeaddr_str[20] = { 0 };
char topic[128] = { 0 };
char topic_head[128] = { 0 };
char subTopics[128] = { 0 };
cJSON *json = cJSON_CreateObject();
cJSON *device = cJSON_CreateObject();
cJSON *identifiers = cJSON_CreateArray();
char *str = NULL;

if (!json) return ;
if (!device) goto OUT;
if (!identifiers) goto OUT1;

snprintf(ieeeaddr_str, sizeof(ieeeaddr_str) - 1, "0x%016llx", u64IeeeAddr);
snprintf(topic_head, sizeof(topic_head) - 1,
"homeassistant/light/0x%016llx",
u64IeeeAddr
);
snprintf(topic, sizeof(topic) - 1, "%s/config", topic_head);\
snprintf(subTopics, sizeof(subTopics) - 1, "%s/set", topic_head);

cJSON_AddItemToArray(identifiers, cJSON_CreateString(ieeeaddr_str));
cJSON_AddItemToObject(device, "identifiers", identifiers);
cJSON_AddStringToObject(device, "manufacturer", "Espressif");
cJSON_AddStringToObject(device, "model", "Espressif ordinary light");
cJSON_AddStringToObject(device, "name", "Espressif.Light");
cJSON_AddStringToObject(device, "sw_version", "0.1.0");
cJSON_AddItemToObject(json, "device", device);
cJSON_AddStringToObject(json, "~", topic_head);
cJSON_AddStringToObject(json, "name", "Espressif.Light");
cJSON_AddStringToObject(json, "cmd_t", "~/set");
cJSON_AddStringToObject(json, "stat_t", "~/state");
cJSON_AddStringToObject(json, "schema", "json");
cJSON_AddStringToObject(json, "unique_id", ieeeaddr_str);

str = cJSON_Print(json);
// appMQTTPublish(topic, str);
mqtt.publish(topic, str);
mqtt.subscribe(subTopics, handleLilygoLight);
ESP_LOGI(
"Zigbee2MQTT",
"Successfully interviewed '%#016llx', device has successfully been paired",
u64IeeeAddr
);
OUT:
cJSON_Delete(json);
return;
OUT1:
cJSON_Delete(json);
cJSON_Delete(device);
}


void espressifLightDelete(uint64_t u64IeeeAddr) {
char ieeeaddr_str[20] = { 0 };
char topic[128] = { 0 };

snprintf(ieeeaddr_str, sizeof(ieeeaddr_str) - 1, "0x%016llx", u64IeeeAddr);

memset(topic, 0, sizeof(topic));
snprintf(topic, sizeof(topic) - 1,
"homeassistant/light/%s/config",
ieeeaddr_str
);
// appMQTTPublish(topic, "");
mqtt.publish(topic, "");
}


void espressifLightReport(uint64_t u64IeeeAddr, uint8_t u8OnOff) {
char topic[128] = { 0 };

cJSON *json = cJSON_CreateObject();
if (!json) return ;

// printf("lilygoLightReport %d\n", u8OnOff);

snprintf(topic, sizeof(topic) - 1, "homeassistant/light/0x%016llx/state", u64IeeeAddr);
if (u8OnOff == 0x01) {
cJSON_AddStringToObject(json, "state", "ON");
} else {
cJSON_AddStringToObject(json, "state", "OFF");
}

// cJSON_AddStringToObject(json, "state", u8OnOff ? "ON": "OFF");
char *str = cJSON_Print(json);
// printf("topic: %s, data: %s\n", topic, str);
// appMQTTPublish(topic, str);
mqtt.publish(topic ,str);
cJSON_Delete(json);
}

void lilygoSensorAdd(uint64_t u64IeeeAddr) {
char ieeeaddr_str[20] = { 0 };
cJSON *json = cJSON_CreateObject();
Expand Down
6 changes: 6 additions & 0 deletions examples/zigbee2mqtt/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ void lilygoLightDelete(uint64_t u64IeeeAddr);

void lilygoLightReport(uint64_t u64IeeeAddr, uint8_t u8OnOff);

void espressifLightAdd(uint64_t u64IeeeAddr);

void espressifLightDelete(uint64_t u64IeeeAddr);

void espressifLightReport(uint64_t u64IeeeAddr, uint8_t u8OnOff);

void lilygoSensorAdd(uint64_t u64IeeeAddr);

void lilygoSensorReport(
Expand Down
52 changes: 48 additions & 4 deletions examples/zigbee2mqtt/zigbee2mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ void appHandleLeaveIndication(ts_MsgLeaveIndicationPayload *payload) {
{
lilygoSensorDelete(device->u64IeeeAddr);
}
else if (!strncmp((const char *)device->au8ModelId,
"ESP32C6.Light",
strlen("ESP32C6.Light")))
{
espressifLightDelete(device->u64IeeeAddr);
}
memset(device, 0, sizeof(DeviceNode));
appDataBaseSave();
}
Expand Down Expand Up @@ -492,6 +498,25 @@ void appHandleZCLreportMsgRcv(ts_MsgZclReportMsgRcvPayload *payload) {
appDataBaseSave();
lilygoSensorAdd(device->u64IeeeAddr);
}
else if (!strncmp((const char *)payload->asAttrList[i].uAttrData.au8AttrData,
"ESP32C6.Light",
strlen("ESP32C6.Light")))
{
appDataBaseSave();
espressifLightAdd(device->u64IeeeAddr);
// The configure method below is needed to make the device reports on/off state changes
// when the device is controlled manually through the button on it.
zbhci_BindingReq(
device->u64IeeeAddr,
1,
0x0006,
0x03,
(ts_DstAddr) {
.u64DstAddr = brigeNode.macAddr
},
1
);
}
}
}
break;
Expand All @@ -503,9 +528,8 @@ void appHandleZCLreportMsgRcv(ts_MsgZclReportMsgRcvPayload *payload) {
if (payload->asAttrList[i].u16AttrID == 0x0000)
{
device->deviceData.light.u8State = payload->asAttrList[i].uAttrData.u8AttrData;
if (!strncmp((const char *)device->au8ModelId,
"LILYGO.Light",
strlen("LILYGO.Light")))
if (!strncmp((const char *)device->au8ModelId, "LILYGO.Light", strlen("LILYGO.Light")) || \
!strncmp((const char *)device->au8ModelId, "ESP32C6.Light", strlen("ESP32C6.Light")))
{
lilygoLightReport(
device->u64IeeeAddr,
Expand Down Expand Up @@ -699,6 +723,25 @@ void appHandleZCLReadResponse(ts_MsgZclAttrReadRspPayload *payload) {
appDataBaseSave();
lilygoSensorAdd(device->u64IeeeAddr);
}
else if (!strncmp((const char *)payload->asAttrReadList[i].uAttrData.au8AttrData,
"ESP32C6.Light",
strlen("ESP32C6.Light")))
{
appDataBaseSave();
espressifLightAdd(device->u64IeeeAddr);
// The configure method below is needed to make the device reports on/off state changes
// when the device is controlled manually through the button on it.
zbhci_BindingReq(
device->u64IeeeAddr,
1,
0x0006,
0x03,
(ts_DstAddr) {
.u64DstAddr = brigeNode.macAddr
},
1
);
}
}
}
break;
Expand All @@ -710,7 +753,8 @@ void appHandleZCLReadResponse(ts_MsgZclAttrReadRspPayload *payload) {
if (payload->asAttrReadList[i].u16AttrID == 0x0000)
{
device->deviceData.light.u8State = payload->asAttrReadList[i].uAttrData.u8AttrData;
if (!strncmp((const char *)device->au8ModelId, "LILYGO.Light", strlen("LILYGO.Light")))
if (!strncmp((const char *)device->au8ModelId, "LILYGO.Light", strlen("LILYGO.Light")) || \
!strncmp((const char *)device->au8ModelId, "ESP32C6.Light", strlen("ESP32C6.Light")))
{
lilygoLightReport(
device->u64IeeeAddr,
Expand Down

0 comments on commit fc7602f

Please sign in to comment.