Skip to content

Commit

Permalink
Merge branch 'update/zigbee_example_update' into 'master'
Browse files Browse the repository at this point in the history
examples: Update Zigbee examples for the latest library

See merge request espressif/esp-idf!20293
  • Loading branch information
chshu committed Oct 17, 2022
2 parents f98a37f + 717a3ac commit dc09f30
Show file tree
Hide file tree
Showing 34 changed files with 526 additions and 784 deletions.
20 changes: 11 additions & 9 deletions examples/zigbee/esp_zigbee_gateway/README.md
Expand Up @@ -3,8 +3,6 @@

# Gateway Example

(See the `README.md` file in the upper level 'examples' directory for more information about examples.)

This example demonstrates how to build a Zigbee Gateway device. It runs on a Wi-Fi SoC such as ESP32, ESP32-C3 and ESP32-S3, with an 802.15.4 SoC like ESP32-H2 running [esp_zigbee_rcp](../esp_zigbee_rcp) to provide 802.15.4 radio.

## Hardware Required
Expand All @@ -29,6 +27,10 @@ ESP32 pin | ESP32-H2 pin

Before project configuration and build, make sure to set the correct chip target using `idf.py set-target esp32` or `idf.py set-target esp32s3`

## Erase the NVRAM

Before flash it to the board, it is recommended to erase NVRAM if user doesn't want to keep the previous examples or other projects stored info using `idf.py -p PORT erase-flash`

## Build and Flash

Build the project, flash it to the board, and start the monitor tool to view the serial output by running `idf.py -p PORT flash monitor`
Expand All @@ -40,13 +42,13 @@ 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:

esp_zigbee_gateway:
I (660) ESP_ZB_GATEWAY: status: -1
I (670) ESP_ZB_GATEWAY: Zigbee stack initialized
I (680) ESP_ZB_GATEWAY: Zigbee rcp device booted
I (1280) ESP_ZB_GATEWAY: Start network formation
I (3060) ESP_ZB_GATEWAY: Formed network successfully (ieee extended address: f9:54:2d:01:a0:03:f7:84, PAN ID: 0x8651)
I (4060) ESP_ZB_GATEWAY: status: 0
I (4400) ESP_ZB_GATEWAY: Network steering started
I (660) ESP_ZB_GATEWAY: status: -1
I (670) ESP_ZB_GATEWAY: Zigbee stack initialized
I (680) ESP_ZB_GATEWAY: Zigbee rcp device booted
I (1280) ESP_ZB_GATEWAY: Start network formation
I (3060) ESP_ZB_GATEWAY: Formed network successfully (ieee extended address: f9:54:2d:01:a0:03:f7:84, PAN ID: 0x8651)
I (4060) ESP_ZB_GATEWAY: status: 0
I (4400) ESP_ZB_GATEWAY: Network steering started

## Gateway Functions

Expand Down
58 changes: 21 additions & 37 deletions examples/zigbee/esp_zigbee_gateway/main/esp_zigbee_gateway.c
Expand Up @@ -41,80 +41,67 @@
#include "esp_zigbee_gateway.h"

#if (!defined ZB_MACSPLIT_HOST && defined ZB_MACSPLIT_DEVICE)
#error Only Zigbee gateway hostdevice should be defined
#error Only Zigbee gateway host device should be defined
#endif

static const char *TAG = "ESP_ZB_GATEWAY";

/********************* Define functions **************************/
static void bdb_start_top_level_commissioning_cb(zb_uint8_t mode_mask)
static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask)
{
if (!bdb_start_top_level_commissioning(mode_mask)) {
ESP_LOGE(TAG, "In BDB commissioning, an error occurred (for example: the device has already been running)");
}
ESP_ERROR_CHECK(esp_zb_bdb_start_top_level_commissioning(mode_mask));
}

void zboss_signal_handler(zb_bufid_t bufid)
void zboss_signal_handler(uint8_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_signal_device_annce_params_t *dev_annce_params = NULL;
zb_zdo_signal_macsplit_dev_boot_params_t *rcp_version = NULL;
zb_uint32_t gateway_version;

switch (sig) {
case ZB_ZDO_SIGNAL_SKIP_STARTUP:
ESP_LOGI(TAG, "Zigbee stack initialized");
bdb_start_top_level_commissioning(ZB_BDB_INITIALIZATION);
esp_zb_bdb_start_top_level_commissioning(ZB_BDB_INITIALIZATION);
break;

case ZB_MACSPLIT_DEVICE_BOOT:
ESP_LOGI(TAG, "Zigbee rcp device booted");
gateway_version = zb_esp_macsplit_get_version();
rcp_version = ZB_ZDO_SIGNAL_GET_PARAMS(p_sg_p, zb_zdo_signal_macsplit_dev_boot_params_t);
ESP_LOGI(TAG, "Zigbee rcp device version: %d.%d.%d", (rcp_version->dev_version >> 24 & 0x000000FF), (rcp_version->dev_version >> 16 & 0x000000FF), (rcp_version->dev_version & 0x000000FF));
ESP_LOGI(TAG, "Zigbee gateway version: %d.%d.%d", (gateway_version >> 24 & 0x000000FF), (gateway_version >> 16 & 0x000000FF), (gateway_version & 0x000000FF));
if (gateway_version != rcp_version->dev_version) {
ESP_LOGE(TAG, "rcp has different Zigbee stack version with Zigbee gateway! Please check the rcp software or other issues");
}
ESP_LOGI(TAG, "Running RCP Version:%s", rcp_version->version_str);
break;

case ZB_BDB_SIGNAL_DEVICE_FIRST_START:
case ZB_BDB_SIGNAL_DEVICE_REBOOT:
if (status == RET_OK) {
ESP_LOGI(TAG, "Start network formation");
bdb_start_top_level_commissioning(ZB_BDB_NETWORK_FORMATION);
esp_zb_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_FORMATION:
if (status == RET_OK) {
zb_ieee_addr_t ieee_address;
zb_get_long_address(ieee_address);
esp_zb_get_long_address(ieee_address);
ESP_LOGI(TAG, "Formed network successfully (ieee extended address: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx)",
ieee_address[7], ieee_address[6], ieee_address[5], ieee_address[4],
ieee_address[3], ieee_address[2], ieee_address[1], ieee_address[0],
ZB_PIBCACHE_PAN_ID());
bdb_start_top_level_commissioning(ZB_BDB_NETWORK_STEERING);
esp_zb_bdb_start_top_level_commissioning(ZB_BDB_NETWORK_STEERING);
} else {
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) {
ESP_LOGI(TAG, "Network steering started");
}
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);
break;

default:
ESP_LOGI(TAG, "status: %d", status);
break;
Expand All @@ -125,26 +112,23 @@ void zboss_signal_handler(zb_bufid_t bufid)
}
}

static void zboss_task(void *pvParameters)
static void esp_zb_task(void *pvParameters)
{
ZB_INIT("zigbee gateway");
zb_set_network_coordinator_role(IEEE_CHANNEL_MASK);
zb_set_nvram_erase_at_start(ERASE_PERSISTENT_CONFIG);
zb_set_max_children(MAX_CHILDREN);
/* initialize Zigbee stack with Zigbee coordinator config */
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZC_CONFIG();
esp_zb_init(&zb_nwk_cfg);
/* initiate Zigbee Stack start without zb_send_no_autostart_signal auto-start */
ESP_ERROR_CHECK(zboss_start_no_autostart());
while (1) {
zboss_main_loop_iteration();
}
ESP_ERROR_CHECK(esp_zb_start(false));
esp_zb_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(),
esp_zb_platform_config_t config = {
.radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
};
/* load Zigbee gateway platform config to initialization */
ESP_ERROR_CHECK(zb_esp_platform_config(&config));
xTaskCreate(zboss_task, "zboss_main", 4096, NULL, 5, NULL);
ESP_ERROR_CHECK(esp_zb_platform_config(&config));
xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
}
17 changes: 12 additions & 5 deletions examples/zigbee/esp_zigbee_gateway/main/esp_zigbee_gateway.h
Expand Up @@ -36,14 +36,21 @@
*/

#include "esp_err.h"
#include "zboss_api.h"
#include "esp_zigbee_core.h"

/* 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 amount of connected devices */
#define INSTALLCODE_POLICY_ENABLE false /* enable the install code policy for security */

#define ZB_ESP_DEFAULT_RADIO_CONFIG() \
#define ESP_ZB_ZC_CONFIG() \
{ \
.esp_zb_role = ESP_ZB_DEVICE_TYPE_COORDINATOR, \
.install_code_policy = INSTALLCODE_POLICY_ENABLE, \
.nwk_cfg.zczr_cfg = { \
.max_children = MAX_CHILDREN, \
}, \
}
#define ESP_ZB_DEFAULT_RADIO_CONFIG() \
{ \
.radio_mode = RADIO_MODE_UART_RCP, \
.radio_uart_config = { \
Expand All @@ -63,7 +70,7 @@
}, \
}

#define ZB_ESP_DEFAULT_HOST_CONFIG() \
#define ESP_ZB_DEFAULT_HOST_CONFIG() \
{ \
.host_connection_mode = HOST_CONNECTION_MODE_NONE, \
}
3 changes: 2 additions & 1 deletion examples/zigbee/esp_zigbee_gateway/main/idf_component.yml
@@ -1,6 +1,7 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp-zboss-lib: "~0.0.4"
espressif/esp-zboss-lib: "~0.1.0"
espressif/esp-zigbee-lib: "~0.2.0"
## Required IDF version
idf:
version: ">=5.0.0"
3 changes: 2 additions & 1 deletion examples/zigbee/esp_zigbee_gateway/partitions.csv
Expand Up @@ -3,4 +3,5 @@
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
zb_storage, data, fat, , 128K,
zb_storage, data, fat, 0x110000, 128K,
zb_fct, data, fat, 0x130000, 1K,
6 changes: 4 additions & 2 deletions examples/zigbee/esp_zigbee_rcp/README.md
Expand Up @@ -3,8 +3,6 @@

# Rcp Example

(See the `README.md` file in the upper level 'examples' directory for more information about examples.)

This test code shows how to configure Zigbee rcp (radio co-processor) device. Rcp doesn't function alone, it needs to work together with Zigbee gateway (see [esp_zigbee_gateway example](../esp_zigbee_gateway))

## Hardware Required
Expand All @@ -18,6 +16,10 @@ This test code shows how to configure Zigbee rcp (radio co-processor) device. Rc

Before project configuration and build, make sure to set the correct chip target using `idf.py --preview set-target esp32h2`

## Erase the NVRAM

Before flash it to the board, it is recommended to erase NVRAM if user doesn't want to keep the previous examples or other projects stored info using `idf.py -p PORT erase-flash`

## Build and Flash

Build the project, flash it to the board by running `idf.py -p <PORT> build flash`
Expand Down
15 changes: 8 additions & 7 deletions examples/zigbee/esp_zigbee_rcp/main/esp_zigbee_rcp.c
Expand Up @@ -46,7 +46,7 @@
#endif
static const char *TAG = "ESP_ZB_RCP";

void zboss_signal_handler(zb_bufid_t bufid)
void zboss_signal_handler(uint8_t bufid)
{
zb_zdo_app_signal_hdr_t *sg_p = NULL;
/* get application signal from the buffer */
Expand All @@ -72,7 +72,7 @@ void zboss_signal_handler(zb_bufid_t bufid)
}
}

static void zboss_task(void * pvParameters)
static void esp_zb_task(void * pvParameters)
{
ZB_INIT("esp_zigbee_rcp");
while (1) {
Expand All @@ -82,11 +82,12 @@ static void zboss_task(void * pvParameters)

void app_main(void)
{
zb_esp_platform_config_t config = {
.radio_config = ZB_ESP_DEFAULT_RADIO_CONFIG(),
.host_config = ZB_ESP_DEFAULT_HOST_CONFIG(),
esp_zb_platform_config_t config = {
.radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
};
/* load Zigbee rcp platform config to initialization */
ESP_ERROR_CHECK(zb_esp_platform_config(&config));
xTaskCreate(zboss_task, "zboss_main", 4096, NULL, 5, NULL);
esp_zb_macsplit_set_version(RCP_COMPILE_DEFINE);
ESP_ERROR_CHECK(esp_zb_platform_config(&config));
xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
}
5 changes: 2 additions & 3 deletions examples/zigbee/esp_zigbee_rcp/main/esp_zigbee_rcp.h
Expand Up @@ -34,15 +34,14 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "zboss_api.h"

#define ZB_ESP_DEFAULT_RADIO_CONFIG() \
#define ESP_ZB_DEFAULT_RADIO_CONFIG() \
{ \
.radio_mode = RADIO_MODE_NATIVE, \
}

#define ZB_ESP_DEFAULT_HOST_CONFIG() \
#define ESP_ZB_DEFAULT_HOST_CONFIG() \
{ \
.host_connection_mode = HOST_CONNECTION_MODE_RCP_UART, \
.host_uart_config = { \
Expand Down
2 changes: 1 addition & 1 deletion examples/zigbee/esp_zigbee_rcp/main/idf_component.yml
@@ -1,6 +1,6 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp-zboss-lib: "~0.0.4"
espressif/esp-zboss-lib: "~0.1.0"
## Required IDF version
idf:
version: ">=5.0.0"
1 change: 0 additions & 1 deletion examples/zigbee/esp_zigbee_rcp/sdkconfig.defaults
Expand Up @@ -15,6 +15,5 @@ CONFIG_PARTITION_TABLE_MD5=y
#
CONFIG_ZB_ENABLED=y
CONFIG_ZB_RCP=y
CONFIG_ZB_CONSOLE_UART_BAUDRATE=115200
# end of ZBOSS Source
# end of Component config
Expand Up @@ -3,20 +3,22 @@

# Light Bulb Example

(See the `README.md` file in the upper level 'examples' directory for more information about examples.)

This test code shows how to configure Zigbee end device and use it as a light bulb
This test code shows how to configure Zigbee end device and use it as HA on/off light bulb

## Hardware Required

* One development board with ESP32-H2 SoC acting as Zigbee end device (loaded with light bulb example)
* One development board with ESP32-H2 SoC acting as Zigbee end device (loaded with HA_on_off_light example)
* A USB cable for power supply and programming
* Choose another ESP32-H2 as Zigbee coordinator (see [light switch example](../light_switch))
* Choose another ESP32-H2 as Zigbee coordinator (see [HA_on_off_switch example](../HA_on_off_switch))

## Configure the project

Before project configuration and build, make sure to set the correct chip target using `idf.py set-target esp32h2`.

## Erase the NVRAM

Before flash it to the board, it is recommended to erase NVRAM if user doesn't want to keep the previous examples or other projects stored info using `idf.py -p PORT erase-flash`

## Build and Flash

Build the project, flash it to the board, and start the monitor tool to view the serial output by running `idf.py -p PORT flash monitor`.
Expand All @@ -28,20 +30,20 @@ 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 bulb:
I (918) ESP_ZB_LIGHT: status: 255
I (901) ESP_ZB_LIGHT: Zigbee stack initialized
I (901) ESP_ZB_LIGHT: Start network steering
I (2611) ESP_ZB_LIGHT: Joined network successfully (Extended PAN ID: aa:98:48:01:a0:03:f7:84, PAN ID: 0x0e8b)
I (5651) ESP_ZB_LIGHT: on/off attribute setting to 1
I (6631) ESP_ZB_LIGHT: on/off attribute setting to 0
I (7331) ESP_ZB_LIGHT: on/off attribute setting to 1
I (8251) ESP_ZB_LIGHT: on/off attribute setting to 0
I (9111) ESP_ZB_LIGHT: on/off attribute setting to 1
I (9671) ESP_ZB_LIGHT: on/off attribute setting to 0
I (918) ESP_ZB_LIGHT: status: 255
I (901) ESP_ZB_LIGHT: Zigbee stack initialized
I (901) ESP_ZB_LIGHT: Start network steering
I (2611) ESP_ZB_LIGHT: Joined network successfully (Extended PAN ID: aa:98:48:01:a0:03:f7:84, PAN ID: 0x0e8b)
I (5651) ESP_ZB_LIGHT: on/off light set to 1
I (6631) ESP_ZB_LIGHT: on/off light set to 0
I (7331) ESP_ZB_LIGHT: on/off light set to 1
I (8251) ESP_ZB_LIGHT: on/off light set to 0
I (9111) ESP_ZB_LIGHT: on/off light set to 1
I (9671) ESP_ZB_LIGHT: on/off light set to 0

## Light Control Functions

* By toggling the switch button (BOOT) on the ESP32-H2 board loaded with the `light switch` example, the LED on this board loaded with `light bulb` example will be on and off.
* By toggling the switch button (BOOT) on the ESP32-H2 board loaded with the `HA_on_off_switch` example, the LED on this board loaded with `HA_on_off_light` example will be on and off.

## Troubleshooting

Expand Down

0 comments on commit dc09f30

Please sign in to comment.