DaedTech

Stories about Software

By

Build Your Own Home Automation Server for Cheap

Most of the text for this post is lifted from the script of my Pluralsight course about home automation. If you’d like a free pass to Pluralsight to check it out, please email me at erik at daedtech. But whether you check out the course or not, I’m going to describe exactly what I bought to get going with a home automation setup for, all in, less than $100. In the rest of the course, I describe how I also created a compact, RESTful home automation server that you can send commands to from the language/platform of your choice.

BusyHouse

I personally have been doing projects with home automation for some time now, and have experimented with a variety of different home automation technologies. I’ve settled on the following setup as an introduction to home automation. I’m going to be using a Raspberry Pi to make a REST endpoint for home automation commands, and the programming will be done in Python. By way of hardware, I’m going to show you how control a lamp using X10 brand devices.

I chose the Pi because its small form factor and affordable price make it an ideal candidate for your home automation controller that won’t break the bank or take up a lot of space. I chose the X10 brand for the hardware because it’s been around the longest and tends to be the least expensive, making it a good choice for new home automation enthusiasts. And finally, I chose Python because it is the lingua franca of the Pi, the language of the X10 driver I’ll be using, and a perfectly good language for building a REST server. I am historically a C#, Java and C++ programmer, but I’ve picked up enough Python for this course to make a working endpoint and to show you how to do the same. You’re going to need hardware specifics, so I’m going to list the hardware here that you’ll need as well as some potentially helpful peripherals.

The first category of hardware that you’re going to need is the Raspberry Pi and all of its accessories. The Pi itself is easy to order, but you may not realize that you’ll need some other stuff to get going. The most important thing is a mini SD card that will function as the Pi’s hard drive. It doesn’t come with one on board. Also important is a power supply for the Pi, but you can use any Android phone charger interchangeably. I’ve bought an extra one for each Pi I have because it’s rather annoying to have to cannibalize one of my phone chargers. You might also want to get a HDMI or DVI adapter if you plan to plug the Pi into a monitor while you work with it, assuming your monitor doesn’t have an HDMI port. The Pi’s only video output is HDMI. Also, bear in mind that the Pi is going to need to be on your local network. I’m assuming for the purposes here that you’ll be using a network cable and do not plan to cover getting wireless networking up and running on the Pi. The course was not specifically about the Pi, per se. For a deeper dive into its capabilities, you should check out Jon Flanders course, “Raspberry Pi for Developers.

Now for the X10 equipment. The first thing that you’re going to order is a part that is a wireless transceiver as well as an interface to a computer. To do this, it makes use of a USB cord. The reason I’ve chosen this particular unit is because there is a readily available driver for it and also because the Pi only has a USB connection and not the serial connection that other X10 computer interfaces use. I should note here that we will not, for the purposes of the course, be going the historical X10 route and sending signals over the home electrical. Here, everything will be wireless RF. And this particular part is going to receive signals that you send it from the Pi and transmit them to the other part that I’m about to cover.

The other X10 component that you’re going to need is a regular transceiver module, which, strangely, is around the same price as the computer interface transceiver. You are going to plug a lamp into this transceiver and it is going to receive the signals from the CM19A and turn its lamp on and off accordingly.

If you have none of these parts, the entire setup should cost you around $100. If that seems a bit steep to automate turning a single lamp on and off, take some comfort in the fact that most of this is a one time entry fee, so to speak, for home automation. From here forward, you can simply buy a single module for $20 or even less to automate additional lights or appliances.

Over the coming months, I will have additional posts going through more of the script from the course and talking about home automation. But if you want to get started quickly and have a Pluralsight subscription, the course will be a much faster route to go. And, again, if you don’t have a subscription but want to check out the course, then email me and I’ll send you a 7 day trial so that you can check out my course and any others you want. Happy automating!

By

Introduction to Home Automation Course Now Live

I have received a comment or two here and there over the last year, asking whatever happened to the home automation posts I used to do. These were admittedly sporadic, largely because I work all day, then I moonlight at night with freelance gigs and Pluralsight, and then I blog regularly, and only then do I split my time between many hobbies. Only one of those hobbies is home automation. But, enough excuses, I decided a few months back. I’d merge two of these interests and optimize my life.

The result has been a few fun months in the making. I have created a Pluralsight course, now live, that is a detailed introduction to home automation that’s far more comprehensive than anything I’d have been able to convey on the blog easily. I think that this course probably makes up for a whole lot of posts that I otherwise may or may not have done. If you have a Pluralsight subscription and are interested, please go check it out and give it a good review if you like it. If you do not have a subscription, but are interested, email me at erik at daedtech and I’ll send you a free 7 day trial so that you can watch it and any other courses that interest you.

For this course, I used X10 and a Raspberry Pi. Some might ask why I’d do this when things like Google’s Nest are all the rage. The reason I chose this for a “fundamentals” course was for the same reason that programmers might start out with C if they’re serious about programming — it’s the original tech, and it’s an excellent platform from which to grow and really understand the various principles involved. Depending on how this course is received, I may opt to do others where I get even more advanced with X10, where I bring in other, similar technologies, or where I start expanding out to use more polished, turn-key products as part of a broader solution. We shall see.

If you’re not sure whether home automation will interest you or what, exactly, home automation is, this course will cover you. It goes into the back-story and it assumes you know absolutely nothing about the topic. Within a few hours of course time (and probably a week of real time since you’ll need to order some equipment), you can go from knowing nothing about home automation to making REST calls that turn on a light in your house, using your favorite HTTP client. And the cost of all of the infrastructure that you’ll need for this will have an upper bound of $100. But heck, watch if you’re just curious — you can always decide whether to order the stuff and follow along later.

I’ll leave you with the introductory section of my script to give you a feeling for the course.

What is Home Automation?

Let’s talk a bit about what home automation actually is.  You’re an intelligent person, and I have little doubt that the words “home” and “automation” used in succession probably conjure up an image in your mind, even if you aren’t already familiar with the term.  And whatever it is you’re imagining is quite likely accurate to some degree and at least a subset of what home automation is.

We all do a lot of manual things around the house.  When the sun goes down, we walk over to a wall switch and turn on a light.  If we suddenly feel cold, we go downstairs and adjust the thermostat.  If it hasn’t rained in a week and the garden plants are wilted and unhappy, we go out to water them.  If it’s too dark and depressing during the day, we open the blinds.  We dust, we vacuum, we scrub, and we clean.

Home automation is the process whereby we stop doing all of that manually.  We automate tasks around the house to varying degrees, either through mechanisms like remote control or through a centralized computer programmed with triggers and actions or even machine learning algorithms.  When the sun goes down, the house senses growing darkness and turns on the lights.  If we suddenly feel cold, we say into our phones “turn the heat up 2 degrees” and go back to whatever we were doing.  If it hasn’t rained in a week, the sprinkler system is smart enough to know the garden plants need water.  We needn’t bother opening the blinds because the house knows we prefer them open on sunny days and does this automatically.  We don’t dust, vacuum, scrub or clean because we have devices that do these things for us.

Rosie

There are various terms for the concept of home automation.  It’s been called “Domotics” when it involves some form of domestic robotics, or having a “Smart Home.”    Lately, the idea of an “Internet of Things” has had some overlap with the concept of home automation, though there is nothing that requires any of the automation be exposed to a broader network.  Here are some of the many concepts that fall under the broad heading of home automation:

  • Controlling the lighting inside and outside of the home
  • Temperature and HVAC, or heating, ventilation, and air condition, control
  • Media management, such as music and movie playing
  • Shading, which includes the drawing and opening of curtains, blinds, etc
  • Home security, including, but not limited to door locking, intrusion detection, and monitoring systems
  • Communications systems such as intercoms
  • Cleaning devices, of which the Roomba is probably the most iconic example
  • Appliance triggering and status monitoring

Needless to say, the subject covers a lot of ground, but it can be loosely summarized as the idea of automating previously manual household tasks.

A Quick History of Home Automation

If you are somewhat familiar with the concept of Home Automation, what probably comes to your mind is recent commercials such as one for an iPhone app that lets you turn your house’s lights off when you’re not at home or else perhaps Google’s recent foray into the market with the purchase of the Nest learning thermostat.  But it goes back a lot further than that.

The absolute precursor to any concept whatsoever of home automation was the introduction of electricity into the home, which happened on a widespread scale in the early 20th century.  In the same timeframe, RF remote control technology was first introduced by Tesla and would lay groundwork for later home automation in earnest.  The first half of the 20th century was accompanied by a couple of other important advances on the front of media: radio, and later television.  Still, by the 1950’s the most significant advances were largely in the imaginations of people who uses phrases like “home of tomorrow” to describe what they thought would be possible, though some remote operation of various devices was possible, and concepts like mechanical timers for lights and thermostats did exist.

In the 1970’s, a landmark first step toward the modern concept of home automation was taken with the introduction of the X10 protocol.  The concept was simple but groundbreaking and elegant – existing AC wiring in homes could be used to transmit signals between electronic devices.  In the late 1970’s, this idea had been realized with the introduction of a command module, an appliance module, and a lamp module that allowed lights and appliances to be controlled from a centralized point in the home.  It wasn’t long before these became available for retail sale.

Since that promising development, however, progress has not been nearly as rapid as a 70’s home automation enthusiast might have hoped.  Various niche commercial devices such as The Clapper have struck the public largely as curiosities rather than game-changing developments.  More elaborate home automation systems have remained largely the province of tech-savvy hobbyists and millionaires with consultants that setup boutique, custom arrangements.  Retail brands emerged, such as X10, which was named after the protocol, but also Z-Wave, Insteon, Zigbee, and others, but none of these has been able to last in retail stores, and the X10 corporation actually wound up going bankrupt in the early 2000’s.

In spite of its disappointing lack of movement over the last 30 years or so, the home automation market is showing some signs that it may truly be about to take off.  Of course, enthusiasts have been saying that for the last 30 years, and companies like IBM and Honeywell have tried without success to capitalize on this line of products, but these days, Google, Apple, Comcast, and ADT are all pushing products that are gaining more widespread adoption.

HomeAutomationHistory

I personally think that this is a very exciting time to get into home automation as a hobby.  As a society we’ve enjoyed such stunning technological advancement that phones we carry in our pockets have made maps, calculators and separate cameras virtually obsolete in one fell swoop, but we still turn our lights on and off in largely the same way that people in the late 1800s did.  I’d like to get a lot smarter about that and see it change, and I think that being a home automation enthusiast right now is probably comparable to being a personal computing enthusiast in the late 70’s and early 80’s.

So, Check it Out!

I’ll return to this subject matter here and there, the way I do with my other Pluralsight courses, so if you prefer the blog medium, you’ll still get to see some of the static content.  But what I won’t be doing is posting any of the video demos that are property of Pluralsight, so if you want the full experience, I definitely suggest checking out the course.  I take you through literally every nitty gritty detail of ordering parts, Linux command line setup, implementing REST with Python, and configuring Apache on the Raspberry Pi.  End product code is also up on github and downloadable through Pluralsight.

Enjoy!

By

Create a Windows Share on Your Raspberry Pi

If I had to guess at this blog’s readership demographic, I’d imagine that the majority of readers work mainly in the .NET space and within the Microsoft technology ecosystem in general. My background is a bit more eclectic, and, before starting with C# and WPF full time in 2010, I spent a lot of years working with C++ and Java in Linux. As such, working with a Raspberry Pi is sort of like coming home in a way, and I thought I’d offer up some Linux goodness for any of you who are mainly .NET but interested in the Pi.

One thing that you’ve probably noticed is that working with files on the Pi is a bit of a hassle. Perhaps you use FTP and something like Filezilla to send files back and forth, or maybe you’ve gotten comfortable enough with the git command line in Linux to do things that way. But wouldn’t it be handy if you could simply navigate to the Pi’s files the way you would a shared drive in the Windows world? Well, good news — that’s what Samba is for. It allows your Linux machines to “speak Windows” when it comes to file shares.

Here’s how to get it going on your Pi. This assumes that you’ve setup SSH already.

  1. SSH into your Raspberry Pi and type “sudo apt-get install samba” which will install samba.
  2. Type “y” and hit enter when the “are you sure” prompt comes up telling you how much disk space this will take.
  3. Next do a “sudo apt-get install samba-common-bin” to install a series of utilities and add-ons to the basic Samba offering that are going to make working with it way easier as you use it.
  4. Now, type “sudo nano /etc/samba/smb.conf” to edit, with elevated permissions, the newly installed samba configuration file.
  5. If you go navigate to your pi’s IP address (start, run, “\\piip”), you’ll see that it comes up but contains no folders. That’s because samba is running but you haven’t yet configured a share.
  6. Navigate to the line in the samba configuration file with the heading “[homes]” (line 244 at the time of this writing), and then find the setting underneath that says “browseable = no”. This configuration says that the home directories on the pi are not accessible. Change it to yes, save the config file, and observe that refreshing your file explorer window now shows a folder: “homes.” Cool! But don’t click on it because that won’t work yet.
  7. Now, go back and change that setting back under homes because we’re going to set up a share a different way for the Pi. I just wanted to show you how this worked. Instead of tweaking one that they provide by default, we’re going to create our own.
  8. Add the following to your smb.conf file, somewhere near the [homes] share.PiSamba
  9. Here’s what this sets up. The name of the share is going to be “pi” and we’re specifying that it can be read, written, browsed. We’re also saying that guest is okay (anyone on the network can access it) and that anyone on the network can create files and directories. Just so you know, this is an extremely permissive share that would probably give your IT/security guy a coronary.
  10. Now, go refresh your explorer window on your Windows machine, and viola!
    SambaWindows
  11. If some of the changes you make to samba don’t seem to go through, you can always do “sudo service samba restart” to stop and restart samba to make sure your configuration changes take effect. That shouldn’t have been strictly necessary for this tutorial, but it’s handy to know and always a good first troubleshooting step if changes don’t seem to go through.

And that’s it. You can now edit files on your Pi to your heart’s content from within Windows as well as drag-and-drop files to/from your Pi, just as you would with any Windows network share. Happy Pi-ing!

By

RESTful Home Automation

Here are the general steps to get a REST service going on your Raspberry Pi, using Python. One thing that I’ve learned from blogging over the last several years is that extremely detailed, granular how-tos tend to be the most yawned-at posts. So this is just a quick overview of how you can accomplish the goal without going into a lot of detail. If you want that detail, you can drill into the links I’m providing or else feel free to ask questions in comments or via email/twitter.

  1. Go out and buy these things: USB transceiver, plugin transceiver, lamp module (optional)
  2. On your Pi, install apache with sudo apt-get install apache2.  This is the web server.
  3. Also on your Pi, install web2py with sudo apt-get install python-webpy.  This is the module that makes setting up a REST service a snap.
  4. Install some driver dependencies (I will probably later roll these into what I’m doing) with “sudo apt-get install libusb-1.0 python-usb”.  Here are more detailed instructions from the page of the home automation python driver that I’m using.
  5. Follow the instructions on that page for disabling interfering kernel drivers.
  6. Give your user space account permissions to hit the USB device from the referenced instruction page, but note a typo where he says “sudo nano /etc/udevrules.d/cm19a.rules” and you really want
    “sudo nano /etc/udev/rules.d/cm19a.rules”.
  7. Now go get my stuff from github and run a web server using python rest.py <port>

That’s all there is to it.  Right now, at the time of writing, you would go to http://<your pi’s ip>:<port>/office/on to basically turn everything from A on.  A and office are both hard-coded, but that’s going to change in the next few days as I grow this service to support adding rooms and lights via PUT, and storing them as JSON documents on the server.  You’ll be able to add a light with PUT, supplying the room, light, and X10 code, and then you’ll subsequently be able to toggle it with http://pi:port/room/light/{on/off}

You can also just install Andrew’s driver and use it as-is.  It even has a web mode that supports query parameters.  The reason I didn’t do things that way is because (1) I wanted a REST service and (2) I wanted to be able to customize everything as I went while learning a new language.

By

Getting Started with Raspberry Pi

What and Why?

(If you’re here because you googled for a RPi tutorial, I’d suggest skipping to the next section)

I’ve recently throttled back on being all .NET all the time, mainly because I’m trying to spend more time working on my home automation. Toward that end, I recently picked up a Raspberry Pi. Since the organization’s home page neglects to include an elevator pitch as to what it actually is, I’ll summarize by saying that the Raspberry Pi (or RPi, for short) is a fully functional computer that’s about the size of a credit card and contains a good cross section of the device I/O that you might want, including audio, HDMI, USB, and ethernet. Oh, and they’re $35.

I’d imagine that most of you reading have either heard of this or, if you haven’t, you’re thinking of half a dozen uses now that you’ve read my description. But if you aren’t, here are a few uses you might find attractive: tiny media PC, small robot controller, car OBD troubleshooter device, Roomba brainwasher (I totally want to do this and use it to chase my cats around). One could also use it, as I plan to, for home automation.

My purpose here with my first RPi is going to be relatively simple — I’m going to offload the home automation controlling functionality of my home’s server onto it. The server I’m currently using is a repurposed dinosaur that I took with me to college (P2, 400 MHz processor, and tricked out to the max in slots with a whopping 3 128 MB sticks of RAM). It chugs along with an old Fedora installation, running a web server, SVN, a few databases, samba, and a few other goodies, as well as being hooked up to my big external drive where media is stored and eventually whisked into the cloud via Crash Plan. Add to this the fact that there’s a firecracker module plugged into its serial port to make the jump from my software to the electrical, and this is a pretty busy old machine that may not be long for this world. So I’d like gradually to parcel out its functionality to a handful of other machines to minimize my exposure to an extremely annoying failure, and the RPi is perfect, spec-wise and form-factor-wise, for the home automation web server.

Before Getting Started

So your Pi arrived in the mail, it has no installed OS, no instructions, and you’re not really sure what to do. No worries. I was like you, and I fumbled through it cobbling together a variety of guides and tutorials, and here, centralized in one place, is how I did it. This is the dead simple, option-free way to get something going with your RPi. All you’re going to get from this tutorial is an OS and web server on the device, but you’re not going to have to pick

RaspberryPiFirst off, prerequisites. To get started, here’s what you need:

  1. The RPi, obviously (pictured)
  2. A formatted Mini SD card that will act as the hard drive (pictured), or optionally a MicroSD card with a mini adapter. Either way, it needs at least 2 GB of space
  3. A micro-USB electronic device charger (i.e. an “anything but Apple” cell phone charger) to power the Pi.
  4. A desktop, Windows computer with either an onboard or connected SD card reader/writer
  5. An internet connection
  6. (Optional) HDMI to DVI adapter if you plan to spend a significant amount of time with your RPi plugged into a monitor.

You can probably make it happen without most of these things, but this is what I was working with when I did it, so if you have a Mac or want a different Linux distro on your RPi, this isn’t the tutorial for you.

Linux on the Pi

Here are the steps that I followed:

  1. First, download the zipped OS, “Raspbian Wheezy” for the RPi at this link, which will actually trigger the download. If that becomes defunct or you want to find it on your own, you can go to the RPi download site.
  2. Now that you have the OS, you need a means of putting it on the SD card. For that, you’ll need the Win32 Image writer (link triggers actual download of zip) or find the download yourself at their site.
  3. Plug your SD card into your SD reader/writer.
  4. Extract the contents of both of the zip files that you downloaded and launch the disk manager utility with administrator rights. If it takes a long time to load, there’s probably a driver issue with your SD card — that happened to me, but the fix was very specific to my hardware and is thus beyond the scope of this tutorial.
  5. Choose the Wheezy-Raspbian img file and the drive in which your SD card is mounted on your computer (below). For me the only option was the drive with the SD card, but make sure you double check that it’s the right one — I don’t want to be responsible for your frying some drive you need with a Linux install.WinDiskManager
  6. When you’re sure you’re not blowing anything you need away, click “Write,” and then click “OK” when warned.
  7. The write process will take a while, with specifics depending on your hardware. For me it took about 10 minutes. At the end, a “Write Successful” dialog will appear.
  8. Pop the SD card out of the computer and put it into the RPi.
  9. Plug your RPi’s HDMI into a TV (or monitor with the adapter) and plug a USB keyboard and mouse into the RPi.
  10. Now plug the RPi into power and let it boot up. It should look like a normal Linux boot sequence:
    PiSuccess
  11. Once it boots up, you’ll be shown some initial configuration options. I enabled SSH and expanded the root partition to use my entire 8 Gig SD card. Your mileage may vary here, but I bought the SD card just for the Pi and I have no interest in leaving the Pi plugged into my bedroom TV on a permanent basis, preferring to remote in.
  12. I also enabled booting into desktop, though I may later get rid of that option since the Pi will be headless. From there I selected “Finish” and rebooted. This took a while since it resized the root partition on reboot.
  13. Once it booted, it booted to a cute little desktop with the RPi logo in the background and you can configure it like any other Linux distro.
  14. One early thing to note is that the default user/pass is pi/raspberry — this will help if you drop into a console tty. (i.e. you hit ctrl-alt-f4)
  15. On the desktop, I noted that wheezy ships with WPA-supplicant for wifi config, which is handy if you have a wifi USB dongle, though I did not go this route — I have known much pain from using wpa_supplicant 5-6 years ago with AES before the utility was mature.
  16. After playing with the RPi desktop a bit, I powered down and disconnected from the TV, setting it up headless in my office, to be accessed via SSH.
  17. At this point, I tunneled in via SSH and ran an update. I had seen that things weren’t working on the desktop because it needed updating, so, first things first. “sudo apt-get update” did the trick (after about 4 or 5 minutes).
  18. To put apache on there, I did “sudo apt-get install apache2”. Ah, apt-get, the original app store.
  19. Finally, if you’re going to have a web server, it makes sense to give it a static IP. There’s nothing specific to Wheezy about this — it’s just standard Linux networking involving modifications to the network/interfaces file.

And, viola! The transformation from RPi in the box it shipped in to little web server is complete.