Book Image

BeagleBone Home Automation Blueprints

By : Rodolfo Giometti
Book Image

BeagleBone Home Automation Blueprints

By: Rodolfo Giometti

Overview of this book

BeagleBone is a microboard PC that runs Linux. It can connect to the Internet and can run OSes such as Android and Ubuntu. BeagleBone is used for a variety of different purposes and projects, from simple projects such as building a thermostat to more advanced ones such as home security systems. Packed with real-world examples, this book will provide you with examples of how to connect several sensors and an actuator to the BeagleBone Black. You’ll learn how to give access to them, in order to realize simple-to-complex monitoring and controlling systems that will help you take control of the house. You will also find software examples of implementing web interfaces using the classical PHP/HTML pair with JavaScript, using complex APIs to interact with a Google Docs account, WhatsApp, or Facebook. This guide is an invaluable tutorial if you are planning to use a BeagleBone Black in a home automation project.
Table of Contents (18 chapters)
BeagleBone Home Automation Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

The BeagleBone Black is an embedded system that is able to run an embedded GNU/Linux distribution as well as normal (and powerful) distributions like Debian or Ubuntu, and to which the user can connect several external peripherals via two dedicated expansion connectors.

Because it has powerful distribution capabilities with an easily expandable embedded board, the BeagleBone Black system is a state-of-the-art device that allows the user to build powerful and versatile monitoring and controlling applications.

This book presents several home automation prototypes in both hardware and software in order to explain to you how we can use the BeagleBone Black board connected with several devices to control your home.

Each prototype is discussed in its respective chapter in both hardware and software, explaining all the connections and the software packages necessary to manage several peripherals. Then, the code to glue all of it together is explained in detail till the final test of each project.

The hardware devices used in this book have been chosen in order to cover all the possible connection types we can encounter while working with a BeagleBone Black board, so you will find I2C, SPI, USB, 1-Wire, serial, and digital and analog devices.

The programming languages used in this book have been chosen according to the rule to find the quickest and easiest solution to solve the current problem; in particular, you can find example code in Bash, C, PHP, Python, HTML, and even JavaScript.

Note

Warning! All the projects in this book are prototypes and cannot be used as final applications.

Neither the author of this book nor Packt Publishing recommend or endorse these products to be used alone or as a component in any application without the necessary modifications to turn these prototypes into final products.

Neither the author of this book nor Packt Publishing will be held liable for unauthorized use of these prototypes. The user can use both the hardware and software of these devices at their own risk!

In the chapters where we will need to use a daemon or a kernel module, or where we will need to recompile the whole kernel, I've added a short description about what the reader should do and where they can get more information regarding the tools used; however, some basic skills in managing a GNU/Linux system, the kernel's modules, or the kernel itself are required (the reader can take a look at the book BeagleBone Essentials, Packt Publishing, written by the author of this book, in order to have more information about these topics).

What this book covers

Chapter 1, Dangerous Gas Sensors, will show how to use the BeagleBone Black to monitor some dangerous gases such as CO, Methane, LPG, and so on in a room and how to enable an acoustic and visive alarm in case of danger. Also, by using a GSM module, the user will be able to send an SMS message to a predefined phone number to alert, for instance, a relative.

Chapter 2, Ultrasonic Parking Assistant, will show how to use the BeagleBone Black to implement a park assistant. We will use an ultrasonic sensor to detect the distance between our car and the garage wall, and some LEDs to give feedback to the driver about the car's position in order to avoid collisions.

Chapter 3, Aquarium Monitor, will show how to make an aquarium monitor through which we'll be able to record all the environment data as well as control the life of our beloved fish from a web panel on our PC, smartphone, or tablet.

Chapter 4, Google Docs Weather Station, will take a look at a simple weather station that can also be used as an Internet-of-Things device. This time, our BeagleBone Black will collect environment data and will send them to a remote database (a Google Docs spreadsheet) in order to be reworked and presented into a shared environment.

Chapter 5, WhatsApp Laundry Room Monitor, will present an implementation of a laundry monitor room with several sensors capable of alerting the user directly on their WhatsApp account when a specific event occurs.

Chapter 6, Baby Room Sentinel, will present a possible implementation of a baby room sentinel capable of monitoring the room by detecting if the baby is crying or if the baby is actually breathing during sleep. Also, as a special feature, the system will be able to measure the baby's temperature with a contactless temperature sensor.

Chapter 7, Facebook Plant Monitor, will show how to implement a plant monitor capable of measuring light, soil moisture, and soil temperature (in the soil and outside it), and how to take some pictures, at specific intervals, via a webcam, and then publishing them on a Facebook timeline.

Chapter 8, Intrusion Detection System, will show how we can implement a low cost intrusion detection system with a reasonable quality level by using our BeagleBone Black and two (or more) webcams. The system will be able to alert the user by sending an e-mail message with a photo of the intruder.

Chapter 9, Twitter Access Control System with Smart Card and RFID, will show how to use a smart card reader as well as two kinds of RFID reader (LF and UHF) in order to show different ways to implement a minimal identification system for access control that can send an alert message to a Twitter account.

Chapter 10, A Lights Manager with a TV Remote Controller, will show how to manage a simple on/off device connected to our BeagleBone Black by using a TV remote controller or any infraredcapable device.

Chapter 11, A Wireless Home Controller with Z-Wave, will present how to implement a little wireless home controller by using a Z-Wave controller connected to our BeagleBone Black and two Z-Wave devices: a wall plug and a multisensor device.

What you need for this book

You need the following prerequisites to get the most from this book.

Software prerequisites

Regarding the software, the reader should have a little knowledge of a non-graphical text editor such as vi, emacs, or nano. Even if the reader can connect an LCD display, a keyboard, and a mouse directly to the BeagleBone Black and can use the graphical interface, we assume in this book that the reader is able to do little modifications to text files by using a text-only editor.

The host computer, that is, the computer the reader will use to cross-compile the code and/or manage the BeagleBone Black, is assumed to be running a GNU/Linux-based distribution. My host PC is running an Ubuntu 14.04 LTS, but the reader can also use a Debian-based system with a little modification, or they may use another GNU/Linux distribution, but with a little effort mainly for the installation of the cross-compiling tool. Systems such as Windows, Mac OS, or similar are not covered in this book due to the fact that we should not use low technology systems to develop code for a high technology system!

Knowing how a C compiler works and how to manage a Makefile could help, but don't worry, all examples start from the very beginning so that even a non-skilled developer should be able to do the job.

This book will present some kernel programming techniques, but it must not be taken as a kernel programming course. You need a proper book for such a topic! However, each example is well documented, and the reader will find several suggested resources.

Regarding the kernel, I'd like to state that by default I used the onboard kernel, that is, version 3.8.13. However, in some chapters, I used a self-compiled kernel, version 3.13.11; in that case, I gave a little tutorial about how to do the job.

If you're using a newer kernel release you may get minor issues, but you should be able to port whatever I did without problems. In the case that you are using a very recent kernel, please consider that the cape manager file /sys/devices/bone_capemgr.9/slots has been relocated to /sys/devices/platform/bone_capemgr/slots, so you have to change all related commands accordingly.

As a final note, I assume that the reader knows how to connect a BeagleBone Black board to the Internet in order to download a package or a generic file.

Hardware prerequisites

In this book, all code is developed for the BeagleBone Black board revision C, but the reader can use an older revision without any issues; in fact, the code is portable and it should work on other systems too!

Regarding the computer peripherals used in this book, I reported in each chapter where I got the hardware and where the reader can buy it, but, of course, they can decide to surf the Internet in order to find a better and cheaper offer. A note on where to find the datasheet is also present.

The reader should not have any difficulty in connecting the hardware presented in this book with the BeagleBone Black since the connections are very few and well documented. Readers don't require any particular hardware skills (apart from knowing how to use a soldering iron); however, a small amount of knowledge of electronics may help.

During the reading, I'm going to refer to BeagleBone Black's pins, especially the ones on the expansion connectors. All the pins used are explained, but, if needed, you can find a complete description of BeagleBone Black's connectors at http://elinux.org/Beagleboard:Cape_Expansion_Headers.

Who this book is for

If you are a developer who wants to learn how to use embedded machine learning capabilities and get access to a GNU/Linux device driver to collect data from a peripheral or to control a device, this is the book for you.

If you wish to manage your home by implementing different kinds of home automation devices that can interact with a smartphone, tablet, or PC, or if you are just working standalone, have some hardware or electrical engineering experience then this book is for you. Knowing the basics of C, Bash, Python, PHP, and JavaScript programming in a UNIX environment is also required.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Codes and command lines

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "To get the preceding kernel messages, we can use both the dmesg and tail -f /var/log/kern.log commands."

A block of code is set as follows:

CREATE TABLE status (
   n VARCHAR(64) NOT NULL,
   v VARCHAR(64) NOT NULL,
   PRIMARY KEY (n)
) ENGINE=MEMORY;

Any command line input or output given on the BeagleBone Black is written as follows:

root@beaglebone:~# make CFLAGS="-Wall -O2" helloworldcc -Wall -O2 helloworld.c -o helloworld

Any command line input or output given on my host computer as a non-privileged user is written as follows:

$ tail -f /var/log/kern.log

When I need to give a command as a privileged user (root) on my host computer the command line input or output is then written as follows:

# /etc/init.d/apache2 restart

The reader should notice that all the privileged commands can also be executed by a normal user by using the sudo command with the form:

$ sudo <command>

So, the preceding command can be executed by a normal user as:

$ sudo /etc/init.d/apache2 restart

Kernel and logging messages

On several GNU/Linux distributions, a kernel message has the following form:

Oct 27 10:41:56 hulk kernel: [46692.664196] usb 2-1.1: new high-speed USB device number 12 using ehci-pci

This is a quite long line for this book, which is why the characters from the beginning of the line until the point where the real information begins are dropped. So, in the preceding example, the line's output will be reported as follows:

usb 2-1.1: new high-speed USB device number 12 using ehci-pci

Long outputs and repeated or less important lines in a terminal are dropped by replacing them with three dots (...), as follows:

output begin
output line 1
output line 2
...
output line 10
output end

File modifications

When the reader should modify a text file, I'm going to use the unified context diff format, since this is a very efficient and compact way to represent a text modification. This format can be obtained by using the diff command with the -u option argument.

As a simple example, let's consider the following text in file1.old:

This is first line
This is the second line
This is the third line
...
...
This is the last line

Suppose we have to modify the third line, as highlighted in the following snippet:

This is first line
This is the second line
This is the new third line modified by me
...
...
This is the last line

The reader can easily understand that reporting the whole file each time for a simple modification is quite obscure and space consuming; however, by using the unified context diff format, the preceding modification can be written as follows:

$ diff -u file1.old file1.new
--- file1.old 2015-03-23 14:49:04.354377460 +0100
+++ file1.new 2015-03-23 14:51:57.450373836 +0100
@@ -1,6 +1,6 @@
 This is first line
 This is the second line
-This is the third line
+This is the new third line modified by me
 ...
 ...
 This is the last line

Now, the modification is very clear and is written in a compact form! It starts with a two-line header where the original file is preceded by --- and the new file is preceded by +++, then there are one or more changed hunks that contain the line differences in the file. The preceding example has just one hunk where the unchanged lines are preceded by a space character, while the lines to be added are preceded by a + character, and the lines to be removed are preceded by a character.

Serial and network connections

In this book, I'm going to mainly use two different kinds of connections to interact with the BeagleBone Black board: the serial console and an SSH terminal. The former can be accessed directly via the connector J1 (never used in this book) or via an emulated way over the same USB connection that is used to power up the board, while the latter can be used via the the USB connection above or via an Ethernet connection.

The serial console is mainly used to manage the system from the command line. It's largely used to monitor the system, especially to take the kernel messages under control.

An SSH terminal is quite similar to the serial console even if is not exactly the same (for example, kernel messages do not automatically appear on a terminal); however, it can be used in the same manner as a serial console to give commands and edit files from the command line.

In the following chapters, I'm going to use a terminal on the serial console or over an SSH connection indifferently to give most of the commands and configuration settings needed to implement all the prototypes explained in this book.

To get access to the USB emulated serial console from your host PC, you can use the minicon command as follows:

$ minicom -o -D /dev/ttyACM0 

Note that on some systems, you may need root privileges to get access to the /dev/ttyACM0 device (in this case, you can use the sudo command to override it).

As stated above, to get access to the SSH terminal, you can use the emulated Ethernet connection over the same USB cable that was used for the serial console. In fact, if your host PC is well configured, when you plug in the USB cable to power up your BeagleBone Black board, after a while, you should see a new cable connection with the IP address 192.168.7.1. Then, you can use this new connection to get access to your BeagleBone Black by using the following command:

The last available communication channel is the Ethernet connection. It is used mainly to download files from the host PC or the Internet, and it can be established by connecting an Ethernet cable to the BeagleBone Black's Ethernet port and then configuring the port according to your LAN settings.

However, it's quite important to point out that you can also get connected to the Internet by using the emulated Ethernet connection that was presented before. In fact, by using the following commands on the host PC (obviously GNU/Linux based), you'll be able to use it as a router, allowing your BeagleBone Black board to surf the Internet as if it was connected to its real Ethernet port:

# iptables --table nat --append POSTROUTING --out- interface eth1 -j MASQUERADE
# iptables --append FORWARD --in-interface eth4 -j ACCEPT
# echo 1 >> /proc/sys/net/ipv4/ip_forward

Then, on the BeagleBone Black, we should set the gateway through the USB cable by using the following command:

root@beaglebone:~# route add default gw 192.168.7.1

Note that the eth1 device is the preferred Internet connection on my host system, while the eth4 device is the BeagleBone Black's device as viewed on my host system so you have to modify the command accordingly in order order to suite your needs.

Other conventions

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

For this book, the example code can also be downloaded from the author's GitHub repository at https://github.com/giometti/beaglebone_home_automation_blueprints.

Just use the following command to get it at once:

$ git clone https://github.com/giometti/beaglebone_home_automation_blueprints

The examples are grouped according to the chapter names, so you can easily find the code during the reading of the book.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/BeagleBoneHomeAutomationBlueprints _ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.