上传文件至 /
DHT11控制头文件
This commit is contained in:
commit
42135322be
32
DHT11control.h
Normal file
32
DHT11control.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
#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
|
Loading…
x
Reference in New Issue
Block a user