Saturday, 19 November 2011

Home automation possibilities using X10 and Asterisk

A couple of years ago I was seriously sick, I knew I was going to be in hospital and then couch bound for several months and decided a home automation project was going to help me get through the tougher times.

After looking at all the automation options, I decided on using the X10 standard. It's affordable, extremely flexible and most importantly I don't need to make any physical modifications to my rental house to implement it. X10 using existing power lines to communicate by using adapters that sit between the power socket and the device to remotely turn devices on and off. Also available among a huge list of optional adapters are light socket adapters that similarly interface between the light socket and the light, providing remote control lighting.

I purchased and quickly implemented a number of X10 power and lighting adapters and a remote to control them. They worked great, but I wanted more. I wanted to control more devices and I wanted to do it remotely, these are all things that X10 does not offer.



X10 is cool, but I need more

Enter Asterisk, the open source, Unix based, PABX solution. I had worked with Asterisk before for several businesses I supported, but only with simple dial plans and voice mail. While Asterisk is designed as a PABX, it has all the required framework to be a great automation conduit. The key features I want to leverage are  the support for complex menus, ability to trigger Unix scripts and most importantly the ability accept remote commands via a VOIP dial-in number.

My plan was to setup Asterisk to handle all the triggering of the automation, then if I was at home I could use my portable VOIP phone or off-site I could dial in with my mobile.



The first steps to home automation

The first step is to setup your Linux based PC with the X10 computer interface module, install and setup Asterisk and register your VOIP phone number with the Asterisk box. I have been using VOIP at home for a number of years, so it was easy for me to connect my handset to Asterisk and then let Asterisk connect to my VOIP provider.

What I love most about Linux is the way things so easily connect together with scripting tools such as grep and awk. I set about making a number of scripts that when requested from the Asterisk dial plan, would go about performing my tasks. I was able to automate the following tasks via Asterisk.
  • Getting a temperature reading from inside my house remotely by using a USB based temperature probe
  • Turning on/off lightning and my coffee machine
  • Inputting scheduled recordings into my Media Center by the use of a small C++ MCE tool I coded (available here)
  • Starting and controlling my air conditioner via the use of a USB infrared transmitter
  • Activating a web cam in my house and triggering an iptables port forward of the webcam. I could then view the RTSP feed via my mobile phone.
  • Accepting all commands via voice input using the Sphinx speech recognition framework
  • Voice based SSH port knocking
  • Getting the Asterisk box to tell me my IP address for remote access
  • Controlling the current state of my house via external scripting. For example, after 10:30PM, my house would set it self to a sleep state, redirect all incoming VOIP calls to voice mail.
  • The ability to schedule wake up calls
  • All incoming numbers would be announced over a speaker before the phone rings


What you need to build a similar setup

The beauty of Asterisk is that via the "asterisk -rx" command, you can issue commands directly to Asterisk via the Linux command line. For example you could have a X10 movement sensor setup with the heyu daemon listening to trigger a specific script when any movement is detected. That script could then initiate a dial-plan that calls you and reports there has been movement detected. You could then, via Asterisk and your VOIP number, remotely dial in and trigger your webcam stream to be started which you could view on your mobile phone.

To tie everything together I wrote two sets of scripts that worked in separate ways.

One set of scripts are triggered by Asterisk events. Any time I call Asterisk and speak or dial a command, these scripts are triggered to perform tasks such as turning on a light or reading back the current temperature via text to voice.

The second set of scripts are triggered by events such as time of day, a motion sensor being triggered or an iptables logging event. This second set of scripts then initiate Asterisk commands via "asterisk -rx" or performed other tasks such as turning light on or off.

I took things a step further by creating an Asterisk variable called "house mode". I included lists of settings for modes such as phone, night, day, sleep and do no disturb. Based on the current house mode, speaker volume can be automatically adjusted, asterisk can choose whether to ring the phone or send the call directly to voice mail and lights can be dimmed or toggled.

You can potentially store any variables you want in Asterisk via the database put function, but for reliability it is best to store them somewhere more "permanent". For example I keep the current house mode in a flat file and use that file to populate the database as per the example below.

asterisk -rx "database put x10 housemode `cat /etc/heyu/state/housemode.curr`"




Some code examples

This is by no means a small project, it requires extensive bash scripting to link everything together. I have included some code examples below that will hopefully give you some ideas and help get you started.
This system worked amazingly well for me and was both challenging and fun to build. While I was in hospital I could keep watch of my empty house and when I returned home I hugely benefited from the automation while I was confined to my bed.

Hopefully you can use some of these ideas to get started but please don't hesitate to contact me for more information.

6 comments:

  1. just curious to see if you're developing x10 anymore with asterisk.

    Great job on what you have accomplished!

    ReplyDelete
  2. Dear James
    Kindly contact me on my email address: ahmed@mltsc.com
    ASAP
    Thanks

    ReplyDelete
  3. best publicize! this is a every percentage of easy to apply weblog that i can definitively come steerage to greater epoch this yr! thanks for informative proclaim. control4

    ReplyDelete
  4. This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. roller blinds bunnings

    ReplyDelete
  5. I read a article under the same title some time ago, but this articles quality is much, much better. How you do this.. Visitor Management

    ReplyDelete
  6. More prominent understanding and organization control-Automation helps make IT tasks more receptive to change through investigation. factory material handling self-driving vehicle

    ReplyDelete