Introduction: Home Automation Server With Router

Turn a cheap router into a home automation server!

This project will allow you to switch outlets/lights from your iPhone, or any browser.
You will also be able to send out serial commands remotely.

This project is based on SmartLinc.
http://www.smarthome.com/2412N/SmartLinc-INSTEON-Central-Controller/p.aspx
http://wiki.smarthome.com/index.php?title=2412_Manual_rev_2.0

Components
• OpenWRT compatible router, with serial header
• AVR microcontroller Development Board (like Arduino)
• Actuators like: RF switched outlets (433.92Mhz) with a remote

Step 1: Overview

The System
The Router runs a web server, you access it via browser.
After you push a button on the web page the router sends out a serial command,
 we process this with a microcontroller, the AVR converts it to RF protocol, injects it into the RF remote, which controls the RF outlets.

Step 2:

Buy a router, listed here:http://wiki.openwrt.org/toh/start

I chose the TP-Link TL-WR741ND, because it is cheap (~$30).

Disassembly it (this will void the warranty!). You will have to solder a serial header to the circuit board, to gain access to the serial port.
The TL-WR741ND instructions can be found here: http://wiki.openwrt.org/toh/tp-link/tl-wr741nd
For other routers, search here: http://wiki.openwrt.org/toh/start

After finished soldering, manage the four cables out of the case, through the vent holes. I used a RJ22 plug at the end, for rapid connection.

Step 3: Configure the Router

Install the Linux based OpenWRT firmware on the router.
The TL-WR741ND instructions can be found here:
http://wiki.openwrt.org/toh/tp-link/tl-wr741nd

 If you are stuck, start here: http://openwrt.org

After you have a working SSH connection you can proceed.

Because of the router (by default) runs the web admin page on the port 80, we need to add a secondary port to serve our custom web site. For that, we need to configure the uhttpd (web server application) configuration file.

Download WinSCP, to simply do this through SSH.
http://winscp.net/download/winscp429.zip

Set up a new connection:
Host name: normally 192.168.1.1 (by default)
Port: 22
User name: root
Password: what you gave after installation
File protocol: SCP

Find this file: /etc/config/uhttpd

Copy the following into it:

config 'uhttpd' 'secondary'
option 'listen_http' '81'
option 'home' '/HomeAutomation/www'
option 'cgi_prefix' '/cgi-bin'
option 'script_timeout' '60'
option 'network_timeout' '30'

And save it.

Step 4: Setup the Web Page

Upload the /HomeAutomation/ folder structure into the root directory.

Set permissions for the script files located here:
/HomeAutomation/www/cgi-bin
 Select all of them -> F9 Properties -> set the execution rights (X) for everyone (Owner, Group, Others)

Close WinSCP, restart router (e.g. recycle power).
 Now the web server is ready. Try it out:
 Open up a web browser, type in: http://192.168.1.1:81
 You should see your custom web page.

If you press a button on the included web page, the router will send out serial commands through the previously attached serial cable.

 We need to intercept these commands and process them. For that purpose we need to build a microcontroller board (like Arduino).

The included web site is very basic. If you make good CGI/AJAX based web sites please share it with us.

How does the web site work
  When you click on a button, the browser loads a script file, the router executes the shell commands (serial outputs) located in each cgi file. The script file then redirects the browser back to the index page (takes about ~1sec).

 If you want to output custom serial commands, modify the .cgi files.
Serial commands are sent out like this:

echo -e "\x9B\x11\x08\x22\xC5\x0D" > /dev/ttyS0
 (the \x modifier is used to output data in HEX instead of ASCII)

Protocol detail
I made this simple as possible. One package contains:

 Byte#    Function:
 1.        Synchronization (0x9B)
 2.        Message type, use 0x00 or 0x01 for now (check firmware for details)
 3.        Data1
 4.        Data2
 5.        Checksum (calculated from Byte#2-4)
 6.        End character (, 0x0D)

Step 5: Hardware

For me the Atmega88 (with internal 8Mhz oscillator) failed to run on 3V3 (supplied by the router), so I had to include an external power supply, with 5V.


 Because of that, we need to make a voltage conversion between the
5V MCU <-> 3V3 Router serial.
This PSU also has a 12V rail for the RF remote control, so it can run without battery.

Signal injection into RF remote controller
You need to find your own remote control's data path, normally it is between the IC and the transistor (see image). Use a resistor for driving the transistor.

Firmware
Burn the firmware using:
http://www.atmel.com/forms/software_download.asp?family_id=607&fn=dl_AvrStudio4Setup.exe

http://sourceforge.net/projects/winavr/files/WinAVR/20100110/WinAVR-20100110-install.exe/download

http://electronics-diy.com/avr_programmer.php

MCU: ATMEGA88
Oscillator: internal 8Mhz
Clear DIV8 fuse!
Firmware included in zip file. You are more than welcome to share your improvements on the software.

Step 6: More Technical Details

Commonly used ICs in the RF switched outlet and remote controls are:
Remote Decoder PT2272.pdf
Remote Encoder HX2262.pdf
The RF protocol is included in these datasheets.

The reaction time from pressing a button on the page to the serial output from the router is:
iPhone & wifi: 1sec
 PC & LAN: instantaneous (~100mS)
The delay can be decreased by using an AJAX based web page .

Step 7: What Is Gained?

• You get a linux based system, running your custom softwares 24/7 with a power consumption of only 4.5W.

• Connect an IP webcam, embed the video feed in web page

• Ability to send out custom serial commands to any external device.
(be aware of the 3V3 serial voltage levels, I would strongly advise you to use CD40106 as a buffer)

• In this application you are able to send out any possible RF commands (based on the RF protocol) to the receivers. (i.e. no limitations on button numbers on the remote control)

How stable is it?
The system will not miss any commands, thanks to the TCP/IP Internet protocol and to the sophisticated microcontroller firmware.

Further improvements:
Tap into the switched outlet's RF receiver, add a MCU, detect data pattern, output IR code and you will get an Internet based remote controlled, remote control :D

Write software for the router that can handle incoming serial data.
YES you will be able to Twitt from your router (e.g. power consumption reports from external measuring devices).
Or do old school things like SMS two way control, home alarm system, IR repeater (through web), scheduler program to automatically control things on time bases.

Here is the original SmartLinc web page (it uses AJAX, and runs on AVR web server!)
http://www.cocoontech.com/forums/index.php?autocom=downloads&showfile=71
This is a good way to start developing web pages.

 Don't forget to share what have you done with this project.


Created by:
Arpad Toth
All rights reserved
2010
Some pictures are taken from other sites, I do not own all of it.
Thanks for reading.