#include <LiquidCrystal.h>
const int rs = 12, en = 10, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include <dht.h>
dht DHT;
#define dht22pin A0
#define moistpin A1
#define relaypin 8
bool pumpstate = 0;
char pumpmotor[][4] = {"OFF\0", "ON \0"};
unsigned long timer, timeout = 2000; // milliseconds
int col = 0;
void setup() {
Serial.begin(115200);
pinMode(relaypin, OUTPUT);
lcd.begin(16, 2);
lcdTemplate();
}
void loop() {
if (millis() - timer >= timeout) { // wait until timeout
timer = millis(); // reset timer
DHT.read22(dht22pin); // read DHT
float temp = DHT.temperature;
float humi = DHT.humidity;
float moisture = analogRead(moistpin); // read moisture sensor
float mstr = (100 - ((moisture / 1023) * 100)); // read moisture sensor
displayData(temp, 2, 0, "TEMP:", 'C'); // data, lcd col, lcd row, prefix, symbol
displayData(humi, 2, 1, " | HUMI:", '%');
displayData(mstr, 12, 0, " | MSTR:", '%');
Serial.print(" | PUMP: ");
if (mstr < 15)
pumpstate = 1;
else
pumpstate = 0;
digitalWrite(relaypin, pumpstate);
lcd.setCursor(12, 1);
lcd.print(pumpmotor[pumpstate]);
Serial.print(pumpmotor[pumpstate]);
Serial.println();
}
}
void displayData(float val, int col, int row, char *prefix, char symbol) {
Serial.print(prefix);
if (val < 100 && val >= 10 || val > -10 && val < 0) { // double digit
Serial.print(" "); // move Serial Monitor cursor right
lcd.setCursor(col, row); // place cursor
lcd.print(" "); // clear hundreds place
col++; // move cursor right
}
if (val < 10 && val >= 0) { // single digit
Serial.print(" ");
lcd.setCursor(col, row);
lcd.print(" "); // clear hundreds and tens place
col += 2;
}
lcd.setCursor(col, row);
if (prefix == " | MSTR:") // look for moisture prefix characters
lcd.print(int(val), 1); // show moisture as an INT on LCD
else
lcd.print(val, 1);
Serial.print(val);
Serial.print(symbol);
}
void lcdTemplate() {
Serial.println("Welcome to Plant Environment Reader");
lcd.setCursor(0, 0);
lcd.print("Welcome to Plant");
lcd.setCursor(0, 1);
lcd.print(" Enviro. Reader ");
delay(1000);
lcd.clear();
lcd.print("T: ");
lcd.write(223); // LCD degree symbol // LCD = 223, Serial Monitor = 176
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("H: %");
lcd.setCursor(10, 0);
lcd.print("M: %");
lcd.setCursor(10, 1);
lcd.print("P:");
}