2022-05-06
汇总起来几个问题:
- 串口输出设置波特率时,监视窗口也要同样设置。
- DHT11是使用3.3V! 这么多年,竟然没有注意这个问题。
- 下载DHT11的函数库。具体用法不述。一般在函数库中会有详细的例程可参考。
- 利用Wemos D1的WIFI特性,把DHT11的数据上传到云端。这个待试。参考以下试试:https://blog.csdn.net/yangyan0716/article/details/103071253
#include <dht11.h>
//#define DHT11PIN 8
const int dhtPin=8
dht11 DHT11;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println();
int chk = DHT11.read(dhtPin);
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (C): ");
Serial.println((float)DHT11.temperature, 2);
delay(2000);
}
#include <dht11.h>
dht11 th; // 注意现在th代表 DHT11 传感器
const int pin = 8; // 请把 DHT11 的 data pin 连到 arduino Pin 8
void setup( ) {
Serial.begin(9600);
}
void loop( ) {
th.read(pin); // 读取 DHT11 传感器
Serial.print(String("") + "Humidity = "+ th.humidity + " %");
Serial.println(String("")+", temperature = "+ th.temperature +".C");
delay(2500); // 规定至少要等 2 秒再次读 dht11
}
开始输出是乱码。
据说esp8266默认是115200,改参数后,输出如下。看来不是这个参数的问题。
image.png
感觉是端口错误,改为如下D8:
image.png
此时,输出不乱码了。但是数据错误:难道是器件有问题?按理说新买的,不可能啊。
image.png急病乱投医。试着将输出强制转换为float.还是不行。
image.png image.png
问题弄明白了:
看到一篇,说DHT11连接3.3v, 重新连接后正常!
https://blog.csdn.net/yangyan0716/article/details/103071253
更详细的参数输出,参考以下例程。这个正常的话,下载到相应的库函数导入,运行是没问题的。但我这边出现错误提示。需要做其他事了。这先放一下。
/* DHT Pro Shield - Simple
*
* Example testing sketch for various DHT humidity/temperature sensors
* Written by ladyada, public domain
*
* Depends on Adafruit DHT Arduino library
* https://github.com/adafruit/DHT-sensor-library
*/
#include "DHT.h"
#define DHTPIN D4 // what pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
网友评论