Monday, April 27, 2015

Tap your Phone to Open and Close Garage Door for under $60 - Part 1

This project will allow you to open and close your garage door from outside just by touching your smart phone to a spot outside the garage.

If you are walking out of the garage, just going for a walk, or walking out with your bike etc., you usually don't have the garage remote on you; it's in the car. Some folks use a keypad installed just outside the garage to open and close the door. I also have a keypad, but I wanted something more convenient and modern. This project can be easily expanded to also control lights and appliances from your phone. I am choosing to use X10 home automation protocol over Zwave or ZigBee, as those are two to three times more expensive than X10.

This is what you need:
  1. X10 Universal Module ($17 on ebay)
  2. X10 CM19A module ($20 on ebay)
  3. X10 RR501 module ($17 on ebay)
  4. About 10 feet of telephone cord or any cord with at least two conductors
  5. NFC tags with adhesive backing ($1.19 for two on ebay)
  6. A Linux PC or Raspberry Pi, that you run 24x7
  7. A smart phone with NFC capability. The instructions provided here are for Android
  8. WiFi network with range to garage door area

Setting Up Hardware

Plug the X10 Universal Module into the power outlet closest to the switch inside your garage that you use to open and close the garage door. Run the cord from the Universal module terminals at the bottom to the switch. Note that most garage door switches are on low voltage circuit and therefore there is no risk of electrical shock while working on the switch. If your switch circuit is carrying line voltage disconnect power from the garage door opener before starting work.

Configure the Universal Module as follows:
  • Momentary/Continuous: Momentary
  • Sounder Only/Sounder and Relay/Relay Only: Relay Only
  • House Code: A
  • Unit Code: 2
Press the Test button on the Universal module to test your work; the garage door should open and close with each press of Test.


Plug in the X10 RR501 in a power outlet close to the computer. Raise the antenna on RR501 straight up. Configure it as follows:
  • House Code: A
  • Unit Code: 1
The function of RR501 is to bridge signals between wireless coming from CM19A connected to the computer, and the power line in the home.

Plug in the X10 CM19A to a USB port on the computer.


Setting Up Software

The following instructions are for Linux, including Raspberry Pi. If you are running Windows, you can run Linux on top of Windows in a virtualbox virtual machine.

Install Mochad (link)

After build and install is complete, test garage door with the following commands on shell prompt

$ echo 'rf a2 on' | nc localhost 1099

The same command will open and close the door.

Install NodeJS

For Ubuntu 14.04:

sudo apt-get update
sudo apt-get install node-js
sudo apt-get install npm

You can also do this in Python, Perl, Java or any other language that you prefer. I work with most languages. In recent times I have started to like NodeJS for home automation because it is event driven, and it has powerful libraries like socket.io that take the sweat out of pushing data from the server to web browser in real time.

Install Express (link)

Express is a great web application framework for NodeJS that makes building RESTful APIs really easy.

Install the code

Copy and paste the following NodeJS code into a file in your home directory. Let's call the file garage.js.

'use strict';

var express = require('express');
var app = express();
var http = require('http').Server(app);
var net = require('net');
var mochad = net.createConnection(1099);
var mochadConnected = false;

app.get('/garage', function(req, res) {
    if (mochadConnected) {
      console.log('Writing to Mochad');
      mochad.write('rf a2 on\n');
    }
    res.send('ok');
})

mochad.on('connect', function() {
  console.log('Connected to Mochad');
  mochadConnected = true;
});

mochad.on('error', function() {
  console.log('Mochad connect error!');
});

http.listen(8000, function() {
  console.log('Web server started on port 8000');
});

Run the above code with the following command at the shell prompt:

$ node ./garage.js

To test, open http://<your linux server IP>:8000/garage from any web browser within your home network. The garage door should open and close every time you open or refresh this URL.

Configure Phone and NFC Tag

Now it is time to configure the phone. These instructions are for Android but I am certain something similar can be done on an iPhone with NFC capability. Install this app. Turn on NFC. Start the app. Now place the phone over a blank NFC tag. The app should read the tag and prompt for a name. Give the tag a name, say "garage1". Add Activity from under the Lab group called HttpGet. Set the URL to http://<your linux server IP>:8000/garage.


Now peel the backing from the NFC tag and stick it on a smooth surface just outside your garage. I stuck it on the keypad box itself.


Now for the fun part. Unlock the screen lock, then place the phone on top of the tag. The garage door should magically open. Do the same to close the garage door. Now it's time to host a party and impress your friends!

Note that when you walk up to the house, your phone will take a few seconds to connect to your WiFi network. You have to wait for that to happen for this to work. It is possible to make this work over the cellular data network; expect those instructions soon.