Raspberry Pi Smart Home Hub installation guide

This guide will show you how to configure a Raspberry Pi as the central server of your smart home, for connecting devices and write your own automation rules.

Home assistant, Node Red and Mosquitto are the most popular open source packages for your smart home, so I chose them as part of this setup.

There are many solutions to automate your home, and the galaxy of products, hubs and software is destined to grow even more in the next years. This guide summarizes the decisions and the steps I took to setup my smart home hub, in the hope to put some clarity between many alternatives.

Choice Drivers

First, I want full control over the products installed, being able to integrate custom hardware and to write my own automations, so all of the software installed is open source (and free).

Some basic experience with linux is needed to follow this guide.

Next, for now all of the installation is local on my home network. With this setup you can of course connect to the web and enable services like Google Home or Alexa. But for now I prefer to focus on managing things locally, expecially not to rely on apps and services that are cloud-based: after all it is like giving the keys to your house to some stranger somewhere in the web.

Hardware Setup

For the hardware side, I chose a basic Raspberry Pi setup, connected via Ethernet to my home router. Even an older Model 2B will work.

Raspberry Pi installed next to the router

The system will be only operated headless via SSH, so there is no need to get a monitor, hdmi cable mouse and keyboard. Even if you’ll spend a bit more, I reccommend investing in a good power supply and cable, otherwise the Pi could not work or be instable. Remember it will be running 24\7.

I plan to add a WiFi router dedicated to home automation, as the number of devices will grow, in order to separate normal WiFi use from the automation network.

Software setup

This is the list of software that will be running on the pi, with download links, installation and configuration instructions. The whole setup may require a couple of evenings, after you have the hardware side ready.

Raspbian OS

Since the system will be headless, the Raspbian Lite version, without a desktop environment, is the most appropriate.

I used Etcher to flash the image on the SD Card, which turned out to be a really easy and straightforward process. Etcher is available for all operating systems.

Next, just pop the card into the Raspberry Pi, power it up and attach it to your router with the Ethernet cable. If you then connect to your router admin page (usually at the address 192.168.1.1 or 192.168.0.1) you can find the IP address of the pi.

I suggest adding a static DHCP entry for the Pi in your router, so it will always have the same address on your network.

Then you can connect via SSH to your setup with the username pi and password raspberry using the following command:

ssh pi@your.pi.add.ress

Change password

When connected, the first thing to do is to change the password of the default user pi:

passwd pi

Set the timezone

Setup the timezone according to your area.

Use the tzselect command to determine the setting, then edit your ~/.profile file and append the timezone, to make the change permanent for the pi user.

For example in my case the line to append is TZ='Europe/Rome'; export TZ.

Set the locale

Setup the locale using the following wizard:

sudo dpkg-reconfigure locales 

Webmin

This step is optional, but I found very convenient to use webmin to monitor the status of any linux server, manage upgrades and see what’s running. It is easier than using the command line, but it is just a matter of personal preference.

Webmin Gui sample

The following steps will install, start, and setup webmin to run at boot.

sudo apt-get install webmin
sudo systemctl start webmin.service
sudo systemctl enable webmin.service

The web interface will then be accessible at https:\\your.pi.address:10000. Please note that you need to connect with ssl (https) at the first time. You can disable that later via the interface, or generate a certificate to make your browser trust webmin.

Mosquitto

This service is the backbone of our home automation network for running custom devices. Mosquitto is the server for MQTT, a machine-to-machine protocol that is open source and lightweight, so that even power constranied microcontrollers can run a simple client to send and receive small messages for status and commands.

The protocol is of the publish-subscribe type, with a central entity - our Raspberry Pi with Mosquitto - managing all interactions.

To install, run and autostart on boot issue the following commands:

sudo apt-get install mosquitto
sudo systemctl start mosquitto.service
sudo systemctl enable mosquitto.service

We can then test that the server is working by listening to a dummy topic and issuing messages to that same topic, simulating an actual device on our home automation network. To do so open two terminals and ssh to the raspberry in both.

In the first terminal subscribe to the dummy topic.

mosquitto_sub -v -t /dummy/topic 

The -v flag will print out the topic that has been received. The -t flag specifies the actual topic to listen to.

In the second terminal issue some messages to the dummy topic

mosquitto_pub -t /dummy/topic -m "hello world" 

You will see that the messages are going through the server and get dispatched to the subscribing process in the first terminal, like in the following image:

Mosquitto test in two terminals

Topics can be composed by any sequence of names, and their structure resembles a filesystem path. You typically organize them according to the physical layout of the devices to control, for example /bedroom/lights/light1/status.

You can subscribe to multiple topics at once by using the + wildcard in place of one topic segment, and the # wildcard in place of one or more topic segments.

Node Red

Node red is a visual programming environment that let’s you build and deploy automations between the devices of your smart home.

You build flows which are constantly running processes composed by a bunch of nodes with specific atomic functions interconnected together. Simple automations like ‘when it’s raining lower the blinds’ become just a matter of interconnecting the proper nodes to each other.

Installation is very simple:

sudo apt-get install nodered
sudo systemctl start nodered.service
sudo systemctl enable nodered.service

You can reach the node red installation at http:\\your.pi.address:1880. To test that everything is working, let’s create a simple flow to publish the message that we tested earlier with mosquitto:

Now open a terminal and ssh into the pi server as before, and subscribe to the dummy topic that will be published by node red:

mosquitto_sub -v -t /dummy/topic 

If you now trigger the inject node with the little button on the left of the node, you’ll see that the messages are delivered to the subscribing process in the terminal, just like in the mosquitto test, but this time originating from your new node red installation.

Node Red test

Docker & Portainer

Docker is a manager of prebuilt images for the installation of software that requires a dedicated environment, dependencies and resources.

Rather than configuring the server with all the settings of individual software packages, Docker allows you to download a preconfigured image and run it inside a container, a sandbox that is much similar to a virtual machine, isolated from the rest of the system.

This is much cleaner that maintaining a set of often obscure settings for multiple software packages on the same machine. Plus the image come already preconfigured with everything needed to run it.

Installation commands:

curl -sSL https://get.docker.com | sh
sudo usermod -aG docker pi

Docker has a web interface, called Portainer, that can be used to easily manage images and running containers. Again, this is optional, but I find very convenient to have a web interface with the status of the system.

To install Portainer:

docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v /home/pi/portainer/data:/data portainer/portainer

The web interface is available at http:\\your.pi.address:9000, where you’ll see that Portainer itself has been configured as your first image and its running container.

Home Assistant

Home Assistant is an open source software that is gaining popularity for its flexibility and support for many third party devices, so it is a natural choice for this setup.

The configuration process still requires to edit files manually and it is not certainly for the average user expecting everything to work out of the box.

Hass.io and installation methods

The Home Assistant team has recently created Hass.io, with an entire operating system (named Hass.os) that you can download to a Raspberry Pi for a one stop install process.

There is some confusion about it, as it is not always clear which are the pros and cons of using Hass.io versus simply installing Home Assistant.

The main advantage of using hass.io is the capability to install addons via a store in the user interface, so it has some advantages, even if I fear this will be used in the future to force users to buy addons or subscriptions.

Installation

There is a method to install hass.io and home assistant with a single script. The method is outlined at this page.

The first step is to install the dependencies, including the optional ones, listed in the page. In my case only these were not part of the standard raspbian install.

sudo apt-get install jq apparmor-utils network-manager

Next just run the configuration script, setting your machine type at the end. In my case it is:

sudo su
curl -sL https://raw.githubusercontent.com/home-assistant/hassio-build/master/install/hassio_install | bash -s -- -m raspberrypi2

The script has to be run as root (!) and will eventually create and run a couple of docker containers and the processes below, to be able to perform system wide tasks, like rebooting the pi.

hassio-apparmor.service                                               
hassio-supervisor.service

Home Assistant Interface

Finally you can point your browser at the new home assistant home page by navigating to http:\\your.pi.address:8123 and start using it.

Home Assistant First page

Conclusions

That’s all for the installation, now you hopefully have a system ready for driving your whole smart home!

Have fun with it, and drop a comment below if you manage to get to the end of this guide.

Comments

comments powered by Disqus