Skip to content

Commit

Permalink
Zigbee: Merging light_coordinator and light switch into one
Browse files Browse the repository at this point in the history
  * 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
  • Loading branch information
likunqiao097304 authored and espressif-bot committed Jun 21, 2022
1 parent d178d1d commit c42628f
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 385 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The examples are grouped into subdirectories by category. Each category director
- `storage` Examples showing data storage methods using SPI flash, external storage like the SD/MMC interface and flash partitioning.
- `system` Demonstrates some internal chip features, or debugging & development tools.
- `wifi` Advanced Wi-Fi features (For network protocol examples, see `protocols` instead.)
- `Zigbee` Zigbee network and device examples.

In addition to these examples, `commmon_components` directory contains code shared by several examples.

Expand Down
8 changes: 3 additions & 5 deletions examples/zigbee/light_sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

## Overview

The folder contains examples demonstrating Zigbee Router, Coordinator, and End-Device roles
The folder contains examples demonstrating Zigbee Coordinator and End-Device roles

* [light_bulb](light_bulb) is a light bulb example demonstrating Zigbee Router role. It provides a simple on/off condition for a Zigbee light. It runs on an 802.15.4 SoC like ESP32-H2. For more details see the example readme file.
* [light_bulb](light_bulb) is a light bulb example demonstrating Zigbee End-Device role. It provides a simple on/off condition for a Zigbee light. It runs on an 802.15.4 SoC like ESP32-H2. For more details see the example readme file.

* [light_coordinator](light_coordinator) is a light coordinator example demonstrating Zigbee Coordinator role. It provides a formation of the Zigbee network. It runs on an 802.15.4 SoC like ESP32-H2. For more details to see the example readme file.

* [light_switch](light_switch) is a light switch example demonstrating Zigbee End-Device role. It provides an on/off toggle to control light. It runs on an 802.15.4 SoC like ESP32-H2. For more details to see the example readme file.
* [light_switch](light_switch) is a light switch example demonstrating Zigbee Coordinator role. It provides an on/off toggle to control light. It runs on an 802.15.4 SoC like ESP32-H2. For more details to see the example readme file.

28 changes: 13 additions & 15 deletions examples/zigbee/light_sample/light_bulb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

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

This test code shows how to configure Zigbee router device and use it as a light bulb
This test code shows how to configure Zigbee end device and use it as a light bulb

## Hardware Required

* One development board with ESP32-H2 SoC acting as Zigbee router (loaded with light bulb example)
* One development board with ESP32-H2 SoC acting as Zigbee end device (loaded with light bulb 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 end-device (see [light switch example](../light_switch))
* Choose another ESP32-H2 as Zigbee coordinator (see [light switch example](../light_switch))

## Configure the project

Expand All @@ -29,17 +28,16 @@ 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 (9638) ESP_ZB_LIGHT: status: 255
I (9638) ESP_ZB_LIGHT: Zigbee stack initialized
I (9648) ESP_ZB_LIGHT: Start network steering
I (12368) ESP_ZB_LIGHT: Joined network successfully (Extended PAN ID: f9:54:2d:01:a0:03:f7:84, PAN ID: 0xf5b5)
I (12398) ESP_ZB_LIGHT: status: 0
I (18158) ESP_ZB_LIGHT: on/off attribute setting to 1
I (19388) ESP_ZB_LIGHT: on/off attribute setting to 0
I (20418) ESP_ZB_LIGHT: on/off attribute setting to 1
I (21558) ESP_ZB_LIGHT: on/off attribute setting to 0
I (22478) ESP_ZB_LIGHT: on/off attribute setting to 1
I (23088) 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 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

## Light Control Functions

Expand Down
42 changes: 27 additions & 15 deletions examples/zigbee/light_sample/light_bulb/main/esp_zb_light.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@
*/

#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_zb_light.h"

/**
* @note Make sure set idf.py menuconfig in zigbee component as zigbee end-device device!
*/
#if !defined ZB_ED_ROLE
#error Define ZB_ED_ROLE in idf.py menuconfig to compile light bulb source code.
#endif

static bulb_device_ctx_t esp_light_ctx = {
/* basic cluster attributes data */
.basic_attr.zcl_version = ZB_ZCL_VERSION,
Expand Down Expand Up @@ -108,8 +117,9 @@ static void bdb_start_top_level_commissioning_cb(zb_uint8_t mode_mask)
*/
void zboss_signal_handler(zb_bufid_t bufid)
{
zb_uint8_t status = ZB_GET_APP_SIGNAL_STATUS(bufid);
zb_zdo_app_signal_type_t sig = zb_get_app_signal(bufid, NULL);
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);

switch (sig) {
case ZB_ZDO_SIGNAL_SKIP_STARTUP:
Expand Down Expand Up @@ -212,31 +222,33 @@ static void esp_zb_light_cb(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_bulb");
zb_set_network_router_role(IEEE_CHANNEL_MASK);
zb_set_max_children(MAX_CHILDREN);
zb_set_network_ed_role(IEEE_CHANNEL_MASK);
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 */
light_driver_init(LIGHT_DEFAULT_OFF);
/* register callback for handling ZCL commands */
ZB_ZCL_REGISTER_DEVICE_CB(esp_zb_light_cb);
/* register light device context (endpoints) */
ZB_AF_REGISTER_DEVICE_CTX(&esp_zb_light_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 light_bulb platform config to initialization */
ESP_ERROR_CHECK(zb_esp_platform_config(&config));
xTaskCreate(zboss_task, "zboss_main", 4096, NULL, 5, NULL);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
/* 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 */

/* groups cluster attributes */
typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion examples/zigbee/light_sample/light_bulb/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=n
# Zboss
#
CONFIG_ZB_ENABLED=y
CONFIG_ZB_ZCZR=y
CONFIG_ZB_ZED=y
# end of Zboss
# end of Component config
5 changes: 0 additions & 5 deletions examples/zigbee/light_sample/light_coordinator/CMakeLists.txt

This file was deleted.

53 changes: 0 additions & 53 deletions examples/zigbee/light_sample/light_coordinator/README.md

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit c42628f

Please sign in to comment.