Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP8266-Based Smart Irrigation System with Soil Moisture Sensing and Wi-Fi Connectivity

View and Edit Project In Cirkit DesignerImage of ESP8266-Based Smart Irrigation System with Soil Moisture Sensing and Wi-Fi Connectivity

Circuit Documentation

Summary

This circuit is designed to monitor soil moisture and temperature/humidity levels, and control a water pump based on the soil moisture data. It uses an ESP-8266 microcontroller to read sensor data, display information on an LCD, and communicate with the Blynk platform for remote monitoring and control. The circuit includes a DHT11 temperature and humidity sensor, a SparkFun Soil Moisture Sensor, a water pump, a relay module to control the pump, and a 9V battery to power the system.

Component List

ESP-8266 Controller

DHT11

Water Pump

Relay Module 5v-30v

9V Battery

SparkFun Soil Moisture Sensor

LCD I2C Display

Wiring Details

ESP-8266 Controller

DHT11

Water Pump

Relay Module 5v-30v

9V Battery

SparkFun Soil Moisture Sensor

LCD I2C Display

Documented Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// Set up the LCD with I2C address (0x27), 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Wi-Fi and Blynk Credentials
char auth[] = "q9Nmm616mjuMJTLeRs9T1PsFx0oA8-PF"; // Your Blynk token
char ssid[] = "Aishe"; // Your Wi-Fi SSID
char pass[] = "@ishe76543"; // Your Wi-Fi Password

BlynkTimer timer;
bool Relay = 0;

// DHT11 Sensor Setup
#define DHTPIN D3 // DHT11 connected to GPIO D3
#define DHTTYPE DHT11 // DHT11 sensor

DHT dht(DHTPIN, DHTTYPE);

// Pin Definitions
#define sensor A0 // Soil moisture sensor connected to A0
#define waterPump D5 // Water pump connected to GPIO D5

void setup() {
  Serial.begin(9600); // Initialize Serial for debugging
  lcd.begin(); // Initialize the LCD
  lcd.backlight(); // Turn on backlight for the LCD

  // Debugging messages for Wi-Fi and Blynk connection
  Serial.println("Starting setup...");

  // Initialize water pump pin
  pinMode(waterPump, OUTPUT);
  digitalWrite(waterPump, HIGH); // Turn off the pump initially

  // Display startup message
  lcd.setCursor(1, 0);
  lcd.print("System Loading");

  // Connect to Wi-Fi
  Serial.println("Connecting to Wi-Fi...");
  lcd.setCursor(0, 1);
  lcd.print("Wi-Fi Connecting");

  WiFi.begin(ssid, pass);
  int wifiAttempts = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    wifiAttempts++;
    if (wifiAttempts > 20) {
      Serial.println("\nFailed to connect to Wi-Fi.");
      lcd.setCursor(0, 1);
      lcd.print("Wi-Fi Fail    ");
      return; // Stop further execution if Wi-Fi fails
    }
  }

  Serial.println("\nWi-Fi Connected!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Wi-Fi Connected");

  // Connect to Blynk server
  Serial.println("Connecting to Blynk...");
  lcd.setCursor(0, 1);
  lcd.print("Blynk Connecting");

  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80); // Connect to Blynk server
  int blynkAttempts = 0;
  while (!Blynk.connected()) {
    delay(500);
    Serial.print(".");
    blynkAttempts++;
    if (blynkAttempts > 20) {
      Serial.println("\nFailed to connect to Blynk.");
      lcd.setCursor(0, 1);
      lcd.print("Blynk Fail    ");
      return; // Stop further execution if Blynk fails
    }
  }

  Serial.println("\nBlynk Connected!");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Blynk Connected");

  // Initialize DHT sensor
  dht.begin();

  // Set up timer intervals for sensor readings
  timer.setInterval(2000L, DHT11sensor); // Read DHT sensor every 2 seconds
  timer.setInterval(1000L, soilMoistureSensor); // Read soil moisture every second
}

// Handle button value from Blynk (for controlling water pump)
BLYNK_WRITE(V7) {
  Relay = param.asInt();
  if (Relay == 1) {
    digitalWrite(waterPump, LOW); // Turn ON water pump
    Serial.println("Relay activated");
  } else {
    digitalWrite(waterPump, HIGH); // Turn OFF water pump
    Serial.println("Relay deactivated");
  }
}

// Average soil moisture sensor readings for stability
int getAverageSoilMoisture() {
  int total = 0;
  const int numReadings = 10;
  for (int i = 0; i < numReadings; i++) {
    total += analogRead(sensor);
    delay(10);
  }
  return total / numReadings;
}

// Function to handle soil moisture readings
void soilMoistureSensor() {
  int rawValue = getAverageSoilMoisture(); // Get averaged raw value from the sensor
  Serial.print("Soil Moisture Raw Value: ");
  Serial.println(rawValue);

  // Map the raw value to percentage (0 = wet, 100 = dry)
  int percentage = map(rawValue, 1023, 0, 0, 100); // Adjust map range as per calibration
  percentage = constrain(percentage, 0, 100); // Ensure value stays within bounds

  // Send moisture value to Blynk and LCD
  Blynk.virtualWrite(V8, percentage); // Send to Blynk Virtual Pin V8
  lcd.setCursor(0, 0); // Set cursor to first row, first column