简体   繁体   English

如何从 DHT11 和 ESP8266 获得准确的温度和湿度值

[英]How can I get accurate value of temperature and humidity from DHT11 and ESP8266

I would like to measure the temperature and humidity from DHT11 sensor and ESP8266.我想测量 DHT11 传感器和 ESP8266 的温度和湿度。 To do so, I made some code by using two libraries.为此,我使用两个库编写了一些代码。

But none of these can give me an accurate number of humidity and temperature value.但是这些都不能给我一个准确的湿度和温度值。 All I can get an output are like followings.我能得到的输出如下。

Temperature = 21.0000 Humidity = 48.0000温度 = 21.0000 湿度 = 48.0000 在此处输入图片说明

Please give me a hint to upgrade my code.请给我一个提示来升级我的代码。

First code第一个代码

#include <DHTesp.h>

DHTesp dht;

void setup(){

  Serial.begin(9600);
  pinMode(0, INPUT);
  dht.setup(0, DHTesp::DHT11);
}
void loop(){
  Serial.print("Temperature = ");
  Serial.println(dht.getTemperature(), 4);
  Serial.print("Humidity = ");
  Serial.println(dht.getHumidity(), 4);
  delay(1000);
}

Seconde code二级代码

#include <DHT.h>
DHT dht(0, DHT11);


void setup() {
  Serial.begin(9600);
  pinMode(0, INPUT);
  dht.begin();
}

void loop() {
 
    float humidity = dht.readHumidity();
    float temp = dht.readTemperature();
    delay(500);
    Serial.println(humidity);

}

From DHT11 datasheet , it only supports resolution to 1 degree Celsius and 8bits for humidity leading to the integer like floats.从 DHT11 数据表来看,它只支持 1 摄氏度的分辨率和 8 位湿度导致整数如浮点数。

Also something to keep in mind is the units that the DHT11 and DHT22 output.另外需要记住的是 DHT11 和 DHT22 输出的单位。

Temperature is returned as Celsius and humidity is a value between 0 - 100 representing the relative humidity.温度以摄氏度返回,湿度是 0 - 100 之间的值,表示相对湿度。

To convert between Celsius and Fahrenheit, you can use this formula要在摄氏度和华氏度之间转换,您可以使用此公式

Serial.print((int)round(1.8*temp+32));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM