Modular v1
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
/* Device Configuration - Permite configurar diferentes tipos de dispositivos Zigbee */
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* ========================= SELEÇÃO DO TIPO DE DISPOSITIVO ========================= */
|
||||
/* Descomente APENAS UMA das opções abaixo para definir o tipo de dispositivo */
|
||||
|
||||
#define DEVICE_TYPE_RELAY /* Relé controlável (on/off light) */
|
||||
// #define DEVICE_TYPE_TEMP_SENSOR /* Sensor de temperatura */
|
||||
// #define DEVICE_TYPE_DOOR_SENSOR /* Sensor de porta/janela */
|
||||
// #define DEVICE_TYPE_MOTION_SENSOR /* Sensor de movimento */
|
||||
|
||||
/* ========================= CONFIGURAÇÃO DE HARDWARE ========================= */
|
||||
|
||||
/* GPIOs comuns (usados por todos os tipos) */
|
||||
#define GPIO_BUTTON_PAIRING 9 /* Botão para pareamento/factory reset */
|
||||
#define GPIO_LED_RGB 8 /* LED RGB indicador de status */
|
||||
|
||||
/* GPIOs específicos por tipo de dispositivo */
|
||||
#ifdef DEVICE_TYPE_RELAY
|
||||
#define GPIO_OUTPUT_RELAY 4 /* Relé de saída */
|
||||
#define HAS_BUTTON_TOGGLE 1 /* Botão também faz toggle do relé */
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_TYPE_TEMP_SENSOR
|
||||
#define GPIO_SENSOR_I2C_SDA 6 /* I2C SDA para sensor de temperatura */
|
||||
#define GPIO_SENSOR_I2C_SCL 7 /* I2C SCL para sensor de temperatura */
|
||||
#define SENSOR_UPDATE_INTERVAL 30000 /* Intervalo de atualização em ms */
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_TYPE_DOOR_SENSOR
|
||||
#define GPIO_SENSOR_REED 5 /* Reed switch para sensor de porta */
|
||||
#define HAS_TAMPER_DETECTION 0 /* Detecção de violação (anti-tamper) */
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_TYPE_MOTION_SENSOR
|
||||
#define GPIO_SENSOR_PIR 5 /* Sensor PIR para detecção de movimento */
|
||||
#define PIR_TIMEOUT_MS 60000 /* Timeout para resetar movimento detectado */
|
||||
#endif
|
||||
|
||||
/* ========================= FUNCIONALIDADES OPCIONAIS ========================= */
|
||||
|
||||
#define FEATURE_LED_INDICATOR 1 /* Habilita LED RGB indicador */
|
||||
#define FEATURE_LONG_PRESS_RESET 1 /* Habilita factory reset por long press (5s) */
|
||||
#define FEATURE_OTA_UPDATE 0 /* Habilita atualização Over-The-Air */
|
||||
|
||||
/* ========================= CONFIGURAÇÕES AVANÇADAS ========================= */
|
||||
|
||||
#define BUTTON_LONG_PRESS_MS 5000 /* Tempo para long press (factory reset) */
|
||||
#define LED_BRIGHTNESS_NORMAL 50 /* Brilho normal do LED (0-255) */
|
||||
#define LED_BRIGHTNESS_DIM 10 /* Brilho reduzido do LED (0-255) */
|
||||
|
||||
/* ========================= VALIDAÇÃO ========================= */
|
||||
|
||||
/* Verifica se pelo menos um tipo de dispositivo foi selecionado */
|
||||
#if !defined(DEVICE_TYPE_RELAY) && !defined(DEVICE_TYPE_TEMP_SENSOR) && \
|
||||
!defined(DEVICE_TYPE_DOOR_SENSOR) && !defined(DEVICE_TYPE_MOTION_SENSOR)
|
||||
#error "Nenhum tipo de dispositivo selecionado! Defina um DEVICE_TYPE_* em device_config.h"
|
||||
#endif
|
||||
|
||||
/* Verifica se mais de um tipo foi selecionado */
|
||||
#if (defined(DEVICE_TYPE_RELAY) + defined(DEVICE_TYPE_TEMP_SENSOR) + \
|
||||
defined(DEVICE_TYPE_DOOR_SENSOR) + defined(DEVICE_TYPE_MOTION_SENSOR)) > 1
|
||||
#error "Múltiplos tipos de dispositivo selecionados! Defina apenas um DEVICE_TYPE_*"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,287 @@
|
||||
/* Device Registry - Implementação de diferentes tipos de dispositivos Zigbee */
|
||||
|
||||
#include "device_registry.h"
|
||||
#include "zcl/esp_zigbee_zcl_common.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_zb_switch.h"
|
||||
|
||||
#ifdef DEVICE_TYPE_RELAY
|
||||
#include "switch_driver.h"
|
||||
#endif
|
||||
|
||||
#if FEATURE_LED_INDICATOR
|
||||
#include "led_indicator.h"
|
||||
#endif
|
||||
|
||||
static const char *TAG = "DEVICE_REGISTRY";
|
||||
|
||||
/* ========================= IMPLEMENTAÇÕES DE DISPOSITIVOS ========================= */
|
||||
|
||||
#ifdef DEVICE_TYPE_RELAY
|
||||
/* ===== RELÉ CONTROLÁVEL (ON/OFF LIGHT) ===== */
|
||||
|
||||
static esp_zb_cluster_list_t* relay_create_clusters(void)
|
||||
{
|
||||
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
|
||||
|
||||
esp_zb_cluster_list_add_basic_cluster(cluster_list, device_create_basic_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
esp_zb_cluster_list_add_identify_cluster(cluster_list, device_create_identify_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
esp_zb_cluster_list_add_on_off_cluster(cluster_list, device_create_onoff_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
|
||||
return cluster_list;
|
||||
}
|
||||
|
||||
static esp_err_t relay_attribute_handler(const esp_zb_zcl_set_attr_value_message_t *message)
|
||||
{
|
||||
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF &&
|
||||
message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID) {
|
||||
|
||||
bool on_off = message->attribute.data.value ? *(bool *)message->attribute.data.value : 0;
|
||||
ESP_LOGI(TAG, "Relay command received: %s", on_off ? "ON" : "OFF");
|
||||
|
||||
relay_set_state(on_off);
|
||||
|
||||
#if FEATURE_LED_INDICATOR
|
||||
led_indicator_set_state(on_off ? LED_RELAY_ON : LED_RELAY_OFF);
|
||||
#endif
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
static void relay_hardware_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing relay hardware (GPIO %d)", GPIO_OUTPUT_RELAY);
|
||||
relay_driver_init();
|
||||
}
|
||||
|
||||
static void relay_update_state(void)
|
||||
{
|
||||
/* Relé é controlado por comandos Zigbee, não precisa polling */
|
||||
}
|
||||
|
||||
static const device_type_t relay_device = {
|
||||
.name = "Zigbee Relay",
|
||||
.device_id = ESP_ZB_HA_ON_OFF_OUTPUT_DEVICE_ID,
|
||||
.endpoint = HA_ONOFF_SWITCH_ENDPOINT,
|
||||
.create_clusters = relay_create_clusters,
|
||||
.attribute_handler = relay_attribute_handler,
|
||||
.hardware_init = relay_hardware_init,
|
||||
.update_state = relay_update_state,
|
||||
};
|
||||
#endif /* DEVICE_TYPE_RELAY */
|
||||
|
||||
/* ========================= SENSOR DE TEMPERATURA ========================= */
|
||||
|
||||
#ifdef DEVICE_TYPE_TEMP_SENSOR
|
||||
|
||||
static esp_zb_cluster_list_t* temp_sensor_create_clusters(void)
|
||||
{
|
||||
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
|
||||
|
||||
esp_zb_cluster_list_add_basic_cluster(cluster_list, device_create_basic_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
esp_zb_cluster_list_add_identify_cluster(cluster_list, device_create_identify_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
esp_zb_cluster_list_add_temperature_meas_cluster(cluster_list, device_create_temperature_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
|
||||
return cluster_list;
|
||||
}
|
||||
|
||||
static esp_err_t temp_sensor_attribute_handler(const esp_zb_zcl_set_attr_value_message_t *message)
|
||||
{
|
||||
/* Sensor de temperatura geralmente não recebe comandos, apenas reporta */
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
static void temp_sensor_hardware_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing temperature sensor (I2C SDA:%d SCL:%d)",
|
||||
GPIO_SENSOR_I2C_SDA, GPIO_SENSOR_I2C_SCL);
|
||||
|
||||
/* TODO: Implementar inicialização do sensor I2C */
|
||||
/* Exemplo: inicializar AHT20, SHT30, BME280, etc. */
|
||||
}
|
||||
|
||||
static void temp_sensor_update_state(void)
|
||||
{
|
||||
/* TODO: Ler temperatura do sensor e atualizar atributo Zigbee */
|
||||
/* Exemplo:
|
||||
* float temp = read_temperature_sensor();
|
||||
* int16_t temp_zigbee = (int16_t)(temp * 100); // Zigbee usa 0.01°C
|
||||
* esp_zb_zcl_set_attribute_val(..., ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID, &temp_zigbee, false);
|
||||
*/
|
||||
}
|
||||
|
||||
static const device_type_t temp_sensor_device = {
|
||||
.name = "Zigbee Temperature Sensor",
|
||||
.device_id = ESP_ZB_HA_TEMPERATURE_SENSOR_DEVICE_ID,
|
||||
.endpoint = HA_ONOFF_SWITCH_ENDPOINT,
|
||||
.create_clusters = temp_sensor_create_clusters,
|
||||
.attribute_handler = temp_sensor_attribute_handler,
|
||||
.hardware_init = temp_sensor_hardware_init,
|
||||
.update_state = temp_sensor_update_state,
|
||||
};
|
||||
#endif /* DEVICE_TYPE_TEMP_SENSOR */
|
||||
|
||||
/* ========================= SENSOR DE PORTA/JANELA ========================= */
|
||||
|
||||
#ifdef DEVICE_TYPE_DOOR_SENSOR
|
||||
|
||||
static esp_zb_cluster_list_t* door_sensor_create_clusters(void)
|
||||
{
|
||||
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
|
||||
|
||||
esp_zb_cluster_list_add_basic_cluster(cluster_list, device_create_basic_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
esp_zb_cluster_list_add_identify_cluster(cluster_list, device_create_identify_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
esp_zb_cluster_list_add_ias_zone_cluster(cluster_list, device_create_ias_zone_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
|
||||
return cluster_list;
|
||||
}
|
||||
|
||||
static esp_err_t door_sensor_attribute_handler(const esp_zb_zcl_set_attr_value_message_t *message)
|
||||
{
|
||||
/* Sensor IAS Zone não recebe comandos, apenas reporta */
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
static void door_sensor_hardware_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing door sensor (Reed Switch GPIO %d)", GPIO_SENSOR_REED);
|
||||
|
||||
/* TODO: Configurar GPIO do reed switch com pull-up e interrupção */
|
||||
}
|
||||
|
||||
static void door_sensor_update_state(void)
|
||||
{
|
||||
/* TODO: Ler estado do reed switch e atualizar IAS Zone status */
|
||||
}
|
||||
|
||||
static const device_type_t door_sensor_device = {
|
||||
.name = "Zigbee Door/Window Sensor",
|
||||
.device_id = ESP_ZB_HA_IAS_ZONE_DEVICE_ID,
|
||||
.endpoint = HA_ONOFF_SWITCH_ENDPOINT,
|
||||
.create_clusters = door_sensor_create_clusters,
|
||||
.attribute_handler = door_sensor_attribute_handler,
|
||||
.hardware_init = door_sensor_hardware_init,
|
||||
.update_state = door_sensor_update_state,
|
||||
};
|
||||
#endif /* DEVICE_TYPE_DOOR_SENSOR */
|
||||
|
||||
/* ========================= SENSOR DE MOVIMENTO ========================= */
|
||||
|
||||
#ifdef DEVICE_TYPE_MOTION_SENSOR
|
||||
|
||||
static esp_zb_cluster_list_t* motion_sensor_create_clusters(void)
|
||||
{
|
||||
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
|
||||
|
||||
esp_zb_cluster_list_add_basic_cluster(cluster_list, device_create_basic_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
esp_zb_cluster_list_add_identify_cluster(cluster_list, device_create_identify_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
esp_zb_cluster_list_add_occupancy_sensing_cluster(cluster_list, device_create_occupancy_cluster(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
|
||||
return cluster_list;
|
||||
}
|
||||
|
||||
static esp_err_t motion_sensor_attribute_handler(const esp_zb_zcl_set_attr_value_message_t *message)
|
||||
{
|
||||
/* Sensor de ocupação não recebe comandos, apenas reporta */
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
static void motion_sensor_hardware_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing motion sensor (PIR GPIO %d)", GPIO_SENSOR_PIR);
|
||||
|
||||
/* TODO: Configurar GPIO do PIR com interrupção */
|
||||
}
|
||||
|
||||
static void motion_sensor_update_state(void)
|
||||
{
|
||||
/* TODO: Ler estado do PIR e atualizar occupancy */
|
||||
}
|
||||
|
||||
static const device_type_t motion_sensor_device = {
|
||||
.name = "Zigbee Motion Sensor",
|
||||
.device_id = ESP_ZB_HA_OCCUPANCY_SENSOR_DEVICE_ID,
|
||||
.endpoint = HA_ONOFF_SWITCH_ENDPOINT,
|
||||
.create_clusters = motion_sensor_create_clusters,
|
||||
.attribute_handler = motion_sensor_attribute_handler,
|
||||
.hardware_init = motion_sensor_hardware_init,
|
||||
.update_state = motion_sensor_update_state,
|
||||
};
|
||||
#endif /* DEVICE_TYPE_MOTION_SENSOR */
|
||||
|
||||
/* ========================= SELETOR DE DISPOSITIVO ========================= */
|
||||
|
||||
const device_type_t* device_get_config(void)
|
||||
{
|
||||
#ifdef DEVICE_TYPE_RELAY
|
||||
return &relay_device;
|
||||
#elif defined(DEVICE_TYPE_TEMP_SENSOR)
|
||||
return &temp_sensor_device;
|
||||
#elif defined(DEVICE_TYPE_DOOR_SENSOR)
|
||||
return &door_sensor_device;
|
||||
#elif defined(DEVICE_TYPE_MOTION_SENSOR)
|
||||
return &motion_sensor_device;
|
||||
#else
|
||||
#error "Nenhum tipo de dispositivo definido!"
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ========================= FUNÇÕES AUXILIARES PARA CLUSTERS ========================= */
|
||||
|
||||
esp_zb_attribute_list_t* device_create_basic_cluster(void)
|
||||
{
|
||||
esp_zb_basic_cluster_cfg_t basic_cfg = {
|
||||
.zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE,
|
||||
.power_source = ESP_ZB_ZCL_BASIC_POWER_SOURCE_DEFAULT_VALUE,
|
||||
};
|
||||
return esp_zb_basic_cluster_create(&basic_cfg);
|
||||
}
|
||||
|
||||
esp_zb_attribute_list_t* device_create_identify_cluster(void)
|
||||
{
|
||||
esp_zb_identify_cluster_cfg_t identify_cfg = {
|
||||
.identify_time = ESP_ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE,
|
||||
};
|
||||
return esp_zb_identify_cluster_create(&identify_cfg);
|
||||
}
|
||||
|
||||
esp_zb_attribute_list_t* device_create_onoff_cluster(void)
|
||||
{
|
||||
esp_zb_on_off_cluster_cfg_t on_off_cfg = {
|
||||
.on_off = ESP_ZB_ZCL_ON_OFF_ON_OFF_DEFAULT_VALUE,
|
||||
};
|
||||
return esp_zb_on_off_cluster_create(&on_off_cfg);
|
||||
}
|
||||
|
||||
esp_zb_attribute_list_t* device_create_temperature_cluster(void)
|
||||
{
|
||||
esp_zb_temperature_meas_cluster_cfg_t temp_cfg = {
|
||||
.measured_value = ESP_ZB_ZCL_TEMP_MEASUREMENT_MEASURED_VALUE_DEFAULT,
|
||||
.min_value = -5000, /* -50°C em unidades de 0.01°C */
|
||||
.max_value = 12500, /* 125°C em unidades de 0.01°C */
|
||||
};
|
||||
return esp_zb_temperature_meas_cluster_create(&temp_cfg);
|
||||
}
|
||||
|
||||
esp_zb_attribute_list_t* device_create_occupancy_cluster(void)
|
||||
{
|
||||
esp_zb_occupancy_sensing_cluster_cfg_t occupancy_cfg = {
|
||||
.occupancy = 0,
|
||||
.sensor_type = ESP_ZB_ZCL_OCCUPANCY_SENSING_OCCUPANCY_SENSOR_TYPE_PIR,
|
||||
};
|
||||
return esp_zb_occupancy_sensing_cluster_create(&occupancy_cfg);
|
||||
}
|
||||
|
||||
esp_zb_attribute_list_t* device_create_ias_zone_cluster(void)
|
||||
{
|
||||
esp_zb_ias_zone_cluster_cfg_t ias_zone_cfg = {
|
||||
.zone_state = ESP_ZB_ZCL_IAS_ZONE_ZONESTATE_NOT_ENROLLED,
|
||||
.zone_type = ESP_ZB_ZCL_IAS_ZONE_ZONETYPE_CONTACT_SWITCH,
|
||||
.zone_status = 0,
|
||||
};
|
||||
return esp_zb_ias_zone_cluster_create(&ias_zone_cfg);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/* Device Registry - Abstração de diferentes tipos de dispositivos Zigbee */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_zigbee_core.h"
|
||||
#include "device_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* ========================= ESTRUTURA DE DISPOSITIVO ========================= */
|
||||
|
||||
typedef struct {
|
||||
const char *name; /* Nome do dispositivo */
|
||||
uint16_t device_id; /* Zigbee Device ID */
|
||||
uint8_t endpoint; /* Endpoint do dispositivo */
|
||||
|
||||
/* Função para criar clusters específicos do dispositivo */
|
||||
esp_zb_cluster_list_t* (*create_clusters)(void);
|
||||
|
||||
/* Handler para atributos específicos do dispositivo */
|
||||
esp_err_t (*attribute_handler)(const esp_zb_zcl_set_attr_value_message_t *message);
|
||||
|
||||
/* Função de inicialização específica do hardware */
|
||||
void (*hardware_init)(void);
|
||||
|
||||
/* Função para atualizar estado do dispositivo */
|
||||
void (*update_state)(void);
|
||||
|
||||
} device_type_t;
|
||||
|
||||
/* ========================= REGISTRO DE DISPOSITIVOS ========================= */
|
||||
|
||||
/* Retorna a configuração do dispositivo ativo */
|
||||
const device_type_t* device_get_config(void);
|
||||
|
||||
/* ========================= FUNÇÕES AUXILIARES PARA CLUSTERS ========================= */
|
||||
|
||||
/* Cria cluster básico (obrigatório para todos os dispositivos) */
|
||||
esp_zb_attribute_list_t* device_create_basic_cluster(void);
|
||||
|
||||
/* Cria cluster identify (obrigatório para todos os dispositivos) */
|
||||
esp_zb_attribute_list_t* device_create_identify_cluster(void);
|
||||
|
||||
/* Cria cluster on/off (para dispositivos controláveis) */
|
||||
esp_zb_attribute_list_t* device_create_onoff_cluster(void);
|
||||
|
||||
/* Cria cluster de temperatura (para sensores) */
|
||||
esp_zb_attribute_list_t* device_create_temperature_cluster(void);
|
||||
|
||||
/* Cria cluster de ocupação (para sensores de movimento/porta) */
|
||||
esp_zb_attribute_list_t* device_create_occupancy_cluster(void);
|
||||
|
||||
/* Cria cluster IAS Zone (para sensores de segurança) */
|
||||
esp_zb_attribute_list_t* device_create_ias_zone_cluster(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
+53
-33
@@ -23,6 +23,8 @@
|
||||
#include "zcl_utility.h"
|
||||
#include "esp_zb_switch.h"
|
||||
#include "led_indicator.h"
|
||||
#include "device_config.h"
|
||||
#include "device_registry.h"
|
||||
|
||||
typedef struct light_bulb_device_params_s {
|
||||
esp_zb_ieee_addr_t ieee_addr;
|
||||
@@ -59,15 +61,29 @@ static void zb_buttons_handler(switch_func_pair_t *button_func_pair)
|
||||
|
||||
static esp_err_t deferred_driver_init(void)
|
||||
{
|
||||
/* Inicializa o LED RGB */
|
||||
/* Get device configuration */
|
||||
const device_type_t *device_config = device_get_config();
|
||||
|
||||
#if FEATURE_LED_INDICATOR
|
||||
/* Initialize LED RGB */
|
||||
led_indicator_init(GPIO_LED_RGB);
|
||||
#endif
|
||||
|
||||
/* Inicializa o relay */
|
||||
relay_driver_init();
|
||||
/* Initialize device-specific hardware */
|
||||
if (device_config->hardware_init) {
|
||||
device_config->hardware_init();
|
||||
}
|
||||
|
||||
/* Inicializa o botão */
|
||||
#ifdef HAS_BUTTON_TOGGLE
|
||||
/* Initialize button with toggle functionality */
|
||||
ESP_RETURN_ON_FALSE(switch_driver_init(button_func_pair, PAIR_SIZE(button_func_pair), zb_buttons_handler), ESP_FAIL, TAG,
|
||||
"Failed to initialize switch driver");
|
||||
#else
|
||||
/* Initialize button without toggle (only pairing/factory reset) */
|
||||
ESP_RETURN_ON_FALSE(switch_driver_init(button_func_pair, PAIR_SIZE(button_func_pair), NULL), ESP_FAIL, TAG,
|
||||
"Failed to initialize switch driver");
|
||||
#endif
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -125,25 +141,23 @@ static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id,
|
||||
|
||||
static esp_err_t zb_attribute_handler(const esp_zb_zcl_set_attr_value_message_t *message)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
bool relay_state = false;
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
if (message->info.dst_endpoint == HA_ONOFF_SWITCH_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) {
|
||||
relay_state = message->attribute.data.value ? *(bool *)message->attribute.data.value : relay_state;
|
||||
ESP_LOGI(TAG, "Relay sets to %s", relay_state ? "On" : "Off");
|
||||
relay_set_state(relay_state);
|
||||
}
|
||||
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);
|
||||
|
||||
/* Get device configuration and delegate to device-specific handler */
|
||||
const device_type_t *device_config = device_get_config();
|
||||
|
||||
if (message->info.dst_endpoint == device_config->endpoint) {
|
||||
if (device_config->attribute_handler) {
|
||||
return device_config->attribute_handler(message);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
|
||||
@@ -243,31 +257,37 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
|
||||
|
||||
static void esp_zb_task(void *pvParameters)
|
||||
{
|
||||
/* initialize Zigbee stack */
|
||||
/* Initialize Zigbee stack */
|
||||
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZR_CONFIG();
|
||||
esp_zb_init(&zb_nwk_cfg);
|
||||
|
||||
/* Get device configuration from registry */
|
||||
const device_type_t *device_config = device_get_config();
|
||||
ESP_LOGI(TAG, "Creating device: %s (ID: 0x%04x)", device_config->name, device_config->device_id);
|
||||
|
||||
/* Create endpoint list */
|
||||
esp_zb_ep_list_t *esp_zb_ep_list = esp_zb_ep_list_create();
|
||||
|
||||
/* Create on/off light endpoint */
|
||||
esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG();
|
||||
/* Create device-specific clusters */
|
||||
esp_zb_cluster_list_t *esp_zb_cluster_list = device_config->create_clusters();
|
||||
|
||||
/* Configure endpoint */
|
||||
esp_zb_endpoint_config_t endpoint_config = {
|
||||
.endpoint = HA_ONOFF_SWITCH_ENDPOINT,
|
||||
.endpoint = device_config->endpoint,
|
||||
.app_profile_id = ESP_ZB_AF_HA_PROFILE_ID,
|
||||
.app_device_id = ESP_ZB_HA_ON_OFF_OUTPUT_DEVICE_ID,
|
||||
.app_device_id = device_config->device_id,
|
||||
.app_device_version = 0
|
||||
};
|
||||
|
||||
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_basic_cluster_create(&(light_cfg.basic_cfg));
|
||||
ESP_ERROR_CHECK(esp_zb_basic_cluster_add_attr(esp_zb_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)ESP_MANUFACTURER_NAME));
|
||||
ESP_ERROR_CHECK(esp_zb_basic_cluster_add_attr(esp_zb_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, (void *)ESP_MODEL_IDENTIFIER));
|
||||
ESP_ERROR_CHECK(esp_zb_cluster_list_add_basic_cluster(esp_zb_cluster_list, esp_zb_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
|
||||
ESP_ERROR_CHECK(esp_zb_cluster_list_add_identify_cluster(esp_zb_cluster_list, esp_zb_identify_cluster_create(&(light_cfg.identify_cfg)), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
|
||||
ESP_ERROR_CHECK(esp_zb_cluster_list_add_groups_cluster(esp_zb_cluster_list, esp_zb_groups_cluster_create(&(light_cfg.groups_cfg)), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
|
||||
ESP_ERROR_CHECK(esp_zb_cluster_list_add_scenes_cluster(esp_zb_cluster_list, esp_zb_scenes_cluster_create(&(light_cfg.scenes_cfg)), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
|
||||
ESP_ERROR_CHECK(esp_zb_cluster_list_add_on_off_cluster(esp_zb_cluster_list, esp_zb_on_off_cluster_create(&(light_cfg.on_off_cfg)), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
|
||||
/* Add manufacturer and model info to basic cluster */
|
||||
esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(
|
||||
esp_zb_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
|
||||
if (basic_cluster) {
|
||||
esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)ESP_MANUFACTURER_NAME);
|
||||
esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, (void *)ESP_MODEL_IDENTIFIER);
|
||||
}
|
||||
|
||||
/* Register endpoint */
|
||||
ESP_ERROR_CHECK(esp_zb_ep_list_add_ep(esp_zb_ep_list, esp_zb_cluster_list, endpoint_config));
|
||||
|
||||
esp_zb_device_register(esp_zb_ep_list);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "led_indicator.h"
|
||||
#include "led_strip_encoder.h"
|
||||
#include "device_config.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/rmt_tx.h"
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_zigbee_core.h"
|
||||
#include "switch_driver.h"
|
||||
#include "device_config.h"
|
||||
#include "led_indicator.h"
|
||||
|
||||
/* Estado atual do relay */
|
||||
@@ -208,6 +209,7 @@ bool switch_driver_init(switch_func_pair_t *button_func_pair, uint8_t button_num
|
||||
|
||||
void relay_driver_init(void)
|
||||
{
|
||||
#ifdef DEVICE_TYPE_RELAY
|
||||
gpio_config_t io_conf = {};
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_OUTPUT;
|
||||
@@ -220,10 +222,12 @@ void relay_driver_init(void)
|
||||
gpio_set_level(GPIO_OUTPUT_RELAY, 0);
|
||||
relay_state = false;
|
||||
ESP_LOGI(TAG, "Relay inicializado no GPIO %d", GPIO_OUTPUT_RELAY);
|
||||
#endif
|
||||
}
|
||||
|
||||
void relay_set_state(bool state)
|
||||
{
|
||||
#ifdef DEVICE_TYPE_RELAY
|
||||
relay_state = state;
|
||||
gpio_set_level(GPIO_OUTPUT_RELAY, state ? 1 : 0);
|
||||
|
||||
@@ -231,14 +235,21 @@ void relay_set_state(bool state)
|
||||
led_indicator_set_state(state ? LED_RELAY_ON : LED_RELAY_OFF);
|
||||
|
||||
ESP_LOGI(TAG, "Relay %s", state ? "LIGADO" : "DESLIGADO");
|
||||
#endif
|
||||
}
|
||||
|
||||
void relay_toggle(void)
|
||||
{
|
||||
#ifdef DEVICE_TYPE_RELAY
|
||||
relay_set_state(!relay_state);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool relay_get_state(void)
|
||||
{
|
||||
#ifdef DEVICE_TYPE_RELAY
|
||||
return relay_state;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -45,12 +45,6 @@ extern "C" {
|
||||
/* user should configure which I/O port as toggle switch input, default is GPIO9 */
|
||||
#define GPIO_INPUT_IO_TOGGLE_SWITCH GPIO_NUM_9
|
||||
|
||||
/* GPIO para controlar o relay */
|
||||
#define GPIO_OUTPUT_RELAY GPIO_NUM_4
|
||||
|
||||
/* GPIO para o LED RGB (WS2812) */
|
||||
#define GPIO_LED_RGB GPIO_NUM_8
|
||||
|
||||
/* config button level depends on the pull up/down setting
|
||||
push button level is on level = 1 when pull-down enable
|
||||
push button level is on level = 0 when pull-up enable
|
||||
|
||||
Reference in New Issue
Block a user