#include <DHT.h>
#include <Keypad.h>
#include <IRremote.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
// Readme:
// Dùng phím # trên keypad để nhập mật khẩu khi cửa đóng và đóng cửa khi cửa mở
// Pass: 1234
// Trên IR remote
// phím 1 để bật đèn
// phím nguồn để tắt đèn
// phím + để mở cửa
// phím - để đóng cửa
#define DHTPIN 10
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {2, 3, 4, 5};
byte pin_column[COLUMN_NUM] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
int recv_pin = 11;
IRrecv irrecv(recv_pin);
Servo myServo;
int servoPin = 13;
int lightPin = 12;
LiquidCrystal_I2C lcd(0x27, 16, 2);
String password = "1234";
String enteredPassword = "";
bool doorOpen = false;
unsigned long lastTempDisplay = 0;
const int IR_LIGHT_ON = 48;
const int IR_LIGHT_OFF = 162;
const int IR_DOOR_OPEN = 2;
const int IR_DOOR_CLOSE = 152;
void setup() {
Serial.begin(9600);
dht.begin();
myServo.attach(servoPin);
myServo.write(0);
irrecv.enableIRIn();
lcd.begin(16, 2);
lcd.setBacklight(LOW);
pinMode(lightPin, OUTPUT);
}
void loop() {
// Display temperature and humidity on LCD every 2 seconds
if (millis() - lastTempDisplay > 2000) {
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temp) && !isnan(humidity)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(temp) + " C");
lcd.setCursor(0, 1);
lcd.print("Humidity: " + String(humidity) + " %");
} else {
lcd.clear();
lcd.print("Sensor Error");
}
lastTempDisplay = millis();
}
char key = keypad.getKey();
if (key == '#') {
if (doorOpen) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Closing Door...");
myServo.write(0);
doorOpen = false;
delay(500);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
enteredPassword = "";
unsigned long start = millis();
while (millis() - start < 10000) { // 10 second timeout
key = keypad.getKey();
if (key) {
if (key == '#') {
if (enteredPassword == password) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Opening Door...");
myServo.write(90);
doorOpen = true;
delay(500);
break;
} else {
lcd.clear();
lcd.print("Incorrect Pass");
delay(500);
enteredPassword = "";
lcd.clear();
lcd.print("Enter Password:");
}
} else if (key == '*') {
enteredPassword = "";
} else {
enteredPassword += key;
lcd.setCursor(0, 1);
lcd.print(enteredPassword);
}
}
}
}
}
if (irrecv.decode()) {
int command = irrecv.decodedIRData.command;
Serial.println(command);
if (command == IR_LIGHT_ON) {
lcd.clear();
digitalWrite(lightPin, HIGH);
lcd.setCursor(0, 1);
lcd.print("Light ON");
} else if (command == IR_LIGHT_OFF) {
lcd.clear();
digitalWrite(lightPin, LOW);
lcd.setCursor(0, 1);
lcd.print("Light OFF");
} else if (command == IR_DOOR_OPEN && !doorOpen) {
lcd.clear();
lcd.print("Opening Door...");
myServo.write(90);
doorOpen = true;
delay(300);
} else if (command == IR_DOOR_CLOSE && doorOpen) {
lcd.clear();
lcd.print("Closing Door...");
myServo.write(0);
doorOpen = false;
delay(300);
}
irrecv.resume();
}
}