1 | /*
|
---|
2 | SendDataToGoogleSpreadsheet
|
---|
3 |
|
---|
4 | Demonstrates appending a row of data to a Google spreadsheet 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 | Since this sketch uses a Google spreadsheet, you'll also need a
|
---|
13 | Google account: substitute the placeholders below for your Google account values.
|
---|
14 |
|
---|
15 | This example assumes basic familiarity with Arduino sketches, and that your
|
---|
16 | Y炭n is connected to the Internet.
|
---|
17 |
|
---|
18 | The columns in your spreadsheet must have labels for the Choreo to
|
---|
19 | work properly. It doesn't matter what the column labels actually are,
|
---|
20 | but there must be text in the first row of each column. This example
|
---|
21 | assumes there are two columns. The first column is the time (in milliseconds)
|
---|
22 | that the row was appended, and the second column is a sensor value.
|
---|
23 | In other words, your spreadsheet should look like:
|
---|
24 |
|
---|
25 | Time | Sensor Value |
|
---|
26 | ------+-----------------
|
---|
27 | | |
|
---|
28 |
|
---|
29 | NOTE that the first time you run this sketch, you may receive a warning from
|
---|
30 | Google, prompting you to authorize access from a 3rd party system.
|
---|
31 |
|
---|
32 | Looking for another API to use with your Arduino Y炭n? We've got over 100 in our Library!
|
---|
33 |
|
---|
34 | This example code is in the public domain.
|
---|
35 |
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <Bridge.h>
|
---|
39 | #include <Temboo.h>
|
---|
40 | #include "TembooAccount.h" // contains Temboo account information,
|
---|
41 | // as described in the footer comment below
|
---|
42 |
|
---|
43 |
|
---|
44 | /*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
---|
45 |
|
---|
46 | // Note that for additional security and reusability, you could
|
---|
47 | // use #define statements to specify these values in a .h file.
|
---|
48 |
|
---|
49 | const String GOOGLE_USERNAME = "your-google-username";
|
---|
50 | const String GOOGLE_PASSWORD = "your-google-password";
|
---|
51 |
|
---|
52 | // the title of the spreadsheet you want to send data to
|
---|
53 | // (Note that this must actually be the title of a Google spreadsheet
|
---|
54 | // that exists in your Google Drive/Docs account, and is configured
|
---|
55 | // as described above.)
|
---|
56 | const String SPREADSHEET_TITLE = "your-spreadsheet-title";
|
---|
57 |
|
---|
58 | const unsigned long RUN_INTERVAL_MILLIS = 60000; // how often to run the Choreo (in milliseconds)
|
---|
59 |
|
---|
60 | // the last time we ran the Choreo
|
---|
61 | // (initialized to 60 seconds ago so the
|
---|
62 | // Choreo is run immediately when we start up)
|
---|
63 | unsigned long lastRun = (unsigned long)-60000;
|
---|
64 |
|
---|
65 | void setup() {
|
---|
66 |
|
---|
67 | // for debugging, wait until a serial console is connected
|
---|
68 | Serial.begin(9600);
|
---|
69 | delay(4000);
|
---|
70 | while(!Serial);
|
---|
71 |
|
---|
72 | Serial.print("Initializing the bridge...");
|
---|
73 | Bridge.begin();
|
---|
74 | Serial.println("Done");
|
---|
75 | }
|
---|
76 |
|
---|
77 | void loop()
|
---|
78 | {
|
---|
79 | // get the number of milliseconds this sketch has been running
|
---|
80 | unsigned long now = millis();
|
---|
81 |
|
---|
82 | // run again if it's been 60 seconds since we last ran
|
---|
83 | if (now - lastRun >= RUN_INTERVAL_MILLIS) {
|
---|
84 |
|
---|
85 | // remember 'now' as the last time we ran the choreo
|
---|
86 | lastRun = now;
|
---|
87 |
|
---|
88 | Serial.println("Getting sensor value...");
|
---|
89 |
|
---|
90 | // get the value we want to append to our spreadsheet
|
---|
91 | unsigned long sensorValue = getSensorValue();
|
---|
92 |
|
---|
93 | Serial.println("Appending value to spreadsheet...");
|
---|
94 |
|
---|
95 | // we need a Process object to send a Choreo request to Temboo
|
---|
96 | TembooChoreo AppendRowChoreo;
|
---|
97 |
|
---|
98 | // invoke the Temboo client
|
---|
99 | // NOTE that the client must be reinvoked and repopulated with
|
---|
100 | // appropriate arguments each time its run() method is called.
|
---|
101 | AppendRowChoreo.begin();
|
---|
102 |
|
---|
103 | // set Temboo account credentials
|
---|
104 | AppendRowChoreo.setAccountName(TEMBOO_ACCOUNT);
|
---|
105 | AppendRowChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
---|
106 | AppendRowChoreo.setAppKey(TEMBOO_APP_KEY);
|
---|
107 |
|
---|
108 | // identify the Temboo Library choreo to run (Google > Spreadsheets > AppendRow)
|
---|
109 | AppendRowChoreo.setChoreo("/Library/Google/Spreadsheets/AppendRow");
|
---|
110 |
|
---|
111 | // set the required Choreo inputs
|
---|
112 | // see https://www.temboo.com/library/Library/Google/Spreadsheets/AppendRow/
|
---|
113 | // for complete details about the inputs for this Choreo
|
---|
114 |
|
---|
115 | // your Google username (usually your email address)
|
---|
116 | AppendRowChoreo.addInput("Username", GOOGLE_USERNAME);
|
---|
117 |
|
---|
118 | // your Google account password
|
---|
119 | AppendRowChoreo.addInput("Password", GOOGLE_PASSWORD);
|
---|
120 |
|
---|
121 | // the title of the spreadsheet you want to append to
|
---|
122 | // NOTE: substitute your own value, retaining the "SpreadsheetTitle:" prefix.
|
---|
123 | AppendRowChoreo.addInput("SpreadsheetTitle", SPREADSHEET_TITLE);
|
---|
124 |
|
---|
125 | // convert the time and sensor values to a comma separated string
|
---|
126 | String rowData(now);
|
---|
127 | rowData += ",";
|
---|
128 | rowData += sensorValue;
|
---|
129 |
|
---|
130 | // add the RowData input item
|
---|
131 | AppendRowChoreo.addInput("RowData", rowData);
|
---|
132 |
|
---|
133 | // run the Choreo and wait for the results
|
---|
134 | // The return code (returnCode) will indicate success or failure
|
---|
135 | unsigned int returnCode = AppendRowChoreo.run();
|
---|
136 |
|
---|
137 | // return code of zero (0) means success
|
---|
138 | if (returnCode == 0) {
|
---|
139 | Serial.println("Success! Appended " + rowData);
|
---|
140 | Serial.println("");
|
---|
141 | } else {
|
---|
142 | // return code of anything other than zero means failure
|
---|
143 | // read and display any error messages
|
---|
144 | while (AppendRowChoreo.available()) {
|
---|
145 | char c = AppendRowChoreo.read();
|
---|
146 | Serial.print(c);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | AppendRowChoreo.close();
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | // this function simulates reading the value of a sensor
|
---|
155 | unsigned long getSensorValue() {
|
---|
156 | return analogRead(A0);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*
|
---|
160 | IMPORTANT NOTE: TembooAccount.h:
|
---|
161 |
|
---|
162 | TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
---|
163 | You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
---|
164 | by inserting your own Temboo account name and app key information. The contents of the file should
|
---|
165 | look like:
|
---|
166 |
|
---|
167 | #define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
---|
168 | #define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
---|
169 | #define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
---|
170 |
|
---|
171 | You can find your Temboo App Key information on the Temboo website,
|
---|
172 | under My Account > Application Keys
|
---|
173 |
|
---|
174 | The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
---|
175 |
|
---|
176 | Keeping your account information in a separate file means you can share the main .ino file without worrying
|
---|
177 | that you forgot to delete your credentials.
|
---|
178 | */
|
---|