#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define DHT1 10
#define DHT2 11
#define DHTTYPE DHT22
#define relayP 12
#define relayL 13
DHT dht1(DHT1, DHTTYPE);
DHT dht2(DHT2, DHTTYPE);
const byte ROW_NUM = 4; //four rows
const byte COL_NUM = 4; //four columns
char keys [ROW_NUM][COL_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte pin_column[COL_NUM] = {4, 3, 2, 1}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COL_NUM );
float tmp1 = 0.0;
float humi1 = 0.0;
float tmp2 = 0.0;
float humi2 = 0.0;
float maxTemp = 0.0;
float minTemp = 0.0;
float maxHumi = 0.0;
float minHumi = 0.0;
void homePage1() {
char key = keypad.getKey();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1.Temp (Sensor 1)");
lcd.setCursor(0, 1);
lcd.print("2.Temp (Sensor 2)");
lcd.setCursor(0, 2);
lcd.print("3.Set Min/Max Temp");
lcd.setCursor(13, 3);
lcd.print("># Next");
if (key = '1') {
temp1();
} else if (key = '2'){
temp2();
} else if (key = '3'){
handleSetTempHumi1f();
} else if (key = '#'){
homePage2();
}
}
void homePage2() {
char key = keypad.getKey();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("4.Show Max/Min Temp");
lcd.setCursor(0, 1);
lcd.print("5.Show Max/Min Humi");
lcd.setCursor(0, 2);
lcd.print("6.Reset All Settings");
lcd.setCursor(0, 3);
lcd.print("<* Prev");
lcd.setCursor(13, 3);
lcd.print("># Next");
if (key = '4') {
displayTemp();
} else if (key = '5') {
displayHumi();
} else if (key = '6') {
handleReset();
} else if (key = '*') {
homePage1();
}
}
void handleReset() {
lcd.clear();
lcd.print("Value reset!!");
delay(2000);
maxTemp = 0.0;
minTemp = 0.0;
maxHumi = 0.0;
minHumi = 0.0;
homePage1();
}
void displayTemp() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Maxtemp:");
lcd.print(maxTemp);
lcd.setCursor(0, 1);
lcd.print("MinTmp:");
lcd.print(minTemp);
lcd.setCursor(0, 3);
lcd.print("< * Previous");
}
void displayHumi() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MaxHumi:");
lcd.print(maxHumi);
lcd.setCursor(0, 1);
lcd.print("MinHumi:");
lcd.print(minHumi);
lcd.setCursor(0, 3);
lcd.print("< * Previous");
}
float mxTmpVal () {
float value = 0.0;
while (true) {
char key = keypad.getKey();
if (key != NO_KEY && isDigit(key)) {
value = value * 10 + (key - '0');
lcd.print(key);
} else if (key == '#') {
break;
}
}
return value;
}
float minTmpVal() {
float value = 0.0;
while (true) {
char key = keypad.getKey();
if (key != NO_KEY && isDigit(key)) {
value = value * 10 + (key - '0');
lcd.print(key);
} else if (key == '#') {
break;
}
}
return value;
}
float maxHumiVal() {
float value = 0.0;
while (true) {
char key = keypad.getKey();
if (key != NO_KEY && isDigit(key)) {
value = value * 10 + (key - '0');
lcd.print(key);
} else if (key == '#') {
break;
}
}
return value;
}
float minHumiVal() {
float value = 0.0;
while (true) {
char key = keypad.getKey();
if (key != NO_KEY && isDigit(key)) {
value = value * 10 + (key - '0');
lcd.print(key);
} else if (key == '#') {
break;
}
}
return value;
}
void handleSetTempHumi1f() {
lcd.clear();
lcd.print("Set Temp (1F)");
lcd.setCursor(0, 1);
lcd.print("Enter Max Temp: ");
// Implement code to set temperature here
float maxTempV = mxTmpVal();
lcd.clear();
lcd.print("Set Temp (1F)");
lcd.setCursor(0, 1);
lcd.print("Enter Min Temp:");
float minTempV = minTmpVal();
lcd.clear();
lcd.print("Set Humid (1F)");
lcd.setCursor(0, 1);
lcd.print("Enter Max Humi: ");
float maxHumiVal1 = maxHumiVal();
lcd.clear();
lcd.print("Set Humid (1F)");
lcd.setCursor(0, 1);
lcd.print("Enter Min Humi: ");
// Implement code to set humidity here
float minHumiVal1 = minHumiVal();
maxTemp = maxTempV;
minTemp = minTempV;
maxHumi = maxHumiVal1;
minHumi = minHumiVal1;
lcd.clear();
Serial.print("Min/Max Temp&Humid are saved!");
lcd.print("Setting saved!!");
delay(1000);
homePage1();
}
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print("Welcome!!");
delay(2000);
homePage1();
}
void temp1() {
float t1 = dht1.readTemperature();
float h1 = dht1.readHumidity();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1st Floor: ");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(h1);
lcd.print(" %");
lcd.setCursor(0, 2);
lcd.print("Temp: ");
lcd.print(t1);
lcd.print(" C");
delay(100);
}
void temp2() {
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("2nd Floor: ");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(h2);
lcd.print(" %");
lcd.setCursor(0, 2);
lcd.print("Temp: ");
lcd.print(t2);
lcd.print(" C");
delay(100);
}
void loop() {
if (tmp1 > maxTemp, tmp2 > maxTemp) {
digitalWrite(relayP, HIGH);
delay(15000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Water Pump Active!");
lcd.setCursor(0, 1);
lcd.print("For 15 Seconds");
delay(2000);
} else {
digitalWrite(relayP, LOW);
}
if (tmp1 < minTemp, tmp2 < minTemp){
digitalWrite(relayL, HIGH);
delay(10000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Light Active!");
lcd.setCursor(0, 1);
lcd.print("For 10 Seconds");
delay(2000);
} else {
digitalWrite(relayL, LOW);
}
}