Connected Devices & Dropbox

Written by cgulabrani | Published 2016/02/10
Tech Story Tags: technology | raspberry-pi | tech

TLDRvia the TL;DR App

[Building a Home Security System using Raspberry Pi & Dropbox]

Hack Week is a huge part of Dropbox’s culture. Every 6 months or so we have a week blocked off on our calendars when we are free to work on anything. The office is transformed into a geek-carnival, with a myriad of uber-interesting ideas flying around and taking shape.

This Hack Week, I decided to work on a hardware project — I built a home security system using Raspberry Pi and Dropbox. In the past, I worked on a project involving the Pi during Dreamforce 2014 — a brief overview is posted here. So it was good to get back to exploring the versatility of the Pi and as an icing on the already interesting cake, integrate it to Dropbox — leveraging the reliability and world-renowned sync capability of Dropbox service to capture data and make it readily available on any platform at any point of time.

So here is what I came up with (an idea that I formulated while waiting for an extremely delayed red-eye to New Orleans from SFO, one freezing December night):

The implementation consists of a magnetic reed switch that is taped/screwed onto a door (ideally the front door of your house), and is mounted on a breadboard connected to the Raspberry Pi. The Pi receives the appropriate signal from the switch indicating the state of the door — open or closed. Depending on the value of the switch feedback, the Pi prompts a DSLR to snap photos and saves them into the home folder on the Pi. A simple python program runs on the Pi, which uploads the pictures to a shared folder within Dropbox, making it inevitably pop up on your Dropbox mobile app. Furthermore, I set up some workflow rules, which fire every time there is a new photo uploaded within the Dropbox shared folder and result in Slack notifications being sent out.

Still with me?

Good! Let’s talk more in detail.

Here is a visual diagram of the implementation I just described:

I broke the project into the following standalone phases:

Phase 1: Have the Pi control your camera

You can refer to my Hacker Daily blog post to do this. It is pretty straightforward and doable in a couple of hours if you follow the exact steps till the end.

Phase 2: Have Pi talk to Dropbox

The second phase of my project was to upload pictures from Pi to Dropbox. This involved establishing a connection from the Pi to Dropbox API using OAuth and uploading the pictures to a specified Dropbox shared folder.

Here are step by step instructions:

Step 2–1 : Create an app on Dropbox

a. Log in to your Dropbox account and go to https://www.dropbox.com/developers/apps

b. Click on Create app, give your app a name and permission type = full access

c. Note the app key and secret key — you will need it later

Step 2–2: Download Dropbox-Uploader

Once you have the app declared, download the fantastic program by Andrea Fabrizi called Dropbox-Uploader. This gives you access to Dropbox from the Pi command line.

Here are the steps to download this program and set up Dropbox API authorization -

a. git clone https://github.com/andreafabrizi/Dropbox-Uploader.git

This creates a folder Dropbox-Uploader in the /home/pi/ directory

b. cd Dropbox-Uploader

Inside the folder, there is a script dropbox_uploader.sh, which we will use to connect to Dropbox using OAuth.

c. Run the script:

./dropbox_uploader.sh

d. It will prompt for the ‘App Key’ and ‘App Secret’ of your Dropbox app that you just created above. After you add the correct keys on the command line, it will ask you to fill in the access level. Make sure you type in ‘Full’ since this is what you had selected as permission type during your app creation.

e. The Pi will then request an OAuth token, and will ask you to navigate to a URL it spits out on the command line, in order to activate the token. Once that succeeds, your setup is complete!

Step 2–3: Testing on command line

dropbox_uploader.sh has an upload action that you can use directly on command line to upload any pictures. The syntax is pretty straightforward -

./dropbox_uploader.sh upload FILE_PATH_ON_PI DIRECTORY_WITHIN_DROPBOX

For e.g,

./dropbox_uploader.sh upload /home/pi/pic.jpg /Pictures/

This will upload pic.jpg which resides in /home/pi/ to the /pictures/ folder in your Dropbox account.

Phase 3: Integrate switch feedback to Raspberry Pi

The final phase of the project was about how to integrate all of it with the reed switch.

What you need -

Implementation:

Step 3–1: First make sure you have GPIO configured on the Pi. I followed these instructions — https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-gpio

Step 3–2: Next, mount the sensor on the breadboard to Pin 18 and the GND pin of the Pi cobbler. It does not matter which wire goes into what pin.

Step 3–3: Connect the Pi cobbler to the breadboard and Raspberry Pi. Be prepared — if you have a brand new breadboard like I did, you will need some serious manual effort to push the cobbler pins into the breadboard.

Step 3–4: Connect the camera to the Raspberry Pi and make sure your Pi detects it by typing gphoto2 — auto-detect on the Pi command line.

Extra credit — Phase 4 : Setting up notifications

I used zapier.com to set up notifications on data hosted in my Dropbox account.

Here are the steps:

Step 4–1 : Create a new account on zapier.com and make a new Zap

Step 4–2: Choose the trigger app = Dropbox and Dropbox triggers = New File in Directory

Step 4–3: Connect to your Dropbox account you created in Phase 2

Step 4–4: Select the directory within Dropbox — in this case it will be the shared folder you are uploading the photos [/intruder_photos in my case]

Step 4–5: Test your trigger rule setup

Step 4–6: Select Action App = Slack

Step 4–7: Select a Slack Action = Send Channel message

Step 4–8: Connect to your Slack account

Step 4–9: Select a channel you want to broadcast the message and draft a message text [everything else is optional]

Step 4–10: Test your setup

*** Bringing it all together ***

Lastly, in order to automate the entire project setup, I wrote a simple python program that is saved on the Raspberry Pi. The program makes the Pi process the input from the magnetic reed switch, prompts the camera to take a photo based on the switch output, and then saves the photo in a specific folder on the Pi — /home/pi/intruder_photos and then calls dropbox_uploader.sh to upload the photo into my Dropbox account. I named the script door_sensor.py and saved it in /home/pi/.

Download and run the python code:

  • You can download the python code here- https://github.com/cgulabrani/home-security-system/tree/door_sensor-code
  • Run the program by typing ‘python door_sensor.py’ on command line on /home/pi/ directory.
  • Fiddle around with the switch [open and close] — the camera should take a photo when the switch is open, upload it to Dropbox and subsequently post a Slack notification to the channel you selected in Step 4–9.

** I hope you have as much fun building this as I did!**


Published by HackerNoon on 2016/02/10