#include "LiquidCrystal_I2C.h"
#include "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT sensor
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
int soilPin = 34;
int ledPin = 2;
LiquidCrystal_I2C lcd(0x27, 20, 4);
DHT dht(DHTPIN, DHTTYPE);
int soilValue = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
lcd.init();
lcd.backlight();
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop()
{
//===================Soil===============
// Reading potentiometer value
soilValue = analogRead(soilPin);
if (soilValue <= 0)
{
Serial.print(F("Dry"));
lcd.setCursor(3, 3);
lcd.print("Dry");
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(3000); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(3000);
}
else if (soilValue > 0 && soilValue <= 60 )
{
Serial.print(F("Semi Wet"));
lcd.setCursor(3, 3);
lcd.print("Semi Wet");
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
else if (soilValue >60 )
{
Serial.print(F("Heavy Wet"));
lcd.setCursor(3, 3);
lcd.print("Heavy Wet");
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(500);
}
//============================================
//=============DHT11=========================
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
//============================================
Serial.print(F("Hum : "));
Serial.print(h);
lcd.setCursor(1, 0);
lcd.print("Hum = ");
lcd.print(h);
lcd.print("%");
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C"));
lcd.setCursor(1, 1);
lcd.print("Temp = ");
lcd.print(t);
lcd.print("°C");
Serial.println(soilValue);
lcd.setCursor(1, 2);
lcd.print("Soil = ");
lcd.print(soilValue);
delay(1000);
}
/*
1. Dry 0
2. Semi Wet >0 <60
3. Too Much 100
0 - 4095
*/