Zigbee: Merging light_coordinator and light switch into one
* Light bulb acting as ZED and bulb server device * Light switch acting as ZC and switch client device * ZC stay starting network steering to permit device join
This commit is contained in:
committed by
BOT
parent
d178d1d58a
commit
c42628fd4f
@@ -9,10 +9,9 @@ This test code shows how to configure Zigbee end device and use it as a light sw
|
||||
|
||||
## Hardware Required
|
||||
|
||||
* One development board with ESP32-H2 SoC acting as Zigbee end device (loaded with light switch example)
|
||||
* One development board with ESP32-H2 SoC acting as Zigbee coordinator (loaded with light switch example)
|
||||
* A USB cable for power supply and programming
|
||||
* Choose another ESP32-H2 as Zigbee coordinator (see [light coordinator example](../light_coordinator))
|
||||
* Choose another ESP32-H2 as Zigbee router (see [light bulb example](../light_bulb))
|
||||
* Choose another ESP32-H2 as Zigbee end-device (see [light bulb example](../light_bulb))
|
||||
|
||||
## Configure the project
|
||||
|
||||
@@ -29,18 +28,24 @@ Build the project, flash it to the board, and start the monitor tool to view the
|
||||
As you run the example, you will see the following log:
|
||||
|
||||
light switch:
|
||||
I (9423) gpio: GPIO[9]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:2
|
||||
I (9523) ESP_ZB_SWITCH: status: -1
|
||||
I (9523) ESP_ZB_SWITCH: Zigbee stack initialized
|
||||
I (9523) ESP_ZB_SWITCH: Start network steering
|
||||
I (11263) ESP_ZB_SWITCH: Joined network successfully (Extended PAN ID: f9:54:2d:01:a0:03:f7:84, PAN ID: 0xf5b5)
|
||||
I (13433) ESP_ZB_SWITCH: Found bulb addr: 0x434a ep: 10
|
||||
I (16433) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (17663) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (18693) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (19833) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (20753) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (21363) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (767) gpio: GPIO[9]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:2
|
||||
I (857) ESP_ZB_SWITCH: status: 255
|
||||
I (857) ESP_ZB_SWITCH: Zigbee stack initialized
|
||||
I (857) ESP_ZB_SWITCH: Start network formation
|
||||
I (1377) ESP_ZB_SWITCH: Joined network successfully (Extended PAN ID: aa:98:48:01:a0:03:f7:84, PAN ID: 0xb8db)
|
||||
I (1837) ESP_ZB_SWITCH: status: 0
|
||||
I (1837) ESP_ZB_SWITCH: Network steering started
|
||||
I (6587) ESP_ZB_SWITCH: status: 0
|
||||
I (7097) ESP_ZB_SWITCH: status: 0
|
||||
I (7147) ESP_ZB_SWITCH: New device commissioned or rejoined (short: 0xd200)
|
||||
I (7717) ESP_ZB_SWITCH: status: 0
|
||||
I (7757) ESP_ZB_SWITCH: status: 0
|
||||
I (9217) ESP_ZB_SWITCH: Found bulb addr: 0x77ea ep: 10
|
||||
I (11657) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (12197) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (12547) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (12857) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
I (17327) ESP_ZB_SWITCH: Send ON/OFF toggle command
|
||||
|
||||
## Light Control Functions
|
||||
|
||||
|
||||
@@ -36,13 +36,15 @@
|
||||
*/
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_zb_switch.h"
|
||||
|
||||
/**
|
||||
* @note Make sure set idf.py menuconfig in zigbee component as zigbee end device!
|
||||
* @note Make sure set idf.py menuconfig in zigbee component as zigbee coordinator device!
|
||||
*/
|
||||
#if !defined ZB_ED_ROLE
|
||||
#error Define ZB_ED_ROLE in idf.py menuconfig to compile light switch (End Device) source code.
|
||||
#if defined ZB_ED_ROLE
|
||||
#error Define ZB_COORDINATOR_ROLE in idf.py menuconfig to compile light switch source code.
|
||||
#endif
|
||||
|
||||
/* define Button function currently only 1 switch define */
|
||||
@@ -56,8 +58,6 @@ static switch_device_ctx_t esp_switch_ctx = {
|
||||
.basic_attr.power_source = ZB_ZCL_BASIC_POWER_SOURCE_UNKNOWN,
|
||||
/* identify cluster attributes data */
|
||||
.identify_attr.identify_time = 0,
|
||||
/* On_Off cluster attributes data */
|
||||
.on_off_attr.on_off = 0,
|
||||
/* bulb parameters */
|
||||
.bulb_params.short_addr = 0xffff,
|
||||
};
|
||||
@@ -233,11 +233,12 @@ static void bdb_start_top_level_commissioning_cb(zb_uint8_t mode_mask)
|
||||
*/
|
||||
void zboss_signal_handler(zb_bufid_t bufid)
|
||||
{
|
||||
zb_zdo_app_signal_hdr_t *p_sg_p = NULL;
|
||||
zb_zdo_app_signal_type_t sig = zb_get_app_signal(bufid, &p_sg_p);
|
||||
zb_ret_t status = ZB_GET_APP_SIGNAL_STATUS(bufid);
|
||||
zb_zdo_app_signal_hdr_t *p_sg_p = NULL;
|
||||
zb_uint8_t status = ZB_GET_APP_SIGNAL_STATUS(bufid);
|
||||
zb_zdo_app_signal_type_t sig = zb_get_app_signal(bufid, &p_sg_p);
|
||||
zb_zdo_signal_device_annce_params_t *dev_annce_params = NULL;
|
||||
zb_ret_t zb_err_code;
|
||||
|
||||
zb_nlme_permit_joining_request_t *request = NULL;
|
||||
switch (sig) {
|
||||
case ZB_ZDO_SIGNAL_SKIP_STARTUP:
|
||||
ESP_LOGI(TAG, "Zigbee stack initialized");
|
||||
@@ -245,13 +246,13 @@ void zboss_signal_handler(zb_bufid_t bufid)
|
||||
break;
|
||||
case ZB_BDB_SIGNAL_DEVICE_FIRST_START:
|
||||
if (status == RET_OK) {
|
||||
ESP_LOGI(TAG, "Start network steering");
|
||||
bdb_start_top_level_commissioning(ZB_BDB_NETWORK_STEERING);
|
||||
ESP_LOGI(TAG, "Start network formation");
|
||||
bdb_start_top_level_commissioning(ZB_BDB_NETWORK_FORMATION);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to initialize Zigbee stack (status: %d)", status);
|
||||
}
|
||||
break;
|
||||
case ZB_BDB_SIGNAL_STEERING:
|
||||
case ZB_BDB_SIGNAL_FORMATION:
|
||||
if (status == RET_OK) {
|
||||
zb_ext_pan_id_t extended_pan_id;
|
||||
zb_get_extended_pan_id(extended_pan_id);
|
||||
@@ -259,17 +260,29 @@ void zboss_signal_handler(zb_bufid_t bufid)
|
||||
extended_pan_id[7], extended_pan_id[6], extended_pan_id[5], extended_pan_id[4],
|
||||
extended_pan_id[3], extended_pan_id[2], extended_pan_id[1], extended_pan_id[0],
|
||||
ZB_PIBCACHE_PAN_ID());
|
||||
/* check the light device address */
|
||||
if (!esp_zb_already_find_light_bulb()) {
|
||||
zb_err_code = ZB_SCHEDULE_APP_ALARM(esp_zb_find_light_bulb, bufid, MATCH_DESC_REQ_START_DELAY);
|
||||
ESP_ERROR_CHECK(zb_err_code);
|
||||
zb_err_code = ZB_SCHEDULE_APP_ALARM(esp_zb_find_light_bulb_timeout, 0, MATCH_DESC_REQ_TIMEOUT);
|
||||
ESP_ERROR_CHECK(zb_err_code);
|
||||
bufid = 0; /* Do not free buffer - it will be reused by find_light_bulb callback */
|
||||
}
|
||||
bdb_start_top_level_commissioning(ZB_BDB_NETWORK_STEERING);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Network steering was not successful (status: %d)", status);
|
||||
ZB_SCHEDULE_APP_ALARM((zb_callback_t)bdb_start_top_level_commissioning_cb, ZB_BDB_NETWORK_STEERING, ZB_TIME_ONE_SECOND);
|
||||
ESP_LOGI(TAG, "Restart network formation (status: %d)", status);
|
||||
ZB_SCHEDULE_APP_ALARM((zb_callback_t)bdb_start_top_level_commissioning_cb, ZB_BDB_NETWORK_FORMATION, ZB_TIME_ONE_SECOND);
|
||||
}
|
||||
break;
|
||||
case ZB_BDB_SIGNAL_STEERING:
|
||||
if (status == RET_OK) {
|
||||
request = ZB_BUF_GET_PARAM(bufid, zb_nlme_permit_joining_request_t);
|
||||
ESP_LOGI(TAG, "Network steering started/refreshed");
|
||||
ZB_SCHEDULE_APP_ALARM((zb_callback_t)bdb_start_top_level_commissioning_cb, ZB_BDB_NETWORK_STEERING, (request->permit_duration)*ZB_TIME_ONE_SECOND);
|
||||
}
|
||||
break;
|
||||
case ZB_ZDO_SIGNAL_DEVICE_ANNCE:
|
||||
dev_annce_params = ZB_ZDO_SIGNAL_GET_PARAMS(p_sg_p, zb_zdo_signal_device_annce_params_t);
|
||||
ESP_LOGI(TAG, "New device commissioned or rejoined (short: 0x%04hx)", dev_annce_params->device_short_addr);
|
||||
/* check the light device address */
|
||||
if (!esp_zb_already_find_light_bulb()) {
|
||||
zb_err_code = ZB_SCHEDULE_APP_ALARM(esp_zb_find_light_bulb, bufid, MATCH_DESC_REQ_START_DELAY);
|
||||
ESP_ERROR_CHECK(zb_err_code);
|
||||
zb_err_code = ZB_SCHEDULE_APP_ALARM(esp_zb_find_light_bulb_timeout, 0, MATCH_DESC_REQ_TIMEOUT);
|
||||
ESP_ERROR_CHECK(zb_err_code);
|
||||
bufid = 0; /* Do not free buffer - it will be reused by find_light_bulb callback */
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -281,29 +294,30 @@ void zboss_signal_handler(zb_bufid_t bufid)
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
static void zboss_task(void *pvParameters)
|
||||
{
|
||||
zb_ret_t zb_err_code;
|
||||
zb_esp_platform_config_t config = {
|
||||
.radio_config = ZB_ESP_DEFAULT_RADIO_CONFIG(),
|
||||
.host_config = ZB_ESP_DEFAULT_HOST_CONFIG(),
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(zb_esp_platform_config(&config));
|
||||
/* initialize Zigbee stack. */
|
||||
ZB_INIT("light_switch");
|
||||
zb_set_network_ed_role(IEEE_CHANNEL_MASK);
|
||||
zb_set_network_coordinator_role(IEEE_CHANNEL_MASK);
|
||||
zb_set_max_children(MAX_CHILDREN);
|
||||
zb_set_nvram_erase_at_start(ERASE_PERSISTENT_CONFIG);
|
||||
zb_set_ed_timeout(ED_AGING_TIMEOUT_64MIN);
|
||||
zb_set_keepalive_timeout(ZB_MILLISECONDS_TO_BEACON_INTERVAL(3000));
|
||||
/* hardware related and device init */
|
||||
switch_driver_init(button_func_pair, PAIR_SIZE(button_func_pair), esp_zb_buttons_handler);
|
||||
/* register on_off switch device context (endpoints) */
|
||||
ZB_AF_REGISTER_DEVICE_CTX(&on_off_switch_ctx);
|
||||
zb_err_code = zboss_start_no_autostart();
|
||||
ESP_ERROR_CHECK(zb_err_code);
|
||||
ESP_ERROR_CHECK(zboss_start_no_autostart());
|
||||
|
||||
while (1) {
|
||||
zboss_main_loop_iteration();
|
||||
}
|
||||
}
|
||||
void app_main(void)
|
||||
{
|
||||
zb_esp_platform_config_t config = {
|
||||
.radio_config = ZB_ESP_DEFAULT_RADIO_CONFIG(),
|
||||
.host_config = ZB_ESP_DEFAULT_HOST_CONFIG(),
|
||||
};
|
||||
/* load Zigbee switch platform config to initialization */
|
||||
ESP_ERROR_CHECK(zb_esp_platform_config(&config));
|
||||
xTaskCreate(zboss_task, "zboss_main", 4096, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
/* Zigbee configuration */
|
||||
#define IEEE_CHANNEL_MASK (1l << 13) /* ZigBee default setting is channel 13 for light example usage */
|
||||
#define ERASE_PERSISTENT_CONFIG ZB_TRUE /* erase network devices before running example */
|
||||
#define MAX_CHILDREN 10 /* the max number of connected devices */
|
||||
|
||||
/* ZCL configuration */
|
||||
#define HA_ONOFF_SWITCH_ENDPOINT 1
|
||||
@@ -58,7 +59,6 @@ typedef struct light_switch_bulb_params_s {
|
||||
typedef struct {
|
||||
zb_zcl_basic_attrs_t basic_attr;
|
||||
zb_zcl_identify_attrs_t identify_attr;
|
||||
zb_zcl_on_off_attrs_t on_off_attr;
|
||||
light_switch_bulb_params_t bulb_params;
|
||||
} switch_device_ctx_t;
|
||||
#define ZB_ESP_DEFAULT_RADIO_CONFIG() \
|
||||
|
||||
@@ -31,6 +31,6 @@ CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=n
|
||||
# Zboss
|
||||
#
|
||||
CONFIG_ZB_ENABLED=y
|
||||
CONFIG_ZB_ZED=y
|
||||
CONFIG_ZB_ZCZR=y
|
||||
# end of Zboss
|
||||
# end of Component config
|
||||
|
||||
Reference in New Issue
Block a user