#include "DHTesp.h"
const int DHT_PIN = 15;
DHTesp dhtSensor;

#include <LiquidCrystal_I2C.h>
#define I2C_ADDR    0x27
#define LCD_COLUMNS 16
#define LCD_LINES   2

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

int suhu; 
int mode; 

int Fan1 = 13;
int Fan2 = 12;
int Fan3 = 14;
int Fan4 = 26;

int ledPin1 = 27; 
int ledPin2 = 4; 

int inputPin = 2;          
int pirState = LOW;        
int val = 0; 
byte Simbol_derajat = B11011111;

#define BLYNK_TEMPLATE_ID "TMPLNQjdXHYw"
#define BLYNK_DEVICE_NAME "Pengendali Monitoring Suhu Ruangan"
#define BLYNK_AUTH_TOKEN "_xfn4k_Tgc8voPxiLhOPAPtz2NRwiD06"

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "Wokwi-GUEST";
char pass[] = "";

BlynkTimer timer;
void myTimerEvent()
{
   TempAndHumidity  data = dhtSensor.getTempAndHumidity();
   suhu=data.temperature;
   Serial.println(suhu);
   Blynk.virtualWrite(V0, suhu);
   if(mode==1){
   if(suhu<=10){
     digitalWrite(Fan1, LOW); 
     digitalWrite(Fan2, LOW);  
     digitalWrite(Fan3, LOW);
     digitalWrite(Fan4, LOW);    
   }
   if(suhu>=10 && suhu<=12){
     digitalWrite(Fan1, HIGH); 
     digitalWrite(Fan2, LOW);  
     digitalWrite(Fan3, LOW);
     digitalWrite(Fan4, LOW);      
   }
   if(suhu>=13 && suhu<=15){
     digitalWrite(Fan1, HIGH); 
     digitalWrite(Fan2, HIGH); 
     digitalWrite(Fan3, LOW);
     digitalWrite(Fan4, LOW);    
   }
   if(suhu>=16 && suhu<=18){
     digitalWrite(Fan1, HIGH); 
     digitalWrite(Fan2, HIGH); 
     digitalWrite(Fan3, HIGH);
     digitalWrite(Fan4, LOW);   
   }
   if(suhu>=20){
     digitalWrite(Fan1, HIGH); 
     digitalWrite(Fan2, HIGH); 
     digitalWrite(Fan3, HIGH);
     digitalWrite(Fan4, HIGH);   
   }
  }
}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); 
  if(mode==0){digitalWrite(13,pinValue);}
  
}

BLYNK_WRITE(V2)
{
  int pinValue = param.asInt(); 
  if(mode==0){digitalWrite(12,pinValue);}
  
}

BLYNK_WRITE(V3)
{
  int pinValue = param.asInt(); 
  if(mode==0){digitalWrite(14,pinValue);}
  
}

BLYNK_WRITE(V4)
{
  int pinValue = param.asInt(); 
  mode=pinValue;
  
}

BLYNK_WRITE(V5)
{
  int pinValue = param.asInt(); 
  digitalWrite(27,pinValue);
  
}
BLYNK_WRITE(V6)

{
  int pinValue = param.asInt(); 
  digitalWrite(4,pinValue);
  
}

BLYNK_WRITE(V7)
{
  int pinValue = param.asInt(); 
  if(mode==0){digitalWrite(26,pinValue);}
  
}


void setup()
{
  lcd.init();
  lcd.backlight();
  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);

  lcd.setCursor(0,0);
  lcd.print("RUANGAN 1"); 

  lcd.setCursor(0,1);
  lcd.print("Suhu:");
  lcd.setCursor(7,1);
  lcd.write(Simbol_derajat);
  lcd.println("C");
  

  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT);     
  pinMode(inputPin, INPUT);

  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  
  pinMode(Fan1, OUTPUT); 
  pinMode(Fan2, OUTPUT); 
  pinMode(Fan3, OUTPUT); 
  pinMode(Fan4, OUTPUT);
}

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

  lcd.setCursor(5, 1);
  lcd.print(suhu);

  val = digitalRead(inputPin); 
 if(mode==1){ 
  if (val == HIGH) {            
    digitalWrite(ledPin1, HIGH); 
    digitalWrite(ledPin2, HIGH); 
    if (pirState == LOW) {
      
      Serial.println("Motion detected!");
      
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin1, LOW); 
    digitalWrite(ledPin2, LOW); 
    if (pirState == HIGH) {
      
      Serial.println("Motion ended!");
      
      pirState = LOW;

      
    }
  }
 }
}