I was playing around with a DHT11 and a ESP8266 NodeMCU ESP-12E and could not get both temp and humidity to display. I ran the script with just temp and just humidity, and they worked fine. After much consternation, I eliminated the error by putting a delay(500) between each reading. I did not try any shorter delays. Here is what my sketch looked like:
#include <DHT11.h>
// Create an instance of the DHT11 class and set the digital I/O pin.
DHT11 dht11(2);
void setup() {
// Initialize serial communication at 115200 baud.
Serial.begin(115200);
}
void loop() {
// Read the temperature from the sensor.
float temperature = dht11.readTemperature();
delay(500); // Required to get both readings consecutively
// Read the humidity from the sensor.
float humidity = dht11.readHumidity();
// If the temperature and humidity readings were successful, print them to the serial monitor.
if (temperature != -1 && humidity != -1) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
// If the temperature or humidity reading failed, print an error message.
Serial.println("Error reading data");
}
// Wait for .5 seconds before the next reading.
delay(500);
}