Home Automation with Raspberry Pi
Home Automation with Raspberry Pi
Raspberry Pi
Koen Vervloesem
an Elektor Publication
LEARN DESIGN SHARE
● This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V.
78 York Street
London W1H 1DP, UK
Phone: (+44) (0)20 7692 8344
© Elektor International Media BV 2020
First published in the United Kingdom 2020
● All rights reserved. No part of this book may be reproduced in any material form, including
photocopying, or storing in any medium by electronic means and whether or not transiently or incidentally
to some other use of this publication, without the written permission of the copyright holder except in
accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a
licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, England W1P 9HE.
Applications for the copyright holder's written permission to reproduce any part of this publication should be
addressed to the publishers. The publishers have used their best efforts in ensuring the correctness of the
information contained in this book. They do not assume, and hereby disclaim, any liability to any party for
any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from
negligence, accident or any other cause.
● ISBN 978-1-907920-94-3
● EISBN 978-3-89576-383-0
● EPUB 978-3-89576-382-3
Images and logos used in this book are courtesy of Material Design Icons, Cisco, and the Raspberry Pi Foundation
Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro
engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops
and delivers high-quality content - via a variety of media channels (e.g., magazines, video, digital media, and social media)
in several languages - relating to electronics design and DIY electronics. [Link]
LEARN DESIGN SHARE
Table of Contents
Table of Contents
• Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Chapter 1 • Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.1 • What is home automation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.2 • Why use a Raspberry Pi as a home automation gateway? . . . . . . . . . . . . . . . . . 15
1.3 • The properties of a good home automation system . . . . . . . . . . . . . . . . . . . . . 16
1.3.1 • Secure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.3.2 • Modular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.3.3 • Open-Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.3.4 • Self-sufficient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.4 • How to use this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.5 • Summary and further exploration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.4.2 • Wi-Fi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
●5
Control Your Home with Raspberry Pi
2.9.3 • Creating a Docker Compose YAML file . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.10 • Summary and further exploration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.2.2 • VLANs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.2.3 • Firewalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.3 • User management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.3.1 • Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.3.2 • Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.3.3 • Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.4 • Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.4.1 • Your threat model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.4.2 • TLS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.4.4 • Creating a CA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
●6
Table of Contents
4.2.3 • A secure Mosquitto setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
●7
Control Your Home with Raspberry Pi
6.3.2 • Dumping raw Bluetooth broadcast data . . . . . . . . . . . . . . . . . . . . . . . . 140
●8
Table of Contents
8.4.1 • Reading sensor values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
●9
Control Your Home with Raspberry Pi
10.3.2 • Creating an AppDaemon app with MQTT: the time . . . . . . . . . . . . . . . . 229
10.3.3 • Creating an AppDaemon app with MQTT: garage door alert . . . . . . . . . . 231
10.4 • Summary and further exploration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
● 10
Table of Contents
12.2 • A Rhasspy base with satellites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
12.2.1 • Hardware requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
• Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
15.1 • Getting the name and ID of a serial device . . . . . . . . . . . . . . . . . . . . . . . . . 312
15.2 • Switching USB ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
15.3 • Disabling the onboard radio chips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
15.3.1 • Disabling onboard Bluetooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
● 11
Control Your Home with Raspberry Pi
15.4 • Disabling the on-board LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
15.4.1 • Raspberry Pi Zero (W) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
• Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
● 12
Preface
• Preface
Ever since the Raspberry Pi was introduced, the popular single-board computer has been
used by enthusiasts to automate their home. That's not a coincidence: the Raspberry Pi is
a powerful computer in a small package, with lots of interfacing options to control various
devices.
In this book, I'll show you how you can automate your home with a Raspberry Pi. You can
do this in many ways and with various software and hardware choices. I'll show you one
way, which is a bit different from what you'll read in many other books, but my approach
has its merits, and I'll explain why.
You'll learn how to use various wireless protocols for home automation, such as Bluetooth,
433.92 MHz radio waves, Z-Wave, and Zigbee. Soon you'll automate your home with Py-
thon, Node-RED, and Home Assistant, and you'll even be able to speak to your home auto-
mation system. All of this in a secure way, with a modular system, completely open-source
and without relying on third-party services.
At the end of the book, you can install and configure your Raspberry Pi as a highly flexible
home automation gateway for your protocols of choice and link various services with MQTT
to make it a system of your own. This DIY (do it yourself) approach is a bit more laborious
than just installing an off-the-shelf home automation system, but in the process, you learn
a lot, and in the end, know exactly what's running your house and how to tweak it. And
that's why you were interested in the Raspberry Pi in the first place, right?
● 13
Control Your Home with Raspberry Pi
Chapter 1 • Introduction
In this introductory chapter, I give a short overview of what home automation is and why
you would use a Raspberry Pi as a home automation gateway. Then I describe what I
consider properties of a 'good' home automation system:
• secure
• modular
• open-source
• self-sufficient
In the rest of this book, I'll explain step by step how to create such a good home automation
system with a Raspberry Pi.
Home automation is the process or result of automating systems that are running at home:
lighting, HVAC (heating, ventilation, and air conditioning), appliances such as washing
machines, blinds, and roller shutters, and so on. A home automation system is also able to
use information from environmental sensors (temperature, humidity, pressure, CO2, …),
smart meters, movement sensors, presence sensors, cameras, and so on.
• a central gateway (also called controller or hub), which controls devices and reads
sensor measurements
• controllable devices
• sensors
The controllable devices and sensors are regularly called "smart devices", although almost
none of them are really smart. Another name you'll see for them is IoT (Internet of Things)
devices because they can be (directly or indirectly) linked to and controlled over the internet.
Figure 1.1 A home automation system consists of a central gateway and various controllable devices and
sensors.
● 14
Chapter 1 ● Introduction
A home automation gateway always has a user interface. Of course, the purpose of home
automation is to automate as much as possible, so the idea is that the user shouldn't have
to use this user interface that much. But a user interface is still essential to:
• configure the home automation gateway: for instance if the sun goes down, close the
blinds;
• manually control your devices: this should still be possible because you can't automate
everything;
• show you a nice dashboard of sensor measurements: for instance to see the inside and
outside temperature.
• Most home automation gateways have a web server running, which supplies a web
interface as the user interface. You can access this web interface on any computer or
mobile device.
• Some systems have a mobile app for Android or iOS, which is generally better adapted
to the specific requirements of mobile devices, such as a smaller screen.
• It's also possible to use a dedicated touch screen, for instance hanging on the wall, to
control your home automation system, and to show you some nice graphs.1
• Last but not least, in recent years home automation systems have added a new kind of
user interface: speech. With a so-called voice assistant or smart assistant (again, they
are not that smart), you can give speech commands to your home automation system
and it replies with spoken messages.
This book is not focused on any of these user interfaces; it's more about the backend
services and how to link and automate them. However, I cover two home automation
projects with a web interface in Chapter 10 (Home Assistant and Node-RED), and create a
voice assistant for your home automation system with Rhasspy (Chapter 12). You should
consult the documentation of these projects if you're more interested in the user interface
side of home automation.
If you buy an off-the-shelf home automation system, the gateway is a small box that looks
somewhat like a router or a Wi-Fi access point. In this book, we'll show you how you can
create your own home automation gateway with a Raspberry Pi.
But why would you do that? Because you can, of course! More seriously, the Raspberry
Pi is what makes the approach in this book possible. We'll go into the advantages of this
approach in the next section, but the number one reason to use a Raspberry Pi as your
home automation gateway is: you are in control.
An off-the-shelf home automation gateway isn't flexible: you can only do with it what the
1 In many cases this touch screen is just running a fullscreen web browser visiting the
home automation gateway's embedded web server.
● 15
Control Your Home with Raspberry Pi
manufacturer allows, you rely on the manufacturer's goodwill to receive updates and new
functionality, and most of the time you can't "hack" on it yourself.2
Contrast this with the Raspberry Pi. You can choose your operating system (which we'll do
in the next chapter), you can choose which communication protocols you'll want to support
(which we'll cover in various chapters in this book), you can choose your user interface,
and so on. You can even choose the case to protect your Raspberry Pi and which expansion
boards you connect, as there's a whole ecosystem of hardware for the Raspberry Pi.
Of course, you can also run home automation software on a more powerful system, such
as a NAS (network-attached storage) or a home server. But the Raspberry Pi has several
advantages to these systems:
• It's very low-power, so it doesn't cost you much to keep it running 24/7.
• For most home automation tasks you don't need the processing power that these other
systems offer.
• The ecosystem of software and hardware for the Raspberry Pi is immense, as well
as the number of resources where you find more information about it.3 This is also
a reason to choose the Raspberry Pi over similar single-board computers from other
manufacturers.
• be secure, so you don't risk someone else controlling your house or spying on you at
home;
• be modular, to make it easy to plug in other protocols or applications;
• only use open-source software;
• be self-sufficient, not relying on cloud systems from Google, Amazon, or other parties.
This is my highly opinionated vision, and it's this vision that I build upon in this book.
If you consider these properties for a moment, you'll see that this vision is almost
diametrically opposed to most off-the-shelf systems you'll find. I'll give some examples in
the next subsections.
It's possible that you don't agree with some of these properties, or that you don't have such
strong feelings about them as I do. That's OK: while I explain an approach in this book
2 With hacking I don't mean gaining unauthorized access to a computer (which is the
connotation the word unfortunately has received). The Jargon File describes a hacker as "a
person who enjoys exploring the details of programmable systems and how to stretch their
capabilities, as opposed to most users, who prefer to learn only the minimum necessary." (The
Jargon File, [Link]
3 For instance, the Raspberry Pi Foundation publishes its official magazine about the
Raspberry Pi, MagPi ([Link] and Elektor (the publisher of this book)
publishes Dutch ([Link] and French ([Link] editions of the
magazine.
● 16
Chapter 1 ● Introduction
that implements this vision, thanks to its modularity you can certainly plug in proprietary
software or cloud systems if you prefer these. Heck, you can even add monolithic and
insecure software, but I don't tell you how.
In the next subsections, I'll go over these four properties in more detail, and I hope that
at the end of this chapter you'll agree with me that this approach to home automation is
the right one.
1.3.1 • Secure
Of course, a home automation system should be secure. No one can be against it, can't
they? A home automation system controls your home, so whoever can break into it can
make your life very miserable.
Unfortunately, even if a manufacturer tells you that his system is secure, chances are that
it isn't. Security is very difficult to attain, and most manufacturers don't want to spend the
resources needed to secure their system.4
Home automation and IoT devices are notoriously insecure. At the Usenix Security
Conference 2019, the Czech security software company Avast and Stanford University
presented their research of household IoT devices. Avast scanned 83 million IoT devices
in 16 million homes around the world of people who agreed to share these data. The
results of the study published in "All Things Considered: An Analysis of IoT Devices on
Home Networks" ([Link] were
staggering:
• 7% percent of all IoT devices support an obsolete, insecure, and completely unencrypted
protocol such as Telnet or FTP.
• Of these, 17% exhibit weak FTP passwords, and 2% have weak Telnet passwords.
• Surveillance cameras have the weakest Telnet profile, with more than 10% of them
that support Telnet with weak credentials.
• 3% percent of the homes are externally visible on the internet and more than half of
those have a known vulnerability or a weak password.
This is not an isolated study. Not a week goes by without some news items about insecure
devices, most of the time because basic security measures such as strong passwords are
not enforced by the manufacturer or basic programming errors have been made. To give
you an idea about what can happen: in 2018 nude videos of the Dutch women's handball
team appeared on a popular porn website because the surveillance cameras of the dressing
room of a sauna were broken into. Imagine if someone can access your baby monitor with
a camera or your security camera in your living room or bedroom…
So what can you do to secure your home automation system? If you choose an off-the-
shelf system: not much. You fully rely on the manufacturer's ability to create a secure
4 Most consumers probably wouldn't want to pay more for a secure home automation
system anyway.
● 17
Control Your Home with Raspberry Pi
system and the goodwill to keep supplying patches that solve security issues that have
been discovered. And the home automation and IoT industries have clearly shown they
are not up to the task. This is one of the reasons why I prefer open-source software. Not
because it is always secure, but because the transparency of the open-source development
process forces developers to create more secure software.
1.3.2 • Modular
There are many competing standards and communication protocols for home automation,
such as Z-Wave, Zigbee, and KNX. Other protocols aren't specific to home automation but
are very usable in this domain too, such as Wi-Fi, Bluetooth, or Near Field Communication
(NFC).
Unfortunately, many off-the-shelf home automation gateways support only a small subset
of these protocols or even use a proprietary protocol that locks you into using devices of
the same manufacturer. That severely limits your choice of products.
You can't know which protocols will become popular in a few years, and maybe you like
one product that uses Z-Wave and another product that uses Zigbee. It should be easy to
interconnect these devices, even when they use different protocols.
This is why a good home automation system should be modular, which makes it possible
to plug in new components when you want to support a new protocol, add a new user
interface or extend its functionality in another way.
Many of the wireless communication protocols for home automation need a dedicated
transceiver because they work on a specific radio frequency. That's where the Raspberry Pi
shines: you can easily connect Z-Wave, Zigbee, or 433.92 MHz transceivers using the USB
ports or the GPIO header. So you can start with a basic Raspberry Pi setup supporting only
IoT devices that are communicating over Wi-Fi and Bluetooth, add an RTL-SDR USB dongle
to read the measurements of your 433.92 MHz weather sensors, later add a Z-Wave HAT
on the board when you start adding Z-Wave sensors to your house and then add a Zigbee
USB transceiver when you want to control some Zigbee lights.
Figure 1.2 A good home automation system is modular enough to support many home automation protocols.
● 18
Chapter 1 ● Introduction
Modularity is also important for software. There's a lot of user-friendly software to make
your Raspberry Pi a home automation gateway.5 So you just install this software on your
Raspberry Pi and that's it: you have a gateway that supports several devices. Some of
these systems are very modular and extensible, others aren't. Many of them support MQTT
(Message Queuing Telemetry Transport), a common language to exchange messages.
MQTT has become the standard for interoperability between various home automation
devices. For instance, if your home automation gateway of choice doesn't support Zigbee
but it does support MQTT, then you only have to run the Zigbee2mqtt software (see Chapter
9), which translates the Zigbee protocol to MQTT messages. Your gateway can then talk to
your Zigbee devices using MQTT.
Modularity also means that you don't have to have one gateway. You can perfectly have
your main gateway in your basement, but install a second gateway with your 433.92 MHz
receiver for your environmental sensors in your living room because that gives you better
coverage to receive data from these wireless sensors. If you're using MQTT, that's very
simple to implement: you just relay the sensor readings that your gateway in the living
room receives to your MQTT broker, after which your main gateway receives the readings
in the MQTT format.
In short: a good modular home automation system means that you can mix and match
the devices that you like, irrespective of their protocol, and you can use the software and
hardware components of your choice, in various locations in your house.
1.3.3 • Open-Source
Most software is being distributed as machine code, so it's not readable for us. If you buy
an off-the-shelf home automation gateway, you generally don't get access to its source
code, so you cannot peek into it to see what it does or to assess its quality. You just have to
believe the manufacturer on his word. Is that enough for you if it's about software that will
get to know you intimately because it processes sensor readings and even camera images
about you in your home? Not for me.
But there's a type of software where you do get access to the source code: free and
open-source software (sometimes abbreviated as FOSS or even FLOSS).6 If you really
want to be precise, there's free software and open-source software, but for the end-user,
the differences are minimal and mostly philosophical. When I talk about "open-source
● 19
Control Your Home with Raspberry Pi
But what when you're not a programmer and don't even understand the source code of
your home automation system? Even then the use of open-source software has a lot of
advantages. It's not because you don't have the programming experience that others can't
help. Most open-source projects have a decentralized software-development model that
encourages open collaboration.
So if you find a bug in the software, or see something wrong in its source code but don't
have the programming experience needed to fix it, just report the bug on the issue tracker
of the project, and hopefully, someone else in the project's community will step in and fix
it. It all depends on the health of the project's community. But a good open-source project
has a vibrant community of developers and users who collaborate to continuously improve
the software.
In the process of writing this book, I participated in various communities of the programs I
covered. I opened issues to report bugs, fixed some bugs, added support for new devices,
contributed documentation, and helped people build their software for Raspberry Pi. This
was all only possible because they are open-source.
And open-source doesn't just mean getting access to the source code, it's much more
than that. If you want to get an idea about what open-source is, I recommend you to read
the Open Source Definition on [Link] by the Open Source Initiative. For
instance, with open-source software you are not only able to read its source code, you are
also allowed to modify it and distribute your modifications.
Even more, when you know a specific software project is open-source, you know that
it doesn't arbitrarily restrict what you can do with it. The Open Source Definition even
explicitly lists that the license of open-source software must not discriminate against any
person or group of persons, nor restrict anyone from making use of the program in a
specific field of endeavor.
Open-source is a complex and nuanced topic, and I recommend you to read about it more
if you're not accustomed to it. The decentralization and transparency of the open-source
development model gives power back to the users, where it belongs. For home automation
that's even more important. I don't want a company having control over my house. That's
why you'll see that all software in this book is open-source.
1.3.4 • Self-sufficient
During the last ten years, there has been a worrying development in the computer
industry: we all depend more and more on (centralized) cloud systems. Unfortunately, the
home automation industry didn't escape this fate. Many popular home automation and IoT
systems depend on a cloud server. Some examples:
● 20
Chapter 1 ● Introduction
This isn't without its problems. In the last couple of years a couple of cloud services for
home automation stopped working for their users:
Revolv Hub
In 2014 Nest bought the company that was selling the Revolv Hub home automation
system, not long after Nest itself was acquired by Google. In 2016 Nest shut down the
servers Revolv Hub depended on, after announcing it with a quiet note on the website
of Revolv a few months earlier. That meant that the $300 Revolv Hub ceased functioning
entirely.
Wink devices
In May 2020 Wink (with the catchphrase "A simpler way to a smarter home") announced
with just a week's notice that it would start charging a monthly fee for the use of their
services. Users that didn't want to pay were no longer be able to access their Wink devices
and their automations were disabled. The Wink Hub, that had been in stores with the
clear description "no required monthly fees, ever", was rendered useless. Ironically, the
announcement ended with the message "Our user community is integral to Wink, and we
want to continue to be your trusted smart home provider."
Now imagine when the highly popular IFTTT would stop their service: suddenly the
automations of millions of people would stop working.
It also just makes no sense to use cloud services to automate your home. Home automation
comes down to: you want one device to be able to respond to another device in your home.
For instance: the motion sensor in your bathroom detects motion at night, and this turns
the bathroom light on for five minutes. If you use IFTTT to link both devices, the motion
sensor has to send a message to the internet, IFTTT relays the message to your bathroom
light (for instance a Philips Hue light), and the bathroom light turns on.
But there's no need for an internet service like IFTTT between both devices because they
both are in your home, so it makes much more sense to link them locally, using a server at
home. This could be a Raspberry Pi running home automation software that doesn't need a
cloud server to function, but does all its processing on-device (or on the edge, as it's called
now). You can perfectly do this with Node-RED or Home Assistant (see Chapter 10). Then
there's no way a company can render your home automation system useless by shutting
down their service, or your bathroom light doesn't turn on at night because your internet
● 21
Control Your Home with Raspberry Pi
connection or IFTTT's servers are down. You're fully in control of your home automation
system.
Figure 1.3 The home automation at the left is cloud-based: a simple motion detection message first goes to a
server over the internet before returning to your light. The self-hosted system on the right makes much more
sense: a Raspberry Pi on your network relays the message without using the internet detour.
Using cloud services for your home automation system has another risk: it invades your
privacy. Look at some of the privacy issues with the services I talked about above:
• If you use the Ring video doorbell, it sends a video of everyone that steps on your
porch to the manufacturer. The Ring company (which has been bought by Amazon in
2018) has a questionable approach to privacy: in January 2019 it was uncovered that
employees have access to the video recordings of all Ring devices and even that the
data are stored unencrypted.
• If you use the Nest Learning Thermostat, Google knows precisely when you are home
and when you aren't.
• If you run a smart speaker like the Amazon Echo, what you tell your house members will
be sent countless times inadvertently to Amazon because the Echo thinks it has heard
its wake word.7 Moreover, Amazon's employees listen to a part of all 'conversations'
with the Echo to improve its algorithms.
• If you use the IFTTT service to link your various other services, you give one company
● 22
Chapter 1 ● Introduction
access to all your home automation services, which is too much power concentrated in
one company's hands: they can see exactly what you're doing.
This book describes a lot of components to support various home automation and IoT
protocols. I don't expect that everyone will want to support all these protocols by installing
these components manually. Home automation platforms such as Home Assistant (described
in Chapter 10) have a fairly complete support for these and many more protocols. You can
use such a platform to turn your Raspberry Pi into a home automation gateway.
However, I'm also a big believer in choice. You will have your preferences. Perhaps:
In all of these cases, you can use one of the open-source projects in this book to link this
protocol to your gateway.
You can look at this book as a DIY manual to create your home automation gateway from
scratch. But it's also describing a software architecture for a secure, modular, open-source,
and self-sufficient home automation system. It's up to you to choose the components that
implement this architecture in your own house, and if a home automation platform such as
Home Assistant or Mozilla IoT WebThings Gateway does the job for you, that's fine. They
don't lock you into their way of doing things, so you could perfectly use them and still link
them to other systems thanks to MQTT and other protocols.
This book expects some familiarity with Raspberry Pi OS (formerly called Raspbian) or
Linux in general. I explain most commands the first time I use them, but if you have
never worked with a Linux system, I recommend reading an introductory text about Linux,
Debian, Raspberry Pi OS, or bash (the Linux command line used in this book).
In various chapters, I show short Python programs that interact with your home automation
system. The Python code in this book is not that advanced. If you don't know Python, you
should be able to understand what the code does, and maybe you will even be able to adapt
it because Python is known for its clarity. However, if you want to make the most out of
this book, I do recommend you to learn some Python. The official Python documentation
([Link] especially the Python tutorial ([Link]
tutorial/[Link]), is a good way to start. A home automation system is typically
something very personal, and being able to program it is the best way to customize8 it to
your taste.
8 All Python code in this book has been developed for and tested on Python 3. Its
predecessor, Python 2, has been retired on January 1 2020 ([Link] and
shouldn’t be used anymore.
● 23
Control Your Home with Raspberry Pi
Note:
This book is not about how you connect sensors, relays, and so on directly to your Raspberry
Pi using the GPIO header, but it strictly covers how you use your Raspberry Pi as a home
automation gateway, collecting data and controlling devices remotely using radio and
network protocols. Of course, it's perfectly possible to let your Raspberry Pi combine both
tasks, but I wouldn't recommend it because it could lead to stability problems, and your
gateway should be as reliable as possible.
Chapter 1: Introduction
The theoretical foundation for this book, with my vision of what good home automation
should look like and why you should use a Raspberry Pi for it.
Chapter 5: TCP/IP
Some TCP/IP-based protocols that everyone can use for home automation without the
need for specialized transceivers, such as Wake-on-LAN, SSH, SNMP, HTTP/REST, and video
surveillance systems.
Chapter 6: Bluetooth
An introduction to Bluetooth Low Energy, including a way to investigate Bluetooth Low
Energy devices and using them to read sensor data and for presence detection.
Chapter 8: Z-Wave
An introduction to the Z-Wave mesh protocol for wireless home automation.
Chapter 9: Zigbee
An introduction to the Zigbee mesh protocol for wireless home automation made popular
● 24
Chapter 1 ● Introduction
Appendix
Some specialized tips that could come in handy in various situations.
Note:
Code examples from this book are published on [Link]
raspberry-pi-home-automation. They can be copied from the GitHub repository one-by-
one when trying the various applications in this book. You can also download them all at
once. Refer to the instructions in the GitHub repository for more information.
In this introductory chapter, I gave a theoretical foundation for this book, with my vision of
what good home automation should look like and why you should use a Raspberry Pi for it.
I argued why your home automation system should be:
• secure
• modular
• open-source
• self-sufficient
I hope the examples I gave you have convinced you that these are important properties of
your home automation system. In the rest of this book, I show you how you create such a
system with a Raspberry Pi.
If you want to read more about some issues I raised in this chapter, I can recommend
● 25
Control Your Home with Raspberry Pi
the special edition of Mozilla's Internet Health Report of November 2019, called "*Privacy
Included: Rethinking the Smart Home" ([Link]
included/). It talks about the so-called "smart home" from a more general point of view
and is much in line with the approach I advocate in this book. The report stresses the
importance of privacy, security, interoperability, and sustainability for smart home devices.
Note:
The use of the ⏎ symbol denotes that code should be typed contiguously on the same line.
● 26
Chapter 2 ● The Raspberry Pi as a home automation gateway
While the previous chapter gave you a theoretical foundation with an overview of what
you're going to build in this book (and especially why), the second chapter gives you
a practical foundation: it's all about preparing your Raspberry Pi for its task as a home
automation gateway.
I'll first talk a bit about which Raspberry Pi models are suitable for home automation, after
which you'll learn how to install Raspberry Pi OS, configure network connectivity and access
your Raspberry Pi over the network using SSH. I also talk a bit about the tmux terminal
multiplexer, which is indispensable for many tasks in this book, and about installing Python
packages. I'll finish this chapter by installing Docker, which is essential to be able to install
various programs in later chapters without messing up your base system.
Since its first release at the beginning of 2012, various Raspberry Pi models have been
released. Most of them, even the first ones, are suitable for home automation, but not
every model is up to every task. I have been running the Domoticz home automation
software on the first Raspberry Pi B model for more than five years to control my home,
and it has only been replaced by a newer model in the last three years.
So if you have an older model like the Raspberry Pi 2 or even the first model lying in your
cupboard, by all means, try it. Whether you can use it as your home automation gateway
depends on how many devices you want to control and which extra software packages you
want to run on your Raspberry Pi.
Note:
These early models don't have Wi-Fi or Bluetooth. If you want to use these wireless
protocols, you'll have to use adapters that plug into the USB ports.
In most cases, I recommend you to use a Raspberry Pi 3B or 3B+ as your home automation
gateway. These models have enough processing power to run most home automation
workloads, and they have Ethernet, Wi-Fi, and Bluetooth. The Raspberry Pi 3A+ (which has
the same processing power as the 3B+) is fine if you don't need Ethernet connectivity and
one USB 2.0 port is enough for you. You can always connect more USB devices with a USB
hub. But note that 512 MB RAM could be insufficient for some applications.
● 27
Control Your Home with Raspberry Pi
Figure 2.1 In many home automation scenarios the Raspberry Pi 3B+ is up to the task.
Figure 2.2 The Raspberry Pi 3A+ is fine as a home automation gateway if you don't need Ethernet
connectivity and one USB 2.0 port is enough.
Only if you're serious about home automation and are running very demanding workloads,
or need the USB 3.0 and Gigabit Ethernet throughput, would I recommend choosing a
Raspberry Pi 4 as your home automation gateway.1 This model exists in versions with 1, 2,
4 and 8 GB. Choose the RAM size depending on your workload.2
● 28
Chapter 2 ● The Raspberry Pi as a home automation gateway
Figure 2.3 You only need the Raspberry Pi 4 for very demanding home automation workloads, for instance for
image recognition.
The Raspberry Pi Zero W(H) has the same processor as the first Raspberry Pi model, but
clocked at 1 GHz instead of 700 MHz. So this is only suitable for minimal home automation
workloads. It has Wi-Fi and Bluetooth connectivity, but no Ethernet and only one micro-USB
OTG connector. So for a gateway, the Raspberry Pi Zero W(H) is seriously handicapped. On
the other hand, because of its low price, small size, and low power consumption, it's a very
interesting model to read Bluetooth sensors at various places in your house, for instance,
to detect in which room you are (see Chapter 6). And in combination with a Raspberry Pi
Camera the Raspberry Pi Zero W can function as a cheap IP camera.
Figure 2.4 The Raspberry Pi Zero W(H) is not powerful enough to run a serious home automation gateway,
but it's perfect as a simple gateway for Bluetooth or Wi-Fi sensors or other devices.
There are still other Raspberry Pi models: the Compute Module, Compute Model 3, and
3+. These are meant for industrial use. While they have their advantages, I'm not going
to use them in this book. This is, after all, a book about home automation, not industrial
● 29
Control Your Home with Raspberry Pi
automation. But if by any chance you have one of these, feel free to try them: they should
work because they are fully compatible with software for the 'normal' Raspberry Pi models.
To sum up, this table shows some specifications of the Raspberry Pi models that are most
relevant for home automation:
Pi Zero
Pi 4B Pi 3B+ Pi 3A+ Pi 3B
W(H)
SoC BCM2711 BCM2837B0 BCM2837B0 BCM2837 BCM2835
Cores 4 4 4 4 1
CPU clock 1.5 GHz 1.4 GHz 1.4 GHz 1.2 GHz 1.0 GHz
RAM 1 / 2 / 4 / 8 GB 1 GB 512 MB 1 GB 512 MB
2 × USB 3.0, 2 1 × micro-
USB 4 × USB 2.0 1 × USB 2.0 4 × USB 2.0
× USB 2.0 USB 2.0 OTG
Gigabit over
Ethernet Gigabit / 10/100 Mbps /
USB 2.0
802.11b/g/n/ 802.11b/g/n/ 802.11b/g/n/
Wi-Fi 802.11n 802.11n
ac dual-band ac dual-band ac dual-band
Bluetooth 5.0 4.2 4.2 4.1 4.1
85.6 mm × 85.6 mm × 65.0 mm × 85.6 mm × 65.0 mm ×
Size
56.5 mm 56.5 mm 56.5 mm 56.5 mm 30.0 mm
Table 2.1 Raspberry Pi models for home automation
The Raspberry Pi is an easy computer board to experiment with. If you pick a Raspberry
Pi, put a Raspberry Pi OS image on the microSD card, and start installing some programs
explained in this book, you'll have a home automation gateway in no time.
It's tempting to start this way, and if it's just to see what the possibilities are, that's no
problem. But you'll run your house on this Raspberry Pi, so you'll have to at least think
about how to make it reliable. These are some aspects you have to consider:
Place
Your home automation gateway should be in an accessible place so it's easy to troubleshoot
it or attach or replace adapters. But at the same time, you don't want it to be too accessible.
For instance, if you have it lying on your desk, the risk is too high that you spill coffee on
it, yank off its power cable, or just that it's in your way.
Storage
The capacity of the microSD card for your Raspberry Pi should be high enough to store all
software, logs, and data. If you install all projects in this book, you need a microSD card
of at least 16 GB. I recommend 32 GB to be on the safe side. Because microSD cards are
notoriously unreliable, especially after a power failure, regularly take a backup of the most
● 30
Chapter 2 ● The Raspberry Pi as a home automation gateway
important data.3
Case
It's perfectly acceptable to use the Raspberry Pi without a case when you are experimenting.
But as soon as you're starting to rely on it as a home automation gateway, you should put
it in a case: this protects your Raspberry Pi from dust or small accidents. Choose a case fit
for your purpose. For instance, if you need to use the GPIO pins, make sure the case gives
access to them.
Cooling
Especially with the Raspberry Pi 4, the temperature mustn't rise too much. If the temperature
of the SoC is between 80 and 85 degrees Celsius, the ARM core(s) will be throttled back
in an attempt to reduce the core temperature. If the temperature of the SoC reaches 85
degrees Celsius, the GPU will also be throttled back. Running for too long on these high
temperatures is detrimental to the health of the processor. If you are doing intensive
computations on your home automation gateway, add a heat sink or a fan. There are even
cases that double as a heat sink with their body.
Power
Use a reliable power supply for your Raspberry Pi, as you don't want your home automation
gateway to fail when the power requirements are not met temporarily. The official Raspberry
Pi power supply is a safe choice, but other adapters that supply 5.1 V 2.5 A (or 3 A for the
Raspberry Pi 4 to be on the safe side) should be fine too. Note that the power consumption
depends on the peripherals you have connected, such as USB transceivers or the Raspberry
Pi Camera Module (which requires 250 mA).
Network
Wi-Fi connections are not as reliable as Ethernet connections. If it's possible, use an
Ethernet connection for your home automation gateway. This way your house doesn't lose
its head when your Wi-Fi access point fails.
3 In this book, you only need to copy the [Link] file and the contents of
the directory /home/pi/containers to have all your home automation data.
● 31
Control Your Home with Raspberry Pi
Figure 2.5 Pimoroni's Fan SHIM is an efficient cooling solution for the Raspberry Pi 4.
Note:
If you have a power outage in your home, the microSD card of your Raspberry Pi may
become corrupt and your home automation gateway will not work anymore after the
power comes back on. If you want to protect for this scenario, use a UPS (uninterruptible
power supply) for your Raspberry Pi. It has an emergency battery with enough power to
safely turn off your Raspberry Pi.
In this book, you'll use Raspberry Pi OS (formerly called Raspbian) as the operating system
for your Raspberry Pi. Raspberry Pi OS is a Linux distribution based on Debian GNU/Linux,
optimized for its use on a Raspberry Pi. It's the official operating system that the Raspberry
Pi Foundation recommends.
You'll use the Lite version: this doesn't have a graphical desktop, so you have to work with
Raspberry Pi OS using the command line. Some familiarity with the Linux command line is
therefore recommended, but I'll explain the commands that you need.
● 32