我已经深入研究了这种情况,发现我遇到了同样的问题(实际上踩到了几个不同的错误)。
4 年后出现,但进一步的信息总是有助于将来参考。
关于你上面的评论:
此时我猜这是硬件问题。每当我跑步
草图,我将垃圾输出到控制台。经调查,
它在第一次 dht1.begin() 之后开始吐出垃圾
叫。我还写了一个遍历所有引脚的 lua 脚本
寻找 DHT 消息,但那里也一无所获。 – 迈克尔
惠勒 2016 年 9 月 20 日在 0:12
注意:垃圾输出通常意味着串行初始化和控制台输出中的串行比特率不同。如果仍然有问题,请检查它;试试下面的疑难解答。
疑难解答:
1.垃圾输出:连续接收垃圾输出
- Set Bit Rate to 115200.
- Check Serial Bit Rates (Code and Serial Monitor).
- Delay DHT Initialization for: 2 Seconds (2000ms).
- Check Wiring.
2。传感器输出为 nan 或 0
- Check Wiring.
- Test with Different Micro-Controller.
- See: 2.1 Bellow
2.1 不同微控制器测试
- If the Test Fails with a Different Controller Consider:
a) Possibly using the Wrong Library.
b) Possible Defective Module.
**Note:** For ESP32 and ESP8266 you need use a different library.
Include "DHTesp.h" instead of "DHT.h".
See code example bellow.
- If it Succeeds with different Controller:
a) DHT22 is not Compatible with Esp8266.
**Note:** I couldn't find a viable Pin that actually worked on my
ESP8266 (NodeMCU).
I'm relying on my own experience; and considering some people
have managed to make it work; consider that perhaps a
different DHT22 sensor model or ESP8266 could work.
3.无输出
- Usually Error in Code.
- Possible Short Circuit in Module and/or Controller.
enter code here
备注:DHT 的微控制器和引脚
-
Uno:引脚 D02
-
纳米:引脚 D02
-
ESP32: RX2(Pin17)
-
ESP8266: 无效(尝试:1,2,4,5,7)
每个微控制器的代码(包括 ESP8266)
Arduino Uno / Nano
#include "DHT.h"
/* Sensor Type */
#define dhtType DHT22
/* Define DHT22 Pin on Arduino */
#define dhtPin 0 // ESP8266 D1 (GPIO 5)
/* Configure DHT Pin and Model */
DHT dht(dhtPin, dhtType);
/* Initialize DHT22 Sensor */
void Init_DHT22()
{
dht.begin();
// Wait a little for the Sensor to Start and Calibrate.
delay(1000);
}
/* Read and Retrieve Temperature from DHT Pin. */
float GetTemperature() { return dht.readTemperature(); }
/* Read and Retrieve Humidity from DHT Pin. */
float GetHumidity() { return dht.readHumidity(); }
ESP32
#include "DHTesp.h"
#ifndef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP32 ONLY!)
#error Select ESP32 board.
#endif
DHTesp dht;
/** Pin Number for DHT Data Pin */
#define dhtPin 17
void DHT22_Init()
{
// Initialize temperature sensor
dht.setup(dhtPin, DHTesp::DHT22);
}
float Temperature() { return dht.getTemperature(); }
float Humidity() { return dht.getHumidity(); }
ESP8266
/*
* Common Pins used for on ESP8266 are: D1, D2; D4 (None Worked for Me).
*/
#include "DHTesp.h"
#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif
#define dhtPin 4
#define dhtType DHT22
DHTesp dht;
void DHT22_Init()
{
dht.setup(dhtPin, DHTesp::dhtType);
}
float Temperature()
{
delay(dht.getMinimumSamplingPeriod());
return dht.getTemperature();
}
float Humidity()
{
delay(dht.getMinimumSamplingPeriod());
return dht.getHumidity();
}
最好的问候