//Program Modifikasi Oleh 
//Rey Stevi Gintinggggg

#include "DHTesp.h"

const int DHT_PIN = 15;

DHTesp dhtSensor;
int suhu; 
int mode=0; 
#define BLYNK_TEMPLATE_ID "TMPL6uPfRzY1N"
#define BLYNK_TEMPLATE_NAME "Tugas Pak Abdul 2"
#define BLYNK_AUTH_TOKEN "qqtd2eaqZSHB91QnCCzfLtt5sWroawmw"


#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("Temp: " + String(data.temperature, 2) + "°C");
  if(mode==1){
    digitalWrite(26,HIGH); //Indikator Automatis
    digitalWrite(27,LOW); 
  if(suhu<=28){ //MODE AUTOMATIS
    digitalWrite(25,LOW); // Kipas 1 OFF
    digitalWrite(33,LOW); // Kipas 2 OFF
    digitalWrite(32,LOW); // Kipas 3 OFF
  }
  if(suhu>=29 && suhu<=30){ //MODE AUTOMATIS
    digitalWrite(25,HIGH); // Kipas 1 ON
    digitalWrite(33,LOW); // Kipas 2 OFF
    digitalWrite(32,LOW); // Kipas 3 OFF   
  }
  if(suhu>=31 && suhu<=32){ //MODE AUTOMATIS
    digitalWrite(25,HIGH); // Kipas 1 ON
    digitalWrite(33,HIGH); // Kipas 2 ON
    digitalWrite(32,LOW); // Kipas 3 OFF   
  }
  if(suhu>=33){ //MODE AUTOMATIS
    digitalWrite(25,HIGH); // Kipas 1 ON
    digitalWrite(33,HIGH); // Kipas 2 ON
    digitalWrite(32,HIGH); // Kipas 3 ON   
  }
  }
   if(mode==0){
     digitalWrite(27,HIGH); //Indikator Manual
     digitalWrite(26,LOW);
   }
 
  Blynk.virtualWrite(V0,suhu);
}

BLYNK_WRITE(V1) // Kipas 1
{
  int pinValue = param.asInt(); 
  if(mode==0){digitalWrite(25,pinValue);} //MODE MANUAL
  
}
BLYNK_WRITE(V2) // Kipas 2
{
  int pinValue = param.asInt(); 
  if(mode==0){digitalWrite(33,pinValue);} //MODE MANUAL
  
}
BLYNK_WRITE(V3) // Kipas 3
{
  int pinValue = param.asInt(); 
  if(mode==0){digitalWrite(32,pinValue);} //MODE MANUAL
  
}
BLYNK_WRITE(V4) 
{
  int pinValue = param.asInt(); 
  mode=pinValue;

}


void setup()
{
  
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  timer.setInterval(1000L, myTimerEvent);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  pinMode(25,OUTPUT); //Kipas 1
  pinMode(33,OUTPUT); //Kipas 2
  pinMode(32,OUTPUT); //Kipas 3
  pinMode(26,OUTPUT); //Indikator Automatis
  pinMode(27,OUTPUT); //Indikator Manual
}

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