# Sensor de Temperatura DHT11 - Guia de Uso ## 🌡️ Sobre o DHT11 O DHT11 é um sensor digital de temperatura e umidade de baixo custo: - **Temperatura**: 0°C a 50°C (±2°C precisão) - **Umidade**: 20% a 90% RH (±5% precisão) - **Interface**: One-wire digital - **Tempo de resposta**: 1 segundo mínimo entre leituras - **Tensão**: 3.3V a 5V ## 🔌 Conexão de Hardware ### Pinagem do DHT11 ``` DHT11 Sensor: ┌─────────┐ │ 1 │ VCC → 3.3V (ESP32-C6) │ 2 │ DATA → GPIO 5 (configurável) │ 3 │ NC → Não conectar │ 4 │ GND → GND └─────────┘ ``` ### Esquema de Ligação ``` ESP32-C6 DHT11 ┌────────┐ 3.3V ──────────────►│ VCC │ │ │ GPIO 5 ────────────►│ DATA │ │ │ │ └─── 10kΩ ───┐ │ │ │ │ │ 3.3V │ │ │ │ NC │ │ │ GND ───────────────►│ GND │ └────────┘ ``` **⚠️ IMPORTANTE:** - Use resistor pull-up de **10kΩ** entre DATA e VCC - Alguns módulos DHT11 já incluem o resistor pull-up ## ⚙️ Configuração ### 1. Ativar Sensor de Temperatura Edite [`main/device_config.h`](main/device_config.h): ```c /* Descomente APENAS sensor de temperatura */ // #define DEVICE_TYPE_RELAY #define DEVICE_TYPE_TEMP_SENSOR /* ← Ative isso */ // #define DEVICE_TYPE_DOOR_SENSOR // #define DEVICE_TYPE_MOTION_SENSOR ``` ### 2. Configurar GPIO No mesmo arquivo: ```c #ifdef DEVICE_TYPE_TEMP_SENSOR #define GPIO_SENSOR_DHT11 5 /* GPIO do DHT11 */ #define SENSOR_UPDATE_INTERVAL 10000 /* 10 segundos */ #endif ``` **GPIOs Recomendadas:** 5, 6, 7, 10, 18, 19, 20, 21 ### 3. Ajustar Intervalo de Atualização ```c #define SENSOR_UPDATE_INTERVAL 10000 /* 10 segundos (recomendado) */ // #define SENSOR_UPDATE_INTERVAL 5000 /* 5 segundos (mínimo seguro) */ // #define SENSOR_UPDATE_INTERVAL 60000 /* 1 minuto (economia energia) */ ``` **⚠️ Mínimo:** 2000ms (2 segundos) - DHT11 requer 1s entre leituras ## 🏗️ Compilar e Gravar ```bash # Compilar idf.py build # Gravar idf.py -p /dev/ttyACM0 flash monitor ``` ## 📊 Formato de Dados Zigbee O DHT11 reporta **temperatura e umidade** no formato Zigbee padrão: ### Cluster de Temperatura (0x0402) - **Atributo**: `MeasuredValue` (0x0000) - **Tipo**: `int16_t` - **Unidade**: 0.01°C - **Exemplo**: 2350 = 23.50°C ### Cluster de Umidade (0x0405) - **Atributo**: `MeasuredValue` (0x0000) - **Tipo**: `uint16_t` - **Unidade**: 0.01% RH - **Exemplo**: 6500 = 65.00% RH ### Leitura no ZHA/Home Assistant O dispositivo aparece automaticamente com **dois sensores**: ```yaml # Sensor de Temperatura sensor: - platform: zha name: "Temperatura Sala" device: "0x98a316fffebf43a4" cluster_id: 0x0402 # Temperature attribute: 0 # Sensor de Umidade sensor: - platform: zha name: "Umidade Sala" device: "0x98a316fffebf43a4" cluster_id: 0x0405 # Humidity attribute: 0 ``` Aparecerá automaticamente como duas entidades: - **sensor.temperatura_sala** - Temperatura em °C - **sensor.umidade_sala** - Umidade relativa em % - **Atualização**: A cada 10 segundos (configurável) ## 🔍 Logs de Funcionamento ``` I (1250) DHT11: DHT11 initialized on GPIO 5 I (1260) DEVICE_REGISTRY: DHT11 sensor task started (update interval: 10000 ms) I (11260) DHT11: Temperature: 23.0°C, Humidity: 65.0% I (11265) DEVICE_REGISTRY: Sensor updated - Temperature: 23.0°C (Zigbee: 2300), Humidity: 65.0% (Zigbee: 6500) I (21265) DHT11: Temperature: 23.5°C, Humidity: 64.0% I (21270) DEVICE_REGISTRY: Sensor updated - Temperature: 23.5°C (Zigbee: 2350), Humidity: 64.0% (Zigbee: 6400) ``` ## ⚠️ Troubleshooting ### Erro: "Timeout waiting for response" ``` E (12000) DHT11: Timeout waiting for response LOW ``` **Causas:** - DHT11 não conectado corretamente - Falta resistor pull-up de 10kΩ - GPIO incorreta - Sensor danificado **Solução:** 1. Verifique conexões (VCC, DATA, GND) 2. Adicione resistor pull-up 10kΩ 3. Teste com outra GPIO 4. Troque o sensor ### Erro: "Checksum error" ``` E (12000) DHT11: Checksum error: calculated=0x5A, received=0x5B ``` **Causas:** - Interferência eletromagnética - Cabo muito longo - Alimentação instável **Solução:** 1. Use cabo curto (<30cm) 2. Adicione capacitor 100nF próximo ao sensor 3. Estabilize alimentação 3.3V 4. Afaste de fontes de ruído ### Aviso: "Read too soon" ``` W (5500) DHT11: Read too soon, wait at least 1 second between reads ``` **Causa:** Tentativa de leitura antes de 1 segundo **Solução:** Aumente `SENSOR_UPDATE_INTERVAL` para no mínimo 2000ms ### Temperatura Sempre 0°C **Causas:** - Sensor não inicializado - Problema no protocolo one-wire - GPIO em conflito **Solução:** 1. Verifique logs de inicialização 2. Teste GPIO com `gpio_get_level()` 3. Use outra GPIO 4. Verifique se pull-up está presente ## 🎯 Otimizações ### Economia de Energia Para dispositivos battery-powered: ```c #define SENSOR_UPDATE_INTERVAL 300000 /* 5 minutos */ ``` Configure também como End Device: ```c /* Em esp_zb_switch.h */ esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG(); /* End Device */ ``` ### Alta Frequência de Atualização Para monitoramento rápido (com alimentação): ```c #define SENSOR_UPDATE_INTERVAL 2000 /* 2 segundos (mínimo) */ ``` ## 📈 Próximas Melhorias ### ✅ Cluster de Umidade - IMPLEMENTADO! O DHT11 agora expõe **temperatura e umidade** via Zigbee automaticamente! ### Alertas de Temperatura/Umidade Implementar notificação quando valores excederem limites: Implementar notificação quando temperatura exceder limites: ```c if (reading.temperature > 30.0f || reading.temperature < 10.0f) { ESP_LOGW(TAG, "Temperature out of range: %.1f°C", reading.temperature); // Enviar alerta via Zigbee } ``` ## 📚 Referências - [DHT11 Datasheet](https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf) - [Zigbee Temperature Measurement Cluster](https://zigbeealliance.org/wp-content/uploads/2019/12/07-5123-06-zigbee-cluster-library-specification.pdf) - [ESP-IDF GPIO API](https://docs.espressif.com/projects/esp-idf/en/latest/esp32c6/api-reference/peripherals/gpio.html) ## 🔗 Compatibilidade **Home Assistant (ZHA)**: ✅ Funciona automaticamente **Zigbee2MQTT**: ✅ Compatível (Temperature Sensor) **Amazon Alexa**: ✅ Via Home Assistant **Google Home**: ✅ Via Home Assistant **Apple HomeKit**: ✅ Via HomeAssistant Bridge