Raspberry Pi Nestbox Camera - Saving the images to removable storage

In part 3 of this how-to guide on building a Raspberry Pi nestbox camera, we set up our motion detection software and configured it so that the program would start running as soon as the Raspberry Pi booted up. With a connection to our local network, we were able to verify the software was working by viewing the camera like a webcam in our browser, however, if the camera is going out of reach of the network or wireless access point, we won't be able to view the output through the browser. That's why we're saving the images as well. Currently, we had set motion to save the images to a folder on the desktop so we could easily navigate there and check images were indeed being recorded. If we were to continue doing this when the nestbox was 'live' we'd have to somehow connect to the Raspberry Pi to check the folder which won't be possible, therefore we need to use removable storage to solve the problem. I'm planning on placing the nestbox is an area where there is no internet reception and no power and I will only be able to check on it every 2-3 days. Leaving aside the power issue, this means there has to be a decent amount of storage available to record footage until I make a visit since I won't be able to check through the browser for any live activity. (It's also important that if the nestbox is occupied you don't cause to much disturbance by coming to collect the footage every day.) So, in order to collect the footage periodically, we'll record the data to a plain old USB stick. I recently picked up two 16Gb USB sticks for about £10 on Amazon (which also have a slim form factor should they need to sit side by side on the Raspberry Pi) meaning as I remove one to take home and view, I can slot the other in so there is always one attached to record to. I realise there were other even smaller storage devices so if space was looking like an issue for your project, you might want to look into something smaller. Slim usb drives I expect 16GB is a bit of overkill since the file size of the images is pretty small but at least they're unlikely to run out of space! From the picture you can see the size difference.

Saving images to our removable storage

We now want to edit where our images are saved to in the motion configuration file. Let's boot up the Pi with the camera and the USB stick attached. After a few minutes, if you have a light on your camera it should turn on and hopefully everything is working. Again, if you want to verify your camera is working visit the IP address of your Raspberry Pi and add on the port we specified in the previous section in your browser: View your webcam using the  IP address of your Raspberry Pi   If you're working from a different machine on your network, open your SSH client (PuTTY for Windows users, Terminal for Macs) and connect to your Raspberry Pi. For me on a Mac, that's:

ssh pi@

We need to remember that different operating systems often use different file systems and therefore a USB stick formatted using NTFS or FAT32 for Windows computers might not be readable on a Mac or Linux computer. Luckily, we can use a FAT filesystem on our USB stick that can be read by Windows and Macs but we need to make sure the Raspberry Pi knows this when it boots up. When the Pi boots up, it assigns an address to all the USB devices attached. This is usually something like /dev/sda1. Let's find out which by doing the following:

  • Power down your Raspberry Pi
  • Attach your USB drive
  • Boot up your Pi

Once booted use the df command to see the attached devices:


The out put will look something like this: df command screen output The line that we're interested in is the one beginning /dev/sd... The letter or number that follow may be different (typically or b and 1 or 2). If you want to verify this is your USB drive, remove it (keeping the power on) and run the command again. The line corresponding to your USB drive will have disappeared. In the above image, you'll also notice a column called Mounted on. When you plug in something like a USB drive it gets mounted to a specific location this is usually the same location each time but to be sure, we can specify where we want a device to be mounted to. The Kingston USB drives I bought for this project mount to /media/KINGSTON. In the case of our nest box camera, this will need to be the same location that we are writing the footage to. At the same time we can also specify the file system we want to use. All of this can be taken care of with one line of code:

  • Open the fstab file:
    sudo nano /etc/fstab
  • Add the following line substituting the sda1 part for your device address if it is different:
    /dev/sda1      /media/usbmount      xfat      uid=motion,gid=motion      0      0

You can also add other lines to cover all the bases incase your Pi assigns a different address to the USB drive like so (avoid my spelling error though!): fstab output

  • Let's make sure the mount point exists and set its permissions:
sudo mkdir /media/usbmount sudo chmod 777 /media/usbmount
  • Update our motion.conf target directory so our mount point:
sudo nano /etc/motion/motion.conf

Test run

Ok, that should be everything needed to allow a fully automated setup. To test everything is working, power down the Raspberry Pi, plug in the camera and the USB drive (try to plug them into the same USB slots each time so keep the addressing consistent) and turn the power on again. As before, you should be able to view the live stream through your browser on your network as long as it's connected. Move around a bit in front of the camera to make sure it's got some motion to detect. Now power down the Pi and remove the USB drive. By opening the USB drive using another computer, you should be able to see the recorded images. If so, then well done, you're ready to go. Just remember to plug the USB drive before booting the Raspberry Pi otherwise the images will not be written to the USB drive properly. The next part of the this guide will look at building a nest box and attaching the Raspberry Pi nest box camera to it for recording in the field.