|
| 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 bulb. |
| 17 | + * |
| 18 | + * The example demonstrates how to use ESP Zigbee stack to create a end device light bulb. |
| 19 | + * The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator. |
| 20 | + * |
| 21 | + * Proper Zigbee mode must be selected in Tools->Zigbee mode |
| 22 | + * and also the correct partition scheme must be selected in Tools->Partition Scheme. |
| 23 | + * |
| 24 | + * Please check the README.md for instructions and more detailed description. |
| 25 | +*/ |
| 26 | + |
| 27 | +#ifndef ZIGBEE_MODE_ED |
| 28 | +#error "Zigbee end device mode is not selected in Tools->Zigbee mode" |
| 29 | +#endif |
| 30 | + |
| 31 | +#include"esp_zigbee_core.h" |
| 32 | +#include"freertos/FreeRTOS.h" |
| 33 | +#include"freertos/task.h" |
| 34 | +#include"ha/esp_zigbee_ha_standard.h" |
| 35 | + |
| 36 | +#defineLED_PIN RGB_BUILTIN |
| 37 | + |
| 38 | +/* Default End Device config */ |
| 39 | +#defineESP_ZB_ZED_CONFIG() \ |
| 40 | +{\ |
| 41 | + .esp_zb_role = ESP_ZB_DEVICE_TYPE_ED, \ |
| 42 | + .install_code_policy = INSTALLCODE_POLICY_ENABLE, \ |
| 43 | + .nwk_cfg ={\ |
| 44 | + .zed_cfg ={\ |
| 45 | + .ed_timeout = ED_AGING_TIMEOUT, \ |
| 46 | + .keep_alive = ED_KEEP_ALIVE, \ |
| 47 | + }, \ |
| 48 | + }, \ |
| 49 | + } |
| 50 | + |
| 51 | +#defineESP_ZB_DEFAULT_RADIO_CONFIG() \ |
| 52 | +{\ |
| 53 | + .radio_mode = RADIO_MODE_NATIVE, \ |
| 54 | + } |
| 55 | + |
| 56 | +#defineESP_ZB_DEFAULT_HOST_CONFIG() \ |
| 57 | +{\ |
| 58 | + .host_connection_mode = HOST_CONNECTION_MODE_NONE, \ |
| 59 | + } |
| 60 | + |
| 61 | +/* Zigbee configuration */ |
| 62 | +#defineINSTALLCODE_POLICY_ENABLEfalse/* enable the install code policy for security */ |
| 63 | +#defineED_AGING_TIMEOUT ESP_ZB_ED_AGING_TIMEOUT_64MIN |
| 64 | +#defineED_KEEP_ALIVE3000/* 3000 millisecond */ |
| 65 | +#defineHA_ESP_LIGHT_ENDPOINT10/* esp light bulb device endpoint, used to process light controlling commands */ |
| 66 | +#defineESP_ZB_PRIMARY_CHANNEL_MASK ESP_ZB_TRANSCEIVER_ALL_CHANNELS_MASK /* Zigbee primary channel mask use in the example */ |
| 67 | + |
| 68 | +/********************* Zigbee functions **************************/ |
| 69 | +staticvoidbdb_start_top_level_commissioning_cb(uint8_t mode_mask) |
| 70 | +{ |
| 71 | +ESP_ERROR_CHECK(esp_zb_bdb_start_top_level_commissioning(mode_mask)); |
| 72 | +} |
| 73 | + |
| 74 | +voidesp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) |
| 75 | +{ |
| 76 | +uint32_t *p_sg_p = signal_struct->p_app_signal; |
| 77 | +esp_err_t err_status = signal_struct->esp_err_status; |
| 78 | +esp_zb_app_signal_type_t sig_type = (esp_zb_app_signal_type_t)*p_sg_p; |
| 79 | +switch (sig_type){ |
| 80 | +case ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP: |
| 81 | +log_i("Zigbee stack initialized"); |
| 82 | +esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_INITIALIZATION); |
| 83 | +break; |
| 84 | +case ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START: |
| 85 | +case ESP_ZB_BDB_SIGNAL_DEVICE_REBOOT: |
| 86 | +if (err_status == ESP_OK){ |
| 87 | +log_i("Start network steering"); |
| 88 | +esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING); |
| 89 | + } else{ |
| 90 | +/* commissioning failed */ |
| 91 | +log_w("Failed to initialize Zigbee stack (status: %s)", esp_err_to_name(err_status)); |
| 92 | + } |
| 93 | +break; |
| 94 | +case ESP_ZB_BDB_SIGNAL_STEERING: |
| 95 | +if (err_status == ESP_OK){ |
| 96 | +esp_zb_ieee_addr_t extended_pan_id; |
| 97 | +esp_zb_get_extended_pan_id(extended_pan_id); |
| 98 | +log_i("Joined network successfully (Extended PAN ID: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx, Channel:%d, Short Address: 0x%04hx)", |
| 99 | + extended_pan_id[7], extended_pan_id[6], extended_pan_id[5], extended_pan_id[4], |
| 100 | + extended_pan_id[3], extended_pan_id[2], extended_pan_id[1], extended_pan_id[0], |
| 101 | +esp_zb_get_pan_id(), esp_zb_get_current_channel(), esp_zb_get_short_address()); |
| 102 | + } else{ |
| 103 | +log_i("Network steering was not successful (status: %s)", esp_err_to_name(err_status)); |
| 104 | +esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_STEERING, 1000); |
| 105 | + } |
| 106 | +break; |
| 107 | +default: |
| 108 | +log_i("ZDO signal: %s (0x%x), status: %s", esp_zb_zdo_signal_to_string(sig_type), sig_type, |
| 109 | +esp_err_to_name(err_status)); |
| 110 | +break; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +staticesp_err_tzb_action_handler(esp_zb_core_action_callback_id_t callback_id, constvoid *message) |
| 115 | +{ |
| 116 | +esp_err_t ret = ESP_OK; |
| 117 | +switch (callback_id){ |
| 118 | +case ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID: |
| 119 | + ret = zb_attribute_handler((esp_zb_zcl_set_attr_value_message_t *)message); |
| 120 | +break; |
| 121 | +default: |
| 122 | +log_w("Receive Zigbee action(0x%x) callback", callback_id); |
| 123 | +break; |
| 124 | + } |
| 125 | +return ret; |
| 126 | +} |
| 127 | + |
| 128 | +staticvoidesp_zb_task(void *pvParameters) |
| 129 | +{ |
| 130 | +esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG(); |
| 131 | +esp_zb_init(&zb_nwk_cfg); |
| 132 | +esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG(); |
| 133 | +esp_zb_ep_list_t *esp_zb_on_off_light_ep = esp_zb_on_off_light_ep_create(HA_ESP_LIGHT_ENDPOINT, &light_cfg); |
| 134 | +esp_zb_device_register(esp_zb_on_off_light_ep); |
| 135 | +esp_zb_core_action_handler_register(zb_action_handler); |
| 136 | +esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK); |
| 137 | + |
| 138 | +//Erase NVRAM before creating connection to new Coordinator |
| 139 | +//esp_zb_nvram_erase_at_start(true); //Comment out this line to erase NVRAM data if you are conneting to new Coordinator |
| 140 | + |
| 141 | +ESP_ERROR_CHECK(esp_zb_start(false)); |
| 142 | +esp_zb_main_loop_iteration(); |
| 143 | +} |
| 144 | + |
| 145 | +/* Handle the light attribute */ |
| 146 | + |
| 147 | +staticesp_err_tzb_attribute_handler(constesp_zb_zcl_set_attr_value_message_t *message) |
| 148 | +{ |
| 149 | +esp_err_t ret = ESP_OK; |
| 150 | +bool light_state = 0; |
| 151 | + |
| 152 | +if(!message){ |
| 153 | +log_e("Empty message"); |
| 154 | + } |
| 155 | +if(message->info.status != ESP_ZB_ZCL_STATUS_SUCCESS){ |
| 156 | +log_e("Received message: error status(%d)", message->info.status); |
| 157 | + } |
| 158 | + |
| 159 | +log_i("Received message: endpoint(%d), cluster(0x%x), attribute(0x%x), data size(%d)", message->info.dst_endpoint, message->info.cluster, |
| 160 | + message->attribute.id, message->attribute.data.size); |
| 161 | +if (message->info.dst_endpoint == HA_ESP_LIGHT_ENDPOINT){ |
| 162 | +if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF){ |
| 163 | +if (message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_BOOL){ |
| 164 | + light_state = message->attribute.data.value ? *(bool *)message->attribute.data.value : light_state; |
| 165 | +log_i("Light sets to %s", light_state ? "On" : "Off"); |
| 166 | +neopixelWrite(LED_PIN,255*light_state,255*light_state,255*light_state); // Toggle light |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +return ret; |
| 171 | +} |
| 172 | + |
| 173 | +/********************* Arduino functions **************************/ |
| 174 | +voidsetup(){ |
| 175 | +// Init Zigbee |
| 176 | +esp_zb_platform_config_t config ={ |
| 177 | + .radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(), |
| 178 | + .host_config = ESP_ZB_DEFAULT_HOST_CONFIG(), |
| 179 | + }; |
| 180 | +ESP_ERROR_CHECK(esp_zb_platform_config(&config)); |
| 181 | + |
| 182 | +// Init RMT and leave light OFF |
| 183 | +neopixelWrite(LED_PIN,0,0,0); |
| 184 | + |
| 185 | +// Start Zigbee task |
| 186 | +xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL); |
| 187 | +} |
| 188 | + |
| 189 | +voidloop(){ |
| 190 | +//empty, zigbee running in task |
| 191 | +} |
0 commit comments