source: rtos_arduino/trunk/arduino_lib/libraries/Bridge/examples/Temboo/UploadToDropbox/UploadToDropbox.ino@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 7.5 KB
RevLine 
[136]1/*
2 UploadToDropbox
3
4 Demonstrates uploading a file to a Dropbox account using Temboo from an Arduino Y炭n.
5
6 Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
7
8 A Temboo account and application key are necessary to run all Temboo examples.
9 If you don't already have one, you can register for a free Temboo account at
10 http://www.temboo.com
11
12 You'll also need a valid Dropbox app and accompanying OAuth credentials.
13 To create a Dropbox app, visit https://www.dropbox.com/developers/apps and
14 do the following:
15
16 1. Create a "Dropbox API app"
17 2. Select "Files and datastores"
18 3. Select "Yes - my app only needs access to the files it creates."
19
20 Once you've created your app, follow the instructions at
21 https://www.temboo.com/library/Library/Dropbox/OAuth/ to run the Initialize and Finalize
22 OAuth Choreos. These Choreos complete the OAuth handshake and retrieve your Dropbox OAuth access tokens.
23
24 This example assumes basic familiarity with Arduino sketches, and that your Y炭n is connected
25 to the Internet.
26
27 Looking for another API to use with your Arduino Y炭n? We've got over 100 in our Library!
28
29 This example code is in the public domain.
30*/
31
32#include <Bridge.h>
33#include <Temboo.h>
34#include "TembooAccount.h" // contains Temboo account information
35 // as described in the footer comment below
36
37
38/*** SUBSTITUTE YOUR VALUES BELOW: ***/
39
40// Note that for additional security and reusability, you could
41// use #define statements to specify these values in a .h file.
42
43// your Dropbox app key, available on the Dropbox developer console after registering an app
44const String DROPBOX_APP_KEY = "xxxxxxxxxx";
45
46// your Dropbox app secret, available on the Dropbox developer console after registering an app
47const String DROPBOX_APP_SECRET = "xxxxxxxxxx";
48
49// your Dropbox access token, which is returned by the FinalizeOAuth Choreo
50const String DROPBOX_ACCESS_TOKEN = "xxxxxxxxxx";
51
52// your Dropbox access token secret, which is returned by the FinalizeOAuth Choreo
53const String DROPBOX_ACCESS_TOKEN_SECRET = "xxxxxxxxxx";
54
55
56boolean success = false; // a flag to indicate whether we've uploaded the file yet
57
58void setup() {
59 Serial.begin(9600);
60
61 // For debugging, wait until a serial console is connected.
62 delay(4000);
63 while(!Serial);
64 Bridge.begin();
65}
66
67void loop()
68{
69 // only try to upload the file if we haven't already done so
70 if (!success) {
71
72 Serial.println("Base64 encoding data to upload...");
73
74 // base64 encode the data to upload
75 String base64EncodedData = base64Encode("Hello, Arduino!");
76
77
78 Serial.println("Uploading data to Dropbox...");
79
80 // we need a Process object to send a Choreo request to Temboo
81 TembooChoreo UploadFileChoreo;
82
83 // invoke the Temboo client
84 // NOTE that the client must be reinvoked and repopulated with
85 // appropriate arguments each time its run() method is called.
86 UploadFileChoreo.begin();
87
88 // set Temboo account credentials
89 UploadFileChoreo.setAccountName(TEMBOO_ACCOUNT);
90 UploadFileChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
91 UploadFileChoreo.setAppKey(TEMBOO_APP_KEY);
92
93 // identify the Temboo Library choreo to run (Dropbox > FilesAndMetadata > UploadFile)
94 UploadFileChoreo.setChoreo("/Library/Dropbox/FilesAndMetadata/UploadFile");
95
96 // set the required choreo inputs
97 // see https://www.temboo.com/library/Library/Dropbox/FilesAndMetadata/UploadFile/
98 // for complete details about the inputs for this Choreo
99
100 // first specify the name of the file to create/update on Dropbox
101 UploadFileChoreo.addInput("FileName", "ArduinoTest.txt");
102
103 // next, the root folder on Dropbox relative to which the file path is specified.
104 // to work with the Dropbox app you created earlier, this should be left as "sandbox"
105 // if your Dropbox app has full access to your files, specify "dropbox"
106 UploadFileChoreo.addInput("Root","sandbox");
107
108 // next, the Base64 encoded file data to upload
109 UploadFileChoreo.addInput("FileContents", base64EncodedData);
110
111 // finally, the Dropbox OAuth credentials defined above
112 UploadFileChoreo.addInput("AppSecret", DROPBOX_APP_SECRET);
113 UploadFileChoreo.addInput("AccessToken", DROPBOX_ACCESS_TOKEN);
114 UploadFileChoreo.addInput("AccessTokenSecret", DROPBOX_ACCESS_TOKEN_SECRET);
115 UploadFileChoreo.addInput("AppKey", DROPBOX_APP_KEY);
116
117 // tell the Process to run and wait for the results. The
118 // return code (returnCode) will tell us whether the Temboo client
119 // was able to send our request to the Temboo servers
120 unsigned int returnCode = UploadFileChoreo.run();
121
122 // a return code of zero (0) means everything worked
123 if (returnCode == 0) {
124 Serial.println("Success! File uploaded!");
125 success = true;
126 } else {
127 // a non-zero return code means there was an error
128 Serial.println("Uh-oh! Something went wrong!");
129 }
130
131 // print out the full response to the serial monitor in all
132 // cases, just for debugging
133 while (UploadFileChoreo.available()) {
134 char c = UploadFileChoreo.read();
135 Serial.print(c);
136 }
137 UploadFileChoreo.close();
138
139 Serial.println("Waiting...");
140 }
141
142 delay(30000); // wait 30 seconds between upload attempts
143}
144
145
146/*
147 A utility function to Base64 encode the specified string
148 by calling a Temboo Utilities Choreo.
149*/
150String base64Encode(String toEncode) {
151
152 // we need a Process object to send a Choreo request to Temboo
153 TembooChoreo Base64EncodeChoreo;
154
155 // invoke the Temboo client
156 Base64EncodeChoreo.begin();
157
158 // set Temboo account credentials
159 Base64EncodeChoreo.setAccountName(TEMBOO_ACCOUNT);
160 Base64EncodeChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
161 Base64EncodeChoreo.setAppKey(TEMBOO_APP_KEY);
162
163 // identify the Temboo Library choreo to run (Utilities > Encoding > Base64Encode)
164 Base64EncodeChoreo.setChoreo("/Library/Utilities/Encoding/Base64Encode");
165
166 // set choreo inputs
167 Base64EncodeChoreo.addInput("Text", toEncode);
168
169 // run the choreo
170 Base64EncodeChoreo.run();
171
172 // read in the choreo results, and return the "Base64EncodedText" output value.
173 // see http://www.temboo.com/arduino for more details on using choreo outputs.
174 while(Base64EncodeChoreo.available()) {
175 // read the name of the output item
176 String name = Base64EncodeChoreo.readStringUntil('\x1F');
177 name.trim();
178
179 // read the value of the output item
180 String data = Base64EncodeChoreo.readStringUntil('\x1E');
181 data.trim();
182
183 if(name == "Base64EncodedText") {
184 return data;
185 }
186 }
187}
188
189/*
190 IMPORTANT NOTE: TembooAccount.h:
191
192 TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
193 You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
194 by inserting your own Temboo account name and app key information. The contents of the file should
195 look like:
196
197 #define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
198 #define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
199 #define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
200
201 You can find your Temboo App Key information on the Temboo website,
202 under My Account > Application Keys
203
204 The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
205
206 Keeping your account information in a separate file means you can share the main .ino file without worrying
207 that you forgot to delete your credentials.
208*/
Note: See TracBrowser for help on using the repository browser.