#include <ESP8266WiFi.h>
#include <Firebase_ESP_Client.h>
#include <Servo.h>

// ================= WIFI =================
const char* ssid = "Rio";
const char* password = "Rio1234567";

// ================= FIREBASE =================
#define API_KEY "AIzaSyC-YYniuYaVJAZl0LywCbC-HKvMED0_uUY"
#define DATABASE_URL "https://poultryrail-1205e-default-rtdb.firebaseio.com/"
#define USER_EMAIL "esp32@gmail.com"
#define USER_PASSWORD "12345678"

FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

// ================= SERVO =================
Servo servo1;
Servo servo2;

#define SERVO1_PIN 14
#define SERVO2_PIN 12

// ================= ULTRASONIK =================
#define TRIG_PIN 2
#define ECHO_PIN 5

unsigned long lastServo = 0;
unsigned long lastUltra = 0;

// ================= STATUS =================
String lastCmd1 = "";
String lastCmd2 = "";

void setup() {

Serial.begin(115200);

// ================= SERVO =================
servo1.attach(SERVO1_PIN);
servo2.attach(SERVO2_PIN);

servo1.writeMicroseconds(1500);
servo2.writeMicroseconds(1500);

// ================= ULTRASONIK =================
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

// ================= WIFI =================
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(300);
Serial.print(".");
}

Serial.println("\nWiFi Connected");

// ================= FIREBASE =================
config.api_key = API_KEY;
config.database_url = DATABASE_URL;

auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;

Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}

// ================= LOOP =================
void loop() {

// ================= WIFI =================
if (WiFi.status() != WL_CONNECTED) {

WiFi.reconnect();
}

// ================= SERVO =================
if (millis() - lastServo > 150) {

lastServo = millis();

// ================= SERVO1 =================
if (Firebase.RTDB.getString(
&fbdo,
"/servo1/action")) {

String cmd1 = fbdo.stringData();

if (cmd1 != lastCmd1) {

lastCmd1 = cmd1;

if (cmd1 == "open") {

servo1.writeMicroseconds(1900);

Serial.println("Servo1 OPEN");

} else {

servo1.writeMicroseconds(1500);

Serial.println("Servo1 CLOSE");
}
}
}

// ================= SERVO2 =================
if (Firebase.RTDB.getString(
&fbdo,
"/servo2/action")) {

String cmd2 = fbdo.stringData();

if (cmd2 != lastCmd2) {

lastCmd2 = cmd2;

if (cmd2 == "open") {

servo2.writeMicroseconds(1100);

Serial.println("Servo2 OPEN");

} else {

servo2.writeMicroseconds(1500);

Serial.println("Servo2 CLOSE");
}
}
}
}

// ================= ULTRASONIK =================
if (millis() - lastUltra > 1000) {

lastUltra = millis();

digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);

digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);

digitalWrite(TRIG_PIN, LOW);

long duration =
pulseIn(ECHO_PIN, HIGH);

float distance =
duration * 0.034 / 2;

if (distance > 2 &&
distance < 400) {

Firebase.RTDB.setFloat(
&fbdo,
"/sensor/jarak",
distance
);

Serial.print("Jarak: ");
Serial.println(distance);
}
}
}

