// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHT22 test!");
dht.begin();
pinMode(LED_BUILTIN, OUTPUT);
}
static bool measure_environment(float* temperature, float* humidity){
*humidity = dht.readHumidity();
*temperature = dht.readTemperature();
if (isnan(*humidity) || isnan(*temperature)) {
Serial.println("Failed to read from DHT Sensor");
return(false);
} else {
return(true);
}
}
static print_temperature_and_humidity(float* temperature, float* humidity){
Serial.print( "T = " );
Serial.print( *temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( *humidity, 1 );
Serial.println( "%" );
}
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 t;
float h;
if( measure_environment( &t, &h ) == true )
{
print_temperature_and_humidity(&t, &h);
}
if(h > 65){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
} else if(h < 60){
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
}