#define BLYNK_TEMPLATE_ID "TMPL6UNybSsmx"
#define BLYNK_TEMPLATE_NAME "keypad"
#define BLYNK_AUTH_TOKEN "OKuFzIB4JExuAl816k3a68QrxoJDZgSh"
#define BLYNK_PRINT Serial
#include <Keypad.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// WiFi
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {5, 18, 19, 21}; // Pin baris
byte colPins[COLS] = {22, 23, 32, 33}; // Pin kolom
Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// Pin Relay, Buzzer, dan LED
#define RELAY_PIN 12
#define BUZZER_PIN 4
#define LED_PIN 2 // LED warna kuning
// Variabel Password
String input_password = "";
const String password = "0807";
int failed_attempts = 0;
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
// Blynk connection
Blynk.begin(auth, ssid, pass);
Serial.println("Sistem Siap & Terhubung Blynk...");
}
void loop() {
Blynk.run();
char key = keypad.getKey();
if (key) {
// Buzzer singkat saat tombol ditekan
tone(BUZZER_PIN, 1000, 50);
delay(50);
noTone(BUZZER_PIN);
Serial.print("Key Pressed: ");
Serial.println(key);
// Validasi password saat '#' ditekan
if (key == '#') {
if (input_password == password) {
relayAndLedSuccess();
} else {
relayFailure();
}
input_password = ""; // Reset input
}
// Reset input saat '*' ditekan
else if (key == '*') {
input_password = "";
Serial.println("Input password direset.");
}
else {
input_password += key;
Serial.print("Password Input: ");
Serial.println(input_password);
}
}
}
void relayAndLedSuccess() {
Serial.println("Password BENAR! Relay dan LED ON.");
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
// Kirim notifikasi Blynk
Blynk.logEvent("pintu_terbuka", "pintu terbuka");
delay(5000); // Relay dan LED ON selama 5 detik
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
Serial.println("Relay dan LED OFF.");
}
void relayFailure() {
failed_attempts++;
Serial.print("Password SALAH! Kesalahan ke-");
Serial.println(failed_attempts);
// Kirim notifikasi Blynk
Blynk.logEvent("b_2", "Password salah");
if (failed_attempts >= 3) {
Serial.println("GAGAL MASUK: Kesalahan 3 Kali!");
// Buzzer berbunyi selama 3 detik
tone(BUZZER_PIN, 1000);
delay(3000);
noTone(BUZZER_PIN);
// Kirim notifikasi Blynk
Blynk.logEvent("g_1", "Gagal Masuk 3 Kali!");
failed_attempts = 0; // Reset jumlah kesalahan
}
}