Sending pictures to Dropbox at regular intervals

We are now going to extend the code we built in the previous section and write some new code that automatically uploads the pictures that were taken by the camera to Dropbox. For this, we will need to build a slightly more complex software than in the previous part.

For now, we only used the Choreos (Temboo libraries) for the Arduino Yún. However, there are actually many other Choreos available for other languages, such as for Python. This is great news because the Linux machine of the Yún is capable of running Python code out of the box.

It's actually much easier to access the Dropbox API from Python, so that's what we are going to use in this part. We will build a Python script that uploads the pictures we took to Dropbox, and call this script from the Arduino sketch using the Bridge library and our picture processes.

I will now explain the content of the Python script. Later, we will save all these lines of code in a separate file, and put it on the SD card along with the Temboo Python SDK.

The Python script starts with the following lines of code:

from temboo.core.session import TembooSession
from temboo.Library.Dropbox.FilesAndMetadata import UploadFile

The Python script will also take an argument: the name of the file to be uploaded. This way, we can directly pass the name of file (built by the Arduino code with a timestamp) to the Python script. The following lines of code do exactly this:

with open(str(sys.argv[1]), "rb") as image_file:
  encoded_string = base64.b64encode(image_file.read())

Inside the script, you need to define your Temboo credentials, as follows:

session = TembooSession('yourTembooName', 'yourTembooApp', 'yourTembooKey')

These are exactly the same credentials we used for Temboo earlier. We then need to declare the upload file Choreo for Python that will be used to automatically upload pictures to Dropbox, as follows:

uploadFileChoreo = UploadFile(session)
uploadFileInputs = uploadFileChoreo.new_input_set()

The next step is to set the different inputs, which you had done when you created your Dropbox app, as follows:

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")

Finally, we can order uploadFileChoreo to upload the file to your Dropbox folder in the corresponding folder of your app, as follows:

uploadFileResults = uploadFileChoreo.execute_with_results(uploadFileInputs)

You can now save this code in a file named upload_picture.py and put it at the root of the SD card. Remember the Temboo Python library we downloaded earlier? It's time to unpack it and place it at the root of the SD card as well. Just make sure that it appears with the name temboo in the root of the SD card, so the Python file we just created can access it correctly. If no pictures have been recorded yet, the following screenshot shows what your SD card folder should look like:

Sending pictures to Dropbox at regular intervals

We also need to slightly modify the Arduino sketch to upload pictures on Dropbox. We used exactly the same code base as in the previous section, so we will only detail the new code that was added.

In the part that is executed when motion is detected, right at the end of the loop, you need to use the picture process again to execute the Python script, as shown in the following code:

picture.runShellCommand("python " + path + "upload_picture.py " + path + filename);
while(picture.running());

Note that we are passing along the same filename and path as the pictures that are recorded on the SD card, so the exact same picture name is recorded locally and sent to Dropbox.

The complete code for this part can be found at https://github.com/openhomeautomation/geeky-projects-yun/tree/master/chapter3/dropbox_log.

You can now put the SD card back into the Arduino Yún, upload the updated Arduino sketch, and head to your Dropbox folder. At this point, you should note that a new folder was created in your Apps folder with the name of your Dropbox app that you set on the Dropbox website, as shown in the following screenshot:

Sending pictures to Dropbox at regular intervals

Now, if motion is detected, the sketch should not only log the pictures on the SD card, but also on your Dropbox folder. If everything is working correctly, you can see that pictures arrive in real time inside your Dropbox folder as the Yún takes the pictures using the USB camera.

The cool aspect about these applications of our project is that this can be done from anywhere in the world. You can do this from your computer, of course, but also from a web browser. Many mobile devices can also run the mobile version of Dropbox, so you can see if somebody has entered your home right from your mobile device. On my computer, for example, Dropbox also sends me a notification that a new file was uploaded, so I can instantly see whether something is happening in my house and can act accordingly.