Supercharge your Raspberry Pi
Get your soldering iron ready
Back up your photos using your Pi
The size of the Raspberry Pi means we can use it to take control of other embedded devices. This may seem a little redundant – the embedded devices obviously have some form of controller already – but it means we can script and extend them in ways that aren't possible (or are, at least, very hard) without the extra device.
Almost anything that you can plug into a normal desktop can be scripted by a Pi, but we're going to look at cameras for a couple of reasons. Firstly, there's support for most in Linux, and secondly there's a range of useful projects you can do once you've grasped the basics.
The best command-line tool for manipulating cameras in Linux is Gphoto2. Get it with:
$ apt-get install gphoto2
Before getting stuck into the project, we'll take a look at this useful tool to see what it can do.
The desktop environment can try to mount the camera, and this can cause Gphoto2 a few problems, so the easiest thing to do is run without it. Open a terminal and run sudo raspi-conf, and under Boot Options, select B1 Console, then reboot.
On our test system, we found that running this way, we could just run everything off the Pi's power supply, but if we tried to use a mouse as well, we needed to upgrade to a powered hub. Obviously, this will depend on the particulars of your peripherals and power supply.
Get daily insight, inspiration and deals in your inbox
Sign up for breaking news, reviews, opinion, top tech deals, and more.
In the new text-only environment, plug in your camera and run:
$ gphoto2 --auto-detect
This will try to find any cameras attached to the Pi. Hopefully, it will pick up yours. While it does support an impressive array, there are a few cameras that won't work. If yours is one of the unlucky few, you'll need to beg, steal or borrow one from a friend before continuing.
Not all supported cameras are equal, and the next step is to see what the camera can do. To list the available actions, run:
$ gphoto2 --auto-detect --abilities
There are, broadly speaking, two main classes of abilities: capture, and upload/download. The former let you take photos with your scripts, and are present mostly on higher-quality cameras. The latter let you deal with photos stored on the memory card, and are present on most supported cameras. In this project, we'll deal only with the second set of abilities.
The simplest command we can send to the camera is to get all the photos stored on it. This is:
$ gphoto2 --auto-detect --get-all-files
Running this will download all the files from the camera into the current directory. This would be fine on a normal computer, but you may not want to do it on a Pi, as you run the risk of filling up your memory card pretty quickly. Instead, we'll copy them onto a USB stick.
To do this in an interactive session, you could simply use a GUI tool to mount the stick then run df -h to see where the USB stick is mounted, and cd to the directory. However, since this will run automatically, we need to know where the device will be.
There are a few ways of doing this, but we'll keep it simple. We'll mount the first partition of the first serial disk, and store the photos there. Here, we're assuming that you're using the default user pi. If you're not, you'll need to adjust the script.
First, we need to create a mount point for the drive. This is just a folder, and can be put anywhere – we're going to spurn convention and put it in our home folder. So before running the script, run:
$ mkdir /home/pi/pic_mount
With this done, we're ready to go. The script to mount the drive and get the photos is:
#!/bin/bash
if mount /dev/sda1 /home/pi/pic_mount ; then
echo "Partition mounted"
cd /home/pi/pic_mount
yes 'n' | gphoto2 -- auto-detect --get-all-files
umount /dev/sda1
else
echo "/dev/sda1 could not be mounted"
fi
yes 'n' is a command that simply emits a stream of n characters. This means that when Gphoto2 prompts to overwrite any previously downloaded files, it will decline. The umount is essential, because it ensures that the drive is properly synced and can be removed.
We've called the script get-pics.sh, and saved it in Pi's home directory. To make it executable, run:
$ chmod +x /home/pi/get-pics.sh
You should now be able to run it manually. You'll need to use sudo because it needs to mount the drive.
The final piece of the puzzle is to get the script to run automatically. To do this we add it to the file /etc/rc.local. This script runs when you boot up, and it executes as root, so there's no need to worry about permissions.
The quickest way to open the file as root is as follows:
$ sudo nano /etc/rc.local
Once done, add this line just before the line exit 0:
/home/pi/get-pics.sh
///end code///
Now all you have to do is plug in your camera (making sure it's turned on) and USB stick, and it'll back up your photos when you boot up.
Taking it further
If you want to run the device headless, as will most likely be the case, you could attach LEDs to the GPIO pins, as shown later in this article, and use these to indicate statuses. As well as saving images to the USB stick, you could upload them to an online service, such as Flickr. See the section on wireless networking on the next page for information on how to connect your Pi to your phone.
You could include some sort of switch to tell your Pi which photos to upload, and which to store on the USB stick – for example upload low-resolution images, and store high-res ones. Or you could create low-resolution versions of the images, and upload those.
Gphoto2 has far more capabilities than we use here, including bindings for Java and Python. For full details, check out the project website here.
Of course, you don't have to stop there. If you have a wireless dongle in your Pi, you could use it to run an HTTP server. With some PHP (or other web language) scripting, you should be able to create an interface to GPhoto2 that will allow you to connect from your mobile phone.
Taking it in a different direction, if your camera supports capture options, you could use your Pi to take photos as well as copy them.