Smart IoT Irrigation System Using Pico W, Wi-Fi, and MQTT

 




🌾 Smart IoT Irrigation System Using Pico W, Wi-Fi, and MQTT

🧭 1. System Overview

This system is a distributed IoT irrigation control network designed for agricultural automation. It uses zone-based irrigation, sensor feedback, and MQTT-based communication between field devices and a central server.

At the field level, medium-pressure sprinklers are arranged in a grid:

  • Spacing: 12 m × 12 m

  • Coverage per sprinkler: ~144 m²

  • 1 acre ≈ 4047 m²

  • Total sprinklers per acre: ~28–30

  • Practical range: 25–35 sprinklers per acre

⚠️ Key design principle

Sprinklers are not controlled individually. Instead, they are grouped into irrigation zones.


💧 2. Zone-Based Irrigation Concept

To simplify control and reduce hardware complexity:

  • 1 zone = 5–10 sprinklers

  • 1 acre = ~3–6 zones

Each zone acts as a single controllable unit.

Each zone contains:

  • 1 solenoid valve (controls water flow)

  • 1 Pico W controller (decision + communication node)

  • Sensors (for environmental monitoring)


🧠 3. System Architecture

The system follows a layered IoT architecture:

[Sensors + Valve]
        ↓
 Raspberry Pi Pico W (Zone Controller)
        ↓ Wi-Fi
 MQTT Broker (Linux Server)
        ↓
 Control Logic + Dashboard

Role of each layer

Pico W (field device):

  • Reads sensor data

  • Controls solenoid valve

  • Sends telemetry to server

  • Receives irrigation commands

Server:

  • Runs automation logic

  • Stores data

  • Sends ON/OFF commands


📡 4. MQTT Communication Model

The system uses MQTT (Message Queuing Telemetry Transport), a lightweight IoT protocol.

📤 Sensor data (Pico → Server)

  • farm/zone1/moisture

  • farm/zone1/temperature

  • farm/zone1/flow

  • farm/zone1/rain

📥 Control commands (Server → Pico)

  • farm/zone1/valve

Example:

  • ON → open valve

  • OFF → close valve


⚙️ 5. Hardware Configuration (Per Zone)

Each irrigation zone uses the following hardware:

📥 Inputs (Sensors)

  • Soil moisture sensor → Analog input (ADC)

  • Temperature sensor (DHT11/DHT22) → Digital GPIO

  • Flow meter → Pulse interrupt input

  • Rain sensor → Digital input

📤 Output (Actuator)

  • Relay module → controls solenoid valve


🧰 6. MicroPython Firmware (Pico W + MQTT)

This is a functional baseline implementation for a single irrigation zone.

📌 Assumptions

  • MQTT broker runs on a Linux server (Mosquitto)

  • Wi-Fi network is available

  • Sensors are connected as described above


📜 Complete MicroPython Code

import network
import time
from machine import Pin, ADC
from umqtt.simple import MQTTClient
import dht

# ---------------- WIFI CONFIG ----------------
WIFI_SSID = "YOUR_WIFI"
WIFI_PASS = "YOUR_PASSWORD"

def connect_wifi():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(WIFI_SSID, WIFI_PASS)

    while not wlan.isconnected():
        print("Connecting WiFi...")
        time.sleep(1)

    print("WiFi Connected:", wlan.ifconfig())

connect_wifi()

# ---------------- MQTT CONFIG ----------------
MQTT_BROKER = "192.168.1.100"
CLIENT_ID = "pico_zone1"

TOPIC_MOISTURE = b"farm/zone1/moisture"
TOPIC_TEMP = b"farm/zone1/temperature"
TOPIC_FLOW = b"farm/zone1/flow"
TOPIC_RAIN = b"farm/zone1/rain"
TOPIC_VALVE = b"farm/zone1/valve"

# Relay (solenoid valve control)
valve = Pin(15, Pin.OUT)

# MQTT callback for commands
def mqtt_callback(topic, msg):
    if topic == TOPIC_VALVE:
        if msg == b"ON":
            valve.value(1)
        else:
            valve.value(0)

client = MQTTClient(CLIENT_ID, MQTT_BROKER)
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(TOPIC_VALVE)

# ---------------- SENSORS ----------------
moisture = ADC(26)
dht_sensor = dht.DHT22(Pin(16))
rain = Pin(17, Pin.IN)

flow_count = 0

def flow_interrupt(pin):
    global flow_count
    flow_count += 1

flow_sensor = Pin(14, Pin.IN, Pin.PULL_UP)
flow_sensor.irq(trigger=Pin.IRQ_FALLING, handler=flow_interrupt)

# ---------------- MAIN LOOP ----------------
while True:
    client.check_msg()

    # Soil moisture conversion
    raw = moisture.read_u16()
    soil_percent = 100 - int((raw / 65535) * 100)

    # Temperature
    try:
        dht_sensor.measure()
        temp = dht_sensor.temperature()
    except:
        temp = None

    # Rain detection
    rain_status = "RAIN" if rain.value() == 0 else "NO_RAIN"

    # Flow measurement
    flow_rate = flow_count
    flow_count = 0

    # Publish sensor data
    client.publish(TOPIC_MOISTURE, str(soil_percent))
    if temp is not None:
        client.publish(TOPIC_TEMP, str(temp))

    client.publish(TOPIC_FLOW, str(flow_rate))
    client.publish(TOPIC_RAIN, rain_status)

    print("Moisture:", soil_percent, "Temp:", temp, "Rain:", rain_status)

    time.sleep(10)

🖥️ 7. MQTT Broker Setup (Linux Server)

Install and start Mosquitto:

sudo apt update
sudo apt install mosquitto mosquitto-clients
sudo systemctl enable mosquitto
sudo systemctl start mosquitto

Test incoming data:

mosquitto_sub -t "farm/#"

🧠 8. Automation Logic (Server Side)

The server runs irrigation rules such as:

IF soil_moisture < 30%
AND rain_status = NO_RAIN
THEN publish "ON" to farm/zone1/valve

Command execution:

mosquitto_pub -t farm/zone1/valve -m ON

🌾 9. Scaling Model (Per Acre)

For a typical 1-acre field:

ComponentQuantity
Sprinklers28–30
Zones3–6
Pico W devices3–6
Soil sensors3–6
Flow meters3–6
Valves3–6

🔥 10. System Advantages

  • Fully automated irrigation

  • Water-efficient (sensor-based control)

  • Real-time monitoring

  • Remote control via MQTT

  • Easily scalable to large farms (100+ acres)

  • Fault detection (leaks, dry zones, blocked flow)


🧠 11. Conceptual Model

  • Sprinklers → water delivery units

  • Valves → physical ON/OFF switches

  • Pico W → zone-level control brain

  • MQTT → communication protocol

  • Server → decision-making system



Comments

Popular posts from this blog

Differences Between Ubuntu 24.04.2 LTS and Ubuntu 25.04

Kapardak Bhasma: A Comprehensive Review and use

Vanga Bhasma: A Traditional Ayurvedic Metallic Formulation and use