#include <DHT.h>
#include <ESP32Firebase.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>

// Firebase Configure
#define FIREBASE_HOST "otospray-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "D2adAjufH9d1p2AJUH5anTCqF2X8lkkzEqsspWVI"

#define DHT_SENSOR_PIN  15
#define DHT_SENSOR_TYPE DHT22
DHT dht(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
int o = 12;

// Wifi configure
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""

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

// RTC
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

void setup() {
  pinMode(o, OUTPUT);
  dht.begin();
  lcd.init();
  lcd.backlight();
  if (! rtc.begin()) {
    lcd.print("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning()) {
    lcd.print("RTC is NOT running!");
  }
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Menghubungkan ke wifi...");
  }
  Serial.println("Wifi Terhubung...");
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

void loop() {
  float temp = dht.readTemperature();
  float humi = dht.readHumidity();
  DateTime now = rtc.now();
  
  lcd.setCursor(0, 0);
  lcd.print("Temp : ");
  lcd.print(temp);
  lcd.print(" C");

  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(humi);
  lcd.print("%");

  if ((now.hour() >= 8) && (now.hour() < 12)) {
    if (humi < 40) {
      digitalWrite(o, HIGH);
      Serial.println("Pompa On");
    } else {
      digitalWrite(o, LOW);
      Serial.println("Pompa Off");
    }
  } else if ((now.hour() >= 17) && (now.hour() < 18)) {
    digitalWrite(o, LOW);
  } 

  // Kirim data ke Firebase
  Firebase.setFloat("/temperature", temp);
  Firebase.setFloat("/humidity", humi);

  delay(1000); // Tunggu satu detik sebelum membaca kembali
}
$abcdeabcde151015202530fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module
GND5VSDASCLSQWRTCDS1307+
NOCOMNCVCCGNDINLED1PWRRelay Module