As I didn't include it in the last episode, here is a recap of the materials used:
- Raspberry Pi Model B (of course!)
- MicroSoft HD LifeCam
- Edimax WiFi USB dongle
- Lexar 32GB USB drive
- Generic 4GB SD card.
- Powered USB hub
For some reason, I had an install of RaspBMC already on this SD card, so I decided to go with it (read: lazy). I am pondering setting a small monitor by the window, and playing some sort of atmospheric videos on it for the kids, but for now I'm going to focus on the cam.
First order of business was to install Nginx, a webserver.
sudo apt-get install nginx
Next, I mounted the 32GB USB drive as a subdirectory under the web server's default directory. So edit the fstab, and set /dev/sda1 as /usr/share/nginx/www/cam, so that I wouldn't fill the poor lil SD card with a lot of pics. Inside the 'cam' directory, I created a small index file as follows:
| (html)(head) | |
| (meta http-equiv="refresh" content="90") | |
| (title)Testing Cam 1.6(/title) | |
| (/head) | |
| (body bgcolor=black text=white) | |
| (h1)Halloween cam 1.6(/h1) | |
| (P) | |
| Front door view: Click (a href=daily/)here |
(br)
(img src=cam.jpg)
(/body)(/html)
This of course refreshes the page every 90 seconds, and as we will see later, this produces either a brand new picture, or an error message. Now on to the more interesting bits.
Building on last year's work, I have used fswebcam. Again I will take 4 frames every minute, and write it to a cam.jpg that is mentioned in the web page code. So I have a small shell script that takes the current cam.jpg, renames it with a date stamp, and moves it to another directory for later. After that, it fires up fswebcam and takes a snap, naming it cam.jpg. I run this every minute in cron.
Here is that code
#!/bin/bash
# Timelapse controller for USB webcam
DIR=/usr/share/nginx/www/cam/
x=1
while [ $x -le 4 ]; do
filename=$(date +"%d%m%Y_%H%M-%S").jpg
mv /usr/share/nginx/www/cam/cam.jpg /usr/share/nginx/www/cam/pics/$filename
# fswebcam -q -d /dev/video0 -r 800x600 --flip v,h --set brightness=100$ --font /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf --top-banner --title "Door Cam Mark 1.6" /usr/share/nginx/www/cam/cam.jpg
fswebcam -q -d /dev/video0 -r 1280x720 -S 10 --flip v,h --font /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf --top-banner --title "Door Cam Mark 1.6" /usr/share/nginx/www/cam/cam.jpg
x=$(( $x + 1 ))
sleep 10;
done;
The most important setting, turned out to be -S to skip a few frames and allow the white balance/brightness to adjust. Before I rediscovered this option, from just after dawn to dusk was just a white box.
And now to the "for later" mentioned before. If I didn't want to do this next step, I could just leave the script creating a new cam.jpg every minute or so, available at check in. This would also greatly reduce the likelihood of having an error image when trying to load the picture as fswebcam is creating it. However, my next fun trick is to create a time lapse video, using all the pictures of the day. This code is here for the MP4 version.
date=$(date +"%d%m%Y")
ls /usr/share/nginx/www/cam/pics/*.jpg > /usr/share/nginx/www/cam/list.txt
mencoder -nosound -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:turbo=1:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh -vf scale=-8:-8,harddup -o /usr/share/nginx/www/cam/daily/$(date +"%d%m%Y").mp4 -mf type=jpeg:fps=10 mf://@/usr/share/nginx/www/cam/list.txt
rm /usr/share/nginx/www/cam/pics/$date*.jpg
I create a list of all the jpg files, put this in a txt file, and run it through mencoder to create an avi file. I am experimenting with creating mp4/x264 instead, as it will be playable on a few more devices.
I believe I will eventually opt to offload the video encoding to another machine, as the Pi is not exactly a CPU powerhouse. Using mp4/x264, it took the poor lil Pi about 5-6 hours to create the video.
Obligatory sample pic from the cam:
Epic mounting and duct tape:
Link to youtube video coming soon!



