Search

Lazy Man Joe's Ramblings

Category

Technology

GardenPi – a garden watering solution built on Raspberry Pi, MQTT and Home-Assistant.io [Part 4/4]

Following on from PART 3

Now that Home Assistant is aware of the Garden Zones, we can “program” some automations in Node-Red.

For now I’ve only implemented a rudimentary little flow:

  • I have 4 different flows to handle each of the zones separately, but I do make sure (manually) that they’re not conflicting with each other (i.e. 1 zone’s start-time is AFTER the end-time of the previous zone, etc.)
  • As it’s winter at the moment here in South Africa, I’ve opted to only water the garden 2 x a week and for only 10 minutes in each zone.

 

My flows looks like this:

GardenPi_NodeRed

And the schedule is as follows (currently Mondays and Thursdays):

GardenPi_Zone1_Schedule

And to make it easier for me, I used a “switch”-node to split the flow based on some logic (if the payload is “on” or “off” then go route 1 or 2)

GardenPi_Zone1_SwitchNode

For the “ON” payload, I use the “call service”-node to call the switch and switch it ON (Home-Assistant handles the mqtt messaging in the background):

GardenPi_Zone1_SwitchOn

At the end of the flow I have a “msg”-node for debugging purposes, which I will probably remove in future.

GardenPi_Zone1_Debug

So a bit of a long-winded 4-part post, but wanted to capture all the aspects of the process.

Future work will include adding some “intelligence” like checking the probability of rain, possible sensors in the ground to check humidity, etc.

Anyways – it’s been LOTS of fun and a great learning experience.

 

Advertisements

GardenPi – a garden watering solution built on Raspberry Pi, MQTT and Home-Assistant.io [Part 3/4]

Continued from PART 2

With all the puzzle pieces in place, now we can make Home-Assistant aware of the Garden Zone “switches”, so we can program and use the in automations.

In the switches.yaml file (remember I split the configuration for switches out into their own file), under the platform: mqtt section I added the Garden Zones / Switches:

platform: mqtt
name: "Garden Zone 1"
command_topic: "lazyhome/garden/output/zone1/set"
state_topic: "lazyhome/garden/input/zone1"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Garden Zone 2"
command_topic: "lazyhome/garden/output/zone2/set"
state_topic: "lazyhome/garden/input/zone2"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Garden Zone 3"
command_topic: "lazyhome/garden/output/zone3/set"
state_topic: "lazyhome/garden/input/zone3"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Garden Zone 4"
command_topic: "lazyhome/garden/output/zone4/set"
state_topic: "lazyhome/garden/input/zone4"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true

You can name the switch anything you want, as this will be the user-friendly name in the Home-Assistant homepage.
The important bits are the command_topic and state_topic sections – these need to align to the configuration you have on your Raspberry Pi with it’s mqtt listener.

I also created some “Groups” (groups.yaml) for Home Assistant to group my different switches into logical sections:

Geysers:
name: Geysers
entities:
- switch.bedroom_geyser
- switch.upstairs_geyser
Garden:
name: Garden
entities:
- switch.Garden_Zone_1
- switch.Garden_Zone_2
- switch.Garden_Zone_3
- switch.Garden_Zone_4

Please note that the switches have “_” to replace the spaces in the names

After updating the configuration, restart HASS and your switches should show up in it’s own group 🙂

GardenPI_Zones

And that’s it !

Home Assistant is now aware of the switches AND you can switch them using the on-screen toggles (which kicks off some mqtt messages in the background, which the Raspberry Pi picks up, switching the relevant GPIO pin, which in turn switches the relay !)

Phew !

😀

GardenPi – a garden watering solution built on Raspberry Pi, MQTT and Home-Assistant.io [Part 2/4]

So in GardenPi – PART 1 I covered the hardware & software configs to get going, but that’s not all yet 🙂

We now have a Raspberry Pi running MQTT and we can switch the GPIO pins by sending messages on the queue.

Next steps:

Connect the pins from GPIO to the relay board

This was fairly easy as the 8 port relay board I used had small pins I could connect directly via small cables from the GPIO pins
As the board also make use of 5V, I used the PWR connector (PIN 2 [5V] and PIN 6 [GND] on the Raspberry Pi) to power the relay board.
I would only be using 1 of the relays at a time, so the power usage will be minimal to draw power directly from the Raspberry Pi.

gardenpi-test-before-boxing.jpg

Make sure MQTT starts at power-on of the Raspberry Pi

To make sure we have a consistent experience, we need to make sure MQTT is started on power-on / reboot of the Raspberry Pi.
I used Supervisor for this as it makes it much easier to set up items which need to start when the Raspberry Pi is restarted.
They cover the configuration of Supervisor in the Pi-MQTT-GPIO setup instructions and I followed it – except I DID NOT set up a virtual environment, as my Raspberry Pi will be dedicated to it’s job.
Basically you install Supervisor;
Then create a /etc/supervisor/conf.d/pi-mqtt-gpio.conf file with your config information for pi-mqtt-gpio [/home/pi/config.yml].
My pi-mqtt-gpio.conf looks like this:

[program:pi_mqtt_gpio]
command = /usr/bin/python -m pi_mqtt_gpio.server /home/pi/config.yml
directory = /home/pi
redirect_stderr = true
stdout_logfile = /var/log/pi-mqtt-gpio.log

and my config.yml file looks like this:

mqtt:
host: 192.168.xxx.xxx
port: 1883
user: “xxx”
password: “xxx”
topic_prefix: lazyhome/garden

gpio_modules:
- name: raspberrypi
module: raspberrypi

digital_outputs:
- name: zone1
module: raspberrypi
pin: 5
on_payload: "ON"
off_payload: "OFF"
initial: low

- name: zone2
module: raspberrypi
pin: 6
on_payload: "ON"
off_payload: "OFF"
initial: low

- name: zone3
module: raspberrypi
pin: 13
on_payload: "ON"
off_payload: "OFF"
initial: low

- name: zone4
module: raspberrypi
pin: 19
on_payload: "ON"
off_payload: "OFF"
initial: low

digital_inputs:
- 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

- name: zone3
module: raspberrypi
pin: 13
on_payload: "ON"
off_payload: "OFF"
pullup: yes
pulldown: no

- name: zone4
module: raspberrypi
pin: 19
on_payload: "ON"
off_payload: "OFF"
pullup: yes
pulldown: no

4 Zones, each with settings for digital_output and digital_input.

OK – so now hardware connected and Raspberry Pi will start the PI-MQTT-GPIO listener on power-up / reboot.

Connecting the sprinklers

As I was basically replacing the controller for the sprinklers I already had in the garden, all I did was connect each of the Zones’ solenoids/valves to the relay board.
It’s fairly straightforward as you just have to remember the relay is a switch and you need to see whether you’re going to use the “Normally Open” [NO] or “Normally Closed” [NC] side of the relay. (Normally Open would mean that the switch is NOT switched on by default and when you trigger the relay, it will close the circuit; i.e. switch things ON – for Normally Closed it’s the other way round 🙂 )
I used “Normally Open” side of the relay as I wanted the Raspberry Pi to actually switch the solenoid on, which will open the valve and let the water through.
The solenoids make use of 24V AC and I already had a transformer to power them – so re-used that.

rainbird_hv

End result was enclosing the Raspberry Pi and relay board in a small housing, connected to the sprinklers in the garden.

GardenPi-Endresult

Next part we go to Home-Assistant to set up & config.

GardenPi – a garden watering solution built on Raspberry Pi, MQTT and Home-Assistant.io [Part 1/4]

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:

Hardware:

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

Software:

SETUP

OS

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.  sudo raspi-config 
    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.  sudo nano /etc/default/locale 
      2. Add the following:
        1. LANG=en_ZA.UTF-8
          LC_ALL=en_ZA.UTF-8
          
LANGUAGE=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.  ifconfig 
    2. My result showed my wlan0 adapter on IP 192.168.2.150
  6. 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
  7. I tested my ssh configuration from my Macbook
    1.  ssh pi@192.168.2.150 
  8. As a last step, I always check whether the latest updates are installed on my raspberrypi
    1. sudo apt-get update
      sudo apt-get upgrade

Now you can move on to the software installation !

Software

Python

OK, we 1st need python installed

sudo apt install python-pip 

PI-MQTT-GPIO

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:

gpiopinout

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.

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.

 

Home-Assistant.io – Grouping your entities / switches in a logical fashion

While adding more & more switches into my Home Assistant setup, I’ve needed to group the items together in a logical manner.

I have switches controlling our geysers, general “playground” switches and I’m also implementing a solution to control our garden irrigation [details to follow soon].

I first split out the switch configuration into their own switches.yaml file:


- platform: mqtt
name: "Bedroom Geyser"
command_topic: "cmnd/Sonoff1/power1"
state_topic: "stat/Sonoff1/POWER1"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Upstairs Geyser"
command_topic: "cmnd/Sonoff1/power2"
state_topic: "stat/Sonoff1/POWER2"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Switch3"
command_topic: "cmnd/Sonoff2/power1"
state_topic: "stat/Sonoff2/POWER1"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Switch4"
command_topic: "cmnd/Sonoff2/power2"
state_topic: "stat/Sonoff2/POWER2"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Garden Zone 1"
command_topic: "lazyhome/garden/output/zone1/set"
state_topic: "lazyhome/garden/input/zone1"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Garden Zone 2"
command_topic: "lazyhome/garden/output/zone2/set"
state_topic: "lazyhome/garden/input/zone2"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Garden Zone 3"
command_topic: "lazyhome/garden/output/zone3/set"
state_topic: "lazyhome/garden/input/zone3"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true
- platform: mqtt
name: "Garden Zone 4"
command_topic: "lazyhome/garden/output/zone4/set"
state_topic: "lazyhome/garden/input/zone4"
qos: 1
payload_on: "ON"
payload_off: "OFF"
retain: true

I then created a groups.yaml file with the following:

Geysers:

name: Geysers
entities:
- switch.bedroom_geyser
- switch.upstairs_geyser

Garden:
name: Garden
entities:
- switch.Garden_Zone_1
- switch.Garden_Zone_2
- switch.Garden_Zone_3
- switch.Garden_Zone_4

Finally I added the following to my configuration.yaml file:

# Add switch config in separate file
switch: !include switches.yaml
# Add group config in separate file
group: !include groups.yaml

The end result looks great !

Groups.png

More info on the Home-Assistant.io GROUP option here – https://www.home-assistant.io/components/group/

Node-RED flow for Geyser management using Sonoff switches and Home Assistant

We have 2 geysers (for other parts in the world it’s called water heaters :-)) in our house and to save some energy (& money), we want to manage when these geysers are switched on.

Typically a geyser will run 24/7 and keep the water heated, even when nobody is home.

So the idea is to put the geysers on a time-schedule for when we’re normally at home, so we only heat the water when we’re going to use it.

To put this in motion, there are a couple of things we need to do:

  1. Connect the Sonoff switch to the geysers [using a Sonoff Dual, so 1 Sonoff & 2 relays]
  2. Configure the Switches in Home Assistant
  3. Configure a flow to ensure the geysers are on at specific times during the week & weekend

Connecting the Sonoff Switch to the Geysers

Geysers tend to draw a lot of power when the element is on and the Sonoff Dual switch is only rated for 16A/10A – which is fine for normal use, but won’t cut it for geyser use.

We need to implement another solution:
– the solution is using a 230V/30Amp relay.

30A-Relay

This will allow us to use the Sonoff switch, but the main load will pass through the relay instead of the  Sonoff switch.

Now we have to connect the geysers in the distribution box to the Sonoff switch [some rewiring required here]

DB-board

That’s it !

[P.S. I already configured the Sonoff on the network, so this wiring was all I needed to do]

Connecting the Sonoff Switch to Home Assistant

To make Home Assistant aware of the switch, we need to add some configuration info into the configuration.yaml file.

I added the following lines: (I moved all the switch configs to a separate switches.yaml file)

Configuration.yaml (add these line)
# Put switch config in separate file
switch: !include switches.yaml

Now in the switches.yaml add the config info for the geyser switches:
– platform: mqtt
name: “Bedroom Geyser”
command_topic: “cmnd/Sonoff1/power1”
state_topic: “stat/Sonoff1/POWER1”
qos: 1
payload_on: “ON”
payload_off: “OFF”
retain: true
– platform: mqtt
name: “Upstairs Geyser”
command_topic: “cmnd/Sonoff1/power2”
state_topic: “stat/Sonoff1/POWER2”
qos: 1
payload_on: “ON”
payload_off: “OFF”
retain: true

As you can see, it uses mqtt (Mosquitto Broker) to communicate with the switches (command_topic & state_topic)

So now these switches will show up in Home Assistant (after a restart of HASS)

HA-Switches

OK now we move to the FUN stuff !

Configure the Node-RED Flow in Home Assistant

We decided that for our purposes the following schedule makes sense:
– Weekdays the geysers need to be on in the morning & the evening
– Weekends we will run the geysers most of the day (as we may shower at any time & tend to be around more)

The flow looks like this:

GeyserFlow

I split the flows into 2 separate ones for possible future changes where the geysers will be on different schedules.

Also, the basic flow is my 1st attempt [put it in place today] – so we may tweak it over time 🙂

Over-ride:

Sometimes we may actually select to switch the geysers on when we want – which we can always do from the Home Assistant front-end.

I’m also thinking of adding a flag to state when we have guests staying over, and then disabling the schedule, etc. [future project :-)]

Another thing I also need to do is have a physical over-ride button connected to the Sonoff switch (there are pins available for this) – this will allow me to manually switch the geysers on in the event the Sonoff loses connectivity to the wifi.

Well, this was an eventful couple of days !

Now deciding on to the next bit of automation ! 🙂

 

Have fun !

Sonoff Wifi Switches – Flashing it with Tasmota Over-The-Air (OTA)

A friend of mine recently introduced me to the Sonoff switches – affordable, easy to use AND RE-programmable ! [Thanks Cliff ;-)]

I got the Sonoff Dual switch, which allows you to switch 2 separate appliances/devices from 1 Wifi switch.

dual2

Anyways, to get going I decided to flash it with the Sonoff-Tasmota firmware (as I wanted to be able to pull it into my Home Assistant setup easily)

The instructions on the Tasmota-Sonoff Wiki is fairly easy to follow and although they suggest you use a wired 3.3V FTDI USB-to-Serial Converter/Programmer (or a Raspberry Pi) – I also noticed you can use Wifi to update the firmware Over-The-Air !

The tool for that is called SonOTA. [Please note not all Sonoff switches may support this – so make sure you have a supported Sonoff Firmware version BEFORE you try to flash with SonOTA)

There are a couple of ways to get SonOTA working:

  1. Using the Windows pre-built .EXE (you can find it here)
    1. I used this as a 1st pass for the 1st Sonoff Dual switch I flashed with the firmware
    2. This is a quick & dirty way – but it CAN work & there’s nothing wrong with this 🙂
  2. Installing the SonOTA software on your machine & customising it as you need (this involves installing Python 3, etc. on your local machine)
    1. Here you can customise some of the setting beforehand
    2. I left all vanilla & it still worked fine 🙂

Once you have SonOTA on your machine, you can run it and follow the instructions on-screen.

The process involves:

  1. Running SonOTA (./sonota.py)
  2. Confirming the configuration of you local machine to ensure you have all the required bits in place
  3. Putting your Sonoff in Access Point (AP) mode [holding the button in for more than 7 seconds] and then connecting to the ITEAD-* wifi network
  4. Installing a small base firmware uploader
  5. The Sonoff will then connect to you local machine to get the actual firmware installed
  6. Just be patient and things will happen 🙂
  7. Once installed, you will go into “FinalStage” mode
    1. Here you connect to the Sonoff [wifi now called FinalStage] and configure it to be able to connect to your wifi network
    2. Once configured, it will reboot and join your wifi
  8. Then you have to check your router /DHCP server to make sure your Sonoff’s IP address gets reserved (to make sure it doesn’t change on power loss / reboot)

Upgrading your Sonoff to the latest Tasmota Firmware

The SonOTA tool flashes a version of Tasmota which may be outdated (you CAN update the config manually, but why worry ? :-))

So once you have your Sonoff on your wifi network, you can connect directly to it and select the “Firmware Upgrade” option

Tasmota-Upgrade

Get the latest Sonoff-Tasmota firmware release here [select the .bin version which is applicable to you, normally just sonoff.bin]

Upload it and wait for the Sonoff to reboot with the latest version.

Great – Now you’re SET !

Next steps:

  1. Connect your Sonoff to the device / devices you want to control & get going !
  2. Integrate into Home Assitant

Have fun !

Home-Assistant.io and Hassio – The best and easiest Home Automation tools to get going !

I’ve played with some home automation stuff on my Raspberry Pi in the past, but setting up the stuff was always a bit involved and I would loose interest somewhere down the line. This meant I’ve always had good intentions, but never really implemented anything (all the work was “proof-of-concept” stuff :-))

Well that has changed in the last couple of days ! 🙂

HomeAssistant-Dashboard

Home-Assistant.io, Hass.io, Sonoff switches (running Tasmota Firmware) and Node-Red changed ALL of that !

What do you need:

  1. Somewhere to run Home Assistant (I used a Raspberry Pi 3)
  2. Install Home Assistant
    1. They have an easy to follow install guide
  3. Once you have Home Assistance installed, you configure Hass.io to help you manage additions, etc. on Home Assistant
    1. A couple of add-ons I use:
    2. Hassio-Dashboard
      1. Configurator (makes it easy to edit Home Assistant configuration yaml files)
      2. Samba share (to connect to the Raspberry Pi and be able to edit files if the front-end breaks)
      3. SSH Server (if you’re more used to using ssh to connect to your Pi)
      4. TasmoAdmin (used because I make use of Sonoff switches running Tasmota firmware)
      5. Mosquitto Broker (used to communicate with your switches)
      6. Node-Red (Visual Environment for putting “flows” together)
  4. You install all of these add-ons via the Hass.io Add-On Store
  5. THEN you need some switches
    1. I’m using the Sonoff switches as they’re easy to use and not too expensive
    2. I installed the Tasmota Firmware on them
  6. Adding your Sonoffs to Home Assistant involves some configuration editing (will cover that in a separate post), but once you have it working – it works like a charm !

Have fun !

 

How to get telnet onto MacOS High Sierra

Sometimes you just want to drop into a terminal window and test some “stuff”.

If you are running MacOS High Sierra (10.13), you may notice that telnet is not available as part of a standard install anymore 😦

Well, the Homebrew guys have come to the rescue and provide an option to install the “missing packages on macOS”.

Head over to brew.sh for more info.

Basically you have to install Homebrew and then after that you can install other packages.

In terminal, paste the following:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

wait for the install to finish (it will download and install the Xcode command-line tools, as well as configure homebrew.)

Once you’ve done this you can install packages like telnet, etc.

For telnet use this command:

brew install telnet

Have fun !

Create a free website or blog at WordPress.com.

Up ↑