HTTP Routing on Arduino Yun

Just digging into the Yun. I'm looking at the REST API and wondering where the routing of HTTP requests is handled.

Out of the box, the Yun handles requests at http://{IP}/arduino, /mailbox and /data

I'm wanting to add handling at, for example, /somestring

I cat'd around in /usr/lib/python2.7/bridge with no luck. Anybody know where this is managed?

I understand this won't be done on the arduino side. I am comfortable in python.

It looks like it's a combination of

and

Which leads to a new problem...

When I try to cat, less or vi the *.lua files on my Yun they all come up as binary. Same happens if I 'scp' to my local machine and open them in vim or sublime text. Anybody seen this/have any idea why it's happening?

A hacky answer to my own question. Edit /etc/httpd.conf to look like this:

A:/:cgi-bin/luci/arduino/%s
A:/arduino:/cgi-bin/luci/arduino%s
A:/data:/cgi-bin/luci/data%s
A:/mailbox:/cgi-bin/luci/mailbox%s

Now requests to http://{IP}/* will be routed the same way as requests to http://{IP}/arduino/*

Works great as long as you don't mind breaking the web-based config. :stuck_out_tongue:

mlangdon:
When I try to cat, less or vi the *.lua files on my Yun they all come up as binary. Same happens if I 'scp' to my local machine and open them in vim or sublime text. Anybody seen this/have any idea why it's happening?

What file type does 'file' indicate for it?

PeterH:

mlangdon:
When I try to cat, less or vi the *.lua files on my Yun they all come up as binary. Same happens if I 'scp' to my local machine and open them in vim or sublime text. Anybody seen this/have any idea why it's happening?

What file type does 'file' indicate for it?

With Lua you can pre-compile your programs into bytecode to enable faster startup (see the man page for "luac"). You would need to find the uncompiled version of those programs.

Oh, ok. Thanks!

If I get the uncompiled versions from github and make changes, do I need to compile them before uploading to the Yun? Or do they auto-compile on first run like Python?

PeterH:

mlangdon:
When I try to cat, less or vi the *.lua files on my Yun they all come up as binary. Same happens if I 'scp' to my local machine and open them in vim or sublime text. Anybody seen this/have any idea why it's happening?

What file type does 'file' indicate for it?

opkg update
opkg install file
root@Arduino:/usr/bin# file /usr/lib/lua/luci/controller/arduino/index.lua
/usr/lib/lua/luci/controller/arduino/index.lua: Lua bytecode, version 5.1

mlangdon:
Oh, ok. Thanks!

If I get the uncompiled versions from github and make changes, do I need to compile them before uploading to the Yun? Or do they auto-compile on first run like Python?

Use luac to compile first.

sonnyyu:
Use luac to compile first.

Which, for future readers, do this first:

opkg install luac

Thanks everyone.

mlangdon:
Just digging into the Yun. I'm looking at the REST API and wondering where the routing of HTTP requests is handled.

Out of the box, the Yun handles requests at http://{IP}/arduino, /mailbox and /data

I'm wanting to add handling at, for example, /somestring

I cat'd around in /usr/lib/python2.7/bridge with no luck. Anybody know where this is managed?

I understand this won't be done on the arduino side. I am comfortable in python.

To make python work here, you need setup python as CGI.

Plan B:

mkdir -p /www/cgi-bin/python
nano /www/cgi-bin/python/python.py
#!/usr/bin/python
import datetime
import cgi
form = cgi.FieldStorage()
id = form.getvalue("id", "(no id)")
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print datetime.datetime.now()
print '
'
print id
print '</body>'
print '</html>'
chmod 755 /www/cgi-bin/python/python.py

http://arduino_ip/cgi-bin/python/python.py?id=100