I have a google app script that I’ve been using to pull data from my spark core every 15 minutes, as shown in the tutorial.
When I debug the script all of the features work as expected. I have been using it for a few days now, with no code changes on the core or on the app script.
The script, below, includes a section that will send an email to me if the spark core does not respond.
For a while, the script would pull data extremely reliably from the spark core. After a few days, it began to not respond every now and then. Now it does not respond most of the time that it’s asked for data.
The problem is, I cannot replicate the behavior. When I execute the google app script manually, it always works.
When I watch the light on the spark core it is connected all the time, I’ve never seen it drop out.
When I get the notification that it has not responded 4 times in a row, I send a GET request manually and get a response right away. I run over and check the light, and it’s breathing cyan.
It seems to only be flaky when google app scripts is executing the trigger based on time.
I’m stumped as to why this would make any difference.
function collectData() {
var sheet = SpreadsheetApp.getActiveSheet();
var d = new Date(); // time stamps are always good when taking reading
var i = 0;
do {
var response = callSpark();
if (i == 3) {
sheet.appendRow([d, "n/a", "n/a", "n/a", "n/a" ]); // append the date, data1, data2 to the sheet
MailApp.sendEmail("xxx@tmomail.net", "" , "The spark core hasn't responded 4 times in a row");
}
i++;
}
while (response == 0 && i < 4);
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
sheet.appendRow([d, p.TSP, p.T, p.HSP, p.H ]); // append the date, data1, data2 to the sheet
var error = Math.abs(p.TSP-p.T);
Logger.log(error);
if (error > 2){
MailApp.sendEmail("xxx@tmomail.net", "" , "The temp is off by more than 2 degrees f");
}
} catch(e)
{
Logger.log("Unable to do second parse");
}
} catch(e)
{
Logger.log("Unable to returned JSON");
}
}
function callSpark() {
try{
var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/xxx/jsondata?access_token=xxx");
return response;
} catch(e)
{
var response = 0;
return response;
}
}
I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy