Raspberry Pi Webcam

How it works
Camera 1
The Hardware
Raspberry Pi
Logitech HD Webcam C310
8GB Play Class 6 SD Card
Powered USB Hub
UBEC
1M 2 pair Ethernet cable
50M Cat5e
Various adapters and cables
24V Adapter
Food Storage Tub

The webcam was chosen due to it being a confirmed working webcam. One source says that it doesn't require a powered USB hub but it wasn't possible to capture a full resolution (1280x960) image without a powered hub.

The Raspberry Pi has a max network speed of 100Mbit, this meant a form of Power over Ethernet was possible using a homemade injector and splitter. This involved using the 2 spare pairs in 50M Cat5e cable to carry the required power for the webcam setup. Over 50M there is a substantial amount of loss in power due the cable's resistance, especially under load. I first tried to inject a 5V supply, this didn't work. 12V with a 5V UBEC didn't work either. There was too much loss and the resulting voltage was only 4V when the Raspberry Pi was plugged in, the red light lit but it wouldn't boot. I had an old spare 24V adapter from (I think) a flatbed scanner. This gives enough power to the UBEC at the end of the 50M cable to be able to provide the USB Hub and Raspberry Pi with a steady 5V.

I used this calculator to get an idea of the losses over certain distances. The 24V adapter is by no means the ideal adapter for the job, but it was a spare and saved me buying a new one.



The Software
Rasbian - Wheezy - 2012-10-28
Imagemagick (for convert)
FTP
fswebcam

The webcam is run via a bash script that is run as a cronjob every 20 seconds.

I had tried to do this using "streamer" and "uvccapture". Both worked fine on a proper PC running Linux but failed on the Raspberry Pi above resolutions of 320x240. fswebcam works perfectly for each frame capture.

The script creates a temporary folder, tells fswebcam to take a photo, it then resizes the image to a number of sizes, uploads the images to an FTP server and once all that is done it deletes the images from the Raspberry Pi. The whole thing takes around 25 seconds with the longest bit being the image conversions.

To decrease the conversion time I overclocked the Raspberry Pi to the "High" setting using raspi-config. This dropped the total script time by a few seconds but my SD card suffered from some file system corruption. The overclocking file system corruption issue is well discussed on the Official Raspberry Pi Forums. Due to this I had to wipe my SD and start from scratch without overclocking.

The bash script is saved as "/usr/bin/webcam-still" and it needs to be executable
(chmod +x /usr/bin/webcam-still)

Bash Script
#!/bin/sh
#

HOSTftp=FTPhostAddress
USERftp=username
PASSftp=password
currdate="`date +%y-%m-%d_%H-%M-%S`"

mkdir /tmp/webcam-$currdate
cd /tmp/webcam-$currdate

fswebcam -c /home/pi/.fswebcam.conf --save still-$currdate.jpg

cp -Rfu still-$currdate.jpg still.jpg
cp -Rfu still.jpg still-1280.jpg

convert still.jpg -resize 320x240 still-320.jpg
convert still.jpg -resize 480x360 still-480.jpg
convert still.jpg -resize 640x480 still-640.jpg

ftp -inp $HOSTftp << EOF
user $USERftp $PASSftp
binary
put still-$currdate.jpg
put still-320.jpg
put still-480.jpg
put still-640.jpg
put still-1280.jpg
bye
EOF

rm *
rm -rf /tmp/webcam-$currdate

exit 0
.fswebcam.conf
device /dev/video0
resolution 1280x960
no-banner
jpeg 90
crontab
*/1 * * * * /usr/bin/webcam-still
*/1 * * * * /bin/sleep 20; /usr/bin/webcam-still
*/1 * * * * /bin/sleep 40; /usr/bin/webcam-still

Camera 2
The Hardware
Raspberry Pi
Canon IXUS 220 HS
2GB SanDisk Class 2 SD Card
1 Channel 5-12v Relay Module
Jumper cables
12V Adapter
UBEC
5M Cat5e
Canon ACK-DC10 Coupler Kit
Various adapters and cables

This is a significantly different setup to Camera 1. Rather than using a webcam it's using a proper compact camera. The camera in question is a Canon IXUS 220 HS running CHDK

Currently the setup is inside so I've not put it in a box or made any special POE system for it. I will hopefully do that in the near future.

Due to a few issues with the camera crashing/turning off every so often I attached a relay between the Raspberry Pi and the camera. By soldering two wires to the camera on/off switch then attaching them to the relay this enables the Rapsberry Pi to turn the camera back on when it turns itself off.

The camera is powered using the DR-10 coupler (from the Canon ACK-DC10 Coupler Kit) attached to the 5V UBEC. The camera battery is 3.7V and the mains coupler is 4.3V but the camera runs fine at 5V.

(Photos to follow)

GPIO - Relay
5V ------ VCC
GND ----- GND
GPIO17 -- IN

The Software
Rasbian - Wheezy - 2012-10-28
Imagemagick (for convert)
FTP
chdk-ptp - Alternative PTP Client for CHDK

The webcam is run via a bash script that is run as a cronjob every 40 seconds.

The Raspbian Debian install is quite stripped back, I uninstalled quite a bit of pre-installed software, including the window manager and X server to reduce the size significantly due to the 2GB SD card.

When using chdk-ptp with the camera connected to the Raspberry Pi via USB it is possible to interact with the camera in a wide range of ways. What I decided to do is nice and simple with chdk-ptp

Delete all photos on the camera (Delete DCIM directory)
Set camera to shooting mode
Set zoom level
Set focus mode
Take photo
Download photo to local directory
Delete all photos again (Delete DCIM directory)
Disconnect

Once the photo is downloaded to the Raspberry Pi it is resized, uploaded via FTP and then deleted. Due to the size of the photos (1600x1200) the resizing process is quite time consuming. If you were to capture at the full resolution of the camera it would take even longer. The whole script takes around 30 seconds in total.

Due to the camera occasionally crashing I've written some fairly crude lines for camera detection using lsusb and grep. If the camera is detected as being off the relay is activated to turn the camera back on.

Bash Script
#!/bin/sh
#

HOSTftp=FTPhostAddress
USERftp=username
PASSftp=password


currdate="`date +%y-%m-%d_%H-%M-%S`"

mkdir /tmp/chdk-download
mkdir /tmp/chdk-cap-$currdate
cd /tmp/chdk-cap-$currdate

lsusb | grep -i -c canon >> lsusbcanon.txt
CAMERA=$(cat lsusbcanon.txt)

if [ $CAMERA = "0" ];
then
echo $currdate "- No camera - No capture - Restarting Camera"
echo "Attempting to restart camera"

echo "17" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio17/direction
echo "1" > /sys/class/gpio/gpio17/value
sleep 0.1
echo "0" > /sys/class/gpio/gpio17/value
echo "17" > /sys/class/gpio/unexport

else
echo $currdate "- Camera Online - Capture"
echo "Capturing Photo"

sudo sh /home/pi/chdkptp/chdkptp-sample.sh -c -e"delete DCIM" -e"exec sys.sleep(2000)" -e"luar switch_mode_usb(1); set_zoom(0); set_prop(6, 3); shoot(); switch_mode_usb(0)" -e"exec sys.sleep(2000)" -e"mdl DCIM /tmp/chdk-download" -e"exec sys.sleep(2000)" -e"delete DCIM" -e"dis"

cp /tmp/chdk-download/1*/*.JPG still-$currdate.jpg
rm -rf /tmp/chdk-download/*
echo "Resizing Photo"

convert still-$currdate.jpg -resize 1280x960 2-still.jpg

echo "FTP Upload"
ftp -inp $HOSTftp << EOF
user $USERftp $PASSftp
binary
cd public_html
put 2-still.jpg
bye
EOF

fi

rm -rf /tmp/chdk-cap-$currdate

echo "Finished - `date +%y-%m-%d_%H-%M-%S`"

exit 0
crontab
*/2 * * * * /usr/bin/chdk-cap
*/2 * * * * /bin/sleep 40; /usr/bin/chdk-cap
*/2 * * * * /bin/sleep 80; /usr/bin/chdk-cap