Calling a Core function from Google script

Hi All,

I’m new here, so please be kind :smile:

I have been programming Arduinos for a couple of years and recently purchased a Spark Core - loving it so far.

I have a simple example working where temperature data is logged from a sensor to a Google spreadsheet. Now this is where I get stuck :frowning: How do I modify the Google script to call a function in the core ie to reset a counter. Basically I need to read temperature data from the core then reset a variable. I guess I need to use spark.function in the script, but this script stuff is new to me.

Any basic example would b gratefully appreciated.

Thanks in advance.

Nick

Hey Nick,

Glad you like the Spark! As far as the scripting stuff goes, this might prove helpful: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
I personally haven’t yet played with the google scripts, but you’re supposed to make a post request, which is what’s described in the above link. Take a look, and let us know if you require any further assistance!

~Jordy

Hi Jordy,

Many thanks for the quick response - I’ll have a read. I have done some more searching and have identified the below example in Spark Docs.

curl https://api.spark.io/v1/devices/0123456789abcdef01234567/brew
-d access_token=1234123412341234123412341234123412341234
-d “args=202,230”

I think im ok with the core side of the programming, its just how the above is put into the Google script. Can it be written as one long line?

Any help appreciated.

Nick

Although it should be possible to combine it to a single browser-compatible URL, I don’t think that’s required as per the above link (@bko, could you help out with this?) You can insert your parameters in the “options” field, which should work.

Hi @scotchern and @Moors7

I don’t know too much about the google scripting environment, but to run a Spark function, you need to do a POST request and there is an example of how on that page referenced above.

Here is a “sketch” of what the google code should look like. You need to fill in the “Spark String Arg”, the access_token, device_id, and SparkFunctionName with your actual values.

   var payload =
   {
     "params" : "Spark String Arg",
     "access_token" : "1234....4321"
   };

   // Because payload is a JavaScript object, it will be interpreted as
   // an HTML form. (We do not need to specify contentType; it will
   // automatically default to either 'application/x-www-form-urlencoded'
   // or 'multipart/form-data')

   var options =
   {
     "method" : "post",
     "payload" : payload
   };

   UrlFetchApp.fetch("https://api.spark.io/v1/devices/4321...1234/SparkFunctionName", options);
 }
5 Likes

Hi @bko,

Thanks ever so much I’ll give it a go, but have to wait until im back from holiday.

Cheers

Bick

1 Like

Hi @bko,

I have just tried this and it works a treat.

Many thanks for your help.

Nick

2 Likes