#include <LiquidCrystal.h>
#include <dht.h>
#define dht_apin A0
dht DHT;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int con = 0;
const int sense_pin = A1;
int relay = 8;
int pump = 0;
void setup() {
pinMode(relay, OUTPUT);
Serial.begin(9600);
Serial.println("Welcome to Plant Environment Reader");
lcd.begin(16, 2);
delay(1000);
lcd.clear();
lcdTemplate();
}
void loop() {
DHT.read22(dht_apin);
int moisture;
float sense_apin;
sense_apin = analogRead(sense_pin);
moisture = (100 - ((sense_apin / 1023) * 100));
Serial.print("\nT:");
pad (DHT.temperature);
Serial.print(DHT.temperature);
Serial.write(176);
Serial.print("C ");
Serial.print("| H:");
pad (DHT.humidity);
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("| M:");
pad (moisture);
Serial.print(moisture);
Serial.print("% ");
Serial.print("| P:");
Serial.print(pump);
if (moisture < 15) {
digitalWrite(relay, HIGH);
pump = 1;
} else {
digitalWrite(relay, LOW);
pump = 0;
}
if (DHT.temperature < 60 || DHT.temperature > -30 && DHT.humidity > 0) {
lcd.setCursor(2, 0);
pad (DHT.temperature);
lcd.print(DHT.temperature);
lcd.setCursor(2, 1);
pad (DHT.humidity);
lcd.print(DHT.humidity);
lcd.setCursor(12, 0);
pad (moisture);
lcd.print(moisture);
lcd.setCursor(13, 1);
if (pump == 0) {
lcd.print("OFF");
} else {
lcd.print("ON ");
}
}
if (DHT.temperature > 60 || DHT.temperature < -30 || DHT.humidity == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Maybe an error");
lcd.setCursor(0, 1);
lcd.print(" has occured!");
delay(2000);
lcd.clear();
lcdTemplate();
}
delay(2000);
}
void lcdTemplate() {
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.setCursor(8, 0);
// lcd.write(223); // LCD degree symbol
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("H:");
lcd.setCursor(8, 1);
lcd.print("%");
lcd.setCursor(10, 0);
lcd.print("M:");
lcd.setCursor(15, 0);
lcd.print("%");
lcd.setCursor(10, 1);
lcd.print("P:");
}
void pad(int val) {
if (val >= 0) {
if (val < 100) {
Serial.print(" ");
lcd.print(" ");
}
if (val < 10) {
Serial.print(" ");
lcd.print(" ");
}
}
}