简体   繁体   中英

connect DHT11 to ESP8266

I connect DHT11 to pin D2 ESP8266. Use this code. In the console displays "Read fail". How can I fix it?

DHT11 tested on Arduino, it is working properly.

#include "DHT.h"
#define DHT11PIN D2


DHT dht11(DHT11PIN, DHT11 );


void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht11.begin();

}

void loop() {
  delay(2000);
  float h11 = dht11.readHumidity();
  float t11 = dht11.readTemperature();
  float f11 = dht11.readTemperature(true);
}

There is no D2 pin in Esp8266. It is an nodemcu definition. So you can use

#define DHT11PIN 4
DHT dht11(DHT11PIN, DHT11 ); 

where D2 is connected to the 4th gpio in Esp8266.

Or, you can correctly set board type from device manager as NodeMCU and add

#include "Arduino.h"

to your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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