#include <ESP32Servo.h>
#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <FirebaseESP32.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
const String url = "http://worldtimeapi.org/api/timezone/Asia/Jakarta";
// Insert your network credentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Insert Firebase project API Key
#define FIREBASE_AUTH "E0oNBBfw7T9KpTKqjFcpoPl11LtTXRTcysXF7wih" //ini ambilnya di bagian Project Settings > Service Accounts > Database Secret
// Insert RTDB URLefine the RTDB URL */
#define FIREBASE_HOST "https://db-burung-default-rtdb.asia-southeast1.firebasedatabase.app/"
FirebaseData firebaseData;
FirebaseData persediaanData;
FirebaseData timerData;
unsigned long lastTime = 0;
unsigned long timerDelay = 5000;
int count = 0;
bool isAuthenticated = false;
String json = "";
String timeNow = "";
const int servoPin = 13;
#define RELAY 15
#define trigPin 4
#define echoPin 2
Servo servo;
//API TIME
void getTime() {
HTTPClient http;
http.useHTTP10(true);
http.begin(url);
http.GET();
String result = http.getString();
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, result);
// Test if parsing succeeds.
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
}
String timenow = doc["datetime"].as<String>();
http.end();
String valid_time = timenow.substring(11, 16);
Serial.println(valid_time);
timeNow = valid_time;
}
void setup() {
servo.attach(servoPin, 500, 2400);
Serial.begin (9600);
pinMode(RELAY, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
}
int pos = 0;
void loop() {
getTime();
if (Firebase.getString(timerData, "/burung/timer/time")){
Serial.println(timerData.stringData());
if (timerData.stringData() == timeNow){
//servo
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servo.write(pos);
delay(15);
}
//water pump
digitalWrite(RELAY, HIGH);
Serial.println("lampu hidup");
delay(1000);
digitalWrite(RELAY, LOW);
Serial.println("lampu mati");
delay(500);
if (Firebase.setString(firebaseData, "/laporan/data", "Berhasil"))
{
Serial.println("Data Terkirim");
}
else
{
Serial.println("Gagal");
Serial.println("Keterangan gagal : " + firebaseData.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}
}
//persediaan air
long durasi, jarak; //Varibel durasi dan jarak
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
durasi = pulseIn(echoPin, HIGH);
jarak = (durasi/2) / 29; //Perhitungan untuk pembacaan jarak
if(jarak < 20 ){
if (Firebase.setString(firebaseData, "/persediaan/status", "Tersedia"))
{
Serial.println("Data Terkirim");
}
else
{
Serial.println("Gagal");
Serial.println("Keterangan gagal : " + firebaseData.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}
else{
if (Firebase.setString(firebaseData, "/persediaan/status", "Tidak Tersedia"))
{
Serial.println("Data Terkirim");
}
else {
Serial.println("Gagal");
Serial.println("Keterangan gagal : " + firebaseData.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}
delay(2000);
}