#define BLYNK_TEMPLATE_ID "TMPL6tDc_WYHI"
#define BLYNK_TEMPLATE_NAME "Keamanan Pintu"
#define BLYNK_AUTH_TOKEN "_EkIC6enYr4p8pnQkorm8vpqRxGjtgcU"
const int relay = 4;
#define BUTTON_PIN 18
int ldrPin = 34;
int ldrVal = 0;
#include <Keypad.h>
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int SW_relay = 0;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const float GAMMA = 0.7;
const float RL10 = 50;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'} };
byte rowPins[ROWS] = {13, 12, 14, 27}; // Pin untuk baris
byte colPins[COLS] = {26, 25, 33, 32}; // Pin untuk kolom
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Konfigurasi password (contoh: "1234")
String password = "0204";
String inputPassword = "";
BLYNK_WRITE(V0)
{
SW_relay = param.asInt();
if (SW_relay ==1){
digitalWrite(relay, HIGH);
Serial.println("Terbuka");
Blynk.virtualWrite(V0, HIGH);
delay(1000);
}
else{
digitalWrite(relay, LOW);
Serial.println("Terkunci");
Blynk.virtualWrite(V0, LOW);
delay(1000);
}
}
BLYNK_CONNECTED() {
Blynk.syncAll();
}
void setup() {
Blynk.begin(auth, ssid, pass);
Serial.begin(115200);
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
pinMode(BUTTON_PIN, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Password: ");
lcd.setCursor(0, 1);
lcd.print("Tekan Bel");
delay(5000);
}
void loop() {
delay(1000);
Blynk.run();
ldrVal = analogRead(ldrPin);
float voltage = ldrVal * (3.3 / 4095.0);
float resistance = 2000 * voltage / (3.3 - voltage);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
int buttonState = digitalRead(BUTTON_PIN);
if (lux < 200) {
Blynk.logEvent("ldr","Ada orang disekitar rumah");
} else { }
if (buttonState == LOW) {
// Jika tombol ditekan, tampilkan pesan di LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Mohon maaf,");
lcd.setCursor(0,1);
lcd.print(" ");
delay(2000);
lcd.setCursor(0, 0);
lcd.print("penghuni rumah");
lcd.setCursor(0, 1);
lcd.print("sedang pergi");
Serial.println("Mohon maaf, Penghuni rumah sedang pergi");
Blynk.logEvent("tombol","Ada tamu");
delay(2000);
} else {
// Jika tombol tidak ditekan, kosongkan baris kedua
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password: ");
lcd.setCursor(0, 1);
lcd.print("Tekan Bel");
}
char key = keypad.getKey();
if (key) {
Serial.println(key);
lcd.setCursor(9, 0);
lcd.print(key);
delay(500);
if (key == '#') { // Tombol '#' untuk submit password
if (inputPassword == password) {
Serial.println("Password benar!");
digitalWrite(relay, HIGH); // Nyalakan relay
Blynk.virtualWrite(V0, HIGH);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Password benar!");
} else {
Serial.println("Password salah!");
digitalWrite(relay, LOW);
Blynk.virtualWrite(V0, LOW);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Password salah!");
}
} else if (key == '*') { // Tombol '*' untuk reset input
inputPassword = "";
} else {
inputPassword += key;
}
}
}