I set up a camera so that we can check in on our cat during the day. I had a Logitech C270 webcam sitting around (Amazon says I bought it June 23 2016). I connected the camera to a Raspberry Pi 3 that was gathering dust and, 24 hours later, I had a working webcam.

The Pi already had Debian installed from a previous project. First, I updated everything with apt. I spent some time updating the configuration of wpa_supplicant to match my current Wi-Fi settings. I then installed webcam, software that grabs an image from an attached camera and uploads it anywhere you like. I copied the default configuration from webcam’s man page to .webcamrc, updated a few settings, added myself to the video group (/dev/video0 is 660, owned by root:video), and started capturing images.

I ran into an issue with the file transfer to my web server. When connecting to my VPS, I use a public key. The location of the key is specified in the -i option to scp. I didn’t see a way to specify that in webcam’s configuration. So, I wrote a wrapper in bash that captured the image locally using webcam and then uploaded the image in a separate step using scp.

I had another issue when capturing images. Webcam would often fail to grab an image from the camera in the first few attempts. It would die because /dev/video0 was missing, busy, or because I didn’t have permission. Oddly, this issue would usually resolve itself after a few attempts. I extended my bash script to run webcam until it successfully captured an image or failed five times, whichever came first. This setup has been reliable so far.

Here’s the script:

#!/bin/bash
tries="5"
while [[ "$tries" != "0" ]]; do
  tries=$(($tries - 1))
  webcam && scp -i keyfile /home/dave/webcam.jpeg dave@example.com:/var/www/ && tries="0"
  sleep 2
done

With the capture and upload working, I wanted to make it run automatically. Technically, webcam can do that on its own but, due to my workarounds, I decided to create a cron task for it.

I added the following line to my crontab:

*/10 8-17 * * * bash /home/dave/capture.bash >/dev/null 2>&1

Cron will run my capture script every ten minutes between 8 a.m. and 5 p.m. I throw away all the output, otherwise cron will send it to me in an email and I don’t want those stacking up.

The Pi and webcam are now set up in an out-of-the-way corner. The camera is pointed at one of our cat’s favorite lounging spots. When we’re out and about, we can pull up the most recent webcam image on our phone and possibly catch our cat napping by the window.