#define BLYNK_TEMPLATE_NAME "Smart Irrigation"
#define BLYNK_AUTH_TOKEN "GOpkCnqR7jDnl8dzDrKWnTXRL7f3PN_Y"
#define BLYNK_TEMPLATE_ID "TMPL51T15fKgK"
#define BLYNK_TEMPLATE_NAME "Smart Irrigation"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <ESP32Servo.h>
#include <RTClib.h> // Added for Real-Time Clock functionality
// Pin definitions
#define DHTPIN 4
#define DHTTYPE DHT22
#define SOIL_MOISTURE_PIN 34
#define RELAY_PIN 18
#define LED_PIN 2
#define SERVO_PIN 5
// Threshold for soil moisture (adjust as necessary)
const int moistureThreshold = 2000;
// Components
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo waterPump;
RTC_DS1307 rtc; // RTC object
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize DHT Sensor
dht.begin();
// Initialize LCD
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Smart Irrigation Sys");
// Initialize Servo
waterPump.attach(SERVO_PIN);
waterPump.write(0); // Initial position
// Initialize pins
pinMode(SOIL_MOISTURE_PIN, INPUT);
pinMode(DHTPIN, INPUT);
pinMode(SERVO_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// Deactivate relay initially
digitalWrite(RELAY_PIN, HIGH);
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1); // Stop if RTC is not found
}
// Check if the RTC is running
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Sets RTC to the date & time this sketch was compiled
}
}
void startWatering() {
waterPump.write(90); // Assume 90 degrees is the pumping position
lcd.setCursor(0, 1);
lcd.print("Watering ON... ");
}
void stopWatering() {
waterPump.write(0); // Assume 0 degrees is the off position
lcd.setCursor(0, 1);
lcd.print("Watering OFF ");
}
void loop() {
// Read sensor values
int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display sensor readings on LCD
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C Hum: ");
lcd.print(humidity);
lcd.print("%");
// Read the current time from the RTC
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// Automatic watering based on soil moisture
if (soilMoistureValue < moistureThreshold) {
digitalWrite(RELAY_PIN, LOW); // Activate relay for automatic watering
startWatering();
} else {
digitalWrite(RELAY_PIN, HIGH); // Deactivate relay
stopWatering();
}
// LED indicator for watering status
if (soilMoistureValue < moistureThreshold) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
// Add a small delay to avoid overwhelming the ESP32
delay(200);
}