#include <FirebaseESP32.h>

#define ledPin 27
int long t1 = 0 ;
int long t2 = 0 ;
int ledstate = LOW;

#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""

#define FIREBASE_HOST "https://latihan-13-94cdc-default-rtdb.firebaseio.com/"
#define FIREBASE_AUTH "0zHcYCthWdwhcVG49gfLtA1ErUvWk52qNpFnn19q"
FirebaseData firebaseData;

void setup(){
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  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(){
    if(Firebase.getString(firebaseData, "/IOT/ledState2")){
    String ledState = firebaseData.stringData();
    if(ledState == "1"){
      digitalWrite(ledPin, HIGH);
      if(ledstate == LOW);
      t1 = millis();
      ledstate == HIGH ;

    }
    if(ledState== "0"){
      digitalWrite(ledPin, LOW);
      if(ledstate == HIGH);
      t2 = millis();
      ledstate == LOW;
    }
    int total = t2 - t1 ;
    Serial.println(total);
    Serial.println("LedState = " + ledState);
  }
}