Connecting DHT11 Using ESP8266 in Arduino

Hello, I am facing issues connecting to cayenne to get accurate results from my DHT11 sensor. But I don’t think this issue is because of the board, sensor or wires because they are able to work when connected through Arduino and monitored using the serial monitor.

When connected with cayenne results are as such. I’ve tried several different codes but all seemed to yield the same results. Does anyone know the issue behind this problem?

Coding 1:
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <DHT.h>

// WiFi network info.
char ssid = “xxxx”;
char wifiPassword = “xxxx”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xxxx”;
char password = “xxxx”;
char clientID = “xxxx”;

DHT dht(2, DHT11);

void setup() {
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
}

void loop() {
Cayenne.loop();
float temp = dht.readTemperature(true); // Fahrenheit
float hum = dht.readHumidity();
Cayenne.virtualWrite(1, temp, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(2, hum, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);
}

CAYENNE_IN(0)
{
digitalWrite(2, !getValue.asInt());
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“Channel %u, value %s”, request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}

Alternate coding 2:
#include <CayenneMQTTESP8266.h>

#include <DHT.h>

#define CAYENNE_DEBUG

#define CAYENNE_PRINT Serial

#define DHTPIN 2

#define DHTTYPE DHT11

DHT dht(2,DHT11);

// WiFi network info.

char ssid = “xxxx”;

char wifiPassword = “xxxx”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.

char username = “xxxx”;

char password = “xxxx”;

char clientID = “xxxx”;

void setup() {

Serial.begin(9600);

Cayenne.begin(username, password, clientID, ssid, wifiPassword);

pinMode(2,OUTPUT);

digitalWrite(2,HIGH);

}

void loop() {

Cayenne.loop();

float temp = dht.readTemperature(HIGH);//celsius

float hum = dht.readHumidity();

Cayenne.virtualWrite(1, temp, TYPE_TEMPERATURE, UNIT_CELSIUS);

Cayenne.virtualWrite(2, hum, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);

}

Results in Cayenne:

can you try the below code:

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <DHT.h>

// WiFi network info.
char ssid = “xxxx”;
char wifiPassword = “xxxx”;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

#define DHTPIN 2     // Digital pin connected to the DHT sensor

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

#define TEMPERATURE_VIRTUAL_CHANNEL 1
#define HUMIDITY_VIRTUAL_CHANNEL 2

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  dht.begin();
}

void loop() {
  Cayenne.loop();
}

// This function is called at intervals to send temperature sensor data to Cayenne in Celsius.
CAYENNE_OUT(TEMPERATURE_VIRTUAL_CHANNEL)
{
  Cayenne.virtualWrite(TEMPERATURE_VIRTUAL_CHANNEL, dht.readTemperature(), "temp", "c");
}

// This function is called at intervals to send humidity sensor data to Cayenne.
CAYENNE_OUT(HUMIDITY_VIRTUAL_CHANNEL)
{
  Cayenne.virtualWrite(HUMIDITY_VIRTUAL_CHANNEL, dht.readHumidity(), "rel_hum", "p");
}