#define BLYNK_TEMPLATE_ID "TMPL6WYL2lU0t"
#define BLYNK_TEMPLATE_NAME "ESP32 Wifi"
#define BLYNK_AUTH_TOKEN "aSP4nntt0SeJ23LDTxEz238qzETlX1Sp"


#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>



#include <DHT.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; //nama hotspot yang digunakan
char pass[] = ""; //password hotspot yang digunakan
#define DHTPIN 5 // Mention the digital pin where you connected
#define DHTTYPE DHT22 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// WidgetTerminal must follow Blynk library include
WidgetTerminal terminal(V4);

void setup(){
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  dht.begin();
  timer.setInterval(2500L, sendSensor);
}

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

void sendSensor(){
  boolean hitemp = 0;
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  if (isnan(h) || isnan(t)) {
  Serial.println("Failed to read from DHT sensor!");
  return;
}

  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
  Serial.print("Temperature : ");
  Serial.print(t);
  Serial.print(" Humidity : ");
  Serial.println(h);

if(t > 30){
  hitemp = 1;
  //Blynk.email("[email protected]", "Alert", "Temperature over 28C!");
  Blynk.logEvent("temp_alert","Temp above 30 degrees");
  Blynk.virtualWrite(V3, hitemp);
  Serial.println("High Temperature arise! ( >30C )");
  Serial.print(hitemp);
  terminal.println("High Temperature arise! ( >30C )");
  terminal.print(hitemp);
}



}

//void terminalPrint() {
//  delay(30000);

  // Erase all values in the terminal
  //terminal.clear(); 


  // Print values, like Serial.print
  //terminal.print();   
  //terminal.print("Temperature : ");
  //terminal.print(t);
  //erminal.print(" Humidity : ");
  //terminal.println(h);
  // Print values, like Serial.println()
  //terminal.println();

 
  // Write a raw data buffer
  //terminal.write();

  // Ensure that data was sent out of device
  //terminal.flush();
	
//}