//ESP32 smart irrigation code
#define BLYNK_TEMPLATE_ID "TMPL6GCpyjb3V"
#define BLYNK_TEMPLATE_NAME "Irigation System"
#define BLYNK_AUTH_TOKEN "Rvn8aXkijT3RJXxuNjXjDuJpBTV0j8HV"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include "DHT.h"
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const int soilMoisturePin = 35; // Analog pin for the soil moisture sensor
// const int waterLevelPin = 34; // Analog pin for the water level sensor
// Constants for soil moisture thresholds
const int SOIL_MOISTURE_LOW = 20; // Adjusted to match the 0-100 range
const int SOIL_MOISTURE_HIGH = 80; // Adjusted to match the 0-100 range
const int pumpPin = 5; // Change this to the actual pin connected to the DC pump on ESP32
int pumpState = LOW; // Initialize pumpState to LOW (off)
int pumpMode = 0; // 0 for automatic, 1 for manual
int waterLevel = 0; // Water level indicator value
unsigned long previousMillis = 0;
const unsigned long interval = 1000; // Interval in milliseconds
float mappedSoilMoisture = 0; // Declare mappedSoilMoisture as a global variable
void setup() {
Serial.begin(115200);
digitalWrite(pumpPin,LOW);
pinMode(pumpPin, OUTPUT);
dht.begin();
Blynk.begin(auth, ssid, pass);
Blynk.virtualWrite(V2, LOW);
Blynk.virtualWrite(V6, LOW); // Initialize the mode button to automatic mode
Blynk.virtualWrite(V5, LOW); // Initialize the pump button to OFF
Blynk.virtualWrite(V8, LOW); // Initialize the new feature button to OFF
delay(3000);
}
BLYNK_WRITE(V4) { // This function is called when the pump button state changes (Manual Control)
if (pumpMode == 1) { // Check if the mode is manual
pumpState = param.asInt(); // Read the state of the pump button (0 for OFF, 1 for ON)
digitalWrite(pumpPin, pumpState); // Turn the pump on or off based on button state
}
}
BLYNK_WRITE(V1) { // This function is called when the mode button state changes
pumpMode = param.asInt(); // Read the state of the mode button (0 for automatic, 1 for manual)
}
void loop() {
Blynk.run();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Read soil moisture or water level based on the flag
if (pumpMode == 0) {
int soilSensorValue = analogRead(soilMoisturePin);
Serial.println(soilSensorValue);
mappedSoilMoisture = map(soilSensorValue, 1023, 2750, 100, 0); // Update mappedSoilMoisture
autoControlPump(mappedSoilMoisture); // Automatically control the pump based on soil moisture if in automatic mode
}
// Read water level
// waterLevel = analogRead(waterLevelPin);
// Serial.println(waterLevel);
// if (waterLevel < 450) {
// waterLevel = 0; // Set water level to 0 if below a threshold
// } else {
// waterLevel = map(waterLevel, 1200, 1600, 0, 100); // Map water level to 0-100
// }
// Read and send sensor data
readAndSendSensorData();
Blynk.virtualWrite(V2, waterLevel); // Send water level to Blynk
}
}
void autoControlPump(float mappedSoilMoisture) {
if (waterLevel < 30 || waterLevel < 20) {
// Water level is too low, do not turn on the pump
pumpState = LOW;
} else if (mappedSoilMoisture <= SOIL_MOISTURE_LOW) {
// If soil moisture is below the threshold and water level is okay, turn the pump ON
pumpState = HIGH;
} else if (mappedSoilMoisture >= SOIL_MOISTURE_HIGH) {
// If soil moisture is above the threshold, turn the pump OFF
pumpState = LOW;
}
digitalWrite(pumpPin, pumpState); // Update the pump state
if (pumpMode == 0) {
Blynk.virtualWrite(V4, pumpState); // Update the pump button status on Blynk
}
}
void readAndSendSensorData() {
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
Blynk.virtualWrite(V0, temperature);
Serial.println(temperature);
} else {
Serial.println(F("Failed to read from DHT sensor!"));
}
if (pumpMode == 0) {
Blynk.virtualWrite(V3, mappedSoilMoisture); // Update mappedSoilMoisture on Blynk
}
}