#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 11
#define DHTTYPE DHT22
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
boolean R=false;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int lightvalue=0;
LiquidCrystal_I2C lcd(0x27,16,2);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
dht.begin();
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Arduino");
lcd.setCursor(3, 1);
lcd.print("Smart Home");
delay(2000);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
digitalWrite(12,LOW);
}
void loop() {
int temperature = dht.readTemperature();
int humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println(("Failed to read from DHT sensor!"));
return;
}
lightvalue=analogRead(A0);
lightvalue=map(lightvalue,1023,0,0,100);
char customKey = customKeypad.getKey();
// toggle relay to turn on/off light using relay when 5 is pressed
if(customKey == '5' && R==false)
{
digitalWrite(12,HIGH);
R=true;
}
else if (customKey == '5' && R==true)
{
digitalWrite(12,LOW);
R=false;
}
//if temperature high on the relay for fan
if (temperature >30)
digitalWrite(13,HIGH);
else
digitalWrite(13,LOW);
delay(200);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temperature);
lcd.print("C ");
lcd.print("Hum.:");
lcd.print(humidity);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Brightness:");
lcd.print(lightvalue);
lcd.print("%");
delay(100);
}