Components and supplies
Arduino Yun
Apps and platforms
Arduino IDE
Project description
Code
Code snippet #1
arduino
1/* 2 SendDataToGoogleSpreadsheet 3 4 Demonstrates appending a row of data to a Google spreadsheet from the Arduino Yun 5 using the Temboo Arduino Yun SDK. 6 7 This example code is in the public domain. 8*/ 9 10#include <Bridge.h> 11#include <Temboo.h> 12#include "TembooAccount.h" // contains Temboo account information 13 14/*** SUBSTITUTE YOUR VALUES BELOW: ***/ 15 16// Note that for additional security and reusability, you could 17// use #define statements to specify these values in a .h file. 18 19const String GOOGLE_USERNAME = "your-google-username"; // use your full google email address 20const String GOOGLE_PASSWORD = "your-google-password"; 21 22// the title of the spreadsheet you want to send data to 23// (Note that this must actually be the title of a Google spreadsheet 24// that exists in your Google Drive/Docs account, and is configured 25// as described above.) 26const String SPREADSHEET_TITLE = "your-spreadsheet-title"; 27 28int numRuns = 1; // execution count, so this doesn't run forever 29int maxRuns = 100; // the max number of times the Google Spreadsheet Choreo should run 30 31void setup() { 32 33 // for debugging, wait until a serial console is connected 34 Serial.begin(9600); 35 delay(4000); 36 while(!Serial); 37 38 Serial.print("Initializing the bridge... "); 39 Bridge.begin(); 40 Serial.println("Done!\ 41"); 42} 43 44void loop() 45{ 46 47 // while we haven't reached the max number of runs... 48 if (numRuns <= maxRuns) { 49 50 Serial.println("Running AppendRow - Run #" + String(numRuns++)); 51 52 // get the number of milliseconds this sketch has been running 53 unsigned long now = millis(); 54 55 Serial.println("Getting sensor value..."); 56 57 // get the value we want to append to our spreadsheet 58 unsigned long sensorValue = getSensorValue(); 59 60 Serial.println("Appending value to spreadsheet..."); 61 62 // we need a Process object to send a Choreo request to Temboo 63 TembooChoreo AppendRowChoreo; 64 65 // invoke the Temboo client 66 // NOTE that the client must be reinvoked and repopulated with 67 // appropriate arguments each time its run() method is called. 68 AppendRowChoreo.begin(); 69 70 // set Temboo account credentials 71 AppendRowChoreo.setAccountName(TEMBOO_ACCOUNT); 72 AppendRowChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); 73 AppendRowChoreo.setAppKey(TEMBOO_APP_KEY); 74 75 // identify the Temboo Library choreo to run (Google > Spreadsheets > AppendRow) 76 AppendRowChoreo.setChoreo("/Library/Google/Spreadsheets/AppendRow"); 77 78 // set the required Choreo inputs 79 // see https://www.temboo.com/library/Library/Google/Spreadsheets/AppendRow/ 80 // for complete details about the inputs for this Choreo 81 82 // your Google username (usually your email address) 83 AppendRowChoreo.addInput("Username", GOOGLE_USERNAME); 84 85 // your Google account password 86 AppendRowChoreo.addInput("Password", GOOGLE_PASSWORD); 87 88 // the title of the spreadsheet you want to append to 89 AppendRowChoreo.addInput("SpreadsheetTitle", SPREADSHEET_TITLE); 90 91 // convert the time and sensor values to a comma separated string 92 String rowData(now); 93 rowData += ","; 94 rowData += sensorValue; 95 96 // add the RowData input item 97 AppendRowChoreo.addInput("RowData", rowData); 98 99 // run the Choreo and wait for the results 100 // The return code (returnCode) will indicate success or failure 101 unsigned int returnCode = AppendRowChoreo.run(); 102 103 // return code of zero (0) means success 104 if (returnCode == 0) { 105 Serial.println("Success! Appended " + rowData); 106 Serial.println(""); 107 } else { 108 // return code of anything other than zero means failure 109 // read and display any error messages 110 while (AppendRowChoreo.available()) { 111 char c = AppendRowChoreo.read(); 112 Serial.print(c); 113 } 114 } 115 116 AppendRowChoreo.close(); 117 } 118 119 Serial.println("Waiting..."); 120 delay(5000); // wait 5 seconds between AppendRow calls 121} 122 123// this function simulates reading the value of a sensor 124unsigned long getSensorValue() { 125 return analogRead(A0); 126}
Comments
Only logged in users can leave comments
Arduino_Scuola
0 Followers
•0 Projects
Table of contents
Intro
3
0