upload a raspberry pi camera image to dropbox with ruby

Sat, Aug 3, 2013

This is a fairly simple wee tutorial on how to get the Raspberry Pi camera module working, taking a picture with it and uploading it to Dropbox using Ruby.

If you don’t have Ruby running on your Pi you can follow the steps on my Ruby on Pi tutorial.

The camera itself is very easy to install if a bit fiddly. The video shows you how to do it:

Once it’s plugged into the Pi, you need to update the system:

sudo apt-get update
sudo apt-get upgrade

and then enable the camera at boot time:

sudo raspi-config

and choose to enable the camera. After a reboot it’s ready to use. If you can’t wait to see it in action you can do:

raspistill -o camera_image.jpg

to take a picture.

So now we have a functioning camera but we want to automatically take a picture and upload it to Dropbox. Ruby is a great option for this using the Dropbox API. By now you should have Ruby installed if you’ve followed my earlier tutorial so it’s just a case of writing some code but first let’s prepare the ground. Let’s create a new directory to hold our code and configure it with a new gemset:

cd ~/dev
mkdir pipic
cd pipic
rvm use 1.9.3@dropbox –rvmrc –create
gem install dropbox-sdk

The second last command creates a new gemset called ‘dropbox’ and configures the pipic directory so that every time you go into it, the dropbox gemset will be selected. This keeps all our dropbox gems in one place. The last command installs the Dropbox SDK.

There are three parts to uploading a file to dropbox. You first have to register your new app with Dropbox. You do that by logging in to the Dropbox Developers site and creating a Dropbox API App. I called mine piPic. You only need to grant access to its own files and datastores which makes it easier to register. You’ll then be given an API key and an app secret which you should note down.

Next you have to grant access to your app to allow it to access your Dropbox account. This is done in code as follows:

Replace YOUR_APP_KEY and YOUR_APP_SECRET with your own values. When you run this code it will display a URL which you need to copy/paste into a browser and grant access to the app. The browser page will then display an authorization code which you need to copy/paste into the terminal window and press enter. The code then asks the Dropbox API to convert the authorization code to an access token which it stores for later use. I chose to separate this initial setup into its own file, pipic_setup.rb as you only need to run it once.

The third and final piece of the jigsaw is taking the picture and uploading it to Dropbox. Again, this is pretty simple stuff:

We first open the access_token file we created in pipic_setup.rb. This is our key to open the Dropbox API and allow us to upload files to our account. After initialising Dropbox we use Ruby’s system command to take a picture with the camera and store the image in the file called camera_image.jpg in the same directory as the code. We then open this image file, pass it to the Dropbox API and upload it to a file called latest.jpg.

When you upload from an app, the files will be relative to the app folder in your Dropbox account. So ‘/latest.jpg’ will end up being ‘Apps/piPic/latest.jpg’.

After running the code you should see a notification appear on your desktop telling you that Dropbox has synced a new file and lo and behold, there is your picture from the Pi’s camera!


From Blank SD Card to Sending Email With Ruby on the Raspberry Pi
Keeping your Raspberry Pi updated with APT
How to set up the camera hardware
Camera board documentation
Camera board documentation PDF
Dropbox Developers
Using the Core API in Ruby

comments powered by Disqus