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.
- 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)
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
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.
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 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.
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)
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
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.