I was looking for a method to keep the plants happy without spending too much money on irrigation systems. Home improvement stores have awesome systems but if you want to control them via a app it gets expensive fast.

So I challanged myself to do it on the cheap and automate it using MQTT.

Parts

The parts we're going to need

You'll need the following things:

Part Price What it does Magnet Valve 2.5$ Lets water through if powered Quick connectors 1/2'' or 3/4'' 2$ Connects the threaded valve to a water source or sprinkler Relay that can be controlled with 3.3V 0.5$ Switches power On/Off to the valve and lets water through or not Some Microcontroller. Raspberry Pi, NodeMCU (ESP8266) or Arduino. 2$ Controlling the relay. I'm going to use a NodeMCU Something that sprays out water. Eg. a sprinkler 3$ You know what this does MQTT Server Free We're going to control the valve using MQTT which is awesome and you can host it on a Raspberry Pi or in the cloud

Note: I don't link to any specific products because I want to encurage you to shop around and also I don't like the idea of referral links.

How it works

So the awesome thing about magnet valves is that they are normally closed and can work with gasses and fluids to up to 5 Bar pressure. If you apply 12V to the contacts, the valve opens and lets the fluid through.

So all we need to do is use a 12V power source and use the relay to power the pump when we need it.

Valve testing. One valve uses 300mA at 12V which is good for the cheap ones. I also tested a more expensive one that used more than 1 Amp. So I'll stick to the cheap ones for this project

How it works: If 12V are applied to the contacts, the liquid can flow. Current stops -> Water stops

Step 1: Connect the components

How to connect the components

Using these simple schematics we can set up the system to be controlled by the controller (in my case a NodeMCU). We'll flash the controller in the next step.

I connected the valves on one terminal and made soldered cables on them. Since the valves have no polarity you can easily connect them with a screw.

Connected valves

Soldered cables

As an enclosure I used a small electrical box. Not ideal but if I use it upside down, the contacts will stay dry

Step 2: Prepare the controller

So as I said I'm using a NodeMCU chip for this project.

>>> Get the code for irrigation.ino <<<

What this code does:

Connect to your Wifi and reconnects if connection is lost

Connect to your MQTT Server

Send "ALIVE" message to status/valvecontrol when the device comes online and set it to "DEAD" via MQTT last will when it goes offline

when the device comes online and set it to "DEAD" via MQTT last will when it goes offline Listen on garden/valve1 and garden/valve2 for "ON" or "OFF" messages to turn the valves off an on

Step 3: Control it via an App

Do you know what's awesome about MQTT? Smartphones can control MQTT devices without you needing to code anything. Just download some MQTT app, set up the connection to your MQTT server and add two switches.

Add a SWITCH panel

And set it up like this. Topic is important and it's a good idea to have the message retained (meaning if the device loses power and reboots, it will get the last state)

Perfect, you should now be able to switch both valves on and off

Last step: Actually use it somewhere

My fiancé asked me if we could automate irrigation of the greenhouse so this was the perfect test for my project.

The greenhouse in question. Until now it needed manual irrigation

I installed the valves inside of the greenhouse

And connected one of them dripping irrigation systems I bought on Ali express

Now we can start and stop the dripping system from the phone

It waters all the plants in the greenhouse at once

Second valve is controlling a different sprinkler in the garden

Where to go from here

I can now control the irrigation from my phone and since I set up my MQTT server securely I can even start watering the plants when I'm not at home. That's awesome but doesn't help me from forgetting to actually do it.

So this can easily be automated with Python and if you want to go all-in you can even distribute moisture sensors in the soil so you can only irrigate when the plants really need it.