We currently have an off-the-shelf garden watering solution [Hunter], but I’d like to have more control of when, etc. the irrigation is switched on.

I used the following items in my solution:


  • Raspberry Pi 2 B+ [using a 16GB SD card]
  • Raspberry Pi Wifi Dongle
  • 8 Port Relay board




I first started prepping my Raspberry Pi and because I’ll be running it as a headless solution, I used the latest Rasbian Lite OS [download here] (Raspbian Stretch) which doesn’t have the UI and you configure all via the command-line

  1. Using Etcher, I wrote the image onto the 16GB SD card
  2. Take your SD card and boot up the Raspberry Pi
    1. Although I am going headless, I used a keyboard and screen to configure the base OS – I need a way to configure the Wifi and the easiest is when you use raspi-config.
  3. I configured the following once the 1st boot-up was done:
    1. [code] sudo raspi-config [/code]
    2. Configure your locale, timezone and keyboard layout [I use the en_ZA.UTF-8 locale settings and US keyboard layout]
    3. Also configure your Wifi settings (SSID & Password, Wifi Country)
    4. Change the network name of your Raspberry Pi (mine’s called GardenPi)
    5. I also updated the password for the default pi user (default password is “raspberry” & it’s a good idea to change it 🙂 )
    6. I then forced an update in the locale file (I’ve had some issues in the past and found if you force it, things work better)
      1. [code] sudo nano /etc/default/locale [/code]
      2. Add the following:
        1. [code]LANG=en_ZA.UTF-8
    7. I also enabled SSH as I will be using SSH to manage the RaspberryPi in future.
  4. Reboot your Raspberry Pi, log in using the pi user and the new password you configured.
  5. Check your ip address
    1. [code] ifconfig [/code]
    2. My result showed my wlan0 adapter on IP
  6. You can now make this IP address ( static on your router to ensure you can easily “find” your raspberry pi when you want to manage it using ssh
  7. I tested my ssh configuration from my Macbook
    1. [code] ssh pi@ [/code]
  8. As a last step, I always check whether the latest updates are installed on my raspberrypi
    1. [code]sudo apt-get update
      sudo apt-get upgrade[/code]

Now you can move on to the software installation !



OK, we 1st need python installed

[code]sudo apt install python-pip [/code]


Once python is installed, you can install PI-MQTT-GPIO (it will install MQTT as part of the setup] (more details here)

[code]pip install pi-mqtt-gpio [/code]

As I already have MQTT running as part of the rest of my Home-Assistant.io setup, I will now only have to configure pi-mqtt-gpio to publish/subscribe to the MQTT messages.

Update your config.yml file with the necessary info, my info below (with some passwords hidden 🙂 )

NOTE – I’m only showing Zone 1 & Zone 2 settings (output & input)

host: mqtt-host-ip-address
port: 1883
user: “*******”
password: “*****”
topic_prefix: lazyhome/garden

– name: raspberrypi
module: raspberrypi

digital_outputs:     #used to send commands to the GPIO pins
– name: zone1
module: raspberrypi
pin: 5       #GPIO pin number – not the board pin, but the GPIO pin
on_payload: “ON”
off_payload: “OFF”
initial: low # This optional value controls the initial state of the pin
retain: yes # This option value controls if the message is retained.

– name: zone2
module: raspberrypi
pin: 6
on_payload: “ON”
off_payload: “OFF”
initial: low

digital_inputs:     #used to read status from GPIO pins
– name: zone1
module: raspberrypi
pin: 5
on_payload: “ON”
off_payload: “OFF”
pullup: yes
pulldown: no

– name: zone2
module: raspberrypi
pin: 6
on_payload: “ON”
off_payload: “OFF”
pullup: yes
pulldown: no

The GPIO pins are referenced as per the following layout:


So to test PI-MQTT-GPIO, you can run it in console mode (running it from the /home/pi folder):

[code] python -m pi_mqtt_gpio.server config.yml [/code]

It should show you connecting to the MQTT server, and then subscribing to the messages, etc.

Quick checklist:

  • Raspberry Pi running Rasbian Stretch Lite, with wifi dongle
  • PI-MQTT-GPIO installed (using python & pip)

In PART 2 we’ll bring is all together in Home Assistant.