aboutsummaryrefslogtreecommitdiff
path: root/libraries/Bridge/examples/SendDataToGoogleSpreadsheet/SendDataToGoogleSpreadsheet.ino
blob: 984bd5392efbf3be5fb701de98606d4aa3858b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
  SendDataToGoogleSpreadsheet

  Demonstrates appending a row of data to a Google spreadsheet
  from the Arduino Yun using the Temboo Arduino Yun SDK.  

  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 
  http://www.temboo.com

  This example assumes basic familiarity with Arduino sketches, and that your 
  Yun is connected to the Internet. For more tutorials on using the Temboo Library 
  and Temboo Arduino Yun SDK, visit http://www.temboo.com/arduino

  Since this sketch uses a Google spreadsheet, you'll also need a 
  Google account: substitute the placeholders below for your Google account values.

  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)
  that the row was appended, and the second column is a sensor value
  (simulated in this example via a random number).

  This example code is in the public domain.

*/

#include <Bridge.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Process.h>
#include "TembooAccount.h" // contains Temboo account information, as described below


const unsigned long RUN_INTERVAL_MILLIS = 60000; // how often to run the Choreo (in milliseconds)

// the last time we ran the Choreo 
// (initialized to 60 seconds ago so the
// Choreo is run immediately when we start up)
unsigned long lastRun = (unsigned long)-60000;

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

  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;
    
    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
    Process AppendRowChoreo;

    // invoke the Temboo client
    AppendRowChoreo.begin("temboo");
    
    // set Temboo account credentials
    AppendRowChoreo.addParameter("-a");
    AppendRowChoreo.addParameter(TEMBOO_ACCOUNT);
    AppendRowChoreo.addParameter("-u");
    AppendRowChoreo.addParameter(TEMBOO_APP_KEY_NAME);
    AppendRowChoreo.addParameter("-p");
    AppendRowChoreo.addParameter(TEMBOO_APP_KEY);
    
    // identify the Temboo Library choreo to run (Google > Spreadsheets > AppendRow)
    AppendRowChoreo.addParameter("-c");
    AppendRowChoreo.addParameter("/Library/Google/Spreadsheets/AppendRow");
    
    // set the required Choreo inputs
    // see https://www.temboo.com/library/Library/Google/Spreadsheets/AppendRow/ 
    // for complete details about the inputs for this Choreo
    
    // your Google username (usually your email address)
    // NOTE: substitute your own value, retaining the "Username:" prefix.
    AppendRowChoreo.addParameter("-i");
    AppendRowChoreo.addParameter("Username:your-google-username");

    // your Google account password
    // NOTE: substitute your own value, retaining the "Password:" prefix.
    AppendRowChoreo.addParameter("-i");
    AppendRowChoreo.addParameter("Password:your-google-password");

    // the title of the spreadsheet you want to append to
    // NOTE: substitute your own value, retaining the "SpreadsheetTitle:" prefix.
    AppendRowChoreo.addParameter("-i");
    AppendRowChoreo.addParameter("SpreadsheetTitle:your-spreadsheet-title");

    // convert the time and sensor values to a comma separated string
    String rowData(now);
    rowData += ",";
    rowData += sensorValue;

    // add the RowData input item
    AppendRowChoreo.addParameter("-i");
    AppendRowChoreo.addParameter("RowData:" + rowData);

    // run the Choreo and wait for the results
    // The return code (rc) will indicate success or failure 
    unsigned int rc = AppendRowChoreo.run();

    // return code of zero (0) means success
    if (rc == 0) {
      Serial.println("Success! Appended " + rowData);
    } else {
      // return code of anything other than zero means failure  
      // read and display any error messages
      while (AppendRowChoreo.available()) {
        Serial.print((char)AppendRowChoreo.read());
      }
    }

    AppendRowChoreo.close();
  }
}

// this function simulates reading the value of a sensor 
// in this example, we're generating a random number
unsigned long getSensorValue() {
  return (unsigned long)random(0, 256);
}

/*
  IMPORTANT NOTE: TembooAccount.h:

  TembooAccount.h is a file referenced by this sketch that contains your Temboo account information. 
  You need to create this file. To do so, make a new tab in Arduino, call it TembooAccount.h, and 
  include the following variables and constants:

  #define TEMBOO_ACCOUNT "myTembooAccountName"  // your Temboo account name 
  #define TEMBOO_APP_KEY_NAME "myFirstApp"  // your Temboo app key name
  #define TEMBOO_APP_KEY  "xxx-xxx-xxx-xx-xxx"  // your Temboo app key

  The same TembooAccount.h file settings can be used for all Temboo SDK sketches.

  Visit https://www.temboo.com/account to access your Temboo account credentials. 

  Keeping your account information in a separate file means you can save it once, 
  then just distribute the main .ino file without worrying that you forgot to delete your credentials.

  (Be sure to delete this comment after creating your TembooAccount.h file!)
*/