从Arduino Yun到Google电子表格的数据

时间:2016-04-29 09:25:34

标签: google-sheets timestamp sensor motion arduino-yun

我正在开展一个项目,我将来自Arduino Yun的时间戳发送到Google电子表格。我有一个PIR传感器连接到Yun。检测到运动时,我将值发送到电子表格。目前,该值将进入一列。

enter image description here

这不太理想,因为我想根据数据创建图表,所以我希望时间和日期分为两个不同的列。如下面的示例。

enter image description here

Arduino Sketch

#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);   
    }
  }
}

如何更改代码以实现上述目标。

1 个答案:

答案 0 :(得分:0)

我为Temboo工作。

您可以通过用逗号分隔rowData中的每个值,在AppendRow Choreo中设置多个列。对于您的情况,您希望您的rowData看起来像这样:“19:35:26,4 / 22/2016”。您可以尝试timeString.replace(" ",",");

之类的内容

希望这会有所帮助。如果您有任何其他问题,请随时通过https://temboo.com/support

与我们联系