#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "dht.h"
#include "ssd1306.h"
#define DHT_PIN 4 // DHT22连接的GPIO引脚
#define OLED_SDA 21 // OLED显示屏的SDA引脚
#define OLED_SCL 20 // OLED显示屏的SCL引脚
void app_main(void)
{
// 初始化DHT传感器
dht_sensor_type_t sensor_type = DHT_TYPE_DHT22; // 使用DHT22传感器
dht_sensor_handle_t dht_handle = dht_init(sensor_type, DHT_PIN);
if (!dht_handle) {
ESP_LOGE("DHT", "Failed to initialize DHT sensor");
return;
}
// 初始化OLED显示屏
ssd1306_handle_t oled_handle = ssd1306_create(SSD1306_I2C_PORT, OLED_SDA, OLED_SCL);
if (!oled_handle) {
ESP_LOGE("OLED", "Failed to initialize OLED display");
return;
}
while (1) {
float temperature, humidity;
if (dht_read_data(dht_handle, &temperature, &humidity) == ESP_OK) {
char temp_str[20];
char hum_str[20];
sprintf(temp_str, "Temp: %.1f C", temperature);
sprintf(hum_str, "Hum: %.1f %%", humidity);
// 在OLED上显示温湿度
ssd1306_clear_screen(oled_handle, false);
ssd1306_display_text(oled_handle, 0, temp_str, 12, false);
ssd1306_display_text(oled_handle, 1, hum_str, 12, false);
ssd1306_refresh_gram(oled_handle);
} else {
ESP_LOGE("DHT", "Failed to read data from DHT sensor");
}
vTaskDelay(pdMS_TO_TICKS(5000)); // 5秒延迟
}
}