|
| 1 | +// Copyright 2023 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @brief This example demonstrates simple Zigbee light switch. |
| 17 | + * |
| 18 | + * The example demonstrates how to use ESP Zigbee stack to control a light bulb. |
| 19 | + * The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator. |
| 20 | + * Button switch and Zigbee runs in separate tasks. |
| 21 | + * |
| 22 | + * Proper Zigbee mode must be selected in Tools->Zigbee mode |
| 23 | + * and also the correct partition scheme must be selected in Tools->Partition Scheme. |
| 24 | + * |
| 25 | + * Please check the README.md for instructions and more detailed description. |
| 26 | + */ |
| 27 | + |
| 28 | +#ifndef ZIGBEE_MODE_ZCZR |
| 29 | +#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" |
| 30 | +#endif |
| 31 | + |
| 32 | +#include "esp_zigbee_core.h" |
| 33 | +#include "freertos/FreeRTOS.h" |
| 34 | +#include "freertos/task.h" |
| 35 | +#include "ha/esp_zigbee_ha_standard.h" |
| 36 | + |
| 37 | +/* Switch configuration */ |
| 38 | +#define GPIO_INPUT_IO_TOGGLE_SWITCH GPIO_NUM_9 |
| 39 | +#define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0])) |
| 40 | + |
| 41 | +typedef enum { |
| 42 | + SWITCH_ON_CONTROL, |
| 43 | + SWITCH_OFF_CONTROL, |
| 44 | + SWITCH_ONOFF_TOGGLE_CONTROL, |
| 45 | + SWITCH_LEVEL_UP_CONTROL, |
| 46 | + SWITCH_LEVEL_DOWN_CONTROL, |
| 47 | + SWITCH_LEVEL_CYCLE_CONTROL, |
| 48 | + SWITCH_COLOR_CONTROL, |
| 49 | +} switch_func_t; |
| 50 | + |
| 51 | +typedef struct { |
| 52 | + uint8_t pin; |
| 53 | + switch_func_t func; |
| 54 | +} switch_func_pair_t; |
| 55 | + |
| 56 | +typedef enum { |
| 57 | + SWITCH_IDLE, |
| 58 | + SWITCH_PRESS_ARMED, |
| 59 | + SWITCH_PRESS_DETECTED, |
| 60 | + SWITCH_PRESSED, |
| 61 | + SWITCH_RELEASE_DETECTED, |
| 62 | +} switch_state_t; |
| 63 | + |
| 64 | +static switch_func_pair_t button_func_pair[] = { |
| 65 | + {GPIO_INPUT_IO_TOGGLE_SWITCH, SWITCH_ONOFF_TOGGLE_CONTROL} |
| 66 | +}; |
| 67 | + |
| 68 | +/* Default Coordinator config */ |
| 69 | +#define ESP_ZB_ZC_CONFIG() \ |
| 70 | + { \ |
| 71 | + .esp_zb_role = ESP_ZB_DEVICE_TYPE_COORDINATOR, \ |
| 72 | + .install_code_policy = INSTALLCODE_POLICY_ENABLE, \ |
| 73 | + .nwk_cfg = { \ |
| 74 | + .zczr_cfg = { \ |
| 75 | + .max_children = MAX_CHILDREN, \ |
| 76 | + }, \ |
| 77 | + } \ |
| 78 | + } |
| 79 | + |
| 80 | +#define ESP_ZB_DEFAULT_RADIO_CONFIG() \ |
| 81 | + { \ |
| 82 | + .radio_mode = RADIO_MODE_NATIVE, \ |
| 83 | + } |
| 84 | + |
| 85 | +#define ESP_ZB_DEFAULT_HOST_CONFIG() \ |
| 86 | + { \ |
| 87 | + .host_connection_mode = HOST_CONNECTION_MODE_NONE, \ |
| 88 | + } |
| 89 | + |
| 90 | +typedef struct light_bulb_device_params_s { |
| 91 | + esp_zb_ieee_addr_t ieee_addr; |
| 92 | + uint8_t endpoint; |
| 93 | + uint16_t short_addr; |
| 94 | +} light_bulb_device_params_t; |
| 95 | + |
| 96 | +/* Zigbee configuration */ |
| 97 | +#define MAX_CHILDREN 10 /* the max amount of connected devices */ |
| 98 | +#define INSTALLCODE_POLICY_ENABLE false /* enable the install code policy for security */ |
| 99 | +#define HA_ONOFF_SWITCH_ENDPOINT 1 /* esp light switch device endpoint */ |
| 100 | +#define ESP_ZB_PRIMARY_CHANNEL_MASK ESP_ZB_TRANSCEIVER_ALL_CHANNELS_MASK /* Zigbee primary channel mask use in the example */ |
| 101 | + |
| 102 | +/********************* Define functions **************************/ |
| 103 | +static void esp_zb_buttons_handler(switch_func_pair_t *button_func_pair) |
| 104 | +{ |
| 105 | + if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) { |
| 106 | + /* implemented light switch toggle functionality */ |
| 107 | + esp_zb_zcl_on_off_cmd_t cmd_req; |
| 108 | + cmd_req.zcl_basic_cmd.src_endpoint = HA_ONOFF_SWITCH_ENDPOINT; |
| 109 | + cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT; |
| 110 | + cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_TOGGLE_ID; |
| 111 | + log_i("Send 'on_off toggle' command"); |
| 112 | + esp_zb_zcl_on_off_cmd_req(&cmd_req); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask) |
| 117 | +{ |
| 118 | + ESP_ERROR_CHECK(esp_zb_bdb_start_top_level_commissioning(mode_mask)); |
| 119 | +} |
| 120 | + |
| 121 | +static void bind_cb(esp_zb_zdp_status_t zdo_status, void *user_ctx) |
| 122 | +{ |
| 123 | + if (zdo_status == ESP_ZB_ZDP_STATUS_SUCCESS) { |
| 124 | + log_i("Bound successfully!"); |
| 125 | + if (user_ctx) { |
| 126 | + light_bulb_device_params_t *light = (light_bulb_device_params_t *)user_ctx; |
| 127 | + log_i("The light originating from address(0x%x) on endpoint(%d)", light->short_addr, light->endpoint); |
| 128 | + free(light); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +static void user_find_cb(esp_zb_zdp_status_t zdo_status, uint16_t addr, uint8_t endpoint, void *user_ctx) |
| 134 | +{ |
| 135 | + if (zdo_status == ESP_ZB_ZDP_STATUS_SUCCESS) { |
| 136 | + log_i("Found light"); |
| 137 | + esp_zb_zdo_bind_req_param_t bind_req; |
| 138 | + light_bulb_device_params_t *light = (light_bulb_device_params_t *)malloc(sizeof(light_bulb_device_params_t)); |
| 139 | + light->endpoint = endpoint; |
| 140 | + light->short_addr = addr; |
| 141 | + esp_zb_ieee_address_by_short(light->short_addr, light->ieee_addr); |
| 142 | + esp_zb_get_long_address(bind_req.src_address); |
| 143 | + bind_req.src_endp = HA_ONOFF_SWITCH_ENDPOINT; |
| 144 | + bind_req.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_ON_OFF; |
| 145 | + bind_req.dst_addr_mode = ESP_ZB_ZDO_BIND_DST_ADDR_MODE_64_BIT_EXTENDED; |
| 146 | + memcpy(bind_req.dst_address_u.addr_long, light->ieee_addr, sizeof(esp_zb_ieee_addr_t)); |
| 147 | + bind_req.dst_endp = endpoint; |
| 148 | + bind_req.req_dst_addr = esp_zb_get_short_address(); |
| 149 | + log_i("Try to bind On/Off"); |
| 150 | + esp_zb_zdo_device_bind_req(&bind_req, bind_cb, (void *)light); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) |
| 155 | +{ |
| 156 | + uint32_t *p_sg_p = signal_struct->p_app_signal; |
| 157 | + esp_err_t err_status = signal_struct->esp_err_status; |
| 158 | + esp_zb_app_signal_type_t sig_type = (esp_zb_app_signal_type_t)*p_sg_p; |
| 159 | + esp_zb_zdo_signal_device_annce_params_t *dev_annce_params = NULL; |
| 160 | + switch (sig_type) { |
| 161 | + case ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP: |
| 162 | + log_i("Zigbee stack initialized"); |
| 163 | + esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_INITIALIZATION); |
| 164 | + break; |
| 165 | + case ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START: |
| 166 | + case ESP_ZB_BDB_SIGNAL_DEVICE_REBOOT: |
| 167 | + if (err_status == ESP_OK) { |
| 168 | + log_i("Start network formation"); |
| 169 | + esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_FORMATION); |
| 170 | + } else { |
| 171 | + log_e("Failed to initialize Zigbee stack (status: %s)", esp_err_to_name(err_status)); |
| 172 | + } |
| 173 | + break; |
| 174 | + case ESP_ZB_BDB_SIGNAL_FORMATION: |
| 175 | + if (err_status == ESP_OK) { |
| 176 | + esp_zb_ieee_addr_t extended_pan_id; |
| 177 | + esp_zb_get_extended_pan_id(extended_pan_id); |
| 178 | + log_i("Formed network successfully (Extended PAN ID: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx, Channel:%d, Short Address: 0x%04hx)", |
| 179 | + extended_pan_id[7], extended_pan_id[6], extended_pan_id[5], extended_pan_id[4], |
| 180 | + extended_pan_id[3], extended_pan_id[2], extended_pan_id[1], extended_pan_id[0], |
| 181 | + esp_zb_get_pan_id(), esp_zb_get_current_channel(), esp_zb_get_short_address()); |
| 182 | + esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING); |
| 183 | + } else { |
| 184 | + log_i("Restart network formation (status: %s)", esp_err_to_name(err_status)); |
| 185 | + esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_FORMATION, 1000); |
| 186 | + } |
| 187 | + break; |
| 188 | + case ESP_ZB_BDB_SIGNAL_STEERING: |
| 189 | + if (err_status == ESP_OK) { |
| 190 | + log_i("Network steering started"); |
| 191 | + } |
| 192 | + break; |
| 193 | + case ESP_ZB_ZDO_SIGNAL_DEVICE_ANNCE: |
| 194 | + dev_annce_params = (esp_zb_zdo_signal_device_annce_params_t *)esp_zb_app_signal_get_params(p_sg_p); |
| 195 | + log_i("New device commissioned or rejoined (short: 0x%04hx)", dev_annce_params->device_short_addr); |
| 196 | + esp_zb_zdo_match_desc_req_param_t cmd_req; |
| 197 | + cmd_req.dst_nwk_addr = dev_annce_params->device_short_addr; |
| 198 | + cmd_req.addr_of_interest = dev_annce_params->device_short_addr; |
| 199 | + esp_zb_zdo_find_on_off_light(&cmd_req, user_find_cb, NULL); |
| 200 | + break; |
| 201 | + default: |
| 202 | + log_i("ZDO signal: %s (0x%x), status: %s", esp_zb_zdo_signal_to_string(sig_type), sig_type, |
| 203 | + esp_err_to_name(err_status)); |
| 204 | + break; |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +static void esp_zb_task(void *pvParameters) |
| 209 | +{ |
| 210 | + esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZC_CONFIG(); |
| 211 | + esp_zb_init(&zb_nwk_cfg); |
| 212 | + esp_zb_on_off_switch_cfg_t switch_cfg = ESP_ZB_DEFAULT_ON_OFF_SWITCH_CONFIG(); |
| 213 | + esp_zb_ep_list_t *esp_zb_on_off_switch_ep = esp_zb_on_off_switch_ep_create(HA_ONOFF_SWITCH_ENDPOINT, &switch_cfg); |
| 214 | + esp_zb_device_register(esp_zb_on_off_switch_ep); |
| 215 | + esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK); |
| 216 | + ESP_ERROR_CHECK(esp_zb_start(false)); |
| 217 | + esp_zb_main_loop_iteration(); |
| 218 | +} |
| 219 | + |
| 220 | +/********************* GPIO functions **************************/ |
| 221 | +static QueueHandle_t gpio_evt_queue = NULL; |
| 222 | + |
| 223 | +static void IRAM_ATTR gpio_isr_handler(void *arg) |
| 224 | +{ |
| 225 | + xQueueSendFromISR(gpio_evt_queue, (switch_func_pair_t *)arg, NULL); |
| 226 | +} |
| 227 | + |
| 228 | +static void switch_gpios_intr_enabled(bool enabled) |
| 229 | +{ |
| 230 | + for (int i = 0; i < PAIR_SIZE(button_func_pair); ++i) { |
| 231 | + if (enabled) { |
| 232 | + enableInterrupt((button_func_pair[i]).pin); |
| 233 | + } else { |
| 234 | + disableInterrupt((button_func_pair[i]).pin); |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +/********************* Arduino functions **************************/ |
| 240 | +void setup() { |
| 241 | + // Init Zigbee |
| 242 | + esp_zb_platform_config_t config = { |
| 243 | + .radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(), |
| 244 | + .host_config = ESP_ZB_DEFAULT_HOST_CONFIG(), |
| 245 | + }; |
| 246 | + |
| 247 | + ESP_ERROR_CHECK(esp_zb_platform_config(&config)); |
| 248 | + |
| 249 | + // Init button switch |
| 250 | + for (int i = 0; i < PAIR_SIZE(button_func_pair); i++) { |
| 251 | + pinMode(button_func_pair[i].pin, INPUT_PULLUP); |
| 252 | + /* create a queue to handle gpio event from isr */ |
| 253 | + gpio_evt_queue = xQueueCreate(10, sizeof(switch_func_pair_t)); |
| 254 | + if ( gpio_evt_queue == 0) { |
| 255 | + log_e("Queue was not created and must not be used"); |
| 256 | + while(1); |
| 257 | + } |
| 258 | + attachInterruptArg(button_func_pair[i].pin, gpio_isr_handler, (void *) (button_func_pair + i), FALLING); |
| 259 | + } |
| 260 | + |
| 261 | + // Start Zigbee task |
| 262 | + xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL); |
| 263 | +} |
| 264 | + |
| 265 | +void loop() { |
| 266 | + // Handle button switch in loop() |
| 267 | + uint8_t pin = 0; |
| 268 | + switch_func_pair_t button_func_pair; |
| 269 | + static switch_state_t switch_state = SWITCH_IDLE; |
| 270 | + bool evt_flag = false; |
| 271 | + |
| 272 | + /* check if there is any queue received, if yes read out the button_func_pair */ |
| 273 | + if (xQueueReceive(gpio_evt_queue, &button_func_pair, portMAX_DELAY)) { |
| 274 | + pin = button_func_pair.pin; |
| 275 | + switch_gpios_intr_enabled(false); |
| 276 | + evt_flag = true; |
| 277 | + } |
| 278 | + while (evt_flag) { |
| 279 | + bool value = digitalRead(pin); |
| 280 | + switch (switch_state) { |
| 281 | + case SWITCH_IDLE: |
| 282 | + switch_state = (value == LOW) ? SWITCH_PRESS_DETECTED : SWITCH_IDLE; |
| 283 | + break; |
| 284 | + case SWITCH_PRESS_DETECTED: |
| 285 | + switch_state = (value == LOW) ? SWITCH_PRESS_DETECTED : SWITCH_RELEASE_DETECTED; |
| 286 | + break; |
| 287 | + case SWITCH_RELEASE_DETECTED: |
| 288 | + switch_state = SWITCH_IDLE; |
| 289 | + /* callback to button_handler */ |
| 290 | + (*esp_zb_buttons_handler)(&button_func_pair); |
| 291 | + break; |
| 292 | + default: |
| 293 | + break; |
| 294 | + } |
| 295 | + if (switch_state == SWITCH_IDLE) { |
| 296 | + switch_gpios_intr_enabled(true); |
| 297 | + evt_flag = false; |
| 298 | + break; |
| 299 | + } |
| 300 | + vTaskDelay(10 / portTICK_PERIOD_MS); |
| 301 | + } |
| 302 | +} |
0 commit comments