Advertisements

Latest comments

Tuesday, 25 September 2012 20:53

Wireless Router Home Automation

Rate this item
(2 votes)
This project eliminate the use of an expensive Wifi/Network Shield but still able to control home appliances from any where. You may already own a wireless router which support OpenWRT or else you can get a very cheap wireless router such as WR703N for this project.
 
In order to eliminate install many packages on the router, I am using HTML and Javascript for the web interface, while hardware interface is using Lua which is come with OpenWRT. The whole process is just install a serial driver (see step below) to make the instruction simple & keep the router free of resources.
 

Prerequisite
  • Wireless router flashed with OpenWRT. This project based on TP-LINK TL-WR1043ND wireless router, you can use TL-WR703N or TL-MR3420 if budget is concerned, any other router that support OpenWRT should work as well.
  • Arduino code, sketch using Ocean Controls KTA223, or any serial device that use the following protocol.

Syntax for receiving message: @aaccpp

  • where aa is the serial device address ranged from 00 to 99 (00 means all Arduino)
  • where cc is the command, on, of, rs (turn on, turn off, relay status)
  • where pp is the channel ranged from 1 to 8

Syntax for replying message: #aapp

  • Where aa is the serial device address ranged from 0 to 99
  • Where pp either 1 or 0 when receiving an ON or OF command, it can be number ranged from 0 to 256 during query relay status. Converted to binary, 0(00000000) means all OFF, 256(11111111) means all ON.
 
 
Installing Serial Driver
Select the correct serial driver for your serial device. I'm using Arduino duemilanove, so SSH to router and enter code below:
opkg update
opkg install kmod-usb-serial-ftdi
 
If you are using Teensy or Arduino Uno
opkg install kmod-usb-acm

If you are using PL2303 based USB/serial adapters
opkg install kmod-usb-serial-pl2303

If you are using CP201x based USB/serial adapters
opkg install kmod-usb-serial-cp210x
 
Verifying Serial Port
Plug in Arduino to the USB port of router, make sure the serial device is working with router, and know the port name assigned to the serial device.
dmesg | grep -i usb
 
It should look like this
Verify USB Serial DriverVerify USB Serial Driver
 
 
Download
Arduino sketch from Ocean Controls: KTA223 RelayDuino
Web interface: wireless router home automation v1.zip
 

Installation
  • Download the web interface and extract to any folder of computer
  • You should see three files and two folders on your computer
  • Using WinSCP copy all the files and folders to /www folder of router
  • Now your router should have
/www/main.css
/www/main.html
/www/main.js
/www/cgi-bin/
/www/images/
 
  • You must have enough permission to run the file in /www/cgi-bin folder. So change the luaSerial file permission to 0755.
chmod 0755 /www/cgi-bin/luaSerial
 
 
Test the CGI (luaSerial) script 
All ON: http://192.168.1.1/cgi-bin/luaSerial?@00ON0
All OFF: http://192.168.1.1/cgi-bin/luaSerial?@00OF0
 

Running the program
  • Open up a browser and navigate to http://192.168.1.1/main.html (192.168.1.1 is the router IP, you should use your own router IP)
 Wireless Router Home Automation -- All OFFWireless Router Home Automation -- All OFF
 
 
Wireless Router Home Automation -- All ONWireless Router Home Automation -- All ON
  • Click on each images to toggle ON and OFF, images will change accordingly.
  • The Tools image & navigator images is not use at the moment.
  • The status bar is use to display serial message which reply from Arduino
 
Testing
As shown in figure below is the Arduino and LED that I use to test the software.
Arduino with LEDArduino with LED
 
 
 
How it works
  • The main program split into 3 files for easy understanding (main.html, main.css, main.js)
  • When main.html started, it load two external files (main.css & main.js)
  • onload (line 9) triggered and execute autoRun() function from main.js
  • Update image caption on screen
  • Issue runCmd(0) to get status from Arduino then start Timer to query status every 10 seconds
  • Update images based on results from Arduino
  • Startup process completed. Waiting for user input......
  • User click on imageButton, say first image
  • javascript:runCmd(1) triggered
  • Clear old Timer and start a new Timer
  • Ajax deal with serialLua
  • serialLua reply status (I name it Arduino message) to Ajax
  • Display Arduino message on status bar
  • Update first imageButton according to Arduino message
Detailed explanation for the code is written inside the source code, I have commented throughout to make modification easy.
 
All the while I'm using Ms Access and Delphi for my projects. I have only little knowledge of HTML and Javascript, recenlty I'm learning how to use Lua because of this project. I'm facing a minor problem in luaSerial.

I don't have a good solution to clear the serial buffer,  I have to use the most stupid way (very shy) to clear the buffer. Refer to code below, I have to issue readSerial() function instead of just using serial:read and serial:flush.

function readSerial()
   while true do
      serialData= serialin:read();serialin:flush()

      if string.len(serialData)>0 then
         serData = serialData
      end

      if string.len(serialData) == 0 then
         return serData
      end

   end
end

Please let me know if you got a good solution.
 
 
Authentication
Authentication is very very very...... simple. Thanks to OpenWRT team.
SSH to router and execute code below. User name & password is same as router login user name & password.
echo '/cgi-bin:root:$p$root' > /etc/httpd.conf; /etc/init.d/uhttpd restart

 

Control Home Appliances over Internet
To control home appliances from any where, you must have a DDNS account and open port for the router. This article explain how to control device over internet.

 

Future Plan
  • Rewrite web interface
  • More control, for example analog control
  • Web cam integrated
  • More......
 

This is a very preliminary release, please report any bugs you may find to the project. 

 

Read 15386 times Last modified on Monday, 07 October 2013 23:21

Comments  

chuc
#8 chuc 2014-10-25 17:52
"Bad Gateway
The process did not produce any response"

But can on/off relay module
Quote
Justin Beck
#7 Justin Beck 2014-09-26 06:27
nothing happens when I click the button.
Quote
maximusz
#6 maximusz 2014-09-06 15:51
Dear friend,

I have a same problem. The "test" script said:

"Bad Gateway
The process did not produce any response"

I have a TPLink 4300 router, and I don't know how will be able to resolve this problem.

thanks
Quote
Admin
#5 Admin 2014-07-13 09:15
The test did not require an Arduino, just navigate to a web page (PHP, etc) or CGI (Lua, Pyton, C, etc) to request a return, so it is not related to serial port.

with my past experiences, I notice that Lua is not working well in certain router. If this is the case, you should use Pyton or PHP, etc.
Quote
sakatgg
#4 sakatgg 2014-07-12 02:04
Router NetGear3800    
Edit file "www/cgi-bin/lu aSerial" , only
#!/usr/bin/lua
print ("test")
browser to "http://192.168.1.1/cgi-bin/luaSerial"
Answer:
"The process did not produce any response"
What is the reason?
These ports should probably also leave luaSerial?

P.S. Experiment with zero - set attitude_adjust ment 12.09 default, set only kmod-usb-acm
Edit file "www/cgi-bin/lu aSerial", only
#!/usr/bin/lua
print("test")
browser to "http://192.168.1.1/cgi-bin/luaSerial"
Answer: Status: Unable to launch the requested CGI program: /www/cgi-bin/lu aSerial: No such file or directory
Quote
Admin
#3 Admin 2014-07-11 21:47
Are you using TL-WR1043ND router? if you are using other router such as TL-MR3020 3G/4G router may not return any message.

Use the following method to test your router whether return any message or not.

Your luaSerial file only contain two line as below:
#!/usr/bin/lua
print("test")

Now navigate your browser to "http://yourRouterIP/cgi-bin/luaSerial"

Your browser should return test
Quote
sakatgg
#2 sakatgg 2014-07-11 03:21
Relay ON and OFF normally, but status is not displayed (message: Bad Gateway The process did not produce any response), and the picture does not change buttons. Browsers using Firefox, Opera, Chrome, Internet Explorer. What's the problem? thank you
Edit file /www/cgi-bin/lu aSerial dev/ttyUSB0 -> dev/ttyACM0
Arduino Uno R3 (Usb controler mega8U2(dev/tty ACM0)), sketch kta223_OCRelayD uinoASCIISlaveS KETCH, OpenWRT trunk ver41459, NetGear3800

Relay using 5V with optocoupler
Quote
Edward COmer
#1 Edward COmer 2013-03-19 11:26
Very interesting. I'll be testing this out with my TL-WR703N
Quote

Add comment


Security code
Refresh