#define BLYNK_PRINT Serial
// Akses Masuk ke masing-masing Blynk
#define BLYNK_TEMPLATE_ID "TMPL6lNuUPtHx"
#define BLYNK_TEMPLATE_NAME "Pemberi Pakan Ikan"
#define BLYNK_AUTH_TOKEN "tmYm0vm_k9yUDutB7mT56FB6UxxanYh"
#define ECHO_PIN 15
#define TRIG_PIN 4
#define vibrator 5
#define buzzer 18
int a = 0;
int indikator = 27;
#include <ESP32Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
Servo myservo;
BlynkTimer timer;
// Bagian Pengaturan Operasi Pin, Blynk
// Sensor Ultrasonic dan Motor Servo
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(vibrator, OUTPUT);
pinMode(indikator, OUTPUT);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, sendSensor);
myservo.attach(2);
myservo.write(0);
// Bagian Mengirim data awal ke Blynk
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V5, "Tertutup");
Blynk.virtualWrite(V4, "Vibrator OFF");
Blynk.virtualWrite(V0, "Buzzer OFF");
Blynk.virtualWrite(V3, 0);
}
void loop() {
Blynk.run(); // Mengaktifkan Blynk
timer.run(); // Mengaktifkan Timer
}
// Membaca Data dari Blynk, dari bagian Virtual Pin 2
// untuk pengaturan kerja Motor Servo
BLYNK_WRITE(V2){
myservo.write(param.asInt());
int pinValue = param.asInt();
if (pinValue > 5) {
digitalWrite(vibrator, HIGH);
Blynk.virtualWrite(V5, "Terbuka "+String(pinValue)+" Derajat");
Blynk.virtualWrite(V4, "Vibrator ON");
}else{
digitalWrite(vibrator, LOW);
Blynk.virtualWrite(V4, "Vibrator OFF");
Blynk.virtualWrite(V5, "Terutup "+String(pinValue)+" Derajat");
}
if (a == 1){
Blynk.virtualWrite(V2, 0);
}
}
// Membaca Data dari Blynk, dari bagian Virtual Pin 3
// untuk bagian Tombol Reset
BLYNK_WRITE(V3){
int reset = (param.asInt());
if (reset == 1){
digitalWrite(vibrator, LOW);
myservo.write(0);
delay(15);
Blynk.virtualWrite(V2, 0);
a = 1;
} else {
a = 0;
}
}
// Bagian untuk mengaktifkan dan membaca data sensor Ultrasonic
void sendSensor()
{
digitalWrite (indikator, HIGH);
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Bagian untuk mengkonversi data sensor ke Jarak
int duration = pulseIn(ECHO_PIN, HIGH);
int distance = duration * 0.034 / 2;
// Kondisi Pakan Penuh
if (a == 0){
if (distance >= 20 && distance <= 100){
Blynk.virtualWrite(V1, "Pakan Masih Ada, Ketinggian "+String(distance)+" Cm");
//Blynk.virtualWrite(V4, "Vibrator ON");
Blynk.virtualWrite(V0, "Buzzer OFF");
}
if (distance > 100 && distance <= 130){
Blynk.virtualWrite(V1, "Pakan Mau Habis, Ketinggian "+String(distance)+" Cm");
//Blynk.virtualWrite(V4, "Vibrator ON");
Blynk.virtualWrite(V0, "Buzzer OFF");
}
if (distance >130){
Blynk.virtualWrite(V1, "Pakan Habis, Ketinggian "+String(distance)+" Cm");
Blynk.virtualWrite(V4, "Vibrator OFF");
Blynk.virtualWrite(V0, "Buzzer Aktif");
Blynk.virtualWrite(V5, "Katub Tertutup");
Blynk.virtualWrite(V2, 0);
digitalWrite(vibrator, LOW);
myservo.write(0);
delay(15);
tone (buzzer, 120);
delay(500);
noTone(buzzer);
}
delay(100);
}
if (a == 1){
Blynk.virtualWrite(V1, "Segera Isi Pakan");
noTone(buzzer);
Blynk.virtualWrite(V0, "Buzzer Tidak Aktif");
}
}