#define BLYNK_TEMPLATE_ID "TMPL6kZoxwnOl"
#define BLYNK_TEMPLATE_NAME "Blynk notifikasi"
#define BLYNK_AUTH_TOKEN "uEWzy9r09IAUKvIOHS6ETVsAja4JNZWZ"
#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN ;
char ssid[] = "Wokwi-GUEST"; //nama hotspot yang digunakan
char pass[] = "";
#define LDR_PIN 34
#define SERVO_PIN 2
#define BUZZER_PIN 4
#define RED_PIN 14
#define GREEN_PIN 12
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
BlynkTimer timer;
int ldrValue;
int pos = 0;
void sendSensor()
{
if(ldrValue == 1){
Blynk.virtualWrite(V0, ldrValue);
}else{
Blynk.virtualWrite(V0, ldrValue);
}
}
void setup() {
Serial.begin(115200);
servo.attach(SERVO_PIN, 500, 2400);
pinMode(LDR_PIN, INPUT);
//pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
//lcd.begin(16, 2); // Initialize the LCD
lcd.init();
lcd.backlight();
lcd.clear();
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000, sendSensor);
}
void loop() {
ldrValue = digitalRead(LDR_PIN);
Serial.println("LDR Value: " + String(ldrValue));
if (ldrValue == 1) {
Serial.println("Light detected! Activating servo and buzzer.");
//tone(BUZZER_PIN, 200);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(15);
}
lcd.setCursor(0, 0);
lcd.print("Kebakaran");
delay(1000);
} else {
for (pos = 180; pos >= 0; pos -= 1) {
servo.write(pos);
delay(15);
}
lcd.setCursor(0, 0);
lcd.print("Aman");
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(RED_PIN, LOW);
//noTone(BUZZER_PIN);
delay(1000);
}
//delay(1000);
lcd.clear();// Clear the LCD for the next reading
Blynk.run(); //menjalankan blynk
timer.run();
}