#define BLYNK_TEMPLATE_ID "TMPLHORa4reJ"
#define BLYNK_TEMPLATE_NAME "SMART HOME CONTROL NOTIFICATION"
#define BLYNK_AUTH_TOKEN "Rp-0GAKHe_kQPOwEnQI_Ga36w5yqHfnl"

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>

//FOR BLYNK
char auth [] = BLYNK_AUTH_TOKEN;
char ssid [] = "Wokwi-GUEST"; // CHANGE TO YOUR OWN WIFI/HOTSPOT
char pass [] = ""; // CHANGE TO YOUR OWN WIFI/HOTSPOT'S PASSWORD


#include "DHT.h"
#include <LiquidCrystal_I2C.h>

#define LCD_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2

//for lcd display
LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLUMNS, LCD_ROWS);


//for DHT sensor
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

int DHTPIN = 15;
DHT dht(DHTPIN, DHTTYPE);

float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();

int relayLamp = 2;
int relayFan = 4;
int relaySocket = 5 ;
int mode = 0;

BlynkTimer timer;

void setup() 
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  
  
  dht.begin(); 

  pinMode(relayLamp, OUTPUT);
  pinMode(relayFan, OUTPUT);
  pinMode(relaySocket, OUTPUT);

  timer.setInterval(1000L, dhtReading);
  
  lcd.begin(16,2);
  lcd.init();
  lcd.backlight();
  lcd.clear();

}

BLYNK_CONNECTED ()
{
  Blynk.syncVirtual(V0);
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V4);
  Blynk.syncVirtual(V5);
  Blynk.syncVirtual(V7);
}

BLYNK_WRITE(V1)
{
  int buttonState = param.asInt();
  if (buttonState == 1)
  {
    digitalWrite(relayLamp, LOW);
    
    lcd.setCursor(0,0);
    lcd.print("LAMP IS ON ");
    lcd.print("           ");
  }
  else
  {
    digitalWrite(relayLamp, HIGH);
    lcd.setCursor(0,0);
    lcd.print("LAMP IS OFF");
    lcd.print("           ");
  }
}
BLYNK_WRITE(V2)
{
  int buttonState = param.asInt();
  if (buttonState == 1)
  {
    digitalWrite(relayFan, LOW);
    lcd.setCursor(0,0);
    lcd.print("FAN IS ON ");    
    lcd.print("           ");
  }
  else
  {
    digitalWrite(relayFan, HIGH);
    lcd.setCursor(0,0);
    lcd.print("FAN IS OFF");
    lcd.print("           ");
  }
}
BLYNK_WRITE(V3)
{
  int buttonState = param.asInt();
  if (buttonState == 1)
  {
    digitalWrite(relaySocket, LOW);
    lcd.setCursor(0,0);
    lcd.print("SOCKET IS ON ");
    lcd.print("             ");
  }
  else
  {
    digitalWrite(relaySocket, HIGH);
    lcd.setCursor(0,0);
    lcd.print("SOCKET IS OFF");   
    lcd.print("             "); 
  }
}

BLYNK_WRITE(V7)
{
  mode = param.asInt();

  if (mode == 1)
  {
    Serial.println("Lcd Activated");
  }
  else
  {
    Serial.println("Lcd Deactivated");
}
}

void dhtReading ()
{
   h = dht.readHumidity();
  // Read temperature as Celsius
   t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  if (mode == 0)
  {
  lcd.clear();
  Serial.println("Temperature: " + String(t) + " C"); 
  Blynk.virtualWrite(V4, t); 

  Serial.println("Humidity: " + String(h) + " %"); 
  Blynk.virtualWrite(V5,h);
  }
  else if (mode == 1)
  {
      lcd.setCursor(0,0);
      lcd.print("TEMP: " + String(t) + " C" );   
      Serial.println("Temperature: " + String(t) + " C"); 
      Blynk.virtualWrite(V4, t); 

      lcd.setCursor(0,1);
      lcd.print("HUM: " + String(h) + " %" );
      Serial.println("Humidity: " + String(h) + " %"); 
      Blynk.virtualWrite(V5,h);
  }
}

void dhtMode()
{
  if (mode == 0)
  {
    dhtReading ();
  }

}
void loop() {
  Blynk.run();
  timer.run();

}
$abcdeabcde151015202530fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module