//Add Header
#include <WiFi.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h> //ex
#include <Adafruit_LiquidCrystal.h>
#include <Keypad.h>
//DHT
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//LED
#define REDPIN 2
#define YELPIN 14
#define GREPIN 27
//PIR(Motion)
#define PIRPIN 12
//Buzzer
#define BUZPIN 5
//LCD
Adafruit_LiquidCrystal lcd(0);
LiquidCrystal_I2C lcd2(0x27, 16, 2); //ex
//Keypad Setting
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] = {16, 17, 18, 19};
byte colpins[COLS] = {25, 26, 32, 33};
Keypad keypad = Keypad(makeKeymap(keys), rowpins, colpins, ROWS, COLS);
//Password Setting
const String secretNum = "0712";
String inputNum = "";
void setup() {
Serial.begin(115200);
dht.begin();
//Initialize LED Pins
pinMode(REDPIN, OUTPUT);
pinMode(YELPIN, OUTPUT);
pinMode(GREPIN, OUTPUT);
//Initialize PIR
pinMode(PIRPIN, INPUT);
//Initialize Buzzer
pinMode(BUZPIN, OUTPUT);
//Initialize LCD
lcd.begin(16, 2);
lcd.setBacklight(LOW);
//Initialize LCD2
lcd2.begin(0);
lcd2.backlight(); //ex
//Initialize Display
lcd.setCursor(0, 0);
lcd.print("System Ready.");
}
void loop() {
float t = dht.readTemperature();
float h = dht.readHumidity();
if(isnan(t) || isnan(h)) {
Serial.println("Failed to read DHT sensor.");
return;
}
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("*C /");
Serial.print(" Humidity: ");
Serial.print(h);
Serial.println("%");
//Control LED
if(t >= 35 && h >= 70) {
digitalWrite(REDPIN, HIGH);
digitalWrite(YELPIN, LOW);
digitalWrite(GREPIN, LOW);
} else if(t >=27 && h >= 50) {
digitalWrite(REDPIN, LOW);
digitalWrite(YELPIN, HIGH);
digitalWrite(GREPIN, LOW);
} else {
digitalWrite(REDPIN, LOW);
digitalWrite(YELPIN, LOW);
digitalWrite(GREPIN, HIGH);
}
//When a person is detected
int priState = digitalRead(PIRPIN);
lcd.clear();
if(priState == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Person detected.");
Serial.println("Person detected.");
} else {
lcd.setCursor(0, 0);
lcd.print("No Person Detected.");
Serial.println("No Person Detected.");
}
//Keypad
char key = keypad.getKey();
if(key) {
inputNum += key;
Serial.println(inputNum);
}
//If the number entered on the keypad is correct or incorrect
if(inputNum.length() == secretNum.length()) {
if(inputNum == secretNum) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door is open.");
Serial.println("Door is open.");
digitalWrite(BUZPIN, HIGH);
delay(1000);
digitalWrite(BUZPIN, LOW);
} else {
inputNum ="";
lcd.print("Door is Close.");
Serial.println("Door is Close.");
delay(1000);
}
}
delay(2000);
}