Home automation is everywhere!
But just what is home automation? One thing I learned while I was doing this project is that there are numerous definitions of the term “Home Automation.” To me, it means using IoT technology to remotely control tasks in my home that I would normally perform manually.
For example, instead of walking up to my living room lamp, reaching out with my hand, and turning it on, I can turn the light on and off from anywhere I have access control, whether that is using a panel somewhere in my home or on my computer, or from half-way around the globe using my smartphone.
That definition sounds neat, but, in practice, a home automation system is a complicated symphony of components from different manufacturers that must be choreographed to perform the functions of the system. The components must understand a common set of protocols — for this project, just one, OOK/ASK encoding that uses the 433.92 MHz frequency band — in order to be controlled, report status, and in general be a part of the home automation system as a whole.
One major complaint about the current home automation landscape is that the various devices are “fragmented.” Typically, devices are from different manufacturers and often speak different protocols. These differences cause headaches for home automation system integrators who must deal with the question of how to stitch these fragmented components together to provide a good homeowner experience.
While I won’t really go into this issue per se, you need to be aware that when you purchase IoT devices that they might not play well together. So, I carefully chose the devices for this tutorial because I tested them myself and I know that they all work together. Be sure to follow the instructions carefully, and unless you know what you’re doing, don’t deviate from my Parts List in this tutorial.
In this tutorial, most major sections start with a short video (< 7 minutes), in which I demonstrate the concepts of IoT devices. The text of the section contains the detailed explanations and figures that explain the concepts I’m trying to convey in that section. Where it makes sense — and so the text in the section flows better — the video will be at the end of the section. The point is that this tutorial contains text, figures, and video, which are all woven together to try to immerse you in the technology so that you have the best possible learning experience. I hope I have succeeded.
What you will build in this series
You will build the skeleton of a remote control system, not a complete home automation system. But, when you’re done with the project, you will be equipped with the knowledge (and hopefully the drive) to take it further if you choose.
The project that you build will control a set of IoT devices, including:
- Smart electric outlets
- Home alarm system
You will control both of these IoT devices from an Android smartphone that is running the Android operating system (or through the Android emulator if you do not have an Android smartphone).
The hardware components of the system are:
- Raspberry Pi 3, Model B, v 1.2, which is the IoT computer that will control the devices that comprise the home automation system
- 433-MHz receiver and transmitter modules, which receive signals from, and transmit signals to, the system
- Wireless electrical outlets that listen for on/off signals in the 433 MHz band and respond accordingly
- Wireless home alarm system with window and door sensors that are controlled by and report status by using signals in the 433 MHz frequency band
You’ll set up the hardware in this part of the tutorial series.
The software components of the system are:
- The Raspbian OS (Stretch release), which is the operating system that runs on your Raspberry Pi 3.
- Various utilities to work with the Pi, and the RF receiver and transmitter modules: Wiring Pi, which is an open source library that is used to access the GPIO pins on the Raspberry Pi rc-switch, which is a low-level C++ library for controlling 433-MHz hardware that runs on various platforms, including the Raspberry Pi 433Utils, which uses rc-switch and wiringPi to communicate with the RF receiver and transmitter
- The home-automation project, which is the high-level code that runs the home automation platform itself. You will write much of this code yourself in Part 2, but don’t worry, I’ve provided a complete solution in GitHub if you get stuck (or just feel like skipping ahead).
- Android Studio. While you can download the home automation app from the Google Playstore, you can also download the source for it from GitHub, and load it up in Android Studio. Or, if you don’t have an Android device, you can run the app code in the Android emulator. Either way, I’ll introduce you to the source code, and Android Studio is the IDE that you’ll use.
Last (but definitely not least) is the IBM Cloud service (Watson IoT Platform) that you’ll use to securely connect your home automation platform and the various devices connected to it (the “things”) to the internet.
By using the Watson IoT platform, you can securely connect your Raspberry Pi to the internet and control it all with the magic of Message Queue Telemetry Transport, or MQTT (okay, it’s not magic, but it is very cool, and an ISO standard).
I’ll show you how to define the API key that the Android application needs and the device ID for the Raspberry Pi. I’ll also show you how to connect the Raspberry Pi that is running your home automation system to the Watson IoT platform. Then, I’ll show you how to control the home automation system through the Android app both from your Android device and from the Android emulator.
You’ll see how to connect the home automation system through the IBM Cloud to the Android app in Part 3 of the tutorial series.
I love DIY projects, but there’s nothing more irritating to me than working on a really cool project, only to find half-way through that the author didn’t use the parts depicted in the illustrations, or worse, is using parts that are no longer available!
All of the parts in this list are readily available from Amazon.com, and I used them myself in this project. You can substitute parts if you have issues getting them from Amazon, but try to stick as closely as possible to the parts list below, or you might run into trouble.
Below you will find a table where you will find the same parts that I use in the project. And, like I said, all of the parts are available from Amazon, so you should be able to order them from pretty much anywhere in the world.
Table 1. Parts List
|Part No.||Description||Price (USD)||Order link|
|1||CanaKit Raspberry Pi 3 with 2.5A Micro USB Power Supply||43||Amazon.com|
|2||CPVAN Window Door Security Alarm Sensor, DIY Wireless Home Security Burglar Alarm System with 1 Siren Unit, 2 Magnetic Sensors, and 1 Remote Control||30||Amazon.com|
|3||Etekcity Unify Made Upgraded Wireless Remote Control Electrical Outlet Switch (H1)||21||Amazon.com|
|4||Kuman 830 MB-102 Tie Points Solderless Breadboard GPIO T Type Expansion Board 65-pcs Jumper Cables wires 40-pin Rainbow Ribbon Cable 100-pcs resistance K73||13||Amazon.com|
|5||UCEC XY-MK-5V / XY-FST 433 MHz Rf Transmitter and Receiver Module Link Kit for Arduino/Arm/McU/Raspberry pi/Wireless DIY(6-pack)||11||Amazon.com|
|6||SanDisk 16 GB Mobile MicroSDHC Class 4 Flash Memory Card With Adapter||7||Amazon.com|
|7||(optional) 433MHz Antenna Helical Spiral Spring Remote Control for Arduino Raspberry Pi||6||Amazon.com|
Project cost (estimated): 125 USD.
Consider these notes about the parts in this parts list:
- Parts 2 and 3: It’s not strictly necessary that you order these exact models (they might not be available or Amazon is out of stock, for example). Make sure, though, that whatever you order uses the OOK/ASK encoding in the 433.92 MHz frequency band, and has a remote control (so you can capture the frequency encoding and pulse length, which I will cover in detail in Part 2 of this series).
- Part 4: You don’t have to use a breakout board. If you already have Female/Male jumper wires, you can directly connect your Raspberry Pi’s GPIO pins to the breadboard. But the breakout board makes working with the GPIO pins on the Pi much easier, and you’ll need jumper wires and a breadboard anyway, so the Kuman bundle is a great deal.
- Part 5: If you don’t order the 6 pack of receiver/transmitter modules, make sure you order at least two of each because there is nothing more disappointing than ordering one of these cheap parts only to find that you got one of the 1% or so that is DOA. (Plus you’ll have spares for all the other DIY 433-MHz projects you’ll want to do when you finish this series!)
- Part 6: You don’t have to use a SanDisk brand (I have used Samsung micro SD flash drives with great success, for example), but the card does need to be a micro SD card, or it won’t fit in your Raspberry Pi! The drive also doesn’t have to be 16 GB, but I recommend that you use at least a 16 GB card. And, make sure that your micro SD card comes with an adapter.
- Part 7: The 433-MHz-specific antennae are optional, but I recommend that you order them and use one in your transmitter module to increase its range by a few meters. You’ll see in the illustrations and videos that I use them.
One final note: I’m assuming that you have a USB keyboard and mouse, and an external display that uses an HDMI connector. You will need these components in order to work with the Raspberry Pi. If not, check out this video that shows you how to do a “headless” setup of your Raspberry Pi, which means “you control it from over the network.” However, you will need an Ethernet cable to do the initial installation.
Set up your Raspberry Pi 3
Now that you have the parts that you need, it’s time to set up your Raspberry Pi 3, which will control all of the devices in your home.
Grab the 16 GB micro SD drive (part #6), and get ready to install (or flash) the Raspbian OS image on to it. After that, you’ll insert the card into the micro SD slot on the Pi, connect an external monitor, keyboard, and mouse, and boot the Pi up for the first time.
Don’t like boring text and screen captures? I get it. How about a video instead? Or, why not use them both?
Flash the Raspbian image
If you’re familiar with flashing OS images onto micro SD media, you can skip this section.
To create the bootable OS image on the micro SD card, you’ll use Etcher. It’s super easy and intuitive to use. If you want to use a different tool, that’s up to you, but the instructions below (and in the video that follows) I’ll use Etcher.
Download the Raspbian Stretch image
Before you can create the Raspbian image on the micro SD card, you have to download the image to your computer. Go to the Raspbian downloads page and choose RASPBIAN STRETCH WITH DESKTOP, which will give you a nice GUI to work with after your Raspberry Pi is up and running.
The image is a bit larger than the “Lite” image, but the GUI is totally worth the extra time it takes to download.
It will take several minutes to download the image, even with a fast connection, so be patient. After the download finishes, be sure to verify the SHA checksum. Figure 1 shows the SHA-256 checksum for the Raspbian Stretch with Desktop image.
Figure 1. Raspbian Stretch with Desktop download page
To verify the checksum on MacOS open a Terminal window, go to the directory where you downloaded the image, and run the command:
shasum -a 256 2017-11-29-raspbian-stretch.zip
You’ll see output similar to the following output:
$ shasum -a 256 ~/Downloads/2017-11-29-raspbian-stretch.zip 64c4103316efe2a85fd2814f2af16313abac7d4ad68e3d95ae6709e2e894cc1b /Users/sperry/Downloads/2017-11-29-raspbian-stretch.zip $
The name of the .zip file that contains the image might vary from what you see (as new releases come out). This output is what it looks like at the time of this writing.
Copy the checksum (the long hexadecimal number) to a text editor. Now copy and paste the checksum from the Raspbian downloads page and compare the two. If they do not match, do not open the file! Instead, first try to download the image again (it might have somehow gotten corrupted during the download process, which would cause the checksums not to match).
If that doesn’t work, go to the Raspberry Pi forum and search for issues related to Stretch (and post a message if you can’t find anything to help you, but always search first). The issue is not always a problem with the file as you can see from this post.
After you download the image to your computer, you’re ready to flash it to the micro SD card that you’ll use to boot the Pi. You might wonder why you can’t just copy the OS image to the SD card like you would any other file. Well, you could certainly do that, but the Pi won’t know what to do with it, and you’ll be staring at a blank screen (hey, and if that’s your thing, I’m not here to judge).
But, if your goal is to get your Pi to boot and run Raspbian, you’ll need to flash the image to the card in a way that the Pi can boot properly. You need a program that can make the micro SD card bootable, and (you guessed it) Etcher can do that.
First, go to the main Etcher page and choose the download that matches your platform. I use MacOS so I’ll choose Download for MacOS.
After you download Etcher, run the installation for your platform. For MacOS, Etcher is downloaded in a DMG file. For Windows, run the setup program (exe). On Linux, extract the .zip file.
Now that Etcher is installed, it’s time to fire it up. Flashing a bootable disk image is a three-step process. First, click Select image, go to the folder where you downloaded the image, select the image, and click Open. The Etcher UI opens.
Next, insert the micro SD card into your computer’s SD drive (using the adapter if necessary), and make a note of how your computer’s OS mounted the drive. Then, click Select drive to select the SD drive as the destination for the bootable image.
Finally, click Flash, and several minutes later you’ll have a bootable image with Raspbian Stretch loaded and ready to go.
Boot up your Pi 3
Now that you’ve flashed the Raspbian Strech OS image onto the micro SD card, insert it into the Pi’s micro SD slot, which is located on the underside of the board, see Figure 3 (labeled “Micro SD Card”).
Underside of the Raspberry Pi 3, Model B
The micro SD card will fit in only one orientation, so if you’re having trouble getting it to seat, make sure that the contacts of the micro SD card are facing towards the board, as shown in Figure 4.
Figure 4. The Raspberry Pi 3, Model B Layout
The Micro SD card usually easily slides into place if you have it oriented properly. Don’t force the card to fit or you could damage the card, your Raspberry Pi, or both.
The Raspberry Pi 3, Model B has 4 USB ports, an HDMI port, and a USB mini-B port to supply the Pi with power. Connect the USB mouse, keyboard, and monitor to the Pi’s USB and HDMI ports. Then, connect the Pi’s power adapter USB mini-B connector to the PWR IN port on the Pi. See Figure 4. Plug the power adapter into an electrical outlet that is suited to the power supply that came with your CanaKit (Part 1), and the Pi should boot up.
When the Pi boots up, if the Pi is booting properly, you’ll see a screen that looks like this one:
When the Pi finishes booting, it will automatically log you in, and you should see a screen like this one:
Don’t see screens like these ones? I’ve flashed disk images and tried to boot the Pi only to see a blank screen staring back at me. In such a case, a number of things wrong might be wrong. First, check to see that your monitor is connected properly (not that that’s ever happened to me). Next, make sure that you inserted the micro SD card all the way into the slot.
If that still doesn’t fix it, chances are something went wrong when you flashed the image. Try to flash the image again. If that doesn’t work, make sure the image you downloaded didn’t get corrupted (file corruption is one of the reasons to use
shasum to verify the checksum).
Secure your Raspberry Pi
Before you go any further, you should change a few of the default settings. To access the Raspberry Pi settings, click the Raspberry in the upper left corner of the screen and choose Preferences > Raspberry Pi Configuration, and you’ll see a dialog like Figure 7.
I recommend that you take these security precautions (see Figure 7):
- Change the default password.
- Disable Auto Login.
Figure 7. Raspberry Pi Configuration Dialog
Change the default password. To change the default password (which is
raspberry, in case you wondered), click the Change Password button. Then, on the dialog that comes up, enter the new password, confirm it, and click OK. The next time you restart your Pi you will need to log in, so make sure and pick a password you will remember (yeah, I know, but I had to say it).
Disable Auto Login. When Auto Login is checked, every time you start your Pi, you will be logged in as the pi user. I tend to work with my Pi a lot over the network (rather than sitting in front of it), so there are lots of times when I turn it on and walk away. If Auto Login was enabled, anyone could walk right up and use my Pi. To prevent this security issue, uncheck As current user next to Auto Login.
Enable SSH. By default, SSH is disabled on your Pi. I recommend that you enable SSH access. I really like being able to use my Pi from anywhere in my home (and I think you will too). Click the Interfaces tab, and select Enabled next to SSH.
Connect the Pi to the network
Finally, your Pi will need to be able to connect to your network. If you’re using the Ethernet port, you can skip this step. In any event, did you know that the Raspberry Pi 3 has built-in wifi? To enable wifi, click the Network icon in the upper right corner of the screen, and select the network that you want to connect to.
Enter the pre-shared key of your wifi network, and click OK.
Your Pi is now configured and secured, and it can connect to the internet, which it will need to be able to do for Parts 2 and 3 of this series.
Connect your Raspberry Pi 3 to a breadboard
So, you’ve ordered all of the parts on the parts list, and you’ve set up your Raspberry Pi. At this point, you might be wondering exactly how you’re going to build this home automation system. I mean, where do the receiver and transmitter modules go? How do they get power? How do they send and receive signals to and from the Pi?
The answer is to use a breadboard.
If you’re like me, at this point you want to build something. Well, you’re in luck. I’ve put together a video that shows you how to build a simple LED circuit so you can get familiar with how the breadboard is laid out and how to use it before you tackle the rest of the project.
Why use a breadboard?
A breadboard, like the one in Figure 9, is a specially constructed circuit board that lets you quickly build electronic circuit prototypes.
Figure 9. A breadboard
To build circuits, you insert jumper wires into the breadboard’s holes to make connections (see Figure 10).
Figure 10. Jumper wires
Internally, the breadboard’s holes are connected in a very specific (and consistent) way. Because of the way the breadboard’s connections are laid out, you know which holes in the breadboard are connected and which ones aren’t, allowing you to easily change the flow of the current in the circuit.
In Figure 11 you can see two types of holes: vertically connected groups of 5 holes where your circuit components connect to each other and horizontally connected holes called the rails. The rails are holes that are connected horizontally along the top and bottom of the breadboard. The rails are where you will supply voltage and ground to your circuits (as in “power rails”).
Figure 11. A breadboard’s internal connections
Set up the breakout module
Now that you’ve seen how a breadboard is laid out, it’s time to set up the expansion module (Part 4). The expansion module has two rows of 20 pins each. Align the expansion module so that each set of horizontal pins is seated across the gap in the middle of the board as shown here:
Wire up the breadboard’s rails
Not all of the rails of the breadboard are connected to each other. This is by design so that different power sources can be supplied to the breadboard for more complex circuits.
For our purposes, we will be using the +5V supply that is coming off of the Pi to supply the receiver and transmitter modules, so all of the rails can be connected to each other. When you have the rails all connected to each other, it will look something like this:
Now all of the breadboard’s rails are connected. I typically use white wires to illustrate power (
+) and black to indicate ground (
-). You don’t have to use black and white jumper wires, you can use whatever colors you want. But, keep in mind that, as your circuits get more complex, using a consistent color coding idiom helps you diagnose problems faster.
Set up the 433-MHz receiver and transmitter
The final step in this part of the tutorial series is to set up the 433-MHz hardware devices. These devices use the ISM frequency band between 433.05 and 434.79 MHz, with a center frequency of 433.92 MHz as defined by the Electronic Code of Federal Regulations, Title 47, Part 15, Subpart C, paragraph 15.231 (I’m not making this up).
About 433-MHz hardware
Hardware that is designed to operate in this frequency band is everywhere. From car remote controls to alarm systems to electrical outlets to home weather stations, the 433 MHz band is full of products that you can control, if you know how.
According to the specification, you are allowed to transmit in the 433 MHz band if the signal meets these conditions:
- It is not continuous
- It cannot transmit for more than 5 seconds after it is activated
- It is not regularly scheduled to control a device (regularly scheduled transmissions to poll for status are allowed)
- It does not exceed 2 seconds per hour
There are also limits to the strength of the signal, etc., etc., etc.. Basically, I’m just trying to say, “Don’t do something stupid like modify your 433-MHz hardware to create your own pirate radio station to broadcast in the 433 MHz band and you should be fine.”
How does it work?
The hardware that we use in this tutorial uses a technique where the transmission’s amplitude (carrier signal strength) is altered to encode information in binary form. This technique is called Amplitude Shift Keying (ASK for short), where the “1” signal is a large amplitude, and the “0” signal is a lower amplitude.
Some 433-MHz devices use a technique called On-Off Keying (OOK), which is a variant of ASK. With the OOK technique, the device transmits a large amplitude to represent a “1”, but no carrier signal at all to represent a “0”, thus saving power versus an ASK device, which must still draw current in order to send a “0” signal.
Asleep yet? No worries. Just remember, at the end of the day, it’s all ones and zeros. And, you’ll have software to help encode and decode the signals from the hardware that you’ll be using to build the project. But, you’ll have to wait until Part 2 for the software building (trust me, it’ll be worth it).
If you ordered the 6 pack of receiver and transmitter modules (Part 5), there are actually 12 items: 6 receivers and 6 transmitters. The receiver and transmitter look different from one another, so you need to be to tell them apart. Figure 13 shows you what they look like.
Figure 13. The 433-MHz receiver and sender modules and their pinouts
Set up the 433-MHz receiver module
First, set up the receiver module. Place it on the breadboard a few holes away from the right edge of the breadboard, like this:
Working around the breadboard clockwise from the upper left corner, follow these steps to make the circuit connections using the jumper wires from the expansion board kit (Part 4):
- Connect the 5V pin of the Pi expansion board to the positive (+) rail
- Connect the GND pin of the Pi expansion board to the negative (-) rail
- Connect the GND pin of the receiver module to the negative (-) rail
- Connect the VCC pin of the receiver module to the positive (+) rail
- Connect the data pin of the receiver module (next to the GND pin) to GPIO pin #27 of the expansion board
When you’re finished, the connections should resemble this:
Set up the 433-MHz transmitter module
Place the transmitter module on the breadboard between the receiver module and the Pi expansion board, like this:
Working right-to-left from the transmitter module, follow these steps to make the circuit connections using the jumper wires from the expansion board kit (Part 4):
- Connect the GND pin of the transmitter module to the negative (-) rail
- Connect the VCC pin of the transmitter module to the positive (+) rail
- Connect the data pin of the receiver module to GPIO pin #4 of the expansion board
Here’s what it looked like when I finished the project, including a case for my Raspberry Pi (which I ordered from —you guessed it —from Amazon.com).
Confused by the previous section? Maybe a video will help. In the video below, I’ll show you how to connect everything up.
Conclusion to Part 1
All the hardware for your home automation system is wired up and ready to go. It might not seem like much because you can’t see anything happen yet, but believe me, you’ve done a lot of work. The hardware portion of the setup is done. The rest will be done with software.
Do you need a break? Or, are you ready to tackle Part 2, where you set up the system software for your home automation system.
The author used the circuit diagrams from these Fritzing components to create his own circuit diagrams for this tutorial:
- AdaFruit library: https://github.com/adafruit/Fritzing-Library/