Sending Message to Serial Port of Router using Lua
- PHP require to install lighttpd,lighttpd-mod-cgi,lighttpd-mod-fastcgi,php5-cgi,php5-fastcgi,libsqlite3, installation is complicated & consume resources.
- ser2net/socat is very simple but did not support web service.
- Lua comes as a default in openWrt and support web service. The goodies is OpenWRT using Luci for web GUI and LuCI is an embedded rapid application development framework written in Lua.
- Arduino duemilanove connected to a TP-LINK TL-WR1043ND router which flashed with OpenWRT.
- Using 16 Channel Relay Arduino Controller for the Arduino sketch
First, we must install USB-Serial library for router in order to making the USB port act as a serial port. Here I install FTDI library since Arduino duemilanove using FTDI chip for interface.
opkg install kmod-usb-serial-ftdi |
Verify USB-Serial port
To make sure the USB-Serial port is working, enter code below. You should see FTDI USB Serial Device converter now attached to ttyUSB0.
dmesg | grep -i usb |
Execute Lua script from Router
The most simple way to execute Lua script is SSH to router and enter lua. Now you can enter Lua command line by line.
login as: root
_______ ________ __ |
Open the Port
This is particular important, it use to prevent autoreset on initiation of serial.
cat /dev/ttyUSB0 |
Arduino should acknowledge and reply message to router. This should look like this
Sending command to USB-Serial
While cat /dev/ttyUSB0 is still running, open another incident and execute script below.
This will open a ttyUSB0 port and send on3 (include carriage return) to the port. Arduino channel 3 should turn on.
io.output("/dev/ttyUSB0")
io.write("on3\r") |
Completed step should look like this.
[email protected]:/www# lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio (double int32) > io.output("/dev/ttyUSB0") > io.write("on3\r") > |
Execute Lua script from a File
Save script below to router /www/test.lua. Do not use notepad to create the file (it is not a unix file format). Here I use WinSCP.
print("Turn on channel 2 and channel 3") |
Run the file as shown in figure below. Channel 2 and Channel 3 should turn on.
[email protected]:/www# lua /www/test.lua |
Open USB-Serial port on Reboot
- Login to router
- Click System -> Startup
- Scroll down until you see Local Startup
- Insert cat /dev/ttyUSB0 in front of 'exit 0' as shown in figure below
- Reboot the router
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing. cat /dev/ttyUSB0
exit 0
|
Execute Lua file from Browser
- The Lua file must place in /www/cgi-bin directory of router.
- The first line of the Lua file must start with #!/usr/bin/lua
- Change file permission to 0755 or 0777.
Having the same example test.lua, copy it to /www/cgi-bin and rename it to test (without extension), you should have /www/cgi-bin/test in the router.
#!/usr/bin/lua
print("Turn on channel 2 and channel 3") io.output("/dev/ttyUSB0") io.write("on2\r") io.write("on3\r") |
Setting File Permission to 0755. This is particular important or else you will not allow to run the file.
chmod 0755 /www/cgi-bin/test
|
Execute Lua script from Browser
- Open a browser
- Enter http://192.168.1.1/cgi-bin/test (192.168.1.1 is the router IP address, you should use your own IP address)
- Channel 2 and Channel 3 should turn on
Authentication
Use code below for authentication before execute Lua file. The user name & password same as the router login user name & password.
echo '/cgi-bin:root:$p$root' > /etc/httpd.conf; /etc/init.d/uhttpd restart |
command="echo yourMessage\r > /dev/ttyUSB0" |