Upload pictures to Dropbox

The Temboo service has moved away from Arduino support. The service access in this older guide will no longer work.
What we want to achieve in this first application is to take a picture whenever some motion is detected by the PIR motion sensor. And when that happens, store this picture locally on the SD card, and upload it to Dropbox. To do so, the code will be composed of two parts. The first one is a Python script that will connect to Dropbox, take a picture on the SD card, and then upload this picture to Dropbox. The reason to use Python for this part is that it is much easier to upload files to Dropbox using Python than directly from the Arduino sketch. The second part of the code will be the Arduino sketch itself, which will basically call the Python script to take pictures via the Bridge library of the Yun.

Let's first code the Python script. It starts by including the required libraries from the Temboo Python SDK:
Download: file
from temboo.core.session import TembooSession
from temboo.Library.Dropbox.FilesAndMetadata import UploadFile
The Python script will also take the name of the picture we want to upload as an argument:
Download: file
with open(str(sys.argv[1]), "rb") as image_file:
  encoded_string = base64.b64encode(image_file.read())
Remember these Temboo credentials that you created earlier ? That's where you need to enter them:
Download: file
session = TembooSession('yourTembooName', 'yourTembooApp', 'yourTembooKey')
We can then create the correct Dropbox library to upload files, called a "Choreo" on Temboo:
Download: file
uploadFileChoreo = UploadFile(session)
uploadFileInputs = uploadFileChoreo.new_input_set()
It's now the time to enter all the informations about your Dropbox account, like your app Key, app Secret, Access Token an Access Token Secret:
Download: file
uploadFileInputs.set_AppSecret("appSecret")
uploadFileInputs.set_AccessToken("accessToken")
uploadFileInputs.set_FileName(str(sys.argv[1]))
uploadFileInputs.set_AccessTokenSecret("accessTokenSecret")
uploadFileInputs.set_AppKey("appKey")
uploadFileInputs.set_FileContents(encoded_string)
uploadFileInputs.set_Root("sandbox")
And finally, upload the file on Dropbox:
Download: file
uploadFileResults = uploadFileChoreo.execute_with_results(uploadFileInputs)
You can now save this code in a file named upload_picture.py. Note that all the files are available on the GitHub repository of the project:

https://github.com/openhomeautomation/arduino-yun-camera

We'll now work on the Arduino sketch. The sketch starts by including the required libraries:
Download: file
#include <Bridge.h>
#include <Process.h>
We also have to declare a process, that we are going to use to call functions on the Linux machine of the Yun (for example the fswebcam utility we used before):
Download: file
Process picture;
We are also going to build a filename for each picture the project will take, that will be stored in a string:
Download: file
String filename;
We also declare the pin on which the PIR motion sensor is connected:
Download: file
int pir_pin = 8;
And the path of the SD card on the Yun:
Download: file
String path = "/mnt/sda1/";
Because we need to call functions on the Linux machine of the Yun, we have to start the Bridge:
Download: file
Bridge.begin();
Then, in the loop() part of the sketch, we check if some motion was detected by the PIR sensor:
Download: file
if (digitalRead(pir_pin) == true) {
If this is the case, we build a unique filename for the picture, with the date at which the picture was taken:
Download: file
filename = "";
picture.runShellCommand("date +%s");
while(picture.running());

while (picture.available()>0) {
  char c = picture.read();
  filename += c;
  } 
filename.trim();
filename += ".png";
We then make the first call to the Linux machine of the Yun, first to take a picture with the fswebcam utility. Note that here, we provide an extra argument with the -r command, which set the resolution. I used the maximum resolution of my camera, which is 720p:
Download: file
picture.runShellCommand("fswebcam " + path + filename + " -r 1280x720");
while(picture.running());
We then make a second call to the Linux machine, this time calling the Python script with the name of the picture as an argument, which will upload the picture to Dropbox:
Download: file
picture.runShellCommand("python " + path + "upload_picture.py " + path + filename);
while(picture.running());
You are now ready to test the project. Again, all the files are available on the GitHub repository of the project:

https://github.com/openhomeautomation/arduino-yun-camera

First, put the Python file at the root of the SD card, and put the SD card back into the Arduino Yun board. Then, upload the Arduino sketch to the Yun. Now, try to trigger the motion sensor, for example by waiving your hand in front of it. You should see that the webcam is being activated shortly after (for example, my webcam has a LED that turns green when it is active).

To check that the project is working correctly, after a while you can check the SD card, you should see that some pictures have been recorded:
You can also check on your Dropbox folder, where the same pictures should have been uploaded. They should be located in your Dropbox apps folder:
This guide was first published on Mar 31, 2014. It was last updated on Mar 31, 2014. This page (Upload pictures to Dropbox) was last updated on Oct 19, 2019.