30
4.0
USING LUA TO WRITE CUSTOM WEB PAGES
Using Lua, the user may create custom web pages to be served by the EM405-8’s embedded web
server. In addition to creating the pages, themselves, the user can add links to the standard
navigation menu that is found on every one of the default web pages of the EM405-8. The
scripts that create the custom web pages have full access to the EM405-8 scripting utilities like
any standard script. Scripts can, among other things, configure hardware, retrieve status,
write/read files, and send/receive data.
Lua based web pages are called from a CGI executable that maps the standard console output
(stdout) to the requesting web browser. In other words, all standard output from the standard I/O
library functions such as
print()
and
io.write()
go directly to the web browser. Therefore, a
Lua script can create a webpage by simply outputting the HTML like the example in Figure 10.
Figure 10 Simple Lua Based Web Page
Note that in the example in Figure 10, double square brackets ([[ & ]]) start and stop the print
statement instead of the standard double quotes (“). In Lua, double square brackets denote a
literal string where there are no special characters or escape sequences. This is especially useful
when using Lua to create a webpage as it allows the developer to input the HTML exactly as it
would be in a standard HTML file.
Unlike standard scripts, web-based scripts do not run in there own thread; therefore they must
not loop forever, nor can they be queried using
list –r
or halted. In fact, for most, if not all,
browsers, a webpage will not be displayed until the script is complete. If a script needs to be run
in a loop, the developer should use a Meta Refresh Tab to instruct the web browser to
automatically refresh the page (i.e. run the script) after a certain period of time.
4.1
URL OF SCRIPT BASED PAGES
As mentioned in section 4.0, Lua web page scripts are called from a CGI executable. This
executable is named
script.cgi
and takes the script filename as a parameter. There are actually
two locations of
script.cgi
one for public pages at /cgi-bin/script.cgi and one for private
password-protected pages at /cgi-bin/private/script.cgi. The script filename is passed to
script.cgi
within the URL using the standard name/value pair parameter passing.
print([[
<html>
<head>
<title>An HTML Page</title>
</head>
<body>
<a “href=http://www.chtech.com”>C&H Technologies, Inc.</a>
</body>
</html>
]])