Sending String from Zigbee Slave to Master via Zigbee Network" in ESP32-C6
Posted: Tue Oct 22, 2024 7:45 am
hello everyone ,
i have two devices: one designated as the master and the other as the slave. The slave device will receive input strings via RS-485 and subsequently transmit these strings to the master device using Zigbee communication in esp32-c6
right now both the devices are connected via zigbee network i am getting bound successful message, slave are sending the string to the master but master is not entering in indication callback because master short add is 0 i.e master is acting as a coordinator, and coordinator will not enter the data indication callback and not receive message like end device do. so anyone can help me regarding this issue...i am attaching me code for reference
slave side code i.e transmitting the string to the master
//****************************************************************************
static void esp_zb_task(void *pvParameters)
{
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG();
esp_zb_init(&zb_nwk_cfg);
esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG();
esp_zb_ep_list_t *esp_zb_on_off_light_ep = esp_zb_on_off_light_ep_create(HA_ESP_LIGHT_ENDPOINT, &light_cfg);
esp_zb_device_register(esp_zb_on_off_light_ep);
esp_zb_core_action_handler_register(zb_action_handler);
esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
ESP_ERROR_CHECK(esp_zb_start(false));
//rs485ReadTask();
esp_zb_main_loop_iteration();
vTaskDelay(pdMS_TO_TICKS(10)); // Yield for 10 ms, adjust as needed
}
void send_zigbee_data(const char *data) {
zigbee_aps_frame_t aps_frame;
memset(&aps_frame, 0, sizeof(aps_frame));
// Fill in the APS frame
aps_frame.dst_addr = 1; // Destination address
aps_frame.cluster_id = 0x0001; // Replace with your cluster ID
//aps_frame.profile_id = 0x0104; // Replace with your profile ID
aps_frame.data = (uint8_t *)data; // Data to send
aps_frame.data_length = strlen(data);
// Send the data
esp_err_t ret = esp_zb_aps_data_request(&aps_frame);
//esp_err_t esp_zb_aps_data_request(esp_zb_apsde_data_req_t *req);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Data sent successfully: %s", data);
} else {
ESP_LOGE(TAG, "Failed to send data: %s", esp_err_to_name(ret));
}
}
vois rs485Read_Task(void)
{
send_zigbee_data(&hmiToGwyReceviedCommandBuffer);
}
void app_main(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
esp_zb_platform_config_t config = {
.radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
};
rs485_dir_clear();
rs485_uart_init();
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_zb_platform_config(&config));
xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
}
receiver side code i.e master is receiving the string via zigbee network in esp32c6
//*************************************************************************************************************
static void esp_zb_task(void *pvParameters)
{
/* initialize Zigbee stack */
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZC_CONFIG();
esp_zb_init(&zb_nwk_cfg);
esp_zb_on_off_switch_cfg_t switch_cfg = ESP_ZB_DEFAULT_ON_OFF_SWITCH_CONFIG();
esp_zb_ep_list_t *esp_zb_on_off_switch_ep = esp_zb_on_off_switch_ep_create(HA_ONOFF_SWITCH_ENDPOINT, &switch_cfg);
esp_zb_device_register(esp_zb_on_off_switch_ep);
esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
//zb_af_set_data_indication(my_zigbee_data_indication_callback);
ESP_ERROR_CHECK(esp_zb_start(false));
while (1)
{
esp_zb_main_loop_iteration();
vTaskDelay(pdMS_TO_TICKS(100)); // Allow other tasks to run
}
//esp_zb_main_loop_iteration();
}
void app_main(void)
{
esp_zb_platform_config_t config = {
.radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
};
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_zb_platform_config(&config));
zb_af_set_data_indication(my_zigbee_data_indication_callback);
ESP_LOGI(TAG, "Callback registered successfully.");
//zb_nvram_register_app1_read_cb(incoming_data_handler);
printf("hello");
//switch_driver_init(button_func_pair, PAIR_SIZE(button_func_pair), esp_zb_buttons_handler);
xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
printf("hello1\n");
}
void my_zigbee_data_indication_callback(esp_zb_apsde_data_ind_t *ind) {
printf("enter1\n");
if (ind) {
ESP_LOGI(TAG, "Received data from address: 0x%04x", ind->src_short_addr);
// Ensure we only read the data if the length is valid
if (ind->asdu && ind->asdu_length > 0) {
const char *received_data = (const char *)ind->asdu;
ESP_LOGI(TAG, "Received data: %s", received_data);
} else {
ESP_LOGE(TAG, "Received data is NULL or length is zero");
}
} else {
ESP_LOGE(TAG, "Received indication is NULL");
}
}
i have two devices: one designated as the master and the other as the slave. The slave device will receive input strings via RS-485 and subsequently transmit these strings to the master device using Zigbee communication in esp32-c6
right now both the devices are connected via zigbee network i am getting bound successful message, slave are sending the string to the master but master is not entering in indication callback because master short add is 0 i.e master is acting as a coordinator, and coordinator will not enter the data indication callback and not receive message like end device do. so anyone can help me regarding this issue...i am attaching me code for reference
slave side code i.e transmitting the string to the master
//****************************************************************************
static void esp_zb_task(void *pvParameters)
{
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG();
esp_zb_init(&zb_nwk_cfg);
esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG();
esp_zb_ep_list_t *esp_zb_on_off_light_ep = esp_zb_on_off_light_ep_create(HA_ESP_LIGHT_ENDPOINT, &light_cfg);
esp_zb_device_register(esp_zb_on_off_light_ep);
esp_zb_core_action_handler_register(zb_action_handler);
esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
ESP_ERROR_CHECK(esp_zb_start(false));
//rs485ReadTask();
esp_zb_main_loop_iteration();
vTaskDelay(pdMS_TO_TICKS(10)); // Yield for 10 ms, adjust as needed
}
void send_zigbee_data(const char *data) {
zigbee_aps_frame_t aps_frame;
memset(&aps_frame, 0, sizeof(aps_frame));
// Fill in the APS frame
aps_frame.dst_addr = 1; // Destination address
aps_frame.cluster_id = 0x0001; // Replace with your cluster ID
//aps_frame.profile_id = 0x0104; // Replace with your profile ID
aps_frame.data = (uint8_t *)data; // Data to send
aps_frame.data_length = strlen(data);
// Send the data
esp_err_t ret = esp_zb_aps_data_request(&aps_frame);
//esp_err_t esp_zb_aps_data_request(esp_zb_apsde_data_req_t *req);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Data sent successfully: %s", data);
} else {
ESP_LOGE(TAG, "Failed to send data: %s", esp_err_to_name(ret));
}
}
vois rs485Read_Task(void)
{
send_zigbee_data(&hmiToGwyReceviedCommandBuffer);
}
void app_main(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
esp_zb_platform_config_t config = {
.radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
};
rs485_dir_clear();
rs485_uart_init();
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_zb_platform_config(&config));
xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
}
receiver side code i.e master is receiving the string via zigbee network in esp32c6
//*************************************************************************************************************
static void esp_zb_task(void *pvParameters)
{
/* initialize Zigbee stack */
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZC_CONFIG();
esp_zb_init(&zb_nwk_cfg);
esp_zb_on_off_switch_cfg_t switch_cfg = ESP_ZB_DEFAULT_ON_OFF_SWITCH_CONFIG();
esp_zb_ep_list_t *esp_zb_on_off_switch_ep = esp_zb_on_off_switch_ep_create(HA_ONOFF_SWITCH_ENDPOINT, &switch_cfg);
esp_zb_device_register(esp_zb_on_off_switch_ep);
esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
//zb_af_set_data_indication(my_zigbee_data_indication_callback);
ESP_ERROR_CHECK(esp_zb_start(false));
while (1)
{
esp_zb_main_loop_iteration();
vTaskDelay(pdMS_TO_TICKS(100)); // Allow other tasks to run
}
//esp_zb_main_loop_iteration();
}
void app_main(void)
{
esp_zb_platform_config_t config = {
.radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
};
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_zb_platform_config(&config));
zb_af_set_data_indication(my_zigbee_data_indication_callback);
ESP_LOGI(TAG, "Callback registered successfully.");
//zb_nvram_register_app1_read_cb(incoming_data_handler);
printf("hello");
//switch_driver_init(button_func_pair, PAIR_SIZE(button_func_pair), esp_zb_buttons_handler);
xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
printf("hello1\n");
}
void my_zigbee_data_indication_callback(esp_zb_apsde_data_ind_t *ind) {
printf("enter1\n");
if (ind) {
ESP_LOGI(TAG, "Received data from address: 0x%04x", ind->src_short_addr);
// Ensure we only read the data if the length is valid
if (ind->asdu && ind->asdu_length > 0) {
const char *received_data = (const char *)ind->asdu;
ESP_LOGI(TAG, "Received data: %s", received_data);
} else {
ESP_LOGE(TAG, "Received data is NULL or length is zero");
}
} else {
ESP_LOGE(TAG, "Received indication is NULL");
}
}