DHT11, LInknode D1, Random Failed to read

I am using a Linknode D1 and have no problems when running the DHT Tester sketch.

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include “DHT.h”

#define DHTPIN D8 // what digital pin we’re connected to

// Uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println(“DHT11 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(f);
Serial.print(” *F\t”);
Serial.print(“Heat index: “);
Serial.print(hif);
Serial.println(” *F”);
}

However when I run the following:

/*
Cayenne ESP8266 WiFi Example

*/

//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneESP8266WiFi.h>

#include “DHT.h”
#define DHTPIN D8 // what digital pin we’re connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);

#include “CayenneDefines.h”

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token = “xxxx”;
// Your network name and password.
char ssid = “xxxx”;
char password = “xxxx”;

void setup()
{
Serial.begin(9600);
Cayenne.begin(token, ssid, password);

}

void loop()
{
Cayenne.run();

// Wait a few seconds between measurements.

delay(3000);

// 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 Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(f)) {
Serial.println(“Failed to read from DHT sensor!”);
return;
}

Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(f);
Serial.println(” *F\t”);

}

// These functions are called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(V0)
{
Cayenne.virtualWrite(V0, dht.readTemperature(true));
}

CAYENNE_OUT(V1)
{
Cayenne.virtualWrite(V1, dht.readHumidity());
}

Most of the time I get the “Failed to read from DHT sensor!” error.

Please suggest how I can fix the inconsistent error.

Thanks!

give this a try. DHT11/DHT22 - temperature and humidity sensor