Saturday, 15 September 2012 15:00

Sending Message to Serial Port of Router using Lua

A few ways to send message to serial port of router which flash with OpenWRT. PHP;ser2net; socat; Lua and etc.
  • 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.
Prerequisite
 
 
 
Installing FTDI for Router
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
[email protected]'s password:


BusyBox v1.15.3 (2011-11-24 00:44:20 CET) built-in shell (ash)
Enter 'help' for a list of built-in commands.

_______ ________ __
| |.-----.-----.-----.| | | |.----.| |_
| - || _ | -__| || | | || _|| _|
|_______|| __|_____|__|__||________||__| |____|
|__| W I R E L E S S F R E E D O M
Backfire (10.03.1, r29592) ------------------------
* 1/3 shot Kahlua In a shot glass, layer Kahlua
* 1/3 shot Bailey's on the bottom, then Bailey's,
* 1/3 shot Vodka then Vodka.
---------------------------------------------------
[email protected]:~# lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio (double int32)
>

 

 

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

[email protected]:/www# cat /dev/ttyUSB0
Conrtonller Ready.

 

 

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")
io.output("/dev/ttyUSB0")
io.write("on2\r")
io.write("on3\r")

 

Run the file as shown in figure below. Channel 2 and Channel 3 should turn on.

[email protected]:/www# lua /www/test.lua
Turn on channel 2 and channel 3

 

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

 

Updated on  04 Jan 2014
A much more simple example to demostrates how to send yourMessage with carriage return to ttyUSB0 port.

command="echo yourMessage\r > /dev/ttyUSB0"
os.execute(command)

 
Read 32454 times Last modified on Sunday, 12 June 2016 10:52

Leave a comment

Back to Top