#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL3XvWT_aYr"
#define BLYNK_TEMPLATE_NAME "poject3"
#define BLYNK_AUTH_TOKEN "a_cy_dD6vN3osGAVkjSQ7tF7rw-pxV5_"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <TimeLib.h>


LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);

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

//BlynkTimer timer;

const byte LED1 = 13; // Merah
const byte  LED2 = 12; // Kuning
const byte  LED3 = 14 ; // Hijau


const byte DHTPIN = 15; // GPIO that is connected to the sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

const byte  Trig = 18;
const byte  Echo = 19;
float jarak = 0;
float ketinggian_air = 0;
float dht_temp;
float  dht_hum;


byte hours, minutes, seconds, dates, months;
int years, indexHariRTC;

void setup()
{
  dht.begin();

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  Serial.begin(9600);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Connecting to Blynk ");
  lcd.setCursor(0, 1);
  lcd.print("WiFi ");

  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, "iot.serangkota.go.id", 8080);
  lcd.setCursor(0, 1);
  lcd.print("Success");
  delay(2000);
  lcd.clear();
}

void loop()
{
  ReadSensorPING();
  ReadSensorDHT();
  Blynk.virtualWrite(V12, dht_hum);
  Blynk.virtualWrite(V13, dht_temp);
  Blynk.virtualWrite(V14, ketinggian_air);
  Blynk.run();
  requestTime();
  lcdDisplay();
}

void lcdDisplay() {
  lcd.setCursor(0, 0);
  lcd.print(hours / 10);
  lcd.setCursor(1, 0);
  lcd.print(hours % 10);
  lcd.setCursor(2, 0);
  lcd.print(":");
  lcd.setCursor(3, 0);
  lcd.print(minutes / 10);
  lcd.setCursor(4, 0);
  lcd.print(minutes % 10);
  lcd.setCursor(5, 0);
  lcd.print(":");
  lcd.setCursor(6, 0);
  lcd.print(seconds / 10);
  lcd.setCursor(7, 0);
  lcd.print(seconds % 10);
  lcd.setCursor(8, 0);

  lcd.setCursor(0, 1);
  lcd.print(dates / 10);
  lcd.setCursor(1, 1);
  lcd.print(dates % 10);
  lcd.setCursor(2, 1);
  lcd.print("/");
  lcd.setCursor(3, 1);
  lcd.print(months / 10);
  lcd.setCursor(4, 1);
  lcd.print(months % 10);
  lcd.setCursor(5, 1);
  lcd.print("/");
  lcd.setCursor(6, 1);
  lcd.print(years / 1000);
  lcd.setCursor(7, 1);
  lcd.print((years % 1000) / 100);
  lcd.setCursor(8, 1);
  lcd.print((years % 100) / 10);
  lcd.setCursor(9, 1);
  lcd.print(years % 10);
  lcd.setCursor(10, 1);




}