feat: Adiciona suporte para sensor de temperatura e umidade DHT11 com integração Zigbee
This commit is contained in:
+99
-12
@@ -4,11 +4,17 @@
|
||||
#include "zcl/esp_zigbee_zcl_common.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_zb_switch.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#ifdef DEVICE_TYPE_RELAY
|
||||
#include "switch_driver.h"
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_TYPE_TEMP_SENSOR
|
||||
#include "dht11_driver.h"
|
||||
#endif
|
||||
|
||||
#if FEATURE_LED_INDICATOR
|
||||
#include "led_indicator.h"
|
||||
#endif
|
||||
@@ -77,6 +83,66 @@ static const device_type_t relay_device = {
|
||||
|
||||
#ifdef DEVICE_TYPE_TEMP_SENSOR
|
||||
|
||||
static TaskHandle_t temp_sensor_task_handle = NULL;
|
||||
|
||||
/* Task para ler sensor DHT11 periodicamente */
|
||||
static void temp_sensor_task(void *pvParameters)
|
||||
{
|
||||
dht11_reading_t reading;
|
||||
TickType_t last_wake_time = xTaskGetTickCount();
|
||||
|
||||
while (1) {
|
||||
/* Aguarda intervalo configurado */
|
||||
vTaskDelayUntil(&last_wake_time, pdMS_TO_TICKS(SENSOR_UPDATE_INTERVAL));
|
||||
|
||||
/* Lê temperatura e umidade do DHT11 */
|
||||
esp_err_t ret = dht11_read(&reading);
|
||||
|
||||
if (ret == ESP_OK && reading.valid) {
|
||||
/* Converte temperatura para formato Zigbee (0.01°C) */
|
||||
int16_t temp_zigbee = (int16_t)(reading.temperature * 100);
|
||||
|
||||
/* Converte umidade para formato Zigbee (0.01% RH) */
|
||||
uint16_t humidity_zigbee = (uint16_t)(reading.humidity * 100);
|
||||
|
||||
/* Atualiza atributos Zigbee */
|
||||
esp_zb_lock_acquire(portMAX_DELAY);
|
||||
|
||||
/* Atualiza temperatura */
|
||||
esp_zb_zcl_set_attribute_val(
|
||||
HA_ONOFF_SWITCH_ENDPOINT,
|
||||
ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT,
|
||||
ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
|
||||
ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID,
|
||||
&temp_zigbee,
|
||||
false
|
||||
);
|
||||
|
||||
/* Atualiza umidade */
|
||||
esp_zb_zcl_set_attribute_val(
|
||||
HA_ONOFF_SWITCH_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_zigbee,
|
||||
false
|
||||
);
|
||||
|
||||
esp_zb_lock_release();
|
||||
|
||||
ESP_LOGI(TAG, "Sensor updated - Temperature: %.1f°C (Zigbee: %d), Humidity: %.1f%% (Zigbee: %u)",
|
||||
reading.temperature, temp_zigbee, reading.humidity, humidity_zigbee);
|
||||
|
||||
#if FEATURE_LED_INDICATOR
|
||||
/* Pisca LED para indicar leitura */
|
||||
led_indicator_set_state(LED_CONNECTED);
|
||||
#endif
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to read DHT11 sensor: %s", esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static esp_zb_cluster_list_t* temp_sensor_create_clusters(void)
|
||||
{
|
||||
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
|
||||
@@ -84,37 +150,48 @@ static esp_zb_cluster_list_t* temp_sensor_create_clusters(void)
|
||||
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);
|
||||
esp_zb_cluster_list_add_humidity_meas_cluster(cluster_list, device_create_humidity_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 */
|
||||
/* Sensor de temperatura 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);
|
||||
ESP_LOGI(TAG, "Initializing DHT11 temperature sensor on GPIO %d", GPIO_SENSOR_DHT11);
|
||||
|
||||
/* TODO: Implementar inicialização do sensor I2C */
|
||||
/* Exemplo: inicializar AHT20, SHT30, BME280, etc. */
|
||||
/* Inicializa driver DHT11 */
|
||||
esp_err_t ret = dht11_init(GPIO_SENSOR_DHT11);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize DHT11: %s", esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
|
||||
/* Cria task para leitura periódica */
|
||||
xTaskCreate(
|
||||
temp_sensor_task,
|
||||
"temp_sensor",
|
||||
4096,
|
||||
NULL,
|
||||
5,
|
||||
&temp_sensor_task_handle
|
||||
);
|
||||
|
||||
ESP_LOGI(TAG, "DHT11 sensor task started (update interval: %d ms)", SENSOR_UPDATE_INTERVAL);
|
||||
}
|
||||
|
||||
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);
|
||||
*/
|
||||
/* A atualização é feita automaticamente pela task periódica */
|
||||
}
|
||||
|
||||
static const device_type_t temp_sensor_device = {
|
||||
.name = "Zigbee Temperature Sensor",
|
||||
.name = "Zigbee Temperature & Humidity Sensor DHT11",
|
||||
.device_id = ESP_ZB_HA_TEMPERATURE_SENSOR_DEVICE_ID,
|
||||
.endpoint = HA_ONOFF_SWITCH_ENDPOINT,
|
||||
.create_clusters = temp_sensor_create_clusters,
|
||||
@@ -267,6 +344,16 @@ esp_zb_attribute_list_t* device_create_temperature_cluster(void)
|
||||
return esp_zb_temperature_meas_cluster_create(&temp_cfg);
|
||||
}
|
||||
|
||||
esp_zb_attribute_list_t* device_create_humidity_cluster(void)
|
||||
{
|
||||
esp_zb_humidity_meas_cluster_cfg_t humidity_cfg = {
|
||||
.measured_value = ESP_ZB_ZCL_REL_HUMIDITY_MEASUREMENT_MEASURED_VALUE_DEFAULT,
|
||||
.min_value = 0, /* 0% RH em unidades de 0.01% */
|
||||
.max_value = 10000, /* 100% RH em unidades de 0.01% */
|
||||
};
|
||||
return esp_zb_humidity_meas_cluster_create(&humidity_cfg);
|
||||
}
|
||||
|
||||
esp_zb_attribute_list_t* device_create_occupancy_cluster(void)
|
||||
{
|
||||
esp_zb_occupancy_sensing_cluster_cfg_t occupancy_cfg = {
|
||||
|
||||
Reference in New Issue
Block a user