1

I am trying to create a trigger in my Google script that will call a function in another script, resetting a value. I am using a particle photon that is logging events/hour. The script just counts. The Google script is using a time driven trigger to collect data from the photon every hour. My problem is that once the data is collect for said hour, the timer is not reset. Is there a way to have the Google script ultimately reset the counter to 0 after each fetch of data?

Here is the Google script:

function collectData() {
  var  sheet = SpreadsheetApp.getActiveSheet();

  var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/xxxxxxxxxx/result?access_token=xxxxxxxxxx");

  try {
    var response = JSON.parse(response.getContentText()); // parse the JSON the Core API created
    var result = unescape(response.result); // you'll need to unescape before your parse as JSON

    try {
      var p = JSON.parse(result); // parse the JSON you created
      var d = new Date(); // time stamps are always good when taking readings
      sheet.appendRow([d, p.count]); // append the date, count to sheet
    } catch(e)
    {
      Logger.log("Unable to do second parse");
    }
  } catch(e)
  {
    Logger.log("Unable to returned JSON");
  }
}

Here is the photon script:

int Gate = D4;
int Led = D7;
int gateState = 0;
int count = 0;
char resultstr[64];

void setup() {

  pinMode(Gate, INPUT);
  pinMode(Led, OUTPUT);
  Particle.variable("result", resultstr, STRING); 

}

void loop() {

  static byte prevState = 1;
  gateState = digitalRead(Gate);
  digitalWrite(Led, LOW);

  if(gateState != prevState) 
  {
    if(gateState == HIGH)
    {
    count ++;
    digitalWrite(Led, HIGH);
    }
    prevState = gateState; 
  } 
    sprintf(resultstr, "{\"count\" :%d}", count);
    delay(50);
}
Kara
  • 6,115
  • 16
  • 50
  • 57

0 Answers0