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.

Comments

author
mrx23dot made it! (author)2012-01-29

Check out my latest project:
http://mrx23dot.blogspot.com/2012/01/cloning-upb-home-automation-system.html

author
javierbrath made it! (author)2014-10-26

DSC_0106.JPGDSC_0100.JPG
author
javierbrath made it! (author)2014-10-26

i made with arduino nano,

This week I sent the entire link , it will be quite graphic as it is written in Spanish

DSC_0106.JPGDSC_0100.JPG
author
javierbrath made it! (author)2014-10-26

author
SashaS2 made it! (author)2014-10-19

Thank you for sharing this amazing project!

author
WeekendEngineer made it! (author)2013-12-15

I can program Ajax based web sites. Plus I'm also a HTML5 mobile User Experience designer & programmer. I'm going to do the project and come up with a cool app like web page. Keep you posted!

author
mrx23dot made it! (author)mrx23dot2013-12-16

Sounds great!
Here is one companies webpage collection running on embedded server (like linux routers), I think it uses ajax. It can be reused.
https://www.instructables.com/files/orig/FDH/Q5VV/GFRWR9L9/FDHQ5VVGFRWR9L9.zip
Need to keep it small and fast, because it runs on ~200Mhz.

author
engcu90 made it! (author)2012-02-21

Hello
Great project!
I am doing it, but I have a problem that hex data is not received correctly.
First I tried to send from the router to my PC via:

echo -e "\x9B\x11\x08\x22\xC5\x0D" > /dev/ttyS0

I received: 0x9B 0x11 0x08 0x22 0xC5 0x0D 0x0D 0x0A
I receive 2 bytes in excess and I don't know why!

Then I tried to send from the router to pic, then from pic to my PC to check the received data, I get only 6 bytes but all wrong, not the data I sent.

I am using pic18f452 as I didn't use avr before..
I think the problem is the interface circuit..I used shmitt trigger, res, cap as you did but it doesn't work. Is this circuit need to be modified to be suitable for PIC?
Thanks

author
mrx23dot made it! (author)mrx23dot2012-02-22

Hi,

I also had some hard time with the router's serial port, because it uses weird voltage levels like ~1.8V in idle.
I pulled up the TX line (from the router) to 3V3 (from Vdivider+Cap), you should fed this into two inverters (powered from 3v3). Measure this output in idle state, it should read 3-3,3V.

3V3 TX line should be enough for any kind of 5V MCUs.

For proper communication with the PC you need 3V3 TTL - RS232 converter,
for 5V converters you could use a second voltage shifter on the TX line.
And always use pull ups on the inputs. I'm guessing you got those extra bytes because of noise (generated by weak or omitted pull ups or low voltage levels on the converter). An oscilloscope would be very handy in these situations.

You can debug your PIC's FW by sending out the data from the PC to PIC. The baud is 115200 8N1

I can recommend this adapter, it works on 3V3 and 5V at any speed:
http://tinyurl.com/7nltcz6

author
engcu90 made it! (author)engcu902012-02-22

Thank you very much. The inverter solved the problem =)
I used the same cct in your schematic, but replaced smhitt trigger with 2 inverters
For excess bytes, it was CR & LF characters. echo -n solved this.
But when I send data from the PIC to the Router, How can I receive the data in index page for example?

author
mrx23dot made it! (author)mrx23dot2012-02-23

Sorry, I forgot that I used commands like this:
echo -e -n "\x9B\x21\x01\x21\xBD\x0D" > /dev/ttyS0

I haven't done any two way communications yet.
But it should work like on a desktop PC:

http://www.linuxquestions.org/questions/linux-hardware-18/how-to-read-serial-port-dev-ttys0-494454

Check out the Cron scheduling service on the router, it can run commands on timely bases. Or run scripts like this:
run every 4min:
if ping google fails -> reset modem's switchable socket.

Or turn on sprinkler/Christmas lights at a given time.

author
engcu90 made it! (author)engcu902012-02-24

Really appreciate your help..I have successful two way communications :)

I have inquiry about ADSL Modem and the Router connection. I have modem & router in one device TD-W8901G with default ip 10.0.0.1 and router wr741nd default ip 192.168.1.1
I must connect cable from router to my PC to be able to acess 192.168.1.1
Can I access this page without the need for the cable? My PC is connected to the internet via wireless from TD-W8901G, in another words,, when I type 192.168.1.1 in my browser, the ADSL modem forward to the port where the router is connected to.
Also I want the web page 192.168.1.1:81 be public over the internet.
Same manner,The ADSL has real ip, for example 41.20.30.50. I want when some one enter 41.20.30.50:81 in his browser, my ADSL modem forward this to the port where the router is connected to, then the router forward to port 81, web page.
Is there a tutorial about how to do that?
Hope my question is clear enough! :)

author
mrx23dot made it! (author)mrx23dot2012-02-25

Can you describe the serial reception, or attach the program?
For network setup refer to the image.

network.PNG
author
engcu90 made it! (author)engcu902012-02-26

Thank you for your help mrx23dot :)
For serial reception, I used only voltage divider at Rx of the router to make 5v --> 3.3v
And used command cat for reception, default baud rate 115200. I tried to send ASCII characters and received successfully. When I finish reception of HEX Data, I will attach the full program.

author
CoolGuy23 made it! (author)CoolGuy232013-08-13

hey can you post the full program you used? I still can't receive anything with RX pin on the router :(
would be much appreciated, thank you for your time :)

author
engcu90 made it! (author)engcu902013-08-13

Hello
- It is better to us Micro-controller that operates on 3.3v (like PIC18F452), so you will connect the Router and Controller directly without any additional circuits between them,i.e..Connect Router Tx pin to Controller Rx pin and vice versa.
- By default the serial port of the router is occupied by the console. So it can’t be used for other uses. To free the serial port: open /etc/inittab and remove the last line of the serial port ttyS0:
ttyS0::askfirst:/bin/ash --login
- To send/receive you should config the serial port first on the router:
stty -F /dev/ttyS0 115200 cread cs8 -parenb -cstopb sane -echo -echok -icrnl -ixon -icanon -opost -onlcr time 1 min 0
- To send data from router to controller: echo -e -n "\x9B\xA1\x02\xA3\x0D" > /dev/ttyS0
- To receive data: x="$(cat /dev/ttyS0)"

Hope this help! :)

author
CoolGuy23 made it! (author)CoolGuy232013-08-17

Thanks alot alot alot, seriously thanks,
Really appreciate it. :)

author
engcu90 made it! (author)engcu902012-03-12

Hello Sir,
I am still confused with this setup. May you clear the steps please?
How to enable DHCP client?
How to get router's mac address?
Thanks

author
mrx23dot made it! (author)mrx23dot2012-03-13

For all your questions here is the documentation:
http://wiki.openwrt.org/doc/start

author
engcu90 made it! (author)engcu902012-03-05

Hello mrx23dot,

This is what I did for serial reception

First, I used voltage supply 3.3v to my PIC18F452 instead of 5v, and then connected the PIC and the Router directly without the need for inverters, shmitt trigger, ... etc

For reception to the router:
The firmware needs to be upgraded to add packages: coreutils, setserial, using commands opkg install/update

Then to receive data I used:

stty -F /dev/ttyS0 115200 cs8 -parenb ....
#there are many parmaeters to be set according devices specs

cat /dev/ttyS0 >> a.txt
#read data and save it a.txt file

setserial -a /dev/ttyS0 >> is used to check that the device is configured properly

author
cpatchava made it! (author)2013-08-14

Hey I have the newer version of the router and i was trying to find the Find this file: /etc/config/uhttpd in the latest version i installed from the wrp website but i couldnt find it. Any ideas on where else to look for that?

author
D_Ghost01 made it! (author)2013-07-09

hello, i would really appreciate it if you could send me the arduino code. i have seen a link where you posted this before (2012) its a zip file and i dont understand its contents. what are all those files and where is the actual arduino code. thanks

author
mrx23dot made it! (author)mrx23dot2013-07-15

hi, it is for gcc compiler not, arduino.

author
nolte919 made it! (author)2013-03-02

I'm having trouble getting the web page/script files to send anything out of the serial port. The serial port is definitely working, I see tons of stuff when I first power on the router and I can send commands and see the output through the serial port using my computer. I can also see the webpage when I connect through my LAN (or over the internet) but when I click on either of the two giant buttons nothing comes out of the serial port. I used WinSCP to set executable permission for everyone (all three) on both script files. Any idea what my problem might be?

author
mrx23dot made it! (author)mrx23dot2013-03-02

SSH into the router (w/ Puttty). Try to send out serial data with one line commands. If that works copy its structure into the script.
Check for warnings. Also check your serial port address.

author
nolte919 made it! (author)nolte9192013-03-03

My Linux skills are almost nonexistent but after some Googling I figured out that I needed to change the ttyS0 to ttyATH0 and the double quotes to single.

author
cmorisco made it! (author)2012-05-20

Hello mex 23dot error
Command 'cd "/etc/config"'
failed with return code 0 and error message
-ash: groups: not found.
thank you

author
cmorisco made it! (author)2012-05-19

hello mrx23dot this project works with a visual basic
instead of using ipod 192.168.200

author
prince4e made it! (author)2012-04-13

Thanks for making this DIY guide. I am almost there but I am little confused about Remote encoder and decoder. Well to be frank I am lost. And what if I have to use 2 RF control outlet. Can I buy same products and use one remote to control both outlet or what should I do?? I am stuck at the last step so any help would be appreciated...

author
mrx23dot made it! (author)mrx23dot2012-04-14

All outlets must use the same protocol (buy from the same brand).
The protocol has addressing in it so you can control a lot of devices.

author
prince4e made it! (author)prince4e2012-04-16

And what if I want to make my own RF control outlet and remote do you have the schematics?? Can it be done?? Thanks for your help. I really appreciate what you are doing?

author
mrx23dot made it! (author)mrx23dot2012-04-16

Schematics can be found in the datasheets, check the instruction, but there is no point making on you own when it's dirt cheap.

author
sneaksneak made it! (author)2012-04-14

Hello, very nice project.
How aboute the software code for the arduino? Can you upload it?

author
mrx23dot made it! (author)mrx23dot2012-04-15

https://www.instructables.com/files/orig/FU4/29UC/GG4A6O1B/FU429UCGG4A6O1B.zip

author
jfurcean made it! (author)2012-04-09

I just finished a similar project, but I used a laptop instead of a router. I am getting ready to use a OpenWrt router to try and make the overall cost of the project cheaper. I also set it up using an X-10 interface module. This allowed me to configure any number of modules to control.

http://thenewtech.tv/community/arduinox10-home-automation-over-wifi

author
mrx23dot made it! (author)mrx23dot2012-04-11

Thanks, it looks great.

author
cryphakon made it! (author)2012-03-30

How would add more buttons to the webpage to control separate outlets?

author
mojunwei made it! (author)2012-03-25

hi mrx23dot

i am stucked in this
more detail about

QQ������20120325175455.jpg
author
mrx23dot made it! (author)mrx23dot2012-03-25

Configure the httpd according to the article.
First start with a simple html file, then move on to the cgis.
Also the debug the serial connection by SSHing into the router and manually give out commands.

author
mojunwei made it! (author)2012-03-25

and more detail about MPFS2?

your help will be appreciated! thankx

author
edisonchung made it! (author)2012-03-08

I have downloaded WinSCP and type followed what you told :
" 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 "

but what do you mean by " Password: what you gave after installation " ?
do u mean the installation of OpenWRT firmware on the router?

author
mrx23dot made it! (author)mrx23dot2012-03-09

Here is a tutorial for that:
http://wiki.openwrt.org/doc/howto/firstlogin

author
edisonchung made it! (author)edisonchung2012-03-10

Thx!!! But I come across another problem ><

After the first login, I changed my password then I login again using:

ssh root@192.168.1.1

I tried to install the web UI using Luci due to I'm using MacOS,
but when I tried to update opkg with the command :

opkg update

It failed(as shown in the image attached), it seems like it has no internet connection,
but in fact, I'm able to browse websites on my browser.
Hope you could help me with this and sorry for my ignorance.

Screen shot 2012-03-11 at 12.51.41 AM.png
author
mrx23dot made it! (author)mrx23dot2012-03-11

hi,
instead of the bleeding edge, download the latest: Backfire 10.03.1 final firmware which already has web GUI in it.
This will save you a lot of time.

https://openwrt.org/

author
ssrinivas made it! (author)2011-09-13

hey mrx23, its a great project. I have decided to do this project as a part of my graduation project. it would be great if u help me. looking forward for some support. thanks man :)

author
Eddy Dean made it! (author)2011-08-21

looks great, but, how could this be done without WINSCP (as this does not work on my computer)?

author
mrx23dot made it! (author)mrx23dot2011-08-21

hi,

you can do it with PUTTY:
http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
but you need to know linux command line, because it doesn't have any GUI.

WINSCP should work easily on any PC
http://tinyurl.com/3ft8u62
maybe try older versions, or ask for support.

cheers,

author
johan_tr2000 made it! (author)2011-07-19

Nice idea mrx23 and joejoerowley.
Can you elaborate in more detail how to set up and configure the 741 so it allowed to remote control from outside wifi network (from internet)?

author
MarkV6 made it! (author)2011-05-20

Nice project, any idea how to send data the other way? For example show a temperature from the serial port to the website.

author
skeegan made it! (author)2010-12-02

Hi mrx23, This project is great- I think it could be really useful for me. I have a couple of questions regarding the schematic (please forgive my electronic ignorance:). Schematic attached-would be great if you could offer some insight. S

schematic.png