365 lines
17 KiB
C
365 lines
17 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
|
*
|
|
* SPDX-License-Identifier: CC0-1.0
|
|
*
|
|
* Zigbee HA_on_off_light Example
|
|
*
|
|
* This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, this
|
|
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
* CONDITIONS OF ANY KIND, either express or implied.
|
|
*/
|
|
#include "esp_zb_light.h"
|
|
#include "esp_check.h"
|
|
#include "esp_log.h"
|
|
#include "nvs_flash.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "ha/esp_zigbee_ha_standard.h"
|
|
|
|
#include "zcl/esp_zigbee_zcl_occupancy_sensing.h"
|
|
|
|
#include "driver/gpio.h" // Make sure this is included
|
|
|
|
#define PIR_GPIO 2
|
|
#define RADAR_GPIO 3
|
|
#define RELAY_GPIO 4
|
|
#define MY_OCCUPANCY_SENSOR_ENDPOINT 12
|
|
|
|
|
|
|
|
static bool current_device_presence = false;
|
|
|
|
|
|
|
|
#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";
|
|
/********************* Define functions **************************/
|
|
static esp_err_t deferred_driver_init(void)
|
|
{
|
|
static bool is_inited = false;
|
|
if (!is_inited) {
|
|
light_driver_init(LIGHT_DEFAULT_OFF);
|
|
is_inited = true;
|
|
}
|
|
return is_inited ? ESP_OK : ESP_FAIL;
|
|
}
|
|
|
|
static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask)
|
|
{
|
|
ESP_RETURN_ON_FALSE(esp_zb_bdb_start_top_level_commissioning(mode_mask) == ESP_OK, , TAG, "Failed to start Zigbee commissioning");
|
|
}
|
|
|
|
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;
|
|
switch (sig_type) {
|
|
case ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP:
|
|
ESP_LOGI(TAG, "Initialize Zigbee stack");
|
|
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 (err_status == ESP_OK) {
|
|
ESP_LOGI(TAG, "Deferred driver initialization %s", deferred_driver_init() ? "failed" : "successful");
|
|
ESP_LOGI(TAG, "Device started up in%s factory-reset mode", esp_zb_bdb_is_factory_new() ? "" : " non");
|
|
if (esp_zb_bdb_is_factory_new()) {
|
|
ESP_LOGI(TAG, "Start network steering");
|
|
esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING);
|
|
} else {
|
|
ESP_LOGI(TAG, "Device rebooted");
|
|
}
|
|
} else {
|
|
ESP_LOGW(TAG, "%s failed with status: %s, retrying", esp_zb_zdo_signal_to_string(sig_type),
|
|
esp_err_to_name(err_status));
|
|
esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb,
|
|
ESP_ZB_BDB_MODE_INITIALIZATION, 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, Short Address: 0x%04hx)",
|
|
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(), esp_zb_get_short_address());
|
|
} else {
|
|
ESP_LOGI(TAG, "Network steering was not successful (status: %s)", esp_err_to_name(err_status));
|
|
esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_STEERING, 1000);
|
|
}
|
|
break;
|
|
default:
|
|
ESP_LOGI(TAG, "ZDO signal: %s (0x%x), status: %s", esp_zb_zdo_signal_to_string(sig_type), sig_type,
|
|
esp_err_to_name(err_status));
|
|
break;
|
|
}
|
|
}
|
|
|
|
void update_and_report_combined_presence(void) {
|
|
bool pir_active = gpio_get_level(PIR_GPIO);
|
|
bool radar_active = gpio_get_level(RADAR_GPIO);
|
|
bool new_presence_detected = pir_active || radar_active;
|
|
|
|
if (new_presence_detected != current_device_presence) {
|
|
current_device_presence = new_presence_detected;
|
|
ESP_LOGI(TAG, "Combined Presence Changed: %s (PIR: %d, Radar: %d)",
|
|
current_device_presence ? "DETECTED" : "CLEAR",
|
|
pir_active, radar_active);
|
|
|
|
// Update Zigbee Occupancy Attribute
|
|
uint8_t occupancy_report_value = current_device_presence ? 0x01 : 0x00; // 0x01 = occupied, 0x00 = unoccupied
|
|
esp_zb_lock_acquire(portMAX_DELAY);
|
|
esp_err_t set_attr_err = esp_zb_zcl_set_attribute_val(
|
|
MY_OCCUPANCY_SENSOR_ENDPOINT, // Endpoint ID of your occupancy sensor
|
|
ESP_ZB_ZCL_CLUSTER_ID_OCCUPANCY_SENSING, // Cluster ID
|
|
ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, // Role
|
|
ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_OCCUPANCY_ID, // Attribute ID
|
|
&occupancy_report_value, // Pointer to the value
|
|
false);
|
|
esp_zb_lock_release();
|
|
|
|
if (set_attr_err == ESP_OK) {
|
|
ESP_LOGI(TAG, "Reported Occupancy to Zigbee: %s", occupancy_report_value ? "Occupied" : "Unoccupied");
|
|
} else {
|
|
ESP_LOGE(TAG, "Failed to set Occupancy attribute, error: %s", esp_err_to_name(set_attr_err));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void presence_polling_task(void *pvParameters) {
|
|
ESP_LOGI(TAG, "Presence Polling Task started");
|
|
while (1) {
|
|
update_and_report_combined_presence();
|
|
vTaskDelay(pdMS_TO_TICKS(200)); // Poll every 200ms
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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; // Original variable for light driver
|
|
|
|
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);
|
|
|
|
// Assuming the On/Off functionality is on HA_ESP_LIGHT_ENDPOINT (or whatever the example uses)
|
|
// We will later rename this endpoint or make it more generic if needed.
|
|
if (message->info.dst_endpoint == HA_ESP_LIGHT_ENDPOINT) { // Use the endpoint ID defined in the example for its light
|
|
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) {
|
|
bool relay_state = message->attribute.data.value ? *(bool *)message->attribute.data.value : false;
|
|
ESP_LOGI(TAG, "Relay (Zigbee command) set to %s", relay_state ? "ON" : "OFF");
|
|
// light_driver_set_power(light_state); // Comment out or delete original light driver call
|
|
gpio_set_level(RELAY_GPIO, relay_state ? 1 : 0); // Control your relay (1 for ON if HIGH triggered)
|
|
}
|
|
}
|
|
}
|
|
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;
|
|
default:
|
|
ESP_LOGW(TAG, "Receive Zigbee action(0x%x) callback", callback_id);
|
|
break;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static void esp_zb_task(void *pvParameters)
|
|
{
|
|
/* initialize Zigbee stack */
|
|
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG(); // Correct for End Device
|
|
esp_zb_init(&zb_nwk_cfg);
|
|
|
|
// --- Existing On/Off Light (Relay) Endpoint Setup ---
|
|
esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG();
|
|
// The esp_zb_ep_list_t should be created ONCE and then have both endpoints added to it.
|
|
esp_zb_ep_list_t *multi_ep_list = esp_zb_ep_list_create(); // Create a new list for all endpoints
|
|
|
|
// esp_zb_ep_list_t *esp_zb_on_off_light_ep = esp_zb_on_off_light_ep_create(HA_ESP_LIGHT_ENDPOINT, &light_cfg);
|
|
// The esp_zb_on_off_light_ep_create likely returns an ep_list with one endpoint.
|
|
// We need to add its contents to our main multi_ep_list or adapt.
|
|
// For simplicity, let's assume esp_zb_on_off_light_ep_create gives us the cluster_list for the light.
|
|
// A more robust way is to manually create the clusters for the on/off light as well.
|
|
|
|
// Let's manually create the clusters for the On/Off Light endpoint for clarity
|
|
// This replaces esp_zb_on_off_light_ep_create for more control with multiple EPs
|
|
esp_zb_attribute_list_t *on_off_basic_cluster = esp_zb_basic_cluster_create(NULL);
|
|
esp_zb_attribute_list_t *on_off_identify_cluster = esp_zb_identify_cluster_create(NULL);
|
|
esp_zb_on_off_cluster_cfg_t on_off_cfg_for_relay;
|
|
on_off_cfg_for_relay.on_off = false; // Initial state OFF
|
|
esp_zb_attribute_list_t *on_off_server_cluster_for_relay = esp_zb_on_off_cluster_create(&on_off_cfg_for_relay);
|
|
|
|
esp_zb_cluster_list_t *relay_cluster_list = esp_zb_zcl_cluster_list_create();
|
|
esp_zb_cluster_list_add_basic_cluster(relay_cluster_list, on_off_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
|
esp_zb_cluster_list_add_identify_cluster(relay_cluster_list, on_off_identify_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
|
esp_zb_cluster_list_add_on_off_cluster(relay_cluster_list, on_off_server_cluster_for_relay, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
|
|
|
zcl_basic_manufacturer_info_t mfg_info = { // Renamed from 'info' to avoid conflict if Occupancy also uses it
|
|
.manufacturer_name = ESP_MANUFACTURER_NAME,
|
|
.model_identifier = ESP_MODEL_IDENTIFIER,
|
|
};
|
|
// Add manufacturer info to the basic cluster of the relay endpoint
|
|
esp_zb_basic_cluster_add_attr(on_off_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, mfg_info.manufacturer_name);
|
|
esp_zb_basic_cluster_add_attr(on_off_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, mfg_info.model_identifier);
|
|
|
|
|
|
esp_zb_endpoint_config_t relay_endpoint_config = {
|
|
.endpoint = HA_ESP_LIGHT_ENDPOINT, // Using the example's endpoint for the relay
|
|
.app_profile_id = ESP_ZB_AF_HA_PROFILE_ID,
|
|
.app_device_id = ESP_ZB_HA_ON_OFF_LIGHT_DEVICE_ID, // Keep as On/Off Light for this endpoint
|
|
.app_device_version = 0
|
|
};
|
|
esp_zb_ep_list_add_ep(multi_ep_list, relay_cluster_list, relay_endpoint_config);
|
|
ESP_LOGI(TAG, "On/Off Light (Relay) Endpoint created.");
|
|
// --- End of On/Off Light (Relay) Endpoint Setup ---
|
|
|
|
|
|
/* --- Create Occupancy Sensor Endpoint --- */
|
|
ESP_LOGI(TAG, "Creating Occupancy Sensor Endpoint (%d)...", MY_OCCUPANCY_SENSOR_ENDPOINT);
|
|
|
|
esp_zb_attribute_list_t *occupancy_basic_cluster = esp_zb_basic_cluster_create(NULL);
|
|
// Add manufacturer info to this endpoint's basic cluster too
|
|
esp_zb_basic_cluster_add_attr(occupancy_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, mfg_info.manufacturer_name);
|
|
esp_zb_basic_cluster_add_attr(occupancy_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, mfg_info.model_identifier);
|
|
|
|
|
|
esp_zb_attribute_list_t *occupancy_identify_cluster = esp_zb_identify_cluster_create(NULL);
|
|
|
|
esp_zb_attribute_list_t *occupancy_sensing_cluster = esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_OCCUPANCY_SENSING);
|
|
uint8_t initial_occupancy_value = 0x00; // Unoccupied
|
|
esp_zb_occupancy_sensing_cluster_add_attr(occupancy_sensing_cluster, ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_OCCUPANCY_ID, &initial_occupancy_value);
|
|
// Optional: Add sensor type attribute
|
|
// uint8_t sensor_type = ESP_ZB_ZCL_OCCUPANCY_SENSOR_TYPE_PIR; // Or a combined type if available
|
|
// esp_zb_occupancy_sensing_cluster_add_attr(occupancy_sensing_cluster, ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_TYPE_ID, &sensor_type);
|
|
|
|
|
|
esp_zb_cluster_list_t *occupancy_cluster_list_for_ep = esp_zb_zcl_cluster_list_create();
|
|
esp_zb_cluster_list_add_basic_cluster(occupancy_cluster_list_for_ep, occupancy_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
|
esp_zb_cluster_list_add_identify_cluster(occupancy_cluster_list_for_ep, occupancy_identify_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
|
esp_zb_cluster_list_add_occupancy_sensing_cluster(occupancy_cluster_list_for_ep, occupancy_sensing_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
|
|
|
esp_zb_endpoint_config_t occupancy_endpoint_config = {
|
|
.endpoint = MY_OCCUPANCY_SENSOR_ENDPOINT,
|
|
.app_profile_id = ESP_ZB_AF_HA_PROFILE_ID,
|
|
.app_device_id = 0x0107,
|
|
.app_device_version = 0
|
|
};
|
|
esp_zb_ep_list_add_ep(multi_ep_list, occupancy_cluster_list_for_ep, occupancy_endpoint_config);
|
|
ESP_LOGI(TAG, "Occupancy Sensor Endpoint created.");
|
|
/* --- End of Occupancy Sensor Endpoint Creation --- */
|
|
|
|
// Register the device with ALL endpoints
|
|
esp_zb_device_register(multi_ep_list); // Use the list that contains both endpoints
|
|
|
|
|
|
ESP_LOGI(TAG, "Configuring reporting for Occupancy sensor...");
|
|
esp_zb_zcl_reporting_info_t occupancy_reporting_info = {
|
|
.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV, // Report from server (our device) to client (HA)
|
|
.ep = MY_OCCUPANCY_SENSOR_ENDPOINT, // Your occupancy sensor endpoint
|
|
.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_OCCUPANCY_SENSING,
|
|
.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
|
|
.attr_id = ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_OCCUPANCY_ID,
|
|
.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID,
|
|
// .dst.addr_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT, // Let stack fill this during binding
|
|
// .dst.dst_addr_u.addr_short = 0; // Coordinator address, usually set after binding
|
|
.u.send_info.min_interval = 1, // Minimum reporting interval (seconds) - report quickly
|
|
.u.send_info.max_interval = 300, // Maximum reporting interval (e.g., 5 minutes)
|
|
.u.send_info.def_min_interval = 1,
|
|
.u.send_info.def_max_interval = 300,
|
|
// For a binary attribute like occupancy, reportable_change is usually not critical,
|
|
// as any change (0 to 1 or 1 to 0) is significant.
|
|
// For analog values, delta is important.
|
|
// .u.send_info.delta.u8 = 1; // For boolean/bitmap, delta isn't typically used this way.
|
|
// The change itself is the report trigger.
|
|
.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC,
|
|
};
|
|
|
|
// This function configures the local device to send reports.
|
|
// The actual sending of reports often relies on a binding being established
|
|
// between this device's occupancy cluster and the coordinator (HA).
|
|
esp_err_t report_cfg_err = esp_zb_zcl_update_reporting_info(&occupancy_reporting_info);
|
|
if (report_cfg_err == ESP_OK) {
|
|
ESP_LOGI(TAG, "Successfully configured reporting for Occupancy attribute.");
|
|
} else {
|
|
ESP_LOGE(TAG, "Failed to configure reporting for Occupancy attribute: %s", esp_err_to_name(report_cfg_err));
|
|
}
|
|
|
|
|
|
esp_zb_core_action_handler_register(zb_action_handler);
|
|
esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
|
|
ESP_LOGI(TAG, "Starting Zigbee stack to join a network (network steering)...");
|
|
ESP_ERROR_CHECK(esp_zb_start(false));
|
|
esp_zb_stack_main_loop();
|
|
}
|
|
|
|
void app_main(void)
|
|
{
|
|
ESP_LOGI(TAG, "Initializing GPIOs...");
|
|
|
|
// Configure PIR GPIO (Your tested config)
|
|
gpio_config_t pir_conf = {
|
|
.intr_type = GPIO_INTR_DISABLE, // For polling initially
|
|
.mode = GPIO_MODE_INPUT,
|
|
.pin_bit_mask = (1ULL << PIR_GPIO),
|
|
.pull_down_en = 0,
|
|
.pull_up_en = 1,
|
|
};
|
|
ESP_ERROR_CHECK(gpio_config(&pir_conf));
|
|
|
|
// Configure Radar GPIO (Your tested config - adjust if different)
|
|
gpio_config_t radar_conf = {
|
|
.intr_type = GPIO_INTR_DISABLE, // For polling initially
|
|
.mode = GPIO_MODE_INPUT,
|
|
.pin_bit_mask = (1ULL << RADAR_GPIO),
|
|
.pull_down_en = 0,
|
|
.pull_up_en = 1,
|
|
};
|
|
ESP_ERROR_CHECK(gpio_config(&radar_conf));
|
|
|
|
// Configure Relay GPIO (Your tested config)
|
|
gpio_config_t relay_conf = {
|
|
.intr_type = GPIO_INTR_DISABLE,
|
|
.mode = GPIO_MODE_OUTPUT,
|
|
.pin_bit_mask = (1ULL << RELAY_GPIO),
|
|
.pull_down_en = 0,
|
|
.pull_up_en = 0,
|
|
};
|
|
ESP_ERROR_CHECK(gpio_config(&relay_conf));
|
|
// Initial relay state (Relay ON if input is HIGH, so set to 0 for OFF)
|
|
gpio_set_level(RELAY_GPIO, 0);
|
|
ESP_LOGI(TAG, "GPIOs Initialized.");
|
|
|
|
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());
|
|
ESP_ERROR_CHECK(esp_zb_platform_config(&config));
|
|
xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
|
|
|
|
xTaskCreate(presence_polling_task, "Presence_Poll_Task", 2048, NULL, 5, NULL);
|
|
ESP_LOGI(TAG, "Presence Polling Task created.");
|
|
}
|