#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <ESP32Servo.h>
#include <DHT.h>
// Konfigurasi LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Konfigurasi DHT22
#define DHTPIN 14 // Pin untuk DHT22
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Konfigurasi Keypad
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] = {15, 2, 4, 5}; // Pin baris
byte colPins[COLS] = {18, 19, 23, 33}; // Pin kolom
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Konfigurasi Servo
Servo myServo;
#define SERVO_PIN 13
// Konfigurasi LED dan LDR
#define LDR_PIN 34 // Pin LDR (AO)
#define LED_LIGHT 25 // LED untuk lampu otomatis
#define LED_FAN 26 // LED untuk kipas
// Kata sandi
const String PASSWORD = "1234";
String inputPassword = "";
void setup() {
// Inisialisasi komponen
Serial.begin(115200);
lcd.begin(20, 4);
lcd.print("System Initializing...");
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
dht.begin();
// Servo dan LED
myServo.attach(SERVO_PIN);
myServo.write(0); // Posisi awal pintu tertutup
pinMode(LED_LIGHT, OUTPUT);
pinMode(LED_FAN, OUTPUT);
}
void loop() {
// Pembacaan keypad
char key = keypad.getKey();
if (key) {
if (key == '#') { // Tombol konfirmasi
if (inputPassword == PASSWORD) {
lcd.clear();
lcd.print("Access Granted!");
myServo.write(90); // Buka pintu
delay(5000);
myServo.write(0); // Tutup pintu
lcd.clear();
lcd.print("Enter Password:");
} else {
lcd.clear();
lcd.print("Access Denied!");
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
}
inputPassword = ""; // Reset input
} else if (key == '*') { // Tombol reset
inputPassword = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Hapus input
} else {
if (inputPassword.length() < PASSWORD.length()) {
inputPassword += key;
lcd.setCursor(0, 1);
lcd.print(inputPassword);
}
}
}
// Baca data dari DHT22
float temp = dht.readTemperature();
float hum = dht.readHumidity();
lcd.setCursor(0, 2);
lcd.print("Temp:");
lcd.print(temp);
lcd.print("C ");
lcd.print("Hum:");
lcd.print(hum);
lcd.print("% ");
// Kontrol LDR untuk lampu otomatis
int ldrValue = analogRead(LDR_PIN);
if (ldrValue < 1000) { // Kondisi gelap
digitalWrite(LED_LIGHT, HIGH);
lcd.setCursor(0, 3);
lcd.print("Lampu: ON ");
} else {
digitalWrite(LED_LIGHT, LOW);
lcd.setCursor(0, 3);
lcd.print("Lampu: OFF");
}
// Simulasikan kipas berdasarkan suhu
if (temp > 30) { // Jika suhu > 30C
digitalWrite(LED_FAN, HIGH);
} else {
digitalWrite(LED_FAN, LOW);
}
delay(1000); // Loop dengan jeda
}