// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("Reading DHT22 data!");
dht.begin();
pinMode(12, OUTPUT); // Green
pinMode(11, OUTPUT); // Red
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float hum1 = dht.readHumidity();
// Read temperature as Celsius
float temp1 = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(hum1) || isnan(temp1)) {
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
Serial.println("Error in reading sensor data!");
while (isnan(hum1) || isnan(temp1)) {
hum1 = dht.readHumidity();
temp1 = dht.readTemperature();
}
}
else {
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
}
// Wait a few seconds between measurements.
delay(3000);
float hum2 = dht.readHumidity();
float temp2 = dht.readTemperature();
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(temp1, hum1, false);
Serial.print("Humidity: ");
Serial.print(round)(hum1 + hum2) / 2));
Serial.print(" % ");
Serial.print("Temperature: ");
Serial.print((temp1 + temp2) / 2);
Serial.print(" *C ");
Serial.print("Heat index: ");
Serial.print(round(hic));
Serial.println(" *C ");
}
Comments