// Prajwal Swaroop Srivastava
#define BLYNK_TEMPLATE_ID "TMPL3721tv1JC"
#define BLYNK_TEMPLATE_NAME "Smart Irrigation System"
#define BLYNK_AUTH_TOKEN "SfUK7sBgQ4xVyRsJ-0lc6M8KukhTXEKD"
#include <DHT.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h> // Blynk library for ESP32
#include <LiquidCrystal.h> // LCD library for 4-bit mode
// Pin and sensor definitions
#define DATA_PIN 13 // Data pin for the DHT22 sensor
#define DHTTYPE DHT22 // Sensor type
#define RELAY_PIN 27 // Pin controlling the relay (water pump)
#define LED_PIN 14 // Pin controlling the status LED
// Initialize the DHT sensor
DHT sensor(DATA_PIN, DHTTYPE);
// Initialize the LCD with pin assignments
LiquidCrystal lcd(19, 4, 18, 21, 5, 15); // RS, E, D4, D5, D6, D7
// Wi-Fi credentials
char ssid[] = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
char pass[] = ""; // Replace with your Wi-Fi password
// Irrigation thresholds
const int HUMIDITY_THRESHOLD = 50; // Humidity threshold for irrigation
const float TEMPERATURE_THRESHOLD = 25.0; // Temperature threshold (°C)
// Timer interval (in milliseconds) for the loop
const int LOOP_DELAY = 2000;
// Flags for system states
bool isIrrigationActive = false;
bool isManualControlEnabled = false; // Tracks the state of manual control
BLYNK_WRITE(V2) {
int buttonState = param.asInt(); // Read the button state
if (buttonState == 1) {
isManualControlEnabled = true; // Enable manual control
deactivateIrrigation(); // Ensure the pump is OFF
Serial.println("Manual control enabled: Pump is OFF.");
lcd.clear();
lcd.print("Manual Mode: ON");
} else {
isManualControlEnabled = false; // Disable manual control
Serial.println("Manual control disabled: Automatic mode active.");
lcd.clear();
lcd.print("Manual Mode: OFF");
}
}
void setup() {
// Start the serial monitor for debugging
Serial.begin(115200);
// Initialize the DHT sensor
sensor.begin();
// Initialize the LCD
lcd.begin(16, 2);
lcd.print("System Starting...");
delay(2000); // Pause to display startup message
lcd.clear();
lcd.print("Connecting WiFi...");
// Display connection attempt
Serial.print("Connecting to Wi-Fi: ");
Serial.println(ssid);
// Initialize Blynk with the authentication token and Wi-Fi credentials
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Configure relay and LED pins as outputs
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// Ensure the relay and LED are off initially
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
// Display system ready message
lcd.clear();
lcd.print("System Ready");
Serial.println("Setup complete. System is ready.");
}
void loop() {
Blynk.run(); // Run the Blynk process
if (!isManualControlEnabled) { // Automatic control logic
// Read temperature and humidity values
float temperature = sensor.readTemperature();
float humidity = sensor.readHumidity();
// Check if sensor data is valid
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Error: Failed to read from DHT sensor.");
lcd.setCursor(0, 0);
lcd.print("Sensor Error!");
} else {
// Display temperature and humidity in the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Update LCD display with temperature and humidity
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print(" %");
// Send sensor data to Blynk virtual pins
Blynk.virtualWrite(V6, temperature); // Send temperature to virtual pin V6
Blynk.virtualWrite(V7, humidity); // Send humidity to virtual pin V7
}
// Automatic control of irrigation
if (shouldIrrigate(humidity, temperature)) {
activateIrrigation(); // Turn on water pump and LED
} else {
deactivateIrrigation(); // Turn off water pump and LED
}
}
delay(LOOP_DELAY);
}
// Function to decide if irrigation should be activated
bool shouldIrrigate(float humidity, float temperature) {
if (humidity < HUMIDITY_THRESHOLD && temperature > TEMPERATURE_THRESHOLD) {
Serial.println("Decision: Activate irrigation. Low humidity and high temperature detected.");
return true;
}
Serial.println("Decision: No irrigation needed. Conditions are normal.");
return false;
}
// Function to activate irrigation
void activateIrrigation() {
if (!isIrrigationActive) { // Activate only if not already active
Serial.println("Irrigation ON: Activating water pump and LED.");
digitalWrite(RELAY_PIN, HIGH); // Turn on relay
digitalWrite(LED_PIN, HIGH); // Turn on LED
Blynk.virtualWrite(V1, 1); // Update Blynk status (ON) on virtual pin V1
isIrrigationActive = true; // Update irrigation state
// Update LCD status
lcd.clear();
lcd.print("Irrigation: ON");
}
}
// Function to deactivate irrigation
void deactivateIrrigation() {
if (isIrrigationActive) { // Deactivate only if currently active
Serial.println("Irrigation OFF: Deactivating water pump and LED.");
digitalWrite(RELAY_PIN, LOW); // Turn off relay
digitalWrite(LED_PIN, LOW); // Turn off LED
Blynk.virtualWrite(V1, 0); // Update Blynk status (OFF) on virtual pin V1
isIrrigationActive = false; // Update irrigation state
// Update LCD status
lcd.clear();
lcd.print("Irrigation: OFF");
}
}