spdis 42135322be 上传文件至 /
DHT11控制头文件
2025-05-09 00:35:17 +08:00

32 lines
767 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef DHT11CONTROL_H
#define DHT11CONTROL_H
#include "DHT.h"
/// 传感器数据引脚定义对应ESP32 GPIO5
#define DHTPIN 5
/// 传感器类型定义
#define DHTTYPE DHT11
// 初始化DHT传感器对象
DHT dht(DHTPIN, DHTTYPE);
inline float* getTempAndHumidity() {
static float data[2]; // 静态数组保存温湿度数据
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// 传感器数据有效性检查
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
data[0] = -999; // 温度错误码
data[1] = -999; // 湿度错误码
} else {
data[0] = temperature;
data[1] = humidity;
}
return data;
}
#endif