#include "DHT.h"

#define RELAY_FAN_PIN A5 // Arduino pin connected to relay which connected to fan
#define HUMID_PIN A4
#define DHTPIN 12           // Arduino pin connected to relay which connected to DHT sensor
#define DHTTYPE DHT22
#define DOOR 11

const int TEMP_THRESHOLD_UPPER = 25; // upper threshold of temperature, change to your desire value
const int TEMP_THRESHOLD_LOWER = 20; // lower threshold of temperature, change to your desire value

DHT dht(DHTPIN, DHTTYPE);

float temperature;    // temperature in Celsius
float humid;
bool doorPrintMsg = false;

void setup()
{
  Serial.begin(9600); // initialize serial
  dht.begin();        // initialize the sensor
  pinMode(RELAY_FAN_PIN, OUTPUT); // initialize digital pin as an output
}

void loop()
{
  // wait a few seconds between measurements.
  delay(2000);

  temperature = dht.readTemperature();;  // read temperature in Celsius
  humid = dht.readHumidity();;
  
  if (isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
  }
  else {
    if (digitalRead(DOOR) == LOW) {
      if (doorPrintMsg == false) {
        Serial.println("Door is open");
        doorPrintMsg = true;
      }
      return;
    }
    doorPrintMsg = false;
    if(temperature > TEMP_THRESHOLD_UPPER && humid > 60){
      Serial.println("The fan is turned on");
      Serial.println("The humid is 60");
      digitalWrite(RELAY_FAN_PIN, HIGH); // turn on
      digitalWrite(HUMID_PIN, HIGH);
      return;
    }
    if(temperature < TEMP_THRESHOLD_LOWER && humid < 60 ){
      Serial.println("The fan is turned off");
      Serial.println("IS BELOW 60");
      digitalWrite(HUMID_PIN, LOW);
      digitalWrite(RELAY_FAN_PIN, LOW); // turn on
      return;
    }
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module