2013-06-26 17:29:57 +02:00
|
|
|
/*
|
|
|
|
SendDataToGoogleSpreadsheet
|
|
|
|
|
2014-05-30 12:17:09 +02:00
|
|
|
Demonstrates appending a row of data to a Google spreadsheet using Temboo from an Arduino Yún.
|
2013-06-28 11:08:56 +02:00
|
|
|
|
|
|
|
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
2013-06-26 17:29:57 +02:00
|
|
|
|
2014-04-03 19:56:46 +02:00
|
|
|
A Temboo account and application key are necessary to run all Temboo examples.
|
|
|
|
If you don't already have one, you can register for a free Temboo account at
|
2013-06-26 17:29:57 +02:00
|
|
|
http://www.temboo.com
|
|
|
|
|
2014-04-03 19:56:46 +02:00
|
|
|
Since this sketch uses a Google spreadsheet, you'll also need a
|
2013-06-26 17:29:57 +02:00
|
|
|
Google account: substitute the placeholders below for your Google account values.
|
|
|
|
|
2014-04-03 19:56:46 +02:00
|
|
|
This example assumes basic familiarity with Arduino sketches, and that your
|
2014-05-30 12:17:09 +02:00
|
|
|
Yún is connected to the Internet.
|
2013-06-28 11:08:56 +02:00
|
|
|
|
2013-06-26 17:29:57 +02:00
|
|
|
The columns in your spreadsheet must have labels for the Choreo to
|
|
|
|
work properly. It doesn't matter what the column labels actually are,
|
|
|
|
but there must be text in the first row of each column. This example
|
|
|
|
assumes there are two columns. The first column is the time (in milliseconds)
|
2013-07-03 09:02:59 +02:00
|
|
|
that the row was appended, and the second column is a sensor value.
|
|
|
|
In other words, your spreadsheet should look like:
|
2014-04-03 19:56:46 +02:00
|
|
|
|
|
|
|
Time | Sensor Value |
|
2013-06-28 11:08:56 +02:00
|
|
|
------+-----------------
|
|
|
|
| |
|
2014-04-03 19:56:46 +02:00
|
|
|
|
2013-06-28 11:08:56 +02:00
|
|
|
NOTE that the first time you run this sketch, you may receive a warning from
|
|
|
|
Google, prompting you to authorize access from a 3rd party system.
|
2014-04-03 19:56:46 +02:00
|
|
|
|
2014-05-30 12:17:09 +02:00
|
|
|
Looking for another API to use with your Arduino Yún? We've got over 100 in our Library!
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
This example code is in the public domain.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Bridge.h>
|
2013-07-08 15:09:09 +02:00
|
|
|
#include <Temboo.h>
|
2013-06-28 11:08:56 +02:00
|
|
|
#include "TembooAccount.h" // contains Temboo account information,
|
2014-04-03 19:56:46 +02:00
|
|
|
// as described in the footer comment below
|
2013-06-28 11:08:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
|
|
|
|
2013-07-03 09:02:59 +02:00
|
|
|
// Note that for additional security and reusability, you could
|
|
|
|
// use #define statements to specify these values in a .h file.
|
|
|
|
|
2013-06-28 11:08:56 +02:00
|
|
|
const String GOOGLE_USERNAME = "your-google-username";
|
|
|
|
const String GOOGLE_PASSWORD = "your-google-password";
|
|
|
|
|
|
|
|
// the title of the spreadsheet you want to send data to
|
2013-07-03 09:02:59 +02:00
|
|
|
// (Note that this must actually be the title of a Google spreadsheet
|
|
|
|
// that exists in your Google Drive/Docs account, and is configured
|
|
|
|
// as described above.)
|
2013-06-28 11:08:56 +02:00
|
|
|
const String SPREADSHEET_TITLE = "your-spreadsheet-title";
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
const unsigned long RUN_INTERVAL_MILLIS = 60000; // how often to run the Choreo (in milliseconds)
|
|
|
|
|
2014-04-03 19:56:46 +02:00
|
|
|
// the last time we ran the Choreo
|
2013-06-26 17:29:57 +02:00
|
|
|
// (initialized to 60 seconds ago so the
|
|
|
|
// Choreo is run immediately when we start up)
|
2014-04-03 19:56:46 +02:00
|
|
|
unsigned long lastRun = (unsigned long)-60000;
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
void setup() {
|
2014-04-03 19:56:46 +02:00
|
|
|
|
2013-06-26 17:29:57 +02:00
|
|
|
// for debugging, wait until a serial console is connected
|
|
|
|
Serial.begin(9600);
|
|
|
|
delay(4000);
|
2014-04-03 19:56:46 +02:00
|
|
|
while(!Serial);
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
Serial.print("Initializing the bridge...");
|
|
|
|
Bridge.begin();
|
|
|
|
Serial.println("Done");
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
// get the number of milliseconds this sketch has been running
|
|
|
|
unsigned long now = millis();
|
|
|
|
|
|
|
|
// run again if it's been 60 seconds since we last ran
|
|
|
|
if (now - lastRun >= RUN_INTERVAL_MILLIS) {
|
|
|
|
|
|
|
|
// remember 'now' as the last time we ran the choreo
|
|
|
|
lastRun = now;
|
2014-04-03 19:56:46 +02:00
|
|
|
|
2013-06-26 17:29:57 +02:00
|
|
|
Serial.println("Getting sensor value...");
|
|
|
|
|
|
|
|
// get the value we want to append to our spreadsheet
|
|
|
|
unsigned long sensorValue = getSensorValue();
|
|
|
|
|
|
|
|
Serial.println("Appending value to spreadsheet...");
|
|
|
|
|
|
|
|
// we need a Process object to send a Choreo request to Temboo
|
2013-07-08 15:09:09 +02:00
|
|
|
TembooChoreo AppendRowChoreo;
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
// invoke the Temboo client
|
2013-07-08 15:09:09 +02:00
|
|
|
// NOTE that the client must be reinvoked and repopulated with
|
|
|
|
// appropriate arguments each time its run() method is called.
|
|
|
|
AppendRowChoreo.begin();
|
2014-04-03 19:56:46 +02:00
|
|
|
|
2013-06-26 17:29:57 +02:00
|
|
|
// set Temboo account credentials
|
2013-07-08 15:09:09 +02:00
|
|
|
AppendRowChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
|
|
AppendRowChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
|
|
AppendRowChoreo.setAppKey(TEMBOO_APP_KEY);
|
2014-04-03 19:56:46 +02:00
|
|
|
|
2013-06-26 17:29:57 +02:00
|
|
|
// identify the Temboo Library choreo to run (Google > Spreadsheets > AppendRow)
|
2013-07-08 15:09:09 +02:00
|
|
|
AppendRowChoreo.setChoreo("/Library/Google/Spreadsheets/AppendRow");
|
2014-04-03 19:56:46 +02:00
|
|
|
|
2013-06-26 17:29:57 +02:00
|
|
|
// set the required Choreo inputs
|
2014-04-03 19:56:46 +02:00
|
|
|
// see https://www.temboo.com/library/Library/Google/Spreadsheets/AppendRow/
|
2013-06-26 17:29:57 +02:00
|
|
|
// for complete details about the inputs for this Choreo
|
2014-04-03 19:56:46 +02:00
|
|
|
|
2013-06-26 17:29:57 +02:00
|
|
|
// your Google username (usually your email address)
|
2013-07-08 15:09:09 +02:00
|
|
|
AppendRowChoreo.addInput("Username", GOOGLE_USERNAME);
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
// your Google account password
|
2013-07-08 15:09:09 +02:00
|
|
|
AppendRowChoreo.addInput("Password", GOOGLE_PASSWORD);
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
// the title of the spreadsheet you want to append to
|
|
|
|
// NOTE: substitute your own value, retaining the "SpreadsheetTitle:" prefix.
|
2013-07-08 15:09:09 +02:00
|
|
|
AppendRowChoreo.addInput("SpreadsheetTitle", SPREADSHEET_TITLE);
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
// convert the time and sensor values to a comma separated string
|
|
|
|
String rowData(now);
|
|
|
|
rowData += ",";
|
|
|
|
rowData += sensorValue;
|
|
|
|
|
|
|
|
// add the RowData input item
|
2013-07-08 15:09:09 +02:00
|
|
|
AppendRowChoreo.addInput("RowData", rowData);
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
// run the Choreo and wait for the results
|
2014-04-03 19:56:46 +02:00
|
|
|
// The return code (returnCode) will indicate success or failure
|
2013-07-03 09:02:59 +02:00
|
|
|
unsigned int returnCode = AppendRowChoreo.run();
|
2013-06-26 17:29:57 +02:00
|
|
|
|
|
|
|
// return code of zero (0) means success
|
2013-07-03 09:02:59 +02:00
|
|
|
if (returnCode == 0) {
|
2013-06-26 17:29:57 +02:00
|
|
|
Serial.println("Success! Appended " + rowData);
|
2013-06-28 11:08:56 +02:00
|
|
|
Serial.println("");
|
2013-06-26 17:29:57 +02:00
|
|
|
} else {
|
2014-04-03 19:56:46 +02:00
|
|
|
// return code of anything other than zero means failure
|
2013-06-26 17:29:57 +02:00
|
|
|
// read and display any error messages
|
|
|
|
while (AppendRowChoreo.available()) {
|
2013-07-03 09:02:59 +02:00
|
|
|
char c = AppendRowChoreo.read();
|
|
|
|
Serial.print(c);
|
2013-06-26 17:29:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendRowChoreo.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 19:56:46 +02:00
|
|
|
// this function simulates reading the value of a sensor
|
2013-06-26 17:29:57 +02:00
|
|
|
unsigned long getSensorValue() {
|
2013-07-03 09:02:59 +02:00
|
|
|
return analogRead(A0);
|
2013-06-26 17:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
IMPORTANT NOTE: TembooAccount.h:
|
|
|
|
|
2013-07-03 09:02:59 +02:00
|
|
|
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
|
|
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
|
|
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
|
|
look like:
|
2013-06-26 17:29:57 +02:00
|
|
|
|
2014-04-03 19:56:46 +02:00
|
|
|
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
2013-06-26 17:29:57 +02:00
|
|
|
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
|
|
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
|
|
|
2014-04-03 19:56:46 +02:00
|
|
|
You can find your Temboo App Key information on the Temboo website,
|
2013-06-28 11:08:56 +02:00
|
|
|
under My Account > Application Keys
|
2013-06-26 17:29:57 +02:00
|
|
|
|
2013-07-03 09:02:59 +02:00
|
|
|
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
|
|
|
2014-04-03 19:56:46 +02:00
|
|
|
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
2013-07-09 20:07:54 +02:00
|
|
|
that you forgot to delete your credentials.
|
2013-06-26 17:29:57 +02:00
|
|
|
*/
|