Page 370 - Hacking Roomba
P. 370
Chapter 15 — RoombaCam: Adding Eyes to Roomba 351
Controlling Roomba with CGI
OpenWrt ships with a rudimentary Common Gateway Interface (CGI) capability as part of
its web server. With the roombacmd C program installed, it can be used in CGI programs.
The most common language used for OpenWrt CGI programs is plain shell scripting. You can
write CGI programs in C, but the cross-compiling gets to be a pain. If you’ve poked around
the /www directory, you may have noticed the OpenWrt web interface is all written in shell.
Listing 15-5 shows roombapanel.cgi, a small CGI shell script used to control Roomba
with roombacmd. The top part of it can be edited to fit the configuration of your system.
The middle part is a simple HTML interface with buttons that point to the CGI and set
the $QUERY_STRING environment variable to a valid roombacmd command. The last part
does the work of commanding Roomba. If a button is pressed, the $cmd variable is set and
that is used as the movement argument to roombacmd. Figure 15-10 shows what the CGI
looks like when running. The PICPATH and SNAPPATH variables aren’t used yet, but they
will be.
Listing 15-5: roombapanel.cgi
#!/bin/sh
# edit this: serial port of the roomba
PORT=”/dev/usb/tts/0”
# edit this: path to roombacmd
ROOMBACMD=”/usr/bin/roombacmd”
# edit this: where the webcam is writing an image
PICPATH=”/tmp/SpcaPic.tif”
# edit this: where archived (“snapshot”) images should be
stored
SNAPPATH=”/mydisk/archive/cam-`date -Is`”
me=”$SCRIPT_NAME”
cmd=”$QUERY_STRING”
cat <<EOF
Content-type: text/html
<html>
[... html interface to make buttons ...]
</html>
EOF
if [ “$cmd” ] ; then
echo “cmd: $cmd”
$ROOMBACMD -p $PORT -- $CMD
fi