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

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

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

Circuit Documentation

Summary of the Circuit

This circuit is designed to monitor environmental parameters such as soil moisture, light intensity, temperature, and humidity. It utilizes a set of sensors including a SparkFun Soil Moisture Sensor, an LDR Module, and a DHT22 sensor for temperature and humidity readings. The ESP32 microcontroller is the central processing unit that reads data from the sensors, processes it, and can send it to a server via WiFi. A 12V single-channel relay is used to control a 5V mini water pump, which can be activated based on the sensor readings. Power is supplied by a 3.3V battery connected to the ESP32 and a 9V battery for the water pump.

Component List

DHT22

12V Single Channel Relay

5V Mini Water Pump

Battery 9V

3.3V Battery

SparkFun Soil Moisture Sensor

ESP32 (30 pin)

LDR Module 3 pin

Wiring Details

DHT22

12V Single Channel Relay

5V Mini Water Pump

Battery 9V

3.3V Battery

SparkFun Soil Moisture Sensor

ESP32 (30 pin)

LDR Module 3 pin

Documented Code

#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>

// Define pins and constants
#define SOIL_MOISTURE_PIN 34  // Analog pin for soil moisture sensor
#define LIGHT_SENSOR_PIN 35   // Analog pin for light sensor
#define DHT_PIN 2             // Digital pin for DHT22 sensor
#define DHT_TYPE DHT22        // Define the DHT sensor type

// WiFi credentials
const char* ssid = "your_SSID";     // Replace with your WiFi SSID
const char* password = "your_PASSWORD";  // Replace with your WiFi password

DHT dht(DHT_PIN, DHT_TYPE);  // Initialize DHT sensor

void setup() {
  Serial.begin(115200);
  dht.begin();  // Start DHT sensor

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  // Read soil moisture
  int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
  float soilMoisturePercent = map(soilMoistureValue, 4095, 0, 0, 100);
  
  // Read light intensity
  int lightValue = analogRead(LIGHT_SENSOR_PIN);
  float lightPercent = map(lightValue, 4095, 0, 0, 100);
  
  // Read temperature and humidity from DHT22
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor");
    return;
  }

  // Display readings on Serial Monitor
  Serial.print("Soil Moisture: ");
  Serial.print(soilMoisturePercent);
  Serial.println("%");

  Serial.print("Light Intensity: ");
  Serial.print(lightPercent);
  Serial.println("%");

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println("°C");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println("%");

  // Send data to a server or cloud (optional)
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String serverPath = "http://your-server.com/data";
    String data = "moisture=" + String(soilMoisturePercent) + "&light=" + String(lightPercent) + "&temperature=" + String(temperature) + "&humidity=" + String(humidity);

    http.begin(serverPath.c_str());
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    int httpResponseCode = http.POST(data);

    if (httpResponseCode > 0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
    } else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }

  // Delay before taking the next set of readings
  delay(2000);
}

This code is designed to run on the ESP32 microcontroller. It initializes the sensors, connects to WiFi, reads sensor data, and outputs the readings to the Serial Monitor. It also includes an optional section to send the data to a server using HTTP POST requests. The pin definitions at the top of the code correspond to the wiring details provided in the previous section.