#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(14, DHT22);
const int ledPinSoil = 2; // Soil moisture sensor LED pin
const int ledPinTemp = 15; // Temperature sensor LED pin
void setup() {
Wire.begin(23, 22); // Initialize the I2C communication with SDA pin 23 and SCL pin 22.
Serial.begin(9600); // Initialize the serial communication at a baud rate of 9600 bits per second.
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
pinMode(ledPinSoil, OUTPUT);
pinMode(ledPinTemp, OUTPUT);
lcd.setCursor(1, 0);
lcd.print("RAGHU");
lcd.setCursor(1, 1);
lcd.print(" 6705223035 ");
delay(500);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("ESP 32");
lcd.setCursor(1, 1);
lcd.print("SOIL MOISTURE");
delay(500);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("JASMINE");
delay(700);
lcd.clear();
}
void loop() {
// Soil moisture sensor
int16_t soilMoisture = analogRead(34);
String soilStatus = soilMoisture <50 ? "DRY" : soilMoisture >50? "WET" : "OK";
lcd.clear();
lcd.print("Soil: ");
lcd.print(soilStatus);
if (soilMoisture <= 300) {
digitalWrite(ledPinSoil, HIGH); // Turn on the LED if the soil is dry
} else {
digitalWrite(ledPinSoil, LOW);
}
delay(2000);
// Temperature sensor
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
lcd.setCursor(0, 0);
lcd.print("Temperature: ");
lcd.print(temperature);
lcd.print("°C");
if (temperature > 38.0) {
digitalWrite(ledPinTemp, HIGH); // Turn on the LED if temperature is high
lcd.setCursor(0, 1);
lcd.print("temperature high ");
} else {
digitalWrite(ledPinTemp, LOW); // Turn off the LED
lcd.setCursor(0, 1);
lcd.print("temp normal");
}
} else {
lcd.setCursor(0, 0);
lcd.print("Error reading DHT sensor");
digitalWrite(ledPinTemp, LOW); // Turn off the LED in case of an error
}
delay(2000); // Delay for 2 seconds before reading again
}