Smart Home Automation with Linux and Raspberry Pi
Total Page:16
File Type:pdf, Size:1020Kb
This book was purchased by [email protected] For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance About the Author ................................................................................................................ xv About the Technical Reviewers ........................................................................................ xvii Acknowledgments ............................................................................................................. xix Introduction ....................................................................................................................... xxi ■ Chapter 1: Appliance Control: Making Things Do Stuff .....................................................1 ■ Chapter 2: Appliance Hacking: Converting Existing Technology .....................................53 ■ Chapter 3: Media Systems: Incorporating the TV and the HiFi ........................................87 ■ Chapter 4: Home Is Home: The Physical Practicalities ..................................................123 ■ Chapter 5: Communication: Humans Talk. Computers Talk ...........................................153 ■ Chapter 6: Data Sources: Making Homes Smart ...........................................................189 ■ Chapter 7: Control Hubs: Bringing It All Together ..........................................................217 ■ Chapter 8: Raspberry Pi ................................................................................................275 Index .................................................................................................................................297 v Introduction Home Automation is anything that your home does for you that makes living there more enjoyable or productive. A Smart Home is one that appears to apply intelligence to make that happen. To my friends, family, and visitors, my home is both smart and automated; I can e-mail my light switches, receive tweets from my CD player, and have a personalized TV guide e-mailed to me every day. To me, my home is a collection of existing open source software, some consumer-level hardware, and small pieces of glue code that make them all interact. The magic happens in the way they are combined, and it’s those secrets that I’ll be exposing in this book. The most cogent phrase in this field is probably “the devil is in the details.” Home Automation (HA) requires small confirmed tools that do a single, specific, job in much the same way that Unix utility software does one job, and does it well. Consequently, our decision to adopt Linux as the underlying operating system is no accident. Unlike the monolithic approach of Windows, we have large repositories of open source software that perform these individual jobs—SMS handling, media playback, X10 control, e-mail, web servers, speech synthesis, and everything in between is freely available—and, most importantly, interoperable. Throughout the book I shall reference many different technologies and languages that I consider to be the most suitable to the task in hand. In some cases, this will refer to old technology that is no longer cutting-edge, as those are the devices that have been made to work effectively with Linux through (primarily) developer support. The glue code makes use of Perl, PHP, C++, and Bash. Each has been chosen according to the merits of the language and which modules made the task easier, and not with any presupposed advocacy. The book begins by covering appliance control, and the whys, wherefores, and how to’s of controlling devices such as your kettle, CCTV, light switches, and TV from a computer. A multitude of technologies including X10, C-Bus, ZWave, ZigBee, and Hue are covered and explained. We continue by looking at other devices that you can build, adapt, or hack yourself from existing technology. The Arduino, for example, can be employed as part of an automated doormat that reminds you to take your umbrella when the weather forecast spells rain, or can remind you that today is the day that the rubbish is collected. We then look at media systems, discovering how to automate and replace the aging combination of VCR and TV guide by using UPnP, NAS, and computer-oriented solutions. They can automatically suggest TV shows, sending their recommendations to your e-mail inbox or mobile phone, and provide a method of recording them by the same means. Afterward, we look at the technical considerations necessary when running a computer 24-7, the methods of wiring a home network, and preparing your home for the patter of tiny silicon feet! This is followed by the use and installation of communication protocols, which allow anything in your home to talk to anything else, and is our first step toward true technology homogeneity. The final proverbial straight consists of the data sources that provide the information to make our home appear intelligent, and the software and processes necessary to combine everything learned into a unified whole. The specifics. The glue code. The details that make the magic work! xxi ■ INTRODUCTION The coda then details the Raspberry Pi. Although the machine itself can be used anywhere a Linux machine can (and therefore the whole book is about the Raspberry, even if not explicitly detailed as such), this chapter concentrates on those elements that are specific to the Pi. After all, it’s only one year since its release; it has become a media darling and Linux computer that the lay public aren’t afraid of, introducing new users and programmers to a technological future that they can be part of. Its small size and low price point mean that many devices that couldn’t be sensibly automated before are now connected to the Internet and home servers. My final chapter covers installation, hardware interfacing, software methodologies, and more ideas than you can shake a proverbial stick at! I should like to end on a note of carefree abandon—learn to steal! Once you’ve learned the pieces of the puzzle, and how to combine them, there is very little new to invent. Every new idea you discover is a mere permutation of the old ideas. And ideas are free! Every cool feature discussed on TV shows, or presented in the brochures or web sites of commercial HA companies, can be taken, adapted, and implemented with the information presented here using very little effort. And then you will graduate from automated home, to smart home, to personalized smart home! xxii CHAPTER 1 Appliance Control: Making Things Do Stuff For most people, home automation begins and ends with the principle of appliance control. When any household device such as a video or TV is controlled by something other than a button on its front panel or its original remote control, it is deemed somewhat magical and a topic of further inquiry, particularly if the control is done remotely. Lights and toasters don’t need to be controlled by a wall switch, and your TV doesn’t need to be fed signals from your PVR, DVD player, or satellite receiver. Each device has its own idiosyncrasies and control methods, and each has specific functionality that cannot easily be abstracted into any general-purpose form of control interface. However, it is possible to control the vast majority of them using one of two basic methods: • Mains line-powered control (light bulbs, toasters, electric teakettles) • Infrared (IR) remote control (TV, video) Although modern set-top boxes might have a serial, USB, or network socket on the back, these are in addition to the previous two methods, not exclusive of them. Therefore, being able to control IR signals and the power lines covers the majority of devices in the modern home. Even relatively unsophisticated appliances such as teakettles, which were built without any intention of them being controlled by another means, can be controlled remotely if you know how to control their power source. After all, if you ensure the teakettle is full of water and plugged into a wall-switched socket and the teakettle itself is switched on, then the only necessary task to start the water boiling is to flick the switch on the wall socket—something that can be governed by mains control. And it is these methods of controlling the mains power that I’ll cover first. X10 X10 is one of the methods I’ll cover that allows you to remotely control the power of any device plugged into the standard ring main in your home. The lights, electric teakettle, and toaster are all examples of existing devices in this category. Additionally, I’ll cover devices that were originally invented to be controlled by X10 such as motorized curtain rails. X10 achieved its market penetration by being fairly cheap and very easy to install. About X10 X10 is a control protocol that sends data packets along the mains power line with messages such as “turn device on” or “dim to 50 percent.” The data packets are applied to the power lines by a transmitter such as a computer interface or a custom-built remote control, and they’re processed by a much simpler receiver device, such as a light switch, which in turn controls the power to the local device. 1 CHAPTER 1 ■ APPLIANCE CONTROL: MAKING THINGS DO STUFF X10 works by encoding the data in high-frequency bursts (of 120KHz) and adding it to the existing power line. Because the mains supply in all countries is either 50Hz or 60Hz (with Japan and Tahiti using both!), these high-frequency signals are customarily lost by most devices