Description
This post is the last of three and describes the software needed to build a web controlled home automation center. The previous two describes how to add a serial interface to a router and how to build a microprocessor lab board.
Features
- Software for the router (OpenWrt)
- Software for the web server (html and CGI)
- Software for the lab board (PIC16F628 assembler)
- Protocol for serial communication between router and lab board
- Protocol for 433MHz radio (and html generation tools)
Development
My first project for the web access lab board is an Internet controlled home automation system. This allows me to control the lights in my apartment with my Smartphone or iPad (accessing a web page). I already have the infrastructure – custom built control panels and hacked low cost receivers (I will try to post them later).
In a way using a separate microprocessor for the radio communication is overkill. You could bit bang the radio data directly from the router using the power LED, but the microprocessor adds the ability to have more features (like IR communication) and for me it was a quicker hack since I had most of the code ready.
This is how it works:
- A mini router is running OpenWrt with a tiny web server
- The web server shows a plan of my apartment with icons for all controllable lights.
- The icons have links that starts CGI scripts
- The CGI scripts are sending control data to the serial port of the router
- A PIC microprocessor decodes the data and transmits it as 433MHz radio signal
- An RF switch or dimmer receives the data wirelessly and changes the attached light accordingly.
Installing the router

It was very easy to install and set up OpenWrt on the router (by following the steps in this link http://embeddedtimes.blogspot.com/2011/09/tp-link-tl-wr703n-tiny-linux-capable.html). The biggest problem was to change the baud rate on the serial port. This is done with stty, but the problem is that stty isn’t included in the busybox that comes with the default OpenWrt package (I tried picoterm, microcosm and setserial without success). You can either install this custom firmware or just replace busybox with this version. Try to avoid deleting all symbolic links to busybox, or you will be busy for a while (trust me, I know…).
Make sure the web server starts on boot and that the baud rate is set to 9600:
stty -F /dev/ttyATH0 raw speed 9600 -crtscts cs8 -parenb -cstopb
When everything is ready you should have web access to the web server on the router and a serial port running @ 9600 8n1
Setting up the web interface

The user interface showing three different mood buttons, a floor plan for the apartment (the magenta box turns off the complete room) and the individual controls for the lights.
I wanted to have a visual control over all the lights in my apartment. I did a graphic floor plan and added light control icons for all the controllable lights and saved it as a png. Then I created a webpage that displayed the image and used image map to link the icons to different CGI scripts. In the future I will encode the data in the html file to a generic CGI script, but as a quick fix I created different CGI scripts for each light, one for light on, one for light off and others for light intensity (see radio protocol further down for more details). I then uploaded the files to the router and set the right permissions to the CGI files. As a final step I connected my logic analyzer to the serial port of the router and verified that everything worked as it should.
Open Source serial protocol

Instead of inventing something new I thought it was a good idea to use a protocol that already exists. I found this open source protocol that is used in a product called tellstick, sold in Sweden. It does the job and it allows support for many different standards of radio recievers. Here is a brief description of the protocol:
The first byte defines type of command; if it is an “extended send command” the following four bytes declare four different timing lengths in 10us values. Then one byte with package length of the data followed by the real data and ending with a “+”. The data is encoded so that the length of each high or low flank is matched to one of the four different timing lengths (written as two bits).
The lab board

The microprocessor receives the serial protocol (using the USART), decodes it and sends it as radio, by bit banging the radio data, to a hacked radio module from a remote control. All the code is written in assembler (1075 lines); you can find the compiled .hex file and the source at the end of this post. I used a PICkit2 programmer attached to the ICSP port of the lab board to upload the microprocessor code. The lab board is designed to be used with a router for web control, but it also works great together with an ftdi cable for USB control from a PC.
Radio protocol

Since the serial protocol is generic and supports several radio protocol standards I could choose the one that I think is the best. For me it is important that each remote receiver can respond to several different transmissions and not just a single id. This allows me to arrange different lights into groups so that a single command can set the light on several devices at once. And by arranging all lights in a room to one command, I can turn off the complete room with one click. It is important that the devices have unique addresses so that my neighbors won’t control my lights by accident. All this is possible with the NEXA (learning code) protocol which allows 67 million different codes. To generate different codes for my devices I wrote this set of JavaScripts that generates the raw data that is transmitted to the lab board to control the lights.
Version tracker – microprocessor lab board files (PIC16F628 assembler)
0.1 Hard coded radio commands
0.2 Telstick protocol
0.3 Added serial interface
0.4 Adjusted timing
Future IR transmission (to control stereo, TV etc)
IR reception (to control the lights from an IR remote control)
Version tracker – JavaScript (to generate nexa commands)
0.1 First version (only using hex code)
0.2 Hex code generation
0.3 Added light intensity support
0.4 Added different hex outputs
Future No updates planned
Documents
Web server package (html and CGI files)
Lab board compiled file (.hex)
Lab board source code (PIC 16F628 assembler)
Radio protocol generator (html with JavaScript)
See also
Licensing

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Pingback: Web access lab board (internet of things) | Johan von Konow
Pingback: Access serial communication on a mini router (no extra connector) | Johan von Konow
WOW!
Very impressive project! I love the simplicity of your solution (in hardware and in software interface).
I’ve just discovered your blog and I’ll bookmark it right now.
It would be great some details (maybe a guide) on how you create a webserver on your router and how do you pass the serial data (from and to) the micro!
I’m actually on Arduino platform but I think it will be quite similar
Anyway, good job!
Thank you for the positive feedback!
Double check these links and let me know if there is anything you are missing?
Install openwrt with webserver:
http://embeddedtimes.blogspot.com/2011/09/tp-link-tl-wr703n-tiny-linux-capable.html
Download the webserver package and go thru the .cgi files to see how the data is being copied to the serial port.
http://vonkonow.com/wordpress/wp-content/uploads/2012/01/webserver.zip
And read thru this link regarding the serial protocol:
http://developer.telldus.se/doxygen/TellStick.html
It would be great to see an Arduino version of the serial to RF conversion. One tip could be to connect the Arduino directly to the USB port of the router. Then you don’t need to solder anything to access the serial port. You will need drivers to the usb2serial adapter on the Arduino board. Google OpenWrt and Arduino and I’m sure you will find how to install the drivers (might be different if you are using old board with FTDI or new board with Atmel chip…)
Good luck and let me know how it goes!
Thanks for very inspiring tutorial.
I connected Arduino to Wr703N and to read out sensors and contorl 433 MHz power outlets.
http://www.instructables.com/id/Wireless-power-outlets-for-home-automation-using-A/
Next will try 433 MHz thermometer.
May be we could collaborate and do some projects togather.
Hi Janis
Nice project!
Did you have any problem to get the Arduino USB2serial driver to work with the router? I think I read that the Uno could be a bit tricky…
It would be great to do something together; do you have anything special in mind?
/j
Hi!
Great write-up. I am going to buy one of these and have a shot at web enabling my home automation system now. My plan is to try and get one of these to talk serial to my home alarm system, to control its relay outputs. My only concern is that I have ZERO Linux or website/CGI etc. experience. Am I hoping for too much??
Is it difficult to creat the scripts to send serial commands out when pressing the website buttons? I was hoping I could get it to receive serial as well so the website can tell me if alarm panel inputs are in alarm etc.
Thanks for sharing your creation