Here is my code. It is basically the esp ‘HA_on_off_light’ that I have modified a bit.
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: LicenseRef-Included
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Espressif Systems
* integrated circuit in a product or a software update for such product,
* must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 4. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_check.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "ha/esp_zigbee_ha_standard.h"
#include "esp_zb_light.h"
#include "esp_adc/adc_oneshot.h"
/**
* @note Make sure set idf.py menuconfig in zigbee component as zigbee end device!
*/
#if !defined ZB_ED_ROLE
#error Define ZB_ED_ROLE in idf.py menuconfig to compile light (End Device) source code.
#endif
static const char *TAG = "ESP_ZB_ON_OFF_LIGHT";
// static int adc_raw[2][10];
static int network_active = 0;
static int16_t counter = 0;
static int16_t humidity = 5000;
static int16_t rel_pressure = 1016;
static esp_zb_temperature_meas_cluster_cfg_t temperature_cfg;
static esp_zb_humidity_meas_cluster_cfg_t humidity_cfg;
static esp_zb_pressure_meas_cluster_cfg_t pressure_cfg;
static esp_zb_analog_input_cluster_cfg_t analog_input_cfg;
/********************* Define functions **************************/
static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask)
{
ESP_ERROR_CHECK(esp_zb_bdb_start_top_level_commissioning(mode_mask));
}
static esp_err_t zb_attribute_handler(const esp_zb_zcl_set_attr_value_message_t *message)
{
esp_err_t ret = ESP_OK;
bool light_state = 0;
ESP_RETURN_ON_FALSE(message, ESP_FAIL, TAG, "Empty message");
ESP_RETURN_ON_FALSE(message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS, ESP_ERR_INVALID_ARG, TAG, "Received message: error status(%d)",
message->info.status);
ESP_LOGI(TAG, "Received message: endpoint(%d), cluster(0x%x), attribute(0x%x), data size(%d)", message->info.dst_endpoint, message->info.cluster,
message->attribute.id, message->attribute.data.size);
if (message->info.dst_endpoint == HA_ESP_LIGHT_ENDPOINT) {
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF) {
if (message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_BOOL) {
light_state = message->attribute.data.value ? *(bool *)message->attribute.data.value : light_state;
ESP_LOGI(TAG, "Light set to %s", light_state ? "On" : "Off");
light_driver_set_power(light_state);
}
}
}
return ret;
}
static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id, const void *message)
{
esp_err_t ret = ESP_OK;
switch (callback_id) {
case ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID:
ret = zb_attribute_handler((esp_zb_zcl_set_attr_value_message_t *)message);
break;
case ESP_ZB_CORE_CMD_DEFAULT_RESP_CB_ID:
{
esp_zb_zcl_cmd_default_resp_message_t *msg = (esp_zb_zcl_cmd_default_resp_message_t*)message;
ESP_LOGI(TAG, "Default Response: Status: 0x%04x, Command: %d", msg->status_code, msg->resp_to_cmd);
ESP_LOGI(TAG, " Src addr: 0x%04x, Dst Addr: 0x%04x, src ep: %d, dst ep: %d, cluster: 0x%04x, profile: 0x%04x",
msg->info.src_address.u.short_addr, msg->info.dst_address, msg->info.src_endpoint, msg->info.dst_endpoint,
msg->info.cluster, msg->info.profile);
ESP_LOGI(TAG, " Resp status: 0x%04x, frame control: 0x%02x, mfg code: 0x%04x, txn: %d, RSSI: %d",
msg->info.status, msg->info.header.fc, msg->info.header.manuf_code, msg->info.header.tsn, msg->info.header.rssi);
ret = ESP_OK;
}
break;
default:
ESP_LOGW(TAG, "Receive Zigbee action(0x%x) callback", callback_id);
break;
}
return ret;
}
void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
{
uint32_t *p_sg_p = signal_struct->p_app_signal;
esp_err_t err_status = signal_struct->esp_err_status;
esp_zb_app_signal_type_t sig_type = *p_sg_p;
esp_zb_zdo_signal_leave_params_t *leave_params = NULL;
switch (sig_type) {
case ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP:
ESP_LOGI(TAG, "Zigbee stack initialized");
esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_INITIALIZATION);
break;
case ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START:
case ESP_ZB_BDB_SIGNAL_DEVICE_REBOOT:
if( sig_type == ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START)
{ ESP_LOGI(TAG, "First Start"); }
else
{ ESP_LOGI(TAG, "Restart"); }
network_active = 0;
if (err_status == ESP_OK) {
ESP_LOGI(TAG, "Start network steering");
esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING);
} else {
/* commissioning failed */
ESP_LOGW(TAG, "Failed to initialize Zigbee stack (status: %d)", err_status);
// esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING);
esp_zb_scheduler_alarm(( esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_STEERING, 1000);
}
break;
case ESP_ZB_BDB_SIGNAL_STEERING:
if (err_status == ESP_OK) {
esp_zb_ieee_addr_t extended_pan_id;
esp_zb_get_extended_pan_id(extended_pan_id);
ESP_LOGI(TAG, "Joined network successfully (Extended PAN ID: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx, Channel:%d)",
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],
esp_zb_get_pan_id(), esp_zb_get_current_channel());
network_active = 1;
} else {
ESP_LOGI(TAG, "Network steering was not successful (status: %d)", err_status);
esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_STEERING, 1000);
network_active = 0;
}
break;
case ESP_ZB_ZDO_SIGNAL_LEAVE:
{
network_active = 0;
leave_params = (esp_zb_zdo_signal_leave_params_t*)esp_zb_app_signal_get_params(p_sg_p);
if( leave_params->leave_type == ESP_ZB_NWK_LEAVE_TYPE_RESET)
{
ESP_LOGI(TAG, "Reset Device");
esp_zb_factory_reset();
}
else
{
ESP_LOGI( TAG, "Leave Signal Received - Status : %d", err_status);
}
}
break;
case ESP_ZB_ZDO_SIGNAL_PRODUCTION_CONFIG_READY:
ESP_LOGI( TAG, "Production Config Ready - Status : %d", err_status);
break;
case ESP_ZB_ZDO_DEVICE_UNAVAILABLE:
ESP_LOGW( TAG, "Device Unavailable - Status: %d", err_status);
network_active = 0;
break;
case ESP_ZB_NLME_STATUS_INDICATION:
ESP_LOGI( TAG, "NLME Status Indication - Status: %d", err_status);
ESP_LOGI(TAG, "...sig-type: %s, p_sg_p: 0x%x", esp_zb_zdo_signal_to_string(sig_type),
*(uint8_t*)esp_zb_app_signal_get_params(p_sg_p));
break;
default:
ESP_LOGI(TAG, "ZDO signal: %d, status: %d", sig_type, err_status);
break;
}
}
static void esp_zb_task(void *pvParameters)
{
char model_id[10] = "\x08" "Weather2";
char manufacturer_id[10] = "\x06" "ConMac";
uint8_t zcl_version = 8;
uint8_t power_source = 4;
ESP_LOGI(TAG, "Starting configuration");
/* initialize Zigbee stack with Zigbee end-device config */
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG();
esp_zb_init(&zb_nwk_cfg);
esp_zb_cluster_list_t *esp_zb_cluster_list = esp_zb_zcl_cluster_list_create();
esp_zb_attribute_list_t *esp_zb_basic_cluster = esp_zb_zcl_attr_list_create( ESP_ZB_ZCL_CLUSTER_ID_BASIC);
esp_zb_basic_cluster_add_attr( esp_zb_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, model_id);
esp_zb_basic_cluster_add_attr( esp_zb_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, manufacturer_id);
esp_zb_basic_cluster_add_attr( esp_zb_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_ZCL_VERSION_ID, &zcl_version);
esp_zb_basic_cluster_add_attr( esp_zb_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_POWER_SOURCE_ID, &power_source);
esp_zb_cluster_list_add_basic_cluster( esp_zb_cluster_list, esp_zb_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_on_off_cluster_cfg_t on_off_cfg;
on_off_cfg.on_off = ESP_ZB_ZCL_ON_OFF_ON_OFF_DEFAULT_VALUE;
esp_zb_attribute_list_t *esp_zb_on_off_cluster = esp_zb_on_off_cluster_create( &on_off_cfg);
esp_zb_cluster_list_add_on_off_cluster( esp_zb_cluster_list, esp_zb_on_off_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_cluster_list_t *esp_zb_cluster_list2 = esp_zb_zcl_cluster_list_create();
esp_zb_cluster_list_add_basic_cluster( esp_zb_cluster_list2, esp_zb_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
temperature_cfg.max_value = 10000;
temperature_cfg.min_value = -2000;
temperature_cfg.measured_value = 1850;
esp_zb_attribute_list_t *temperature_cluster = esp_zb_temperature_meas_cluster_create( &temperature_cfg);
esp_zb_cluster_list_add_temperature_meas_cluster( esp_zb_cluster_list2, temperature_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
humidity_cfg.max_value = 10000;
humidity_cfg.min_value = 0;
humidity_cfg.measured_value = 5000;
esp_zb_attribute_list_t *humidity_cluster = esp_zb_humidity_meas_cluster_create(&humidity_cfg);
esp_zb_cluster_list_add_humidity_meas_cluster( esp_zb_cluster_list2, humidity_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
ESP_LOGI( TAG, "Adding pressure cluster");
pressure_cfg.max_value = 1100;
pressure_cfg.min_value = 900;
pressure_cfg.measured_value = 1013;
int16_t scaledValue = 10132;
int16_t minScaledValue = 9000;
int16_t maxScaledValue = 11000;
int8_t scale = 1;
esp_zb_attribute_list_t *pressure_attr_list = esp_zb_pressure_meas_cluster_create(&pressure_cfg);
ESP_LOGI( TAG, "Extending pressure cluster");
esp_zb_cluster_add_attr( pressure_attr_list, ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT, 0x0010,
ESP_ZB_ZCL_ATTR_TYPE_S16, ESP_ZB_ZCL_ATTR_ACCESS_READ_ONLY, &scaledValue);
esp_zb_cluster_add_attr( pressure_attr_list, ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT, 0x0011,
ESP_ZB_ZCL_ATTR_TYPE_S16, ESP_ZB_ZCL_ATTR_ACCESS_READ_ONLY, &minScaledValue);
esp_zb_cluster_add_attr( pressure_attr_list, ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT, 0x0012,
ESP_ZB_ZCL_ATTR_TYPE_S16, ESP_ZB_ZCL_ATTR_ACCESS_READ_ONLY, &maxScaledValue);
esp_zb_cluster_add_attr( pressure_attr_list, ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT, 0x0014,
ESP_ZB_ZCL_ATTR_TYPE_S16, ESP_ZB_ZCL_ATTR_ACCESS_READ_ONLY, &scale);
ESP_LOGI( TAG, "Adding pressure cluster");
esp_zb_cluster_list_add_pressure_meas_cluster(esp_zb_cluster_list2, pressure_attr_list, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
ESP_LOGI( TAG, "Pressure Cluster Added");
analog_input_cfg.status_flags = 0;
analog_input_cfg.out_of_service = false;
analog_input_cfg.present_value = 123.4;
esp_zb_attribute_list_t *analog_cluster = esp_zb_analog_input_cluster_create(&analog_input_cfg);
esp_zb_cluster_list_add_analog_input_cluster( esp_zb_cluster_list2, analog_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_ep_list_t *esp_zb_ep_list = esp_zb_ep_list_create();
esp_zb_ep_list_add_ep( esp_zb_ep_list, esp_zb_cluster_list, HA_ESP_LIGHT_ENDPOINT,
ESP_ZB_AF_HA_PROFILE_ID, ESP_ZB_HA_ON_OFF_OUTPUT_DEVICE_ID);
esp_zb_ep_list_add_ep( esp_zb_ep_list, esp_zb_cluster_list2, HA_ESP_TEMPERATURE_ENDPOINT,
ESP_ZB_AF_HA_PROFILE_ID, ESP_ZB_HA_TEMPERATURE_SENSOR_DEVICE_ID);
esp_zb_device_register( esp_zb_ep_list);
esp_zb_core_action_handler_register( zb_action_handler);
esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
ESP_ERROR_CHECK(esp_zb_start(false));
// Start the stack working
esp_zb_main_loop_iteration();
}
static void adc_task( void *pvParameters)
{
// adc_oneshot_unit_handle_t adc_handle;
// adc_oneshot_unit_init_cfg_t adc_cfg =
// {
// .unit_id = ADC_UNIT_1,
// .ulp_mode = ADC_ULP_MODE_DISABLE,
// };
// ESP_ERROR_CHECK( adc_oneshot_new_unit( &adc_cfg, &adc_handle));
// adc_oneshot_chan_cfg_t config =
// {
// .bitwidth = ADC_BITWIDTH_DEFAULT,
// .atten = ADC_ATTEN_DB_12,
// };
// ESP_ERROR_CHECK( adc_oneshot_config_channel( adc_handle, ADC_CHANNEL_0, &config));
while(1)
{
if( network_active)
{
// ESP_ERROR_CHECK( adc_oneshot_read( adc_handle, ADC_CHANNEL_0, &adc_raw[0][0]));
// ESP_LOGI( TAG, "ADC1 Channel 0 = %04x", adc_raw[0][0]);
esp_zb_zcl_status_t status;
status = esp_zb_zcl_set_attribute_val( HA_ESP_TEMPERATURE_ENDPOINT,
ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID, &counter, false);
if( status != ESP_ZB_ZCL_STATUS_SUCCESS)
{
ESP_LOGE( TAG, "Setting temp attribute failed: %d", status);
}
status = esp_zb_zcl_set_attribute_val( HA_ESP_TEMPERATURE_ENDPOINT,
ESP_ZB_ZCL_CLUSTER_ID_REL_HUMIDITY_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
ESP_ZB_ZCL_ATTR_REL_HUMIDITY_MEASUREMENT_VALUE_ID, &humidity, false);
if( status != ESP_ZB_ZCL_STATUS_SUCCESS)
{
ESP_LOGE( TAG, "Setting temp attribute failed: %d", status);
}
status = esp_zb_zcl_set_attribute_val( HA_ESP_TEMPERATURE_ENDPOINT,
ESP_ZB_ZCL_CLUSTER_ID_PRESSURE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
ESP_ZB_ZCL_ATTR_PRESSURE_MEASUREMENT_VALUE_ID, &rel_pressure, false);
if( status != ESP_ZB_ZCL_STATUS_SUCCESS)
{
ESP_LOGE( TAG, "Setting temp attribute failed: %d", status);
}
counter++;
if( counter >= temperature_cfg.max_value)
{
counter = temperature_cfg.min_value;
}
humidity += 10;
if( humidity >= humidity_cfg.max_value)
{
humidity = humidity_cfg.min_value;
}
rel_pressure++;
if( rel_pressure >= pressure_cfg.max_value)
{
rel_pressure = pressure_cfg.min_value;
}
}
vTaskDelay( pdMS_TO_TICKS(2000));
}
}
void app_main(void)
{
esp_zb_platform_config_t config = {
.radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
};
ESP_ERROR_CHECK(nvs_flash_init());
/* load Zigbee light_bulb platform config to initialization */
ESP_ERROR_CHECK(esp_zb_platform_config(&config));
/* hardware related and device init */
light_driver_init(LIGHT_DEFAULT_OFF);
xTaskCreate( adc_task, "ADC Task", 4096, NULL, 5, NULL);
xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
}
The changes are:
- added another task that will ultimately read the ADCs and other inputs to provide the data to bse sent - right now all it does is send values every 2 seconds for testing purposes
- some minor changes to the esp_zb_app_signal_handler to restart on failed connections etc
- esp_zb_task now creates the various clusters and devices explicitly rather than some of the ‘higher level’ functions that Espressif provide (for m education as much as anything)
You wil see that it has a cluster that sets the manufacturer, model etc (change the strings to whatever you want) plus the on-off switch. Other clusters provides the (pseudo) temperature, humidity and pressure values.
There is a generic ‘analog input’ cluster as well but that is where the ZHA does not provide support and so it is ignored.
Happy hacking.
Susan