#define BLYNK_TEMPLATE_ID "TMPL6yZocaJcJ"
#define BLYNK_TEMPLATE_NAME "Sistem Sekuritas rumah"
#define BLYNK_AUTH_TOKEN "328USjTxGoLoFsdCMcVJ5IzIBDbcDT0Y"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define pinPIR 13
#define buzzer 18
#define lampu 4
int a;
int saklar = 0;
const float GAMMA = 0.7;
const float RL10 = 50;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
BLYNK_WRITE(V4){
saklar = (param.asInt());
if (saklar == 0) {
a = LOW;
}else{
a = HIGH;
}
}
void baca_sensor_gerak(){
int analogValue = analogRead(36);
float voltage = analogValue * 5/4095.0;
float resistance = 2000 * voltage / (1 - voltage / 5);
int lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Cahaya: "+String(lux)+" lux");
Blynk.virtualWrite(V0, lux);
if (a == LOW){
if (lux < 50){
int nilaiPir = digitalRead((pinPIR));
if(nilaiPir == 1){
tone(buzzer, 120);
Blynk.virtualWrite(V1, "Ada Orang Masuk");
Blynk.virtualWrite(V3, "Buzzer Aktif");
Blynk.virtualWrite(V2, "Lampu Menyala dan sistem sekuritas ON");
digitalWrite (lampu, HIGH);
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("Ada Orang Masuk");
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("Sistem Sekuritas ON");
}
else{
noTone(buzzer);
Blynk.virtualWrite(V1, "Tidak Ada Gerakan");
Blynk.virtualWrite(V3, "Buzzer Tidak Aktif");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("Tidak Ada Orang");
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("Sistem Sekuritas ON");
}
}
if (lux > 50){
Blynk.virtualWrite(V2, "Lampu Mati, Sistem Sekuritas OFF");
digitalWrite (lampu, LOW);
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("Sistem Sekuritas OFF");
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("Kondisi Siang Hari");
}
}else{
noTone(buzzer);
digitalWrite (lampu, LOW);
Blynk.virtualWrite(V2, "Sistem Sekuritas OFF, Set Saklar");
Blynk.virtualWrite(V1, "OFF");
Blynk.virtualWrite(V3, "OFF");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("Sistem Sekuritas OFF");
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("Aktifkan Sistem");
}
}
void setup() {
lcd.init();
lcd.backlight();
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, baca_sensor_gerak);
pinMode(lampu, OUTPUT);
pinMode(pinPIR, INPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
delay(1000);
lcd.setCursor (0, 0);
lcd.print("Sekuritas Rumah");
baca_sensor_gerak();
Blynk.run();
timer.run();
}
NOCOMNCVCCGNDINLED1PWRRelay Module