#include "DHT.h"
#define DHTPIN 4 // what digital pin the DHT22 is conected to
#define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.setTimeout(2000);
// Wait for serial to initialize.
while(!Serial) { }
Serial.println("Device Started");
Serial.println("-------------------------------------");
Serial.println("Running DHT!");
Serial.println("-------------------------------------");
}
int timeSinceLastRead = 0;
void loop() {
// Report every 2 seconds.
if(timeSinceLastRead > 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!");
timeSinceLastRead = 0;
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(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
timeSinceLastRead = 0;
}
delay(100);
timeSinceLastRead += 100;
}
Very āoriginalā method of counting the time intervals, not really compatible with Blynk
But aside from that (and posted unformatted code ) it should work - without Blynk. If it doesnāt, and you are sure the IN/OUT pin is correct , then check your dht, choose another oneā¦
What I meant earlier and I think marvin means the same is that you have your pins wrong. For example on a wemos d1 pin D2 is gpio pin 4, so if I connect a sensor to pin D2 and then read pin ā2ā Iāll get nothing because Iām reading the wrong pin.
so 1. check if you got it right
2. if you did and it fails, try another pin
3. if that fails too try another dht (it might be broken).
However you say it does work on a nodemcu so its fairly save to assume you got the pins wrong.
did you try everything else?
also: did you exclude each element from being faulty? (That is test each element you use separately inside a known and working configuration).
also, the fact that you get serial output from the board is a nice indicator that its not completely faulty. you just need to test each element separately until you figure out whatās going on.
another thing you can try (but it really should not make any difference) is define the input as āD2ā instead of ā4ā.
Here is the summary:
I connected ground with ground, power with 3v3 and data pin with pin 2 (#define DHTPIN 4).
Then, I tried using ground with ground, power with 3v3 and data pin with pin 4 (#define DHTPIN 2).
Then I tried ground with ground, power with 3v3 and data pin with pin 3 (#define DHTPIN 5).
In all above cases I got the same error message on serial monitor. What else should I try ?