Components and supplies
Arduino Yun
Apps and platforms
Arduino IDE
Project description
Code
Code snippet #1
arduino
1/* 2 UploadToDropbox 3 4 Demonstrates uploading a file to Dropbox using the Temboo Arduino Yun SDK. 5 6 This example code is in the public domain. 7*/ 8 9#include <Bridge.h> 10#include <Temboo.h> 11#include "TembooAccount.h" // contains Temboo account information 12 13/*** SUBSTITUTE YOUR VALUES BELOW: ***/ 14 15// Note that for additional security and reusability, you could 16// use #define statements to specify these values in a .h file. 17 18// your Dropbox app key, available on the Dropbox developer console after registering an app 19const String DROPBOX_APP_KEY = "xxxxxxxxxx"; 20 21// your Dropbox app secret, available on the Dropbox developer console after registering an app 22const String DROPBOX_APP_SECRET = "xxxxxxxxxx"; 23 24// your Dropbox access token, which is returned by the FinalizeOAuth Choreo 25const String DROPBOX_ACCESS_TOKEN = "xxxxxxxxxx"; 26 27// your Dropbox access token secret, which is returned by the FinalizeOAuth Choreo 28const String DROPBOX_ACCESS_TOKEN_SECRET = "xxxxxxxxxx"; 29 30 31boolean success = false; // a flag to indicate whether we've uploaded the file yet 32 33void setup() { 34 Serial.begin(9600); 35 36 // For debugging, wait until a serial console is connected. 37 delay(4000); 38 while(!Serial); 39 Bridge.begin(); 40} 41 42void loop() 43{ 44 // only try to upload the file if we haven't already done so 45 if (!success) { 46 47 Serial.println("Base64 encoding data to upload..."); 48 49 // base64 encode the data to upload 50 String base64EncodedData = base64Encode("Hello, Arduino!"); 51 52 53 Serial.println("Uploading data to Dropbox..."); 54 55 // we need a Process object to send a Choreo request to Temboo 56 TembooChoreo UploadFileChoreo; 57 58 // invoke the Temboo client 59 // NOTE that the client must be reinvoked and repopulated with 60 // appropriate arguments each time its run() method is called. 61 UploadFileChoreo.begin(); 62 63 // set Temboo account credentials 64 UploadFileChoreo.setAccountName(TEMBOO_ACCOUNT); 65 UploadFileChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); 66 UploadFileChoreo.setAppKey(TEMBOO_APP_KEY); 67 68 // identify the Temboo Library choreo to run (Dropbox > FilesAndMetadata > UploadFile) 69 UploadFileChoreo.setChoreo("/Library/Dropbox/FilesAndMetadata/UploadFile"); 70 71 // set the required choreo inputs 72 // see https://www.temboo.com/library/Library/Dropbox/FilesAndMetadata/UploadFile/ 73 // for complete details about the inputs for this Choreo 74 75 // first specify the name of the file to create/update on Dropbox 76 UploadFileChoreo.addInput("FileName", "ArduinoTest.txt"); 77 78 // next, the root folder on Dropbox relative to which the file path is specified. 79 // to work with the Dropbox app you created earlier, this should be left as "sandbox" 80 // if your Dropbox app has full access to your files, specify "dropbox" 81 UploadFileChoreo.addInput("Root","sandbox"); 82 83 // next, the Base64 encoded file data to upload 84 UploadFileChoreo.addInput("FileContents", base64EncodedData); 85 86 // finally, the Dropbox OAuth credentials defined above 87 UploadFileChoreo.addInput("AppSecret", DROPBOX_APP_SECRET); 88 UploadFileChoreo.addInput("AccessToken", DROPBOX_ACCESS_TOKEN); 89 UploadFileChoreo.addInput("AccessTokenSecret", DROPBOX_ACCESS_TOKEN_SECRET); 90 UploadFileChoreo.addInput("AppKey", DROPBOX_APP_KEY); 91 92 // tell the Process to run and wait for the results. The 93 // return code (returnCode) will tell us whether the Temboo client 94 // was able to send our request to the Temboo servers 95 unsigned int returnCode = UploadFileChoreo.run(); 96 97 // a return code of zero (0) means everything worked 98 if (returnCode == 0) { 99 Serial.println("Success! File uploaded!"); 100 success = true; 101 } else { 102 // a non-zero return code means there was an error 103 Serial.println("Uh-oh! Something went wrong!"); 104 } 105 106 // print out the full response to the serial monitor in all 107 // cases, just for debugging 108 while (UploadFileChoreo.available()) { 109 char c = UploadFileChoreo.read(); 110 Serial.print(c); 111 } 112 UploadFileChoreo.close(); 113 114 Serial.println("Waiting..."); 115 } 116 117 delay(30000); // wait 30 seconds between upload attempts 118} 119 120 121/* 122 A utility function to Base64 encode the specified string 123 by calling a Temboo Utilities Choreo. 124*/ 125String base64Encode(String toEncode) { 126 127 // we need a Process object to send a Choreo request to Temboo 128 TembooChoreo Base64EncodeChoreo; 129 130 // invoke the Temboo client 131 Base64EncodeChoreo.begin(); 132 133 // set Temboo account credentials 134 Base64EncodeChoreo.setAccountName(TEMBOO_ACCOUNT); 135 Base64EncodeChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); 136 Base64EncodeChoreo.setAppKey(TEMBOO_APP_KEY); 137 138 // identify the Temboo Library choreo to run (Utilities > Encoding > Base64Encode) 139 Base64EncodeChoreo.setChoreo("/Library/Utilities/Encoding/Base64Encode"); 140 141 // set choreo inputs 142 Base64EncodeChoreo.addInput("Text", toEncode); 143 144 // run the choreo 145 Base64EncodeChoreo.run(); 146 147 // read in the choreo results, and return the "Base64EncodedText" output value. 148 // see http://www.temboo.com/arduino for more details on using choreo outputs. 149 while(Base64EncodeChoreo.available()) { 150 // read the name of the output item 151 String name = Base64EncodeChoreo.readStringUntil('\\x1F'); 152 name.trim(); 153 154 // read the value of the output item 155 String data = Base64EncodeChoreo.readStringUntil('\\x1E'); 156 data.trim(); 157 158 if(name == "Base64EncodedText") { 159 return data; 160 } 161 } 162}
Comments
Only logged in users can leave comments
Arduino_Scuola
0 Followers
•0 Projects
Table of contents
Intro
0
0