Simple Raspberry Pi Interval Camera with Google Drive Upload

## Note: updated version can be found here ##

This is essentially my notes for replicating a Raspberry Pi timelapse camera setup I have but I decided to publish it for others to use as well.  It assumes familiarity with Raspbian so I won't go into details until the camera and uploading sections.

Hardware

Total price ~$75 USD

I selected the older Pi B+ model because I had an extra and because the B+ enclosure supports mounting the standard camera module cleanly within.  Kudos to the person/team who came up with that idea.


Setup the Pi

  • Download and install Raspbian to the SD card normally. I used the newly released Raspbian Jessie.
  • Run #raspi-config
    • Expand filesystem
    • Enable camera
    • Set timezone
    • Change password
    • Finish (reboot)
  • Edit /etc/wpa_supplicant/wpa_supplicant.conf to setup WiFi authentication
  • Edit /etc/network/interfaces to change wlan0 from manual to dhcp
  • Update raspbian: sudo apt-get update; sudo apt-get dist-upgrade

Use a volatile memory for photo cache

Since we'll be routinely writing and erasing 3MB photos, it's a good idea to use tmpfs in RAM. 
  • Edit /etc/fstab
  • Append: tmpfs /tmp tmpfs defaults,noatime,nosuid,size=80m 0 0
  • Reboot or remount: mount -a
You could try to get more aggressive in reducing SD writes by moving /var/log and other heavily written directories to tmpfs but for reasons I didn't dig into, moving all the directories suggested in this blog caused the Pi to hang on boot while mounting partitions. There's presumably a difference in how Jessie and Wheezy react to those changes.


Setup Google Drive upload

After reviewing several Linux Google Drive client alternatives I decided to go with gdrive for uploading.  It's a currently maintained standalone binary with no dependencies, uses oath delegated permissions, and has a simple command line interface with no risk of me accidentally deleting all my Drive files with a bad sync command. 
  • Download the RPi binary of gdrive and make it executable
  • run ./gdrive and follow the instructions to link it to your Google Drive
  • Create a target folder for the photos using gdrive
    • # ./gdrive folder -t timelapse
    • gdrive will return the newly created folder details for "timelapse" including the ID which will look something like 0gtB8nmiKwJ2N3Rse9aF9zdG8

Shoot & upload script

Save the following script as a file and make it executable.  Modify paths and folder ID as needed.  
The script will take a photo to the temp ramdrive, upload it to the specified folder and delete the temp file if the upload was successful.  If the upload failed, it will copy the photo to non-volatile storage so that it can be manually downloaded.
#!/bin/bash
DATE=$(date +"%Y-%m-%dT%H%M%S")
raspistill -o /tmp/$DATE.jpg
/home/pi/gdrive upload -f /tmp/$DATE.jpg -p <folder ID>
if [ $? -eq 0 ]
then 
  rm /tmp/$DATE.jpg
else 
  mv /tmp/$DATE.jpg /home/pi/Pictures/$DATE.jpg
fi
See the raspistill documentation for other options to fix white balance, orientation or other camera settings.


Automate with crontab

Use a time lapse calculator to determine the best shot interval for your desired video keeping in mind that Pi photos are 2.5-3MB each.  I setup my Pi to shoot once a minute from 7:00am until 8:59pm everyday.  Crontab tools are available to help with the syntax.
# crontab -e
* 7-20 * * * /home/pi/timelapse.sh >/dev/null 2>&1

Test

Of course it's best to test after each step since that makes troubleshooting much simpler but like I said, this is more like a public version of my own notes.

From this point you should be able to power off your Pi and position it for best photos. The script should work automatically after restoring power to the Pi.  Keep an eye on your Drive directory to see that photos are being uploaded as expected.

Use any of the online tutorials for turning the photos into a timelapse video.

Update 1

Turns out that selfie-stick phone clips are a perfect fit for Raspberry Pi (~65mm wide).  Now I can attach this camera using a tripod or my favorite, magnetic Gorillapod.









Popular posts from this blog

Loose Link Pins on the Huawei Watch (hwatch) Band

Updated Raspberry Pi Timelapse Camera with Google Drive Upload