An excellent example of home automation is a self-operating irrigation system. Whether for a large garden or a small flower pot, you never want to do it manually. Fortunately, creating a system that handles this task autonomously with additional remote control and telemetry capabilities is straightforward with unicontrol when coupled with a standard ESP8266-based relay module.
Consider the following target:
- Four independent lines (a common strategy for larger areas due to limited water pressure)
- The system is programmed to automatically open each line successively for 10 minutes starting at 5:00 AM, but only if certain conditions are met:
- There has been no recent rainfall
- The soil moisture is below a predetermined threshold
- Additionally, the system supports a physical button for manual control. Pressing this button will:
- Initiate the complete 4-line cycle if it is not running
- End the cycle if the last line is active
- Proceed to the next line otherwise
The ESP12F-X4 relay module introduced here, in conjunction with suitable sensors, can effectively perform this task.
Budget
Item | Pcs | Unit price (€) |
ayatec unicontrol mini | 1 | 8 |
ESP12F 4-Channel relay module | 1 | 8 – 20 |
Soil moisture sensor (analog output) | 1 | 1 |
Infrared obstacle sensor module | 1 | 1 – 2 |
Jumper cables | 1 | < 1 |
Resistor – 1x10k , 1x 100k , 1x 220k | 1 | < 1 |
Rain sensor (logical output) | 1 | 5 – 40 |
0.96” TFT Display (optional) | 1 | 1 – 2 |
Push button | 1 | < 1 |
Depending on the chosen parts, you may expect cost of this setup in the range of €25 – €70.
Module Assembly
Normally, you should avoid using D3
and D4
pins as inputs if they can unintentionally assume a LOW
state at boot, as this would cause a boot failure. A push button with a normally open state, for example, does not violate this.
When printing the board’s enclosure, do not forget to open the pre-cut holes for the connector and, possibly, the display. Check the dedicated article for more details.
Start by attaching the connector and the optional display to the upper part of the enclosure. Using a glue gun is usually sufficient.
The most significant challenge of this assembly, however, is managing the inputs. Given that the X4 relay module lacks ready-to-use input connectors or a voltage divider for the analog input, these elements need to be prepared ahead of the final assembly.
Analog input:
As per the schematics and accompanying images, solder the 10k
, 100k
, and 220k
resistors to the ADC
, 3V3
, and GND
pins, respectively.
To improve sturdiness and prevent unwanted contacts, apply a glue gun or a suitable type of resin.
You may notice four resistors soldered in the gallery, contrary to the three in the schematic. This is because a 220k
resistor was unavailable, so I used a series composed of a 200k and a 20k resistor instead.
Now, connect the sensor’s legs to Pin 1 (A0
) and Pin 4 (GND
) of the connector, respectively. The direction of the sensor is not relevant.
Digital inputs:
The remaining two pins on the external connector are linked to the digital inputs D3
(IO0) and D7
(IO13). Though their connection is straightforward, you may need to solder their respective cables directly onto the connector, as you might not find enough space to simply plug in the jumper cables.
As the button is present on the D3
pin, it is critical that you use a button with a normally open state. Otherwise, the relay module will not boot if the button is unpressed following any restart. Connect one leg of the button to Pin 2 of the connector (D3
) and the other to Pin 4 of the connector (GND
). Direction is not relevant.
Pin 3 of the connector (D7
) expects one leg of the rain sensor (with the other leg on connector’s Pin 4). The sensor is expected to close the circuit in case of recent rain and remain open otherwise (the NO type). If your sensor’s logic is opposite, you will need to reflect this in the setup of the process Autostart (see below). Again, the direction of the sensor should not be relevant. You should, however, check your sensor’s manual for confirmation.
Display:
This is as simple as it gets. The display only requires four F-F jumper cables, connecting the following display and on-board pins:
VCC
to3V3
or5V
GND
toGND
SCL
toIO5
SDA
toIO4
Setup
The following sections will assume your ESP8266 has been successfully flashed and accessed. For instructions on flashing the board with the unicontrol software, you can refer to the introductory tutorial specifically written for this module. Afterward, you can follow the First boot tutorial to set up the Wi-Fi and establish a connection to the device.
If you do not wish to manually set up your device, you can download the backup files with settings used in this tutorial from the link below:
For guidance on working with the backup files, please refer to our user guide. Please, make sure that you import all files.
Peripherals
Once you’ve accessed the module via the Interface, navigate to the Peripheral menu and ensure that:
D3
andD7
are set asLogical IN
A0
is set asAnalog
D0
,D5
,D6
, andD8
are set asGeneral OUT
(relays)
Having a Display on D1
and D2
is optional, but it greatly enhances the user experience.
D0
is HIGH
at boot, meaning the connected relay will close for a brief moment during each reboot. If this is not acceptable, you may need to use a different pin.
Also, don’t forget to Edit the A0’s Mapping if needed. This will determine the translation of the sensor’s output voltage into values used by the processes. For detailed information on how to accomplish this, refer to our user guide.
Memory slot
To keep track of the cycle so that the button functions properly, we need to use a Memory slot that will determine the course of action for other processes. The slot Mem 1 will assume following values:
- 0 – the cycle is off
- 1 – the cycle is on with the active first line
- 2 – the cycle is on with the active second line
- 3 – the cycle is on with the active third line
- 4 – the cycle is on with the active fourth line
For this purpose, create two processes:
- Process MemINC:
- Primary output:
Mem 1
- Action:
Increase
- Mem value:
1
- Primary output:
- Process MemRST:
- Primary output:
Mem 1
- Action:
Clear
- Primary output:
These two processes will be invoked as secondary outputs by other processes and will manage the recording of the cycle’s progress. MemINC will add 1
to the memory slot Mem 1, while MemRST will reset it back to 0
. The current state of Mem 1 will then be used as conditions in the main processes.
When defining auxiliary processes, which are intended solely for use as Secondary outputs by other processes, it’s typical not to use any Constraint, Input, or Event.
Pump sequence
The core of the relay module setup, however, will be a sequence of processes controlling the pump relays (we will use the sequencing technique described here). Start by creating four processes and name them Line_1 through Line_4. For each of them, set:
- Main state:
Auto
- Display:
Show
(if the display is in use) - Off event:
Timeout
- Running time:
3 seconds
(testing value) - Primary output:
D8
,D5
,D6
, andD0
, respectively
Additionally, set the Secondary outputs as follows:
Process | Secondary output | Response |
Line_1 | Mem_INC | Off -> On |
Line_1 | Line_2 | Off -> On |
Line_2 | Mem_INC | Off -> On |
Line_2 | Line_3 | Off -> On |
Line_3 | Mem_INC | Off -> On |
Line_3 | Line_4 | Off -> On |
Line_4 | Mem_RST | Off -> On |
- None of the processes can be initiated on their own. They must be started by a user or another process.
- Each process runs for exactly 3 seconds. Upon turning itself
OFF
, the Mem 1 memory slot is incremented by 1 and the next process is turnedON
in the case of the Line_1, Line_2, and Line_3 processes. In the case of the Line_4 process, the Mem 1 is reset to 0 and no further action is taken.
With the described setup, pressing On in the Line_1 process will result in the following workflow:
Button
While the physical button is not crucial for the setup, it enhances the user-friendliness of the irrigation system by providing immediate and simple physical access to perform virtually any task. As there is only one available button due to limited inputs, we need to differentiate the button’s function based on the current Mem 1 value. This can only be achieved by creating five independent processes, constrained by Mem 1. Name them Button_0 through Button_5.
For these processes, use the following setup (with the rest of the settings kept at their default values, as usual):
As the button is present on the D3
pin, it is critical that you use a button with the normally open state. Otherwise, the device will not boot with the button unpressed following any restart.
Please note that the Mem constraint conditions need to be expressed as inequalities. If you need to input an equality, you can achieve the same using two inequalities (see below examples).
Button_0
- Main state:
Auto
- Variable constraints:
Mem 1 < 1
- Input source:
D3
(button) - Control period:
2/10 sec
- On event:
Falling edge
- Off event:
Rising edge
- Secondary outputs:
Line_1
/On -> On
MemINC
/On -> On
Button_1
- Main state:
Auto
- Variable constraints:
Mem 1 <
2Mem 1 >
0
- Input source:
D3
(button) - Control period:
2/10 sec
- On event:
Falling edge
- Off event:
Rising edge
- Secondary outputs:
Line_1
/On -> Off
Line_2
/On -> On
MemINC
/On -> On
Button_2
- Main state:
Auto
- Variable constraints:
Mem 1 <
3Mem 1 >
1
- Input source:
D3
(button) - Control period:
2/10 sec
- On event:
Falling edge
- Off event:
Rising edge
- Secondary outputs:
Line_2
/On -> Off
Line_3
/On -> On
MemINC
/On -> On
Button_3
- Main state:
Auto
- Variable constraints:
Mem 1 <
4Mem 1 >
2
- Input source:
D3
(button) - Control period:
2/10 sec
- On event:
Falling edge
- Off event:
Rising edge
- Secondary outputs:
Line_3
/On -> Off
Line_4
/On -> On
MemINC
/On -> On
Button_4
- Main state:
Auto
- Variable constraints:
Mem 1 >
3
- Input source:
D3
(button) - Control period:
2/10 sec
- On event:
Falling edge
- Off event:
Rising edge
- Secondary outputs:
Line_4
/On -> Off
MemRST
/On -> On
Autostart process
So far, the cycle can only be launched manually. To automate it, we need to add a process that will initiate the Line_1 process each morning at 05:00 if there was no recent rainfall and the soil is not wet. To achieve this, create the following process (default otherwise):
- Name:
Autostart
- Main state:
Auto
- Timer:
05:00
–05:01
- Variable constraints:
D7
=1
(or0
, depending on sensor type)
- Input source:
A0
(rainfall sensor) - Control period:
10 sec
- On event:
Condition
- Condition: < 50% (or as required)
- Secondary outputs:
Line_1
/On -> On
MemINC
/On -> On
This process represents the core of the automation itself, with three key inputs – timer, variable constraint, and main input. Based on your own preferences, you may define different times when the irrigation should be launched (up to four independent time windows or even more with another process), the D7
constraint (defining the value representing recent rainfall according to your sensor), and the A0
condition representing the soil moisture threshold.
Additionally, if you wish to collect telemetry on soil moisture, you may also set set following in the Input section:
There are many combinations of how the functionalities of the Autostart process could be achieved or further improved. The description here is just one of many options.
MQTT setup
As usual, I will be using my preferred testing tools, the test.mosquitto.org broker and the IoT MQTT Panel app, with the following placeholder settings in the Wireless/MQTT menu:
- Connection:
Enabled
- Broker:
test.mosquitto.org
- Port:
1883
- User: empty
- Topic Level 1:
johndoe5896
- Topic Level 2:
garden
- Topic Level 3:
irrigation
Please bear in mind that the broker detailed above is only suitable for testing purposes and you should choose a more secure option for operation. Also, do not forget to customize the placeholder topic root johndoe5896/garden/irrigation
to minimize the risk of interference with other users.
You may send/receive following useful messages:
- ON button – sends Payload
1
to the Topicjohndoe5896/garden/irrigation/sub/Line_[X]/outset
- OFF button – sends Payload
0
to the Topicjohndoe5896/garden/irrigation/sub/Line_[X]/outset
- Relay State LED – receives the current relay state from Topic
with Payloadjohndoe5896/garden/irrigation/pub/Line_[X]/output
0
(relay off) or1
(relay on) - Progress bar – receives the current Mem 1 value from Topic
johndoe5896/garden/irrigation/pub/MEM1/output
with Payload0
(relay off) or1
(relay on) - Moisture data – if the Moisture process is used, you may receive the soil moisture level as a Payload from Topic
johndoe5896/garden/irrigation/pub/Autostart/input
- Device State LED – receives the current device state from Topic
johndoe5896/garden/irrigation/pub/conn
with Payload0
(offline) or1
(online) - Device IP address – receives the device’s current local network IP address as a Payload from Topic
johndoe5896/garden/irrigation/pub/ip
Although the irrigation setup is fully automated and does not require intervention from the user, you may still find very useful to continuously monitor the telemetry or occasionally access it manually. You can check our user guide for a full reference on MQTT protocol options.
Finalizations
Finally, once everything is tested and checked, your brand-new relay module can be installed in its dedicated place.
Below, you can download the backup files with settings used in this tutorial:
For guidance on working with the backup files, please refer to our user guide. Please, make sure that you import all files.
Were you successful in creating your own homemade irrigation system?
Ideas for further improvement
1
With more advanced hardware like our in-house PCB, replacing the standard relay module, you may combine this also with the rain tank level monitor, its automatic refill and additional fail-safes to prevent pumps from running dry.
2
The PRO version of the software will let you define much more pre-set schemes of irrigation, differentiated by, for example, ground moisture or temperature. Additionally, the PRO version will let you access the relays directly, without the necessity to defined special processes.
3
When using the relay MQTT telemetry in NodeRED, you can easily keep track of the past cycles or estimated water consumption.