IBM Developer Day | Bengaluru | March 14th Register now
By Anna Gerber | Updated January 3, 2018 - Published June 30, 2017
This article is part of the IoT 101 learning path, a quick-start guide for IoT developers.
Sometimes the most effective home automation projects are the ones that solve very simple problems.
Here’s my simple home automation IoT solution. I have trouble hearing my door bell when I’m working upstairs wearing headphones, so I’m going to create a smart doorbell system:
I’ll expose all of these sensors as Apple Home Kit devices. Home Kit is Apple’s home automation framework that allows you to read from sensors and control smart devices that are installed in your home by using the Home app from an iPhone or iPad running iOS 10, so I’ll be able to see the sensor readings from my phone ().
I’ll step through how I built my home automation solution with Node-RED running on a Raspberry Pi with a handful of inexpensive sensors and actuator. You can use this tutorial to build your IoT skills and build a similar home automation solution, or you can adapt the concepts as you get started with developing your own IoT solutions.
Transcript of video
I use a MacBook Pro as my development environment, however any Linux or Windows PC with wifi and equivalent software should be suitable. I used the following software and hardware to build my home automation system.
I’ll be running my home automation device off a USB power supply plugged into the main power, so I won’t need to worry about batteries or power management. Running off main power also means that it doesn’t have to be a priority for me to select a low power networking technology like Zigbee that would typically be adopted for commercial home automation systems.
Wifi was my preferred choice of networking technology for this project because I already have a Wifi network in my house, and the range of my home automation system only needs to reach within the local area network (LAN) in my house. Wifi also has the advantage of having a high data throughput, which means I should have no trouble gathering data from the connected sensors. You can read more about selecting networking technologies in my article “Connecting all the things in the Internet of Things.”
I’ve chosen to use a Raspberry Pi Zero W but you could use any Raspberry Pi with wifi. The Raspberry Pi Zero W is a readily available, small, and inexpensive single-board computer with built-in Bluetooth and wifi, which makes it ideal for IoT solutions that need to hook in with an existing wifi network or integrate with consumer devices by using Bluetooth. The Raspberry Pi is a popular board among hobbyists, so there are many online resources and compatible devices available to support developers. I’ll be running the device off a USB power supply plugged into the main power, so I won’t need to worry about batteries or power management.
For more detail on the hardware requirements to consider when selecting hardware for IoT projects, read my IoT Hardware Guide.
I’ve selected an off-the-shelf Passive Infra-red (PIR) sensor to detect motion, and a DHT-11 digital temperature and humidity sensor module to attach to my device. These sensors are both inexpensive digital 5V sensors that are compatible with both Arduino and Raspberry Pi. The Raspberry Pi does not have a hardware analog-to-digital converter on board, so digital sensors are a good choice over analog sensors that would require an additional analog-to-digital converter. The output components I’ll be using include a Piezo buzzer element to beep whenever motion is detected during prototyping, and a 5V relay with a 12V LED strip that will be triggered by motion in the final iteration of the system.
Follow the steps in this video to connect the PIR sensor, buzzer, and DHT11 temperature and humidity sensor. Use the figures and table that follow the video to help you assemble the circuit.
If your Raspberry Pi did not come with the GPIO pins attached, you’ll need to solder the pins onto the board first. The circuit that I used is shown in .
For each component, the positive pin connects to 5V, the negative pin connects to ground (GND), and the data pin connects to a GPIO pin on the Raspberry Pi pin headers, as shown in . In , the number of the pin in the headers is in the middle column, while the Raspberry Pi GPIO pin identifier is shown in the third column. The pin layout of the Raspberry Pi pins is provided in after the table.
When you attach components to the Raspberry Pi GPIO headers, refer to the pin diagram in .
If you have chosen to use a different Raspberry Pi with a wifi adapter that allows you to initially set up the Pi over an Ethernet cable, you can watch the Hands-on IoT video for directions on how to set up your Pi without a keyboard, mouse, or monitor.
The Pi Zero is tiny, so it does not have full-sized ports for HDMI or USB on board. It is possible to add a mini-HDMI-to-HDMI cable or adapter, and also a micro-USB on-the-go adapter and USB hub to plug in a monitor, USB mouse, and keyboard. After you have the Raspberry Pi set up, however, you won’t need those peripheral components, so I prefer to skip the dongles and configure the Pi Zero in headless mode.
Here are the steps to setting up your Raspberry Pi Zero W for this tutorial:
If you are using a micro SD card that was preinstalled with Raspbian, you can skip this step. However, if you are starting with a blank card, you’ll first need to install the latest version of the Raspbian operating system onto the micro SD card. I’m using an 8 GB micro SD card and setting up the card from my MacBook by using an SD card adapter. You can follow along with the instructions on the Raspberry Pi website.
I am not planning on switching between operating systems, so for this project, I decided to use the Raspbian Jessie image, rather than NOOBS. I’ve installed the Etcher app, so flashing the image to the SD card is as simple as selecting the downloaded .zip file, selecting the micro SD card, and then clicking Flash ().
You also need to add some configuration files on the SD card to set up the wireless network settings to connect the Pi to the local network.
You need to add a file named wpa_supplicant.conf in the root of the SD card. This file contains the wifi settings, including the network password and SSID. Use a text editor like Sublime Text or Atom to edit the contents of the file, add the following lines and specify your values for the ssid and psk variables:
ssid="<wifi network name>"
You also need to add a file called ssh (the file itself doesn’t need to contain any content) to the root level of the SD card. The presence of this file will enable SSH on the Pi when it starts up.
Now you can insert the micro SD card into the Raspberry Pi Zero W, plug a micro USB cable into the power jack on the Pi, and power it up.
Assuming your wireless router is set up to automatically assign IP addresses to connected devices over DHCP, you won’t know which IP address your Pi has been assigned to begin with. However, you can use mDNS with the .local special-use domain name to find and connect to the Pi without needing to know the address. The most common implementation of mDNS is Apple’s Bonjour service. If you are on a Mac, this service will work out of the box. On Linux, this service is provided by Avahi (Zeroconf). On Windows, if you have iTunes installed, you will already have Bonjour installed. If not, the quickest way to get your Pi up and running is to install the Apple Bonjour print services.
By default, the Raspberry Pi is configured with the host name raspberrypi and the default user is pi, so you can connect to the Pi by using SSH:
After you are connected to the Pi, you can run the ifconfig command to find its IP address, or check the startup messages when you run node-red. If you don’t want to install Bonjour to use mDNS, you may prefer to check your home router DNS logs to find the IP address of the pi and substitute the IP address for the raspberrypi.local host name.
See this Hands-on IoT video for an introduction to Node-RED.
Node-RED is a development tool with a visual web interface for wiring together IoT flows that connect physical devices, APIs, and other online services. Node-RED and node.js come installed by default with Raspbian Jessie, however the version that is bundled with it is a little old. So, one of the first things I did after I booted up the Raspberry Pi was to update Node-RED and node.js and also install some third-party modules to make it easier to work with the DHT-11 temperature sensor and home kit.
You can read more about running Node-RED on Raspberry Pi in the Node-RED documentation.
Node-RED provides a graphical web interface for programming IoT flows. The flows connect nodes that represent physical devices and their attached sensors and actuators, with nodes that implement custom functions as well as nodes that provide interfaces to libraries or services, and specify how messages with data payloads are passed between them. We can add additional kinds of nodes by installing additional (contrib) modules. Modules can be added by using the web interface by going to the menu at the upper-right of the web interface, then selecting ‘Manage palette’ and searching for contributed modules under the Install tab in the sidebar on the left-hand-side of the screen.
In this video, I SSH into my Raspberry Pi Zero, start and upgrade Node-RED, and then install the node-red-contrib-homekit module that is used for integrating with the Apple Home Kit, all by using the web user interface.
Trascript of video
In addition to installing Node-RED modules by using the web interface, you can also install modules by using npm on the command line.
For example, install the homebridge module to allow integration with the Apple Home Kit accessories:
sudo npm install ‑g homebridge
To make it easier to work with the DHT11 temperature and humidity sensor, you need to install the node-red-contrib-dht-sensor module (https://flows.nodered.org/node/node-red-contrib-dht-sensor). The documentation for this module lists a dependency on the BCM2835 library and the node-red-dht module. So, we’ll need to install those modules first. The installation instructions and the latest version of the BCM2835 library are available at http://www.airspayce.com/mikem/bcm2835. From the command line on the Raspberry Pi, download and install the library by using the following commands:
tar xzf bcm2835‑1.52.tar.gz
sudo make check
sudo make install
After you install the library, change to the .node-red directory and install the node-dht-sensor and the node-red-contrib-dht modules by using npm.
sudo npm install ‑‑unsafe‑perm ‑g node‑dht‑sensor
sudo npm install ‑‑unsafe‑perm ‑g node‑red‑contrib‑dht‑sensor
You can list the modules that you have installed from within the .node-red directory with npm:
npm ls ‑‑depth=0
I’ve found it’s a good idea to restart Node-RED after I install modules manually:
sudo service nodered restart
After you install the DHT module, you’ll see the additional DHT22 node type appear in your Node-RED palette (in the web interface).
I used Node-RED to set up the logic for my home automation system. I use it to read from and control the components hooked up to the Raspberry Pi’s GPIO pins. I split the functions into two flows. The first flow reads from the PIR sensor and triggers the buzzer when motion is detected. It also exposes the PIR sensor reading to the Apple Home Kit (you can read more about the home kit in the Apple documentation). The second flow reads from the temperature and humidity sensor and sets up two Home Kit devices, one for the temperature, and one for the humidity, so that I’ll be able to see these sensor values from the Home app.
Follow the steps in the video to set up a Node-RED flow between the PIR sensor and Buzzer so that whenever the PIR sensor detects movements the buzzer beeps. I also configure an Apple Home Kit node so that the state of the motion sensor can be displayed in the Apple Home app on my phone.
You can import the configuration for the completed flow from the MotionSensor.json file in my GitHub repo.
To import a flow, go to the hamburger menu at the upper-right of the web interface, and select Import From > Clipboard then paste in the configuration for the new flow.
Follow the steps in this video to add a second flow for the DHT11 temperature and humidity sensor, and expose the sensors as Apple Home Kit devices so you can read the temperature in degrees Celsius, and the relative humidity as a percentage, from within the Home app.
You can import the completed temperature and humidity sensor flow from the TemperatureAndHumiditySensor.json file in my GitHub repo.
You can share any of the flows that you create by exporting them to JSON format. To export a flow, select all of the nodes in the flow first, and then go to the hamburger menu and select Export (if you don’t select any nodes, the export options are disabled).
Follow the steps in this video to add a relay to the circuit to turn an LED light strip on and off, and trigger this behavior with the PIR sensor. I also step through setting up a home kit device to view and control the state of the LED strip.
While the Piezo buzzer was great during prototyping for giving instant feedback on whether my door sensor was working, I found that it wasn’t very loud after I put it inside the plastic box I was using as an enclosure, so I decided to remove it from the final version of my circuit. Removing the Piezo buzzer also gave me more space in the enclosure to install my relay, the electrically controlled switch that is used to turn a strip of LED lights on and off again.
The circuit diagram for attaching the relay is shown in . As outlined in Table 1, The PIR motion sensor is attached to pin 7, the DHT11 temperature and humidity sensor is attached on pin 40, and the relay has been added on pin 13.
Each component needs to be connected to a ground and 5V pin, and while there are plenty of ground pins, there are only two 5V pins on the Pi Zero, so you can either use a small breadboard or solder some wires together to share the 5V connection between all three components.
The LED strip needs to be powered by a separate 12V power supply, and wired up to the normally open (marked NO) terminals on the relay () so that activating the relay will close these terminals, which will trigger the LED to light up.
After you create all of the electronic circuits and Node-RED flows, the next step is to install the home automation system. I’ve bundled all of the wires and components together into an enclosure that is made from a clear plastic container with cutouts for the PIR sensor and cables. (I 3D-printed a custom case and waterproofed it using silicone for a more permanent enclosure.)
This video steps you through connecting the relay and LED strip and bundling the components into a prototype enclosure.
I’ve stepped through building the circuits and programming the flows for a basic Raspberry-Pi-based home automation system using Node-RED, with Apple Home Kit integration.
Some obvious improvements to this prototype system would be to add a light sensor and only trigger the LED strip to turn on when the strip is in shade or darkness. I have a Bluetooth Low Energy tag (Chipolo) on my house keys, and there is a Node-RED Chipolo module for reading these tags, so an alternative improvement would be to read the tag to recognize when I am approaching the door to turn the lights on, rather than triggering the lights by motion alone.
Because this home automation system is a prototype, I haven’t focused on security, however by default Node-RED is not secured. So before installing this system, the next step would be to add authentication and secure the editor (See https://nodered.org/docs/security).
The author used the circuit diagrams from these fritzing components to create her own circuit diagrams for this tutorial:
In this series, you will learn how to build a simple - but complete - home automation system. In doing…
After presenting a broad overview of the concepts and skills that IoT developers need, this learning path guides you through…
To be a part of the IoT technological revolution, you’ll need to explore the architectures, designs, and development best practices…
Back to top