Page 361 - Hacking Roomba
P. 361

342       Part III — More Complex Interfacing




                             The spcacat program spews a lot of information as it interrogates the camera to figure out
                             what it’s capable of. It then prints a line each time it takes a picture. This is a lot of text and
                             after a while you’ll get tired of it. If so, create a little script called camstart and have it contain:
                             #!/bin/sh
                             ln -fs /dev/v4l/video0 /dev/video0
                             spcacat -d /dev/video0 -g -f jpg -p 1000 -o > /dev/null
                             Move your new camstart program to /usr/bin and use it instead:
                             root@OpenWrt:~# vi camstart               # write the above and save
                             root@OpenWrt:~# chmod +x camstart
                             root@OpenWrt:~# mv camstart /usr/bin
                             root@OpenWrt:~# camstart &


                             Viewing Images
                             You now have a program writing a new JPEG image to the file /tmp/SpcaPict.tif once a
                             second and doing it entirely in the background. To view that image, you could copy it to your
                             PC, but it’s easy to put it within the document root of the OpenWrt built-in web server. This
                             server is located at /www, so the easiest thing to do is create a symbolic link from the image to
                             somewhere under /www, like so:
                             root@OpenWrt:~# ln -s /tmp/SpcaPict.tif /www/SpcaPict.tif

                             Now you can point your web browser at your OpenWrt box and view webcam images. If your
                             WRTSL54G has an IP address of 192.168.0.101, then go to http://192.168.0.101/
                             SpcaPict.tif. Press Reload on your browser to get an updated image.

                             You may wonder why you don’t just run the webcam program from inside /www. It will write
                             there without this symlink business. The problem is that /www is part of the OpenWrt flash mem-
                             ory. Flash memory has a limited number of write cycles, a few ten thousand. For a USB disk this
                             is okay because you can easily replace it, but it’s almost impossible to replace the flash soldered
                             down inside the WRT. If the webcam program were allowed to continuously write to the flash,
                             the flash memory would be worn out within a day.

                             Pressing Reload all the time is tedious. With a little JavaScript you can have the browser
                             refresh the image for you. Listing 15-1 shows a small dynamic HTML page to accomplish
                             this, called roombacam.html. When you have this file, put it anywhere in the /www directory.
                             You may need to adjust the symlink to SpcaPict.tif so that it’s in the same directory as
                             roombacam.html. Figure 15-8 shows the resulting web page. You can now easily watch the
                             world from the perspective of your Roomba as it goes about its business.


                               Listing 15-1: roombacam.html: Auto-Refreshes the Webcam Image
                               <html>
                               <head>
                               <title> Roomba Camera </title>
                               <script language=”JavaScript”>
                               <!--
   356   357   358   359   360   361   362   363   364   365   366