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
- Raspbian Stretch Lite
- Moqsuitto MQTT
- Pi-MQTT-GPIO [exposes Raspberry Pi GPIO pins over MQTT]
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
- Using Etcher, I wrote the image onto the 16GB SD card
- Take your SD card and boot up the Raspberry Pi
- 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.
- I configured the following once the 1st boot-up was done:
- Configure your locale, timezone and keyboard layout [I use the en_ZA.UTF-8 locale settings and US keyboard layout]
- Also configure your Wifi settings (SSID & Password, Wifi Country)
- Change the network name of your Raspberry Pi (mine’s called GardenPi)
- I also updated the password for the default pi user (default password is “raspberry” & it’s a good idea to change it 🙂 )
- 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)
sudo nano /etc/default/locale
- Add the following:
LANG=en_ZA.UTF-8 LC_ALL=en_ZA.UTF-8 LANGUAGE=en_ZA.UTF-8
- I also enabled SSH as I will be using SSH to manage the RaspberryPi in future.
- Reboot your Raspberry Pi, log in using the pi user and the new password you configured.
- Check your ip address
- My result showed my wlan0 adapter on IP 192.168.2.150
- You can now make this IP address (192.168.2.150) static on your router to ensure you can easily “find” your raspberry pi when you want to manage it using ssh
- I tested my ssh configuration from my Macbook
- As a last step, I always check whether the latest updates are installed on my raspberrypi
sudo apt-get update sudo apt-get upgrade
Now you can move on to the software installation !
OK, we 1st need python installed
sudo apt install python-pip
Once python is installed, you can install PI-MQTT-GPIO (it will install MQTT as part of the setup] (more details here)
pip install pi-mqtt-gpio
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)
mqtt: host: mqtt-host-ip-address port: 1883 user: "*******" password: "*****" topic_prefix: lazyhome/garden gpio_modules: - 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):
python -m pi_mqtt_gpio.server config.yml
It should show you connecting to the MQTT server, and then subscribing to the messages, etc.
- 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.