Smart WiFi Controlled Irrigation System Using Home Assistant and ESPHome
by RudSpring in Circuits > Electronics
9878 Views, 49 Favorites, 0 Comments
Smart WiFi Controlled Irrigation System Using Home Assistant and ESPHome


As a lazy gardener and electronica hobbyist I need my plants to grow without the hassle of filling watering cans every evening, such that I can sit on my deck in the sun and enjoy the flowers instead.
Situation sketch
I don't have a backyard, instead I have a deck located above a small pond that holds enough water to feed all my plants. The only thing I need is a solution that takes the water from the pond below and distribute it over a few plant boxes. I have an ever growing home automation setup using Home Assistant. Many sensors, devices and automation's are connected, including a few custom made ESPHome powered devices.
- Water is very nearby in a pond only 60cm below the deck.
- Plants are located in 5 plant boxes mounted at the railing, 110cm above the deck.
- Fully functional Home Assistant setup (running on a Arch Linux based Raspberry Pi 4)
- Power outlet is available at the deck.
Early learning's
Last year I created an initial setup using ESP32. One of the three major shortcomings was an unstable WiFi connection. The WiFi strength is not that strong at the deck and the pump had difficulties reconnecting each time it lost the connection. I discovered that my ESPHome powered devices have a much more robust WiFi connection. The second shortcoming was the uneven water pressure needed for plant boxes at railing versus the plant boxes on the deck itself. The third issue was the limited amount of water a single pump could handle. Therefor the new solution is using two independent water pumps.
User requirements
These are the set of functions that defines the minimum viable product.
- Automatically pump water from the pond to the five flower boxes.
- Two separate pumps, one for flower boxes at railing, other for boxes on the deck.
- Only water the plants at fixed times in morning and evening for short period of time.
- Only water the plants on warm and sunny days.
- Set automation in Home Assistant, depending on outside temperature and weather forecast.
Future requirements
- Solar powered solution, as watering is only needed at warm and sunny days.
- Add a rain sensor and disable automation's when it is raining.
- Add a moisture sensors in the plant boxes to determine the more exact amount of water to use
Supplies
Prerequisites
- Home Assistant installation, including ESPHome integration and weather intergration.
- ESPHome installation at laptop or computer
Tools
- Computer running ESPHome
- Soldering iron
- Hot-glue
- 3D printer (optional)
Bill of material (+/- € 50,-)
- ESP32, ESP-Wroom-32 development board (€3,82)
- Power supply, 12 Volt, 2 Amp (€7,42)
- Power connector male/female (€1,00)
- L298N driver board (€1,13)
- 12 Volt self-priming water pump (2x €8,95)
- 8mm water filter (2x €1,53)
- Micro drip irrigation system, including 10m hose (€11,18)
- ABS plastic housing (€8,95)
I order everything at Aliexpress, except for the 12V self-priming pump which i got from Ben's Electronics.
Electrical Setup

A) The Power Supply delivers 12 Volts to the Motor Driver.
B) The Motor Driver delivers 5 Volts to the ESP32.
C) The Motor Driver delivers power to both Water Pumps.
D) The ESP32 controls Motor Driver through two gpio data pins
Motor Driver Board (L298N)

5 Volt output
The jumper nearby the 12V input must be placed to let the driver board generate 5 volts. (If the jumper is removed, these 5 volts logic must be powered using an external 5 volts power source.) With the jumper in place the L298N motor driver board can then directly power the ESP32 also.
Input pins
The Motor Driver has 6 digital inputs, 3 per motor. The ENA and ENB are the pulse width modulation (PWM) inputs that determine the speed of the motor. The inputs In1/In2 and In3/In4 steer the direction of the motor. I started wiring all these input pins to the ESP32, but these inputs are not going to be changed. Once the motor direction is checked the In1, In2, In3 and In4 inputs can be hardwired to 5V/Gnd respectively.
- In1/In3 = high, In2/In4 = low -> Forward direction (What we need for the pumps)
- In1/In3 = low, In2/In4 = high -> Reverse direction
- In1/In3 = low, In2/In4 = low -> Active brake
- In1/In3 = high, In2/In4 = high -> Active brake
Jumper on ENA and ENB
ENA and ENB are per default connected to 5 volts via two jumpers. When pwm is used to set the speed of the motors, these jumpers have to be removed.
Output pins
Both motors connected to the motor output pins of the driver board.
Self-priming Diaphragm Pump (YX DC12V 365 - 13170)


The first pump I tried was a simple and cheap AD20P-1230C 12 volts brushless DC pump (see image at left). But this pump was not capable of sucking the water from below. So I learned that I needed a so-called 'self-priming' diaphragm pump to do so. That's when I ordered this YX DC12V (see image at right).
The main parameters of this pump include:
- Model: R365 DC micro diaphragm pump
- Working voltage: DC 12V
- No load current: 0.23A
- Maximum flow: 2-3 liters/minute
- Outlet maximum pressure: 1-2.5 kg
- Maximum lift: 1-2.5 meters
- Maximum suction: 2 meters
So it can suck the water from 2 meters below, which is more than sufficient, as in my case I only require to bring the water about 90cm up. Then it has to push the water up another meter. Anyway, this pump satisfies the technical requirements I have.
ESP32 Wiring

ESP32
The ESP32 is a cheap, yet powerful chip by Espressif Systems. I use it a lot in all kind of simple wireless automation projects. Here are a few reasons why I moved from using Arduino to ESP32:
- The ESP32 has many input and output pins.
- The ESP32 has integrated both Bluetooth and WiFi for wireless data transfer
- In combination with ESPHome, the ESP32 can be integrated in Home Assistant with the least amount of code.
- ESPHome features over-the-air updates
- ESPHome features simple local webserver
Connections
- The 5 Volts power is connected from the motor driver board to the Vin pin of the ESP32
- Ground is connected to Gnd
- The gpio pin D14 is connected to ENA for PWM of motor A
- The gpio pin D32 is connected to ENB for PWM of motor B
In some photo's you will see that I have connected the In1, In2, In3 and In4 pins from the motor driver to the pins D27, D26, D25 and D33 of the ESP32 as well. Bit since they are not changed by the program I have connected these pins to 5 Volt (In1/In3) and ground (In2/In4) permanently in the final version.
Mechanical Setup




Needles to say that the whole setup needs to be housed in a water and weather proof housing. I have designed a supporting structure that holds all components, and that can be easily mounted in a standard ABS housing. These 3D printed supports holds the pumps (2x), the motor driver and one for the ESP32.
The wet part and the dry part
I have separated the water part from the electronic part as much as possible to avoid any short circuit due to water leakage. The bottom part of the housing is the 'wet' part. There are four openings at the bottom to connect the four hoses. The top part is the 'dry' part that houses the electronics. The power connector for the 12V power supply is located at the side.
Uploading the ESPHome Code
In esphome: the name of the device is defined, as well as the exact ESP board being used. If the In1, In2, In3 and In4 pins of the motor driver board are connected directly to 5V (In1, In3) and ground (In2, In4), the entire on_boot entry can be removed.
In wifi: the network credentials have to be given. If the system cannot connect it will act as a wifi acces point with the credentials given under ap:. The captive_portal: option enables this WiFi fallback. I have chosen for a static IP address just because I have given all my ESPHome device a static address as it's supposed to connect quicker.
The api: entry ensures that Home Assistant will find the device.
The web_server: entry creates a web server through which the device can be controlled directly.
The ota: entry allows for updates via the WiFi network instead of through usb connection.
In the output: entry the two motor control pins are defined. The ledc platform is chosen for PWM control with a frequency of 1000Hz. If the In1, In2, In3 and In4 pins of the motor driver board are connected directly to 5V (In1, In3) and ground (In2, In4), these definitions are not used and can be removed.
In the light: entry two light switch controls are defined. These are the switches as how they will appear in Home Assistant. Monochromatic allows to set the speed of the pump between 0% and 100%.
Finally the sensor: entry defines the reporting of the WiFi signal strength.
#
# Smart WiFi Controlled Irrigation System Using Home Assistant and ESPHome
#
# Generic ESP32, name, platform and initial GPIO pin states
esphome:
name: waterpump
platform: ESP32
board: esp-wrover-kit
# The on_boot entry can be removed when pins In1, In2, In3 and In4 are directly
# connected to 5V (In1, In3) and Gnd (In2, In4) respectively.
on_boot:
then:
- output.turn_on: gpio_in1
- output.turn_off: gpio_in2
- output.turn_on: gpio_in3
- output.turn_off: gpio_in4
# WiFi, connect to network as defined in secrets.yaml file.
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_pass
# I have preference for fixed IP address, comment out for dynamic IP address
manual_ip:
static_ip: 192.168.1.95
gateway: 192.168.1.1
subnet: 255.255.255.0
# fast_connect: true
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Waterpump Fallback Hotspot"
password: "Waterpump"
# Enable Captive Portal in case connection to defined WiFi network fails.
captive_portal:
# Enable logging
logger:
# Enable Home Assistant API, required to auto discover new ESPHome devices
api:
# Enable local web server for direct connection outside Home Assistant
web_server:
port: 80
# Enable over-the-air updates
ota:
# Define the output pins connected to the Motor Driver board
# The entries for pins In1, In2, In3 and In4 can be removed when directly
# connected to 5V (In1, In3) and Gnd (In2, In4) respectively.
output:
# Motor 1 - ENA, Enable A (= PWM)
- platform: ledc
pin: GPIO14
id: gpio_m1
frequency: "1000Hz"
# Motor 1 - IN1
- platform: gpio
pin: GPIO27
id: gpio_in1
# Motor 1 - IN2
- platform: gpio
pin: GPIO26
id: gpio_in2
# Motor 2 - IN3
- platform: gpio
pin: GPIO25
id: gpio_in3
# Motor 2 - IN4
- platform: gpio
pin: GPIO33
id: gpio_in4
# Motor 2 - ENB, Enable B (= PWM)
- platform: ledc
pin: GPIO32
id: gpio_m2
frequency: "1000Hz"
# Define two light switches in HA that control both water pumps
light:
# Motor 1
- platform: monochromatic
output: gpio_m1
name: "Water pump 1"
# Motor 1
- platform: monochromatic
output: gpio_m2
name: "Water pump 2"
# Define a WiFi strength sensor to indicate the WiFi strength of connection
sensor:
- platform: wifi_signal
name: "Waterpump_WiFi_Signal"
update_interval: 60s
filters:
- median:
window_size: 7
send_every: 4
send_first_at: 3
Setting Up the Pump in Home Assistant

The ESPHome integration automatically discovers new devices when they come alive. The automation is setup such that it runs every morning at 7:00 and every evening at sunset. But only when the sky is clear, cluody or partly-cloudy.
automation:
- alias: 'waterpump automation' trigger: # Every morning at 7:00 - platform: time at: "07:00:00" # every evening at sun set - platform: sun event: sunset condition: # Only run when sky is clear, cloudy or partly-cloudy - condition: or conditions: - condition: state entity_id: weather.waterfront state: 'clear' - condition: state entity_id: weather.waterfront state: 'cloudy' - condition: state entity_id: weather.waterfront state: 'partlycloudy' action: # Switch the pump on, and let it run for 3 minutes - service: light.turn_on entity_id: - light.water_pump_1 - light.water_pump_2 - delay: minutes: 3 - service: light.turn_off entity_id: - light.water_pump_1 - light.water_pump_2 # Just to be sure, switch it off once more - delay: seconds: 30 - service: light.turn_off entity_id: - light.water_pump_1 - light.water_pump_2