// we are using the DHTesp library 
#include "DHTesp.h"

//declaration of variables
//giving names to sensor pins attached to uC
const int DHT_PIN = 15;
const int LDR = 34;
DHTesp dhtSensor;
int LDR_reading_A0 = 0;
const int RAIN = 5;
int pneumatic_left_open = 18;
int pneumatic_right_open = 19;
int pneumatic_left_close = 21;
int pneumatic_right_close = 22;
int heat_gun_relay = 23;
int exhaust_fan_1_relay = 33;
int exhaust_fan_2_relay = 25;

//variables
boolean RAIN_bool;
float temp;
float humidity;
 
//codes in "void setup" only run once when the uC is turned on
//usually has configuration codes
void setup() {
  //selection of baud rate
  Serial.begin(115200);
  //setting pin 15 as input to get reading from DHT22
  pinMode(DHT_PIN, INPUT);
  //using the DHTesp library to configure the sensor to get reading from it
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  //setting pin 34 as input to get reading from LDR
  pinMode(LDR, INPUT);
  // setting pin 5 as input to get reading from rain sensor
  pinMode(RAIN, INPUT);
  //setting pins 18,19,21,22 as output to operate the relays connected to the pneumatic cylinders
  pinMode(pneumatic_left_open, OUTPUT);
  pinMode(pneumatic_right_open, OUTPUT);
  pinMode(pneumatic_left_close, OUTPUT);
  pinMode(pneumatic_right_close, OUTPUT);
  // settting pin 23 as output to operate relay connected to heat gun
  pinMode(heat_gun_relay, OUTPUT);
  pinMode(exhaust_fan_1_relay, OUTPUT);
  pinMode(exhaust_fan_2_relay, OUTPUT);
}

void loop() {
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  LDR_reading_A0 = analogRead(LDR);
  RAIN_bool = digitalRead(RAIN);
  temp = data.temperature;
  humidity = data.humidity;
  Serial.println("Temp: " + String(temp, 2) + "°C");
  Serial.println("Humidity: " + String(humidity, 1) + "%");
  //as we increase lux (light level), LDR reading decreases
  //since ADC is of 12b; (2^12)-1= 4095 is the max reading from LDR
  Serial.print("Light Intensity (A0): ");
  Serial.println(LDR_reading_A0);
  //we are using digital states to detect rain; 1=yes, 0=no
  Serial.print("Rain: ");
  Serial.println(RAIN_bool);
  Serial.println("---");\

  //set all pneumatic relays to be off when the "if else loops" are exited
  digitalWrite(pneumatic_left_open, LOW);
  digitalWrite(pneumatic_right_open, LOW);
  digitalWrite(pneumatic_left_close, LOW);
  digitalWrite(pneumatic_right_close, LOW);

  // if rain is detected OR LDR detects low lux level (night/cloudy) OR its too cold, close roof
  // turn on heat gun
  // turn on fan 

  if (RAIN_bool == 1|| LDR_reading_A0 >= 2048){
  Serial.println("Night Time / Raining ==> ROOF CLOSED");

    //trigger pneumatics to close roof
    digitalWrite(pneumatic_left_close, HIGH);
    digitalWrite(pneumatic_right_close, HIGH);

    //check temp to continue the drying process
      if (temp <=30){
        //too cold, turn on heater and turn off exhaust fans
      digitalWrite(heat_gun_relay, HIGH);
      digitalWrite(exhaust_fan_1_relay, LOW);
      digitalWrite(exhaust_fan_2_relay, LOW);
      Serial.println("Cold ==> FANS OFF, HEATER ON");
      }

      else if  (temp >= 32){
        //too hot, turn off heater, turn on exhaust fans
      digitalWrite(heat_gun_relay,LOW);
      digitalWrite(exhaust_fan_1_relay, HIGH);
      digitalWrite(exhaust_fan_2_relay, HIGH);
      Serial.println("Hot ==> FANS ON, HEATER OFF");
      }
    
  }
  // once it is day time, we look at two scenarios
  //Day time but cloudy -> roof should be open and heater should be on
  //Day time and sunny -> roof should be open but heater should be off

  // this is loop condition indicates day time
  //Assumption: Day time, LDR reading is less than 2048 due to brightness
  // we have used "AND" beacuse the roof should only open in no rain
  else if (LDR_reading_A0 < 2048 && RAIN_bool == 0 ){
    Serial.println("Day Time / Not Raining ==> ROOF OPEN");
    //trigger pneumatics to open roof
    digitalWrite(pneumatic_left_open, HIGH);
    digitalWrite(pneumatic_right_open, HIGH);

    //checking for the two scenarios
    //DAY TIME BUT CLOUDY
    //Assumption: LDR reading will be between 1024 and 2047 with high humidity
    if (LDR_reading_A0 <= 2047 && LDR_reading_A0 >= 1024){
      //turn on heat gun
      digitalWrite(heat_gun_relay, HIGH);
      Serial.println("Cloudy ==> HEATER ON");
    } 
    //DAY TIME BUT SUNNY
    //Assumption: LDR reading will be less than equal to 1023
     else if (LDR_reading_A0 <= 1023){
      //turn off heat gun
      digitalWrite(heat_gun_relay, LOW);
      Serial.println("Sunny ==> HEATER OFF");
    } 
  }

  // one second delay to allow readings to stabilize
  // also note that the DHT22 sensor allows us to take readings no less than every second
    delay(1000);
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module