Project tutorial
Upload a File to your Dropbox Account with Temboo

Upload a File to your Dropbox Account with Temboo © GPL3+

We'll show you how to make your Arduino Yún upload a file to your Dropbox account. Then your Yún can remember everything for you.

  • 1,187 views
  • 0 comments
  • 1 respect

Components and supplies

Apps and online services

About this project

1. Get Set Up

  • Make sure you have a Temboo account. If you don't already have one, you can register for a free account here.
  • You'll also need a valid Dropbox account and a Dropbox app, which you can create here. The app name and domain can be whatever you like. Make sure to use the settings shown in the screenshot below when creating your new Dropbox app.
  • Make sure that your Yún is connected to the Internet.

2. Write the Sketch

Copy the sketch code below into a new tab in your Arduino IDE. This code calls the UploadFile Choreo, and you will need to replace the placeholder values in the code with your own Dropbox app/OAuth details.

/*
 UploadToDropbox
 Demonstrates uploading a file to Dropbox using the Temboo Arduino Yun SDK.
 This example code is in the public domain.
*/
#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.
// your Dropbox app key, available on the Dropbox developer console after registering an app
const String DROPBOX_APP_KEY = "xxxxxxxxxx";
// your Dropbox app secret, available on the Dropbox developer console after registering an app 
const String DROPBOX_APP_SECRET = "xxxxxxxxxx";
// your Dropbox access token, which is returned by the FinalizeOAuth Choreo
const String DROPBOX_ACCESS_TOKEN = "xxxxxxxxxx";
// your Dropbox access token secret, which is returned by the FinalizeOAuth Choreo
const String DROPBOX_ACCESS_TOKEN_SECRET = "xxxxxxxxxx";
boolean success = false; // a flag to indicate whether we've uploaded the file yet
void setup() {
 Serial.begin(9600);
 // For debugging, wait until a serial console is connected.
 delay(4000);
 while(!Serial);
 Bridge.begin();
}
void loop()
{
 // only try to upload the file if we haven't already done so
 if (!success) {
   Serial.println("Base64 encoding data to upload...");
   // base64 encode the data to upload
   String base64EncodedData = base64Encode("Hello, Arduino!");
   Serial.println("Uploading data to Dropbox...");
   // we need a Process object to send a Choreo request to Temboo    
   TembooChoreo UploadFileChoreo;
   // invoke the Temboo client
   // NOTE that the client must be reinvoked and repopulated with
   // appropriate arguments each time its run() method is called.
   UploadFileChoreo.begin();
   // set Temboo account credentials
   UploadFileChoreo.setAccountName(TEMBOO_ACCOUNT);
   UploadFileChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
   UploadFileChoreo.setAppKey(TEMBOO_APP_KEY);
   // identify the Temboo Library choreo to run (Dropbox > FilesAndMetadata > UploadFile)
   UploadFileChoreo.setChoreo("/Library/Dropbox/FilesAndMetadata/UploadFile");
   // set the required choreo inputs
   // see https://www.temboo.com/library/Library/Dropbox/FilesAndMetadata/UploadFile/
   // for complete details about the inputs for this Choreo
   // first specify the name of the file to create/update on Dropbox
   UploadFileChoreo.addInput("FileName", "ArduinoTest.txt");
   // next, the root folder on Dropbox relative to which the file path is specified.
   // to work with the Dropbox app you created earlier, this should be left as "sandbox"
   // if your Dropbox app has full access to your files, specify "dropbox"
   UploadFileChoreo.addInput("Root","sandbox");
   // next, the Base64 encoded file data to upload
   UploadFileChoreo.addInput("FileContents", base64EncodedData);
   // finally, the Dropbox OAuth credentials defined above
   UploadFileChoreo.addInput("AppSecret", DROPBOX_APP_SECRET);
   UploadFileChoreo.addInput("AccessToken", DROPBOX_ACCESS_TOKEN);
   UploadFileChoreo.addInput("AccessTokenSecret", DROPBOX_ACCESS_TOKEN_SECRET);
   UploadFileChoreo.addInput("AppKey", DROPBOX_APP_KEY);
   // tell the Process to run and wait for the results. The 
   // return code (returnCode) will tell us whether the Temboo client 
   // was able to send our request to the Temboo servers
   unsigned int returnCode = UploadFileChoreo.run();
   // a return code of zero (0) means everything worked
   if (returnCode == 0) {
       Serial.println("Success! File uploaded!");
       success = true;
   } else {
     // a non-zero return code means there was an error
     Serial.println("Uh-oh! Something went wrong!");
   }
   // print out the full response to the serial monitor in all
   // cases, just for debugging
   while (UploadFileChoreo.available()) {
     char c = UploadFileChoreo.read();
     Serial.print(c);
   }
   UploadFileChoreo.close();
   Serial.println("Waiting...");
 }
 delay(30000); // wait 30 seconds between upload attempts
}
/*
 A utility function to Base64 encode the specified string
 by calling a Temboo Utilities Choreo.
*/
String base64Encode(String toEncode) {
   // we need a Process object to send a Choreo request to Temboo
   TembooChoreo Base64EncodeChoreo;
   // invoke the Temboo client
   Base64EncodeChoreo.begin();
   // set Temboo account credentials
   Base64EncodeChoreo.setAccountName(TEMBOO_ACCOUNT);
   Base64EncodeChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
   Base64EncodeChoreo.setAppKey(TEMBOO_APP_KEY);
   // identify the Temboo Library choreo to run (Utilities > Encoding > Base64Encode)
   Base64EncodeChoreo.setChoreo("/Library/Utilities/Encoding/Base64Encode");
    // set choreo inputs
   Base64EncodeChoreo.addInput("Text", toEncode);
   // run the choreo
   Base64EncodeChoreo.run();
   // read in the choreo results, and return the "Base64EncodedText" output value.
   // see http://www.temboo.com/arduino for more details on using choreo outputs.
   while(Base64EncodeChoreo.available()) {
     // read the name of the output item
     String name = Base64EncodeChoreo.readStringUntil('\x1F');
     name.trim();
     // read the value of the output item
     String data = Base64EncodeChoreo.readStringUntil('\x1E');
     data.trim();
     if(name == "Base64EncodedText") {
       return data;
     }
   }
} 

3. Create Your Header File

The sketch above references the TembooAccount.h header file, which contains your Temboo account information.

If you are currently logged in, you'll see your account details in the code snippet below (otherwise you'll see placeholder values). Copy the code snippet into a new tab in Arduino and call it TembooAccount.h.

With both files in place you are ready to upload the sketch and send a file to Dropbox from your Yún. Up, upload, and away!

#define TEMBOO_ACCOUNT "accountName"  // your Temboo account name 
#define TEMBOO_APP_KEY_NAME "myFirstApp"  // your Temboo app key name
#define TEMBOO_APP_KEY  "abc123xxxxxxxxxxxxxx"  // your Temboo app key

4. What's Next?

Now that your files are safety stored in Dropbox, why not check out the other 2000+ Choreos in our Library and start thinking about all the possibilities for your next Yún project.

5. Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.

Code

Code snippet #1Arduino
/*
  UploadToDropbox
  
  Demonstrates uploading a file to Dropbox using the Temboo Arduino Yun SDK.
  
  This example code is in the public domain.
*/

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information

/*** SUBSTITUTE YOUR VALUES BELOW: ***/

// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.

// your Dropbox app key, available on the Dropbox developer console after registering an app
const String DROPBOX_APP_KEY = "xxxxxxxxxx";

// your Dropbox app secret, available on the Dropbox developer console after registering an app 
const String DROPBOX_APP_SECRET = "xxxxxxxxxx";

// your Dropbox access token, which is returned by the FinalizeOAuth Choreo
const String DROPBOX_ACCESS_TOKEN = "xxxxxxxxxx";

// your Dropbox access token secret, which is returned by the FinalizeOAuth Choreo
const String DROPBOX_ACCESS_TOKEN_SECRET = "xxxxxxxxxx";


boolean success = false; // a flag to indicate whether we've uploaded the file yet

void setup() {
  Serial.begin(9600);
  
  // For debugging, wait until a serial console is connected.
  delay(4000);
  while(!Serial);
  Bridge.begin();
}

void loop()
{
  // only try to upload the file if we haven't already done so
  if (!success) {
    
    Serial.println("Base64 encoding data to upload...");
    
    // base64 encode the data to upload
    String base64EncodedData = base64Encode("Hello, Arduino!");


    Serial.println("Uploading data to Dropbox...");

    // we need a Process object to send a Choreo request to Temboo    
    TembooChoreo UploadFileChoreo;

    // invoke the Temboo client
    // NOTE that the client must be reinvoked and repopulated with
    // appropriate arguments each time its run() method is called.
    UploadFileChoreo.begin();
    
    // set Temboo account credentials
    UploadFileChoreo.setAccountName(TEMBOO_ACCOUNT);
    UploadFileChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    UploadFileChoreo.setAppKey(TEMBOO_APP_KEY);

    // identify the Temboo Library choreo to run (Dropbox > FilesAndMetadata > UploadFile)
    UploadFileChoreo.setChoreo("/Library/Dropbox/FilesAndMetadata/UploadFile");
    
    // set the required choreo inputs
    // see https://www.temboo.com/library/Library/Dropbox/FilesAndMetadata/UploadFile/
    // for complete details about the inputs for this Choreo

    // first specify the name of the file to create/update on Dropbox
    UploadFileChoreo.addInput("FileName", "ArduinoTest.txt");

    // next, the root folder on Dropbox relative to which the file path is specified.
    // to work with the Dropbox app you created earlier, this should be left as "sandbox"
    // if your Dropbox app has full access to your files, specify "dropbox"
    UploadFileChoreo.addInput("Root","sandbox");

    // next, the Base64 encoded file data to upload
    UploadFileChoreo.addInput("FileContents", base64EncodedData);
   
    // finally, the Dropbox OAuth credentials defined above
    UploadFileChoreo.addInput("AppSecret", DROPBOX_APP_SECRET);
    UploadFileChoreo.addInput("AccessToken", DROPBOX_ACCESS_TOKEN);
    UploadFileChoreo.addInput("AccessTokenSecret", DROPBOX_ACCESS_TOKEN_SECRET);
    UploadFileChoreo.addInput("AppKey", DROPBOX_APP_KEY);

    // tell the Process to run and wait for the results. The 
    // return code (returnCode) will tell us whether the Temboo client 
    // was able to send our request to the Temboo servers
    unsigned int returnCode = UploadFileChoreo.run();

    // a return code of zero (0) means everything worked
    if (returnCode == 0) {
        Serial.println("Success! File uploaded!");
        success = true;
    } else {
      // a non-zero return code means there was an error
      Serial.println("Uh-oh! Something went wrong!");
    }
    
    // print out the full response to the serial monitor in all
    // cases, just for debugging
    while (UploadFileChoreo.available()) {
      char c = UploadFileChoreo.read();
      Serial.print(c);
    }
    UploadFileChoreo.close();

    Serial.println("Waiting...");
  }

  delay(30000); // wait 30 seconds between upload attempts
}


/*
  A utility function to Base64 encode the specified string
  by calling a Temboo Utilities Choreo.
*/
String base64Encode(String toEncode) {
  
    // we need a Process object to send a Choreo request to Temboo
    TembooChoreo Base64EncodeChoreo;

    // invoke the Temboo client
    Base64EncodeChoreo.begin();
    
    // set Temboo account credentials
    Base64EncodeChoreo.setAccountName(TEMBOO_ACCOUNT);
    Base64EncodeChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    Base64EncodeChoreo.setAppKey(TEMBOO_APP_KEY);

    // identify the Temboo Library choreo to run (Utilities > Encoding > Base64Encode)
    Base64EncodeChoreo.setChoreo("/Library/Utilities/Encoding/Base64Encode");
 
     // set choreo inputs
    Base64EncodeChoreo.addInput("Text", toEncode);
    
    // run the choreo
    Base64EncodeChoreo.run();
    
    // read in the choreo results, and return the "Base64EncodedText" output value.
    // see http://www.temboo.com/arduino for more details on using choreo outputs.
    while(Base64EncodeChoreo.available()) {
      // read the name of the output item
      String name = Base64EncodeChoreo.readStringUntil('\x1F');
      name.trim();

      // read the value of the output item
      String data = Base64EncodeChoreo.readStringUntil('\x1E');
      data.trim();

      if(name == "Base64EncodedText") {
        return data;
      }
    }
} 

Comments

Similar projects you might like

Send An Email From Your Gmail Account With Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,390 views
  • 0 comments
  • 2 respects

Read And Update Databases With Temboo

Project tutorial by Temboo_OFFICIAL

  • 2,419 views
  • 2 comments
  • 5 respects

Send an SMS using Twilio with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,481 views
  • 1 comment
  • 1 respect

Post Data to a Google Spreadsheet with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,228 views
  • 0 comments
  • 7 respects

Control your Yún from your Phone with Temboo

Project tutorial by Temboo_OFFICIAL

  • 921 views
  • 0 comments
  • 1 respect

Back Up SD File to Dropbox

Project tutorial by Andrei Florian

  • 3,711 views
  • 6 comments
  • 36 respects
Add projectSign up / Login