Problem Dht22 + esp8266

Hello,
I have a problem with the dht22 sensor. Shows me incorrect values in cayenne iot and console. I mentioned that I changed the sensor dht22 and esp8266 because I thought they broke but the problem persists. What do you suggest I do?

#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define DHTPIN 12
#define DHTTYPE DHT22 // DHT 22 (AM2302)

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

// Cayenne authentication info.
char username = “…”;
char password = “…”;
char clientID = “…”;

unsigned long lastMillis = 0;

DHT dht(DHTPIN, DHTTYPE);

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

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

}


Thanks a lot.

you are missing dht.begin(); in void setup()

2 Likes

Thank you very much! All it’s ok now. Have a great day!!! :slight_smile:

1 Like

Hello, i have another problem with my project.
I set up the sensor to display in celsius temperature but in Cayenne appears in F.
Code remain the same.

change this to:

float f = dht.readTemperature();

you are also sending data at a very rapid rate from the main loop. Have a look at this topic Sending MQTT messages within rate limits

1 Like