#include <DHT.h>
//Constants
#define DHTPIN 4 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11 (AM2302)
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
//Variables
int led=13;
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
void setup()
{
Serial.begin(9600);
dht.begin();
pinMode(led, OUTPUT);
}
void loop() {
delay(2000);
// Read data and store it to variables hum and temp
float hum = dht.readHumidity();
float tempC = dht.readTemperature();
float tempF = dht.readTemperature(true);
// Check for errors
if (isnan(hum) || isnan(tempC)) {
Serial.println("Failed to read from DHT sensor!");
return; // Exit the loop if reading fails
}
// Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(tempC);
Serial.println(" C");
Serial.print(tempF);
Serial.println(" F");
if (tempC < 25) {
digitalWrite(led, HIGH);
Serial.println("COOL CLIMATE");
} else {
digitalWrite(led, LOW); // Turn off the LED if not hot
}
delay(2000); // Delay 2 sec.
}