#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL4UHNFFAtK"
#define BLYNK_TEMPLATE_NAME "Smart Irrigation System"
#define BLYNK_AUTH_TOKEN "ZvwoCJIF3vdfntf_Cf0DU5G8pcDtp6zY"
#include <LiquidCrystal_I2C.h>
#include <DHT_U.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Pin definitions
#define DHTPIN 4
#define DHTTYPE DHT22
#define SOIL_MOISTURE_PIN 34
#define RELAY_PIN 18
#define LED_PIN_GREEN 2
#define LED_PIN_RED 23
#define SERVO_PIN 5
// Threshold for soil moisture (adjust as necessary)
const int moistureThreshold = 500;
// Components
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns and 2 rows
Servo waterPump;
// Blynk Virtual Pins
#define VPIN_TEMP V2
#define VPIN_HUMIDITY V0
#define VPIN_SOIL_MOISTURE V1
#define VPIN_RELAY V3
#define VPIN_LED_RED V4
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize DHT Sensor
dht.begin();
// Initialize LCD
lcd.begin(16, 2); // 16 columns and 2 rows
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(RELAY_PIN, OUTPUT);
pinMode(LED_PIN_GREEN, OUTPUT);
pinMode(LED_PIN_RED, OUTPUT);
// Deactivate relay initially
digitalWrite(RELAY_PIN, HIGH);
// Connect to WiFi and Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, "Wokwi-GUEST", "");
}
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...");
}
BLYNK_WRITE(VPIN_RELAY) {
int relayState = param.asInt(); // Get the value from the app (0 or 1)
if (relayState == 1) {
digitalWrite(LED_PIN_GREEN, HIGH);
startWatering();
} else {
digitalWrite(LED_PIN_GREEN, LOW);
stopWatering();
}
}
// This function will be called every time the V4 switch is toggled
BLYNK_WRITE(VPIN_LED_RED) {
int switchState = param.asInt(); // Get the state of the switch (0 or 1)
// Control the LED based on the switch state
if (switchState == 1) {
digitalWrite(LED_PIN_RED, HIGH); // Turn LED on
} else {
digitalWrite(LED_PIN_RED, LOW); // Turn LED off
}
}
void loop() {
Blynk.run(); // Run Blynk process
// 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("%");
// Send sensor readings to Blynk
Blynk.virtualWrite(VPIN_TEMP, temperature);
Blynk.virtualWrite(VPIN_HUMIDITY, humidity);
Blynk.virtualWrite(VPIN_SOIL_MOISTURE, soilMoistureValue);
// Automatic watering based on soil moisture
if (soilMoistureValue < moistureThreshold && digitalRead(RELAY_PIN) == HIGH) {
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN_GREEN, HIGH);
Blynk.virtualWrite(V3, HIGH); // Activate relay for automatic watering
startWatering();
} else if (soilMoistureValue >= moistureThreshold && digitalRead(RELAY_PIN) == LOW) {
digitalWrite(LED_PIN_GREEN, LOW);
digitalWrite(RELAY_PIN, HIGH);
Blynk.virtualWrite(V3, LOW); // Deactivate relay
stopWatering();
}
// LED indicator for watering status
// if (soilMoistureValue < moistureThreshold) {
// digitalWrite(LED_PIN_GREEN, HIGH);
// } else {
// digitalWrite(LED_PIN_GREEN, LOW);
// }
// Add a small delay to avoid overwhelming the ESP32
delay(2000);
}
// // 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();
// }
// #define BLYNK_PRINT Serial
// #define BLYNK_TEMPLATE_ID "TMPL4UHNFFAtK"
// #define BLYNK_TEMPLATE_NAME "Smart Irrigation System"
// #define BLYNK_AUTH_TOKEN "ZvwoCJIF3vdfntf_Cf0DU5G8pcDtp6zY"
// #include <LiquidCrystal_I2C.h>
// #include <DHT_U.h>
// #include <ESP32Servo.h>
// #include <WiFi.h>
// #include <WiFiClient.h>
// #include <BlynkSimpleEsp32.h>
// // 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
// #define POT_PIN 32 // Potentiometer pin
// // Threshold for soil moisture (adjust as necessary)
// const int moistureThreshold = 2000;
// // Components
// DHT dht(DHTPIN, DHTTYPE);
// LiquidCrystal_I2C lcd(0x27, 16, 2);
// Servo waterPump;
// // Blynk Virtual Pins
// #define VPIN_TEMP V2
// #define VPIN_HUMIDITY V0
// #define VPIN_SOIL_MOISTURE V1
// #define VPIN_RELAY V3
// // System state variable
// bool systemEnabled = false;
// int potThreshold = 2000; // Adjust based on the range of the potentiometer
// 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(RELAY_PIN, OUTPUT);
// pinMode(LED_PIN, OUTPUT);
// pinMode(POT_PIN, INPUT); // Potentiometer pin
// // Deactivate relay initially
// digitalWrite(RELAY_PIN, HIGH);
// // Connect to WiFi and Blynk
// Blynk.begin(BLYNK_AUTH_TOKEN, "Wokwi-GUEST", "");
// }
// 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...");
// }
// BLYNK_WRITE(VPIN_RELAY) {
// int relayState = param.asInt(); // Get the value from the app (0 or 1)
// if (relayState == 1) {
// systemEnabled = true;
// lcd.setCursor(0, 0);
// } else {
// systemEnabled = false;
// stopWatering(); // Ensure the servo is off when the system is off
// lcd.setCursor(0, 0);
// }
// }
// void loop() {
// Blynk.run(); // Run Blynk process
// // Read the potentiometer value
// int potValue = analogRead(POT_PIN);
// // Determine system state based on the potentiometer value and Blynk switch
// if (systemEnabled && potValue > potThreshold) {
// systemEnabled = true;
// } else if (potValue <= potThreshold) {
// systemEnabled = false;
// }
// if (systemEnabled) {
// // 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("%");
// // Send sensor readings to Blynk
// Blynk.virtualWrite(VPIN_TEMP, temperature);
// Blynk.virtualWrite(VPIN_HUMIDITY, humidity);
// Blynk.virtualWrite(VPIN_SOIL_MOISTURE, soilMoistureValue);
// // 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(2000);
// } else {
// // If the system is off, turn off the servo and LED
// stopWatering();
// digitalWrite(LED_PIN, LOW);
// digitalWrite(RELAY_PIN, HIGH); // Ensure the relay is deactivated
// }
// }
// #define BLYNK_PRINT Serial
// #define BLYNK_TEMPLATE_ID "TMPL4UHNFFAtK"
// #define BLYNK_TEMPLATE_NAME "Smart Irrigation System"
// #define BLYNK_AUTH_TOKEN "ZvwoCJIF3vdfntf_Cf0DU5G8pcDtp6zY"
// #include <LiquidCrystal_I2C.h>
// #include <DHT_U.h>
// #include <ESP32Servo.h>
// #include <WiFi.h>
// #include <WiFiClient.h>
// #include <BlynkSimpleEsp32.h>
// // 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;
// // Blynk Virtual Pins
// #define VPIN_TEMP V2
// #define VPIN_HUMIDITY V0
// #define VPIN_SOIL_MOISTURE V1
// #define VPIN_RELAY V3
// 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(RELAY_PIN, OUTPUT);
// pinMode(LED_PIN, OUTPUT);
// // Deactivate relay initially
// digitalWrite(RELAY_PIN, HIGH);
// // Connect to WiFi and Blynk
// Blynk.begin(BLYNK_AUTH_TOKEN, "Wokwi-GUEST", "");
// }
// void startWatering() {
// waterPump.write(90); // Assume 90 degrees is the pumping position
// lcd.setCursor(0, 1);
// lcd.print("Watering ON...");
// Blynk.virtualWrite(VPIN_RELAY, 1); // Update Blynk app
// }
// void stopWatering() {
// waterPump.write(0); // Assume 0 degrees is the off position
// lcd.setCursor(0, 1);
// lcd.print("Watering OFF...");
// Blynk.virtualWrite(VPIN_RELAY, 0); // Update Blynk app
// }
// void loop() {
// Blynk.run(); // Run Blynk process
// // 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("%");
// // Send sensor readings to Blynk
// Blynk.virtualWrite(VPIN_TEMP, temperature);
// Blynk.virtualWrite(VPIN_HUMIDITY, humidity);
// Blynk.virtualWrite(VPIN_SOIL_MOISTURE, soilMoistureValue);
// // 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(2000);
// }
// #define BLYNK_PRINT Serial
// #define BLYNK_TEMPLATE_ID "TMPL4UHNFFAtK"
// #define BLYNK_TEMPLATE_NAME "Smart Irrigation System"
// #define BLYNK_AUTH_TOKEN "ZvwoCJIF3vdfntf_Cf0DU5G8pcDtp6zY"
// #include <LiquidCrystal_I2C.h>
// #include <DHT_U.h>
// #include <ESP32Servo.h>
// #include <WiFi.h>
// #include <WiFiClient.h>
// #include <BlynkSimpleEsp32.h>
// // 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;
// 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(RELAY_PIN, OUTPUT);
// pinMode(LED_PIN, OUTPUT);
// // Deactivate relay initially
// digitalWrite(RELAY_PIN, HIGH);
// }
// 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("%");
// // 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(2000);
// }