#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// LCD I2C 20x4 - Alamat I2C biasanya 0x27
LiquidCrystal_I2C lcd(0x27, 20, 4);
// DHT Sensor setup
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Pin push button & LED
const int doorPin = 2; // Push button untuk simulasi reed switch
const int buttonPin = 3; // Push button untuk kontrol LED
const int ledPin = 4; // Output LED
bool ledState = false;
bool lastButtonState = HIGH;
void setup() {
// LCD & sensor init
lcd.init();
lcd.backlight();
dht.begin();
// Pin mode
pinMode(doorPin, INPUT_PULLUP); // Simulasi reed switch
pinMode(buttonPin, INPUT_PULLUP); // Kontrol LED
pinMode(ledPin, OUTPUT);
// Pesan awal
lcd.setCursor(0, 0);
lcd.print("Smart Home Ready!");
delay(2000);
lcd.clear();
}
void loop() {
// Baca sensor suhu & kelembaban
float temp = dht.readTemperature();
float hum = dht.readHumidity();
// Tampilkan suhu
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp, 1);
lcd.print(" C ");
// Tampilkan kelembaban
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(hum, 1);
lcd.print(" % ");
// Baca status pintu (push button sebagai reed switch)
int doorState = digitalRead(doorPin);
lcd.setCursor(0, 2);
if (doorState == LOW) {
lcd.print("Pintu: TERBUKA ");
} else {
lcd.print("Pintu: TERTUTUP ");
}
// Tombol kontrol LED toggle
bool currentButtonState = digitalRead(buttonPin);
if (lastButtonState == HIGH && currentButtonState == LOW) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
lastButtonState = currentButtonState;
// Status lampu di LCD
lcd.setCursor(0, 3);
lcd.print("Lampu: ");
lcd.print(ledState ? "NYALA " : "MATI ");
delay(300); // Delay kecil agar LCD tidak flicker
}