工具

  • esp8266 开发板
  • DHT 温湿度检测模块
  • 杜邦线

实验连线

esp 8266 核心版 的 D4 与 DHT 模块的 out 相连, “ 3v” 与 vcc 相连
“G” 与 “GND” 相连

程序代码

添加管理库
d79e6e80b38cb3d6ae850e95689b605d.png

报Adafruit_Sensor 没有解决方法如下

要使用DH11需要先下载DH11的函数库,打开Arduino后,管理库,在搜索 DH11 即可搜索到 DHT_sensor_library。
打开示例 DHTtester ,编译上传,会发现一个错误,大致意思是缺少 Adafruit_Sensor.h 这个头文件,可在 https://github.com/adafruit/Adafruit_Sensor 此处下载。
将下载后的压缩包解压后,找到 Adafruit_Sensor.h 文件,复制到 库文件夹 DHT_sensor_library 下即可,重新打开 Arduino后,编译就没有错误了。
上传之后可以看到 湿度,C温度,F温度,体感C温度,体感F温度。
链接:https://www.jianshu.com/p/8c14ee4e4b57

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <DHT.h> //调用 dnt11驱动库
#define DHTPIN D4 //说明 数据接口 为8266 开发板的D4口
#define DHTTYPE DHT11 // 说明 使用的模块是 DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
// put your setup code here, to run once:
dht.begin(); // 开始测量
Serial.begin(9600); // 打开串口

}

void loop() {
// put your main code here, to run repeatedly:
delay(200); //延时,等待测量完成
float h = dht.readHumidity(); //读取湿度
float t = dht.readTemperature(); // 读取温度
Serial.print("当前湿度");
Serial.print(h);
Serial.println("%");
Serial.print("当前温度");
Serial.print(t);
Serial.println("°C");
delay(2000); // 2s 测一次
}