Function doesn't return correct values?

I’ve been experimenting with functions and the REST API, and I can’t get this to work:

void setup() {
    Spark.function("test", testFunction);
}

int testFunction(String args) {
    return 200;
}

The response I get is:

{
  "cmd": "VarReturn",
  "name": "test",
  "TEMPORARY_allTypes": {
    "string": " \u0000P\u0000",
    "uint32": 536891392,
    "number": 536891392,
    "double": null,
    "raw": " \u0000P\u0000"
  },
  "result": " \u0000P\u0000",
  "coreInfo": {
    "last_app": "foo",
    "last_heard": "2013-12-26T09:47:18.521Z",
    "connected": false,
    "deviceID": "50ff6e065067545607360587"
  }

I should expect the TEMPORARY_allTypes to contain 200 in the response - but it doesn’t. Anybody have any idea why?

Thanks
Delphy

This may or may not be the error, but I’ve had this output a few times, from the following two causes:
a) Send a command which is incorrectly typed (i.e. if you sent testFanction or some other variant)
or
b) Send a command while the spark core is actually not connected to the cloud (check the LED to see it’s still connected).

You should expect something like:

{
  "id": "0123456789abcdef01234567",
  "name": "YourDeviceName",
  "last_app": null,
  "connected": true,
  "return_value": 200
}

I just tried this code and it’s working fine for me:

bool state = HIGH;
void setup() {
    pinMode(D7,OUTPUT);
    digitalWrite(D7,HIGH); // turn on D7 by default
    Spark.function("test", testFunction);
}
int testFunction(String args) {
    state = !state; // toggle the state
    digitalWrite(D7,state);
    return 200;
}

The added benefit of including D7 in the mix, is that you can see the onboard LED toggle on/off for ever successful command you send. If the Core is not receiving the command, you’ll note that the LED will not toggle.

If you send the wrong funcKey which is test, you should see something like this:

{
  "id": "0123456789abcdef01234567",
  "name": "YourDeviceName",
  "last_app": null,
  "connected": true,
  "return_value": {
    "Error": "Something went wrong calling this function: Unknown Function: testjunk"
  }
}

If your device is offline you will see this response:

{
  "error": "Timed out."
}

If you are using Curl commands it’s formatted like this:
curl https://api.spark.io/v1/devices/0123456789abcdef01234567/test -d access_token=1234123412341234123412341234123412341234

where 0123456789abcdef01234567 is your code ID and the access_token ID is your actual token ID.

You can also try using a nice browser extension for Chrome called Advanced Rest Client found here: https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

Example of how to use it:

Here’s an example response:

@andrew Yeah it’s probably the latter, since my Spark loses it’s connection to the spark cloud fairly consistently. It was however able to flash the new code and do the API call. So, I’ll try it later on I guess.

@BDub lighting up D7 is kind of hard when the spark is in my meter cabinet and I’m upstairs. :wink: But thanks for all the tips!

No problem! Try getting it working on the table first where you can see it, then move it to a remote location. Right now since we don’t have a good way to do Serial debugging, it’s a little hard to debug when you don’t have the Core in front of you.

Maybe the Spark Team can comment on why you might see the response you are seeing. So far I haven’t seen anything quite like that.

Ok I was able to get the same response as you by putting the following line in my browser’s address bar:
https://api.spark.io/v1/devices/0123456789abcdef01234567/test?access_token=1234123412341234123412341234123412341234

This returns:

{
  "cmd": "VarReturn",
  "name": "test",
  "TEMPORARY_allTypes": {
    "string": " \u0000P\u0000",
    "uint32": 536891392,
    "number": 536891392,
    "double": null,
    "raw": " \u0000P\u0000"
  },
  "result": " \u0000P\u0000",
  "coreInfo": {
    "last_app": "foo",
    "last_heard": "2013-12-26T17:15:05.654Z",
    "connected": false,
    "deviceID": "0123456789abcdef01234567"
  }
}

The reason this doesn’t work is because your browser will only send a GET request, and it needs to be a POST request. I think the error returned could be clearer about what you’ve done wrong though.

Try using the Curl command, or the Chrome extension I recommended. I bet you’ll find it works perfectly after you get the POST request happening.

1 Like

I actually was using curl to do this, but using curl as a GET request, so this is the issue. Thanks for the help! :slight_smile:

1 Like

Ahh good to know. Can you share how you entered your command into curl such that it was generating a GET request?

As far as I know, entering it like this makes it a POST request:

curl https://api.spark.io/v1/devices/0123456789abcdef01234567/test -d access_token=1234123412341234123412341234123412341234

I would think for GET you’d have to do something like:

curl https://api.spark.io/v1/devices/0123456789abcdef01234567/test?access_token=1234123412341234123412341234123412341234

Is that what you where doing?

Yes, I had previously used the same for a Spark.variable request and didn’t change the type of request from GET to POST. I know how to to HTTP POST in cURL it just didn’t twig that it was supposed to be one. :slight_smile:

Hey,
I am a bit new at this, and am trying some way of getting data out of the core as I can’t install the serial driver thanks to the digital signature requirement( I know I can disable it, but.)

So, I tried the extension, and am using the GET to expose a variable. I still get the same.

{
“cmd”: “VarReturn”,
“name”: “f”,
“TEMPORARY_allTypes”: {
“string”: " \u0000P\u0000",
“uint32”: 536891392,
“number”: 536891392,
“double”: null,
“raw”: " \u0000P\u0000"
},
“result”: " \u0000P\u0000",
“coreInfo”: {
“last_app”: “foo”,
“last_heard”: “2013-12-30T19:30:24.847Z”,
“connected”: false,
“deviceID”: “48ff72063067555026242287”
}
}
I was expecting a float variable of ~20
What could be the step I am overlooking?

@kidrock I just tried to create a test program that exposed a float variable and I got a compiler error. I don’t think FLOAT is a supported type yet for Spark.variable().

Here’s my code that didn’t compile:

bool state = HIGH;
float floaty = 20.5;
void setup() {
    pinMode(D7,OUTPUT);
    digitalWrite(D7,HIGH); // turn on D7 by default
    Spark.function("test", testFunction);
    Spark.variable("floaty", &floaty, FLOAT);
}
int testFunction(String args) {
    state = !state; // toggle the state
    digitalWrite(D7,state);
    return 200;
}

You could use a string to get your data through a GET request from Spark.variable() like wgbartley has done here:

You will receive back something like this currently:

{
  "cmd": "VarReturn",
  "name": "mystr",
  "TEMPORARY_allTypes": {
    "string": "20.25",
    "uint32": 842018354,
    "number": 842018354,
    "double": null,
    "raw": "20.25"
  },
  "result": "20.25",
  "coreInfo": {
    "last_app": "foo",
    "last_heard": "2013-12-31T02:18:20.493Z",
    "connected": false,
    "deviceID": "098712304987123046761928"
  }
}

GET request formatted like this:
https://api.spark.io/v1/devices/53ff6a065067544838360587/mystr?access_token=0826376570961234796129873409817245
which works in your browser address bar, once you put a valid access_token in :wink:

1 Like

After some general head banging last night, I realised Float wasn’t implemented yet. I used the same string approach and now am trying to learn some PHP to set up a graph! :slight_smile:

Works if using the data type “double” instead of float! (or compiles at least)

D.G.