2016-04-29 77 views
0

我正在開發一個項目,從Arduino Yun發送時間戳到Google Spreadsheet。我有一個PIR傳感器連接到Yun。當檢測到運動時,我將該值發送到電子表格。目前該值將進入一列。從Arduino Yun到Google電子表格的數據

enter image description here

因爲我想創建一個從數據的圖表,所以我想在兩個不同的列的時間和日期這是不理想的。像下面的示例一樣。

enter image description here

Arduino的素描

#include <Bridge.h> 
#include <Temboo.h> 
#include "TembooAccount.h" 
#include <Process.h> 

int pir_pin = 8; 

Process date; 
int hours, minutes, seconds; 
int lastSecond = -1; 

const String GOOGLE_CLIENT_ID = ""; 
const String GOOGLE_CLIENT_SECRET = ""; 
const String GOOGLE_REFRESH_TOKEN = ""; 

const String SPREADSHEET_TITLE = ""; 

int numRuns = 1; 
int maxRuns = 100; 

void setup() { 

    Serial.begin(9600); 
    delay(4000); 

    pinMode(pir_pin, INPUT); 

    while (!Serial); 
    Serial.println("Time Check"); 

    if (!date.running()) { 
    date.begin("date"); 
    date.addParameter("+%T %D"); 
    date.run(); 
    } 

    Serial.print("Initializing the bridge... "); 
    Bridge.begin(); 
    Serial.println("Done!\n"); 
} 

void loop() 
{ 
    if (digitalRead(pir_pin) == true) { 

    if (!date.running()) { 
     date.begin("date"); 
     date.addParameter("+%T %D"); 
     date.run(); 
    } 

    while (date.available() > 0) { 
     String timeString = date.readString(); 

     int firstColon = timeString.indexOf(":"); 
     int secondColon = timeString.lastIndexOf(":"); 

     String hourString = timeString.substring(0, firstColon); 
     String minString = timeString.substring(firstColon + 1, secondColon); 
     String secString = timeString.substring(secondColon + 1); 

     hours = hourString.toInt(); 
     minutes = minString.toInt(); 
     lastSecond = seconds; 
     seconds = secString.toInt(); 

     if (numRuns <= maxRuns) { 

     Serial.println("Running AppendRow - Run #" + String(numRuns++)); 

     unsigned long now = millis(); 

     Serial.println("Getting sensor value...");  
     Serial.println("Appending value to spreadsheet...");  

     TembooChoreo AppendRowChoreo; 

     AppendRowChoreo.begin();  
     AppendRowChoreo.setAccountName(TEMBOO_ACCOUNT); 
     AppendRowChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); 
     AppendRowChoreo.setAppKey(TEMBOO_APP_KEY);  
     AppendRowChoreo.setChoreo("/Library/Google/Spreadsheets/AppendRow"); 

     AppendRowChoreo.addInput("ClientID", GOOGLE_CLIENT_ID); 

     AppendRowChoreo.addInput("ClientSecret", GOOGLE_CLIENT_SECRET); 

     AppendRowChoreo.addInput("RefreshToken", GOOGLE_REFRESH_TOKEN); 

     AppendRowChoreo.addInput("SpreadsheetTitle", SPREADSHEET_TITLE); 

     String rowData = timeString;  
     AppendRowChoreo.addInput("RowData", rowData);   
     unsigned int returnCode = AppendRowChoreo.run();  

     if (returnCode == 0) { 
      Serial.println("Success! Appended " + rowData); 
      Serial.println(""); 
     } else { 

      while (AppendRowChoreo.available()) { 
      char c = AppendRowChoreo.read(); 
      Serial.print(c); 
      } 
     } 
     AppendRowChoreo.close(); 
     } 
     Serial.println("Waiting..."); 
     delay(5000); 
    } 
    } 
} 

我將如何改變代碼來實現上述。

回答

0

我爲Temboo工作。

通過用逗號分隔rowData中的每個值,您可以在AppendRow Choreo中設置多個列。對於你的情況,你會希望你的rowData看起來像這樣:「19:35:26,4/22/2016」。你可以嘗試類似timeString.replace(" ",",");的東西。

希望這會有所幫助。如果您有任何其他問題,請隨時聯繫我們https://temboo.com/support

相關問題