# Function doesn't return correct values?

**URL:** <https://community.particle.io/t/function-doesnt-return-correct-values/1000>\
**Category:** Firmware\
**Created:** [December 26, 2013, 9:52am UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000 "2013-12-26T09:52:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Delphy](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@Delphy](https://community.particle.io/u/Delphy)\
**Post date:** [December 26, 2013, 9:52am UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/1 "2013-12-26T09:52:43Z")

</div>

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

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

int testFunction(String args) {
    return 200;
}
```

The response I get is:

```auto
{
  "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

---

<div class="post-metadata">

**Author:** ![andrew](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@andrew](https://community.particle.io/u/andrew)\
**Post date:** [December 26, 2013, 12:55pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/2 "2013-12-26T12:55:51Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [December 26, 2013, 4:23pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/3 "2013-12-26T16:23:05Z")

</div>

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](https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo)

Example of how to use it:  
 ![](https://sea2.discourse-cdn.com/flex026/uploads/particle/121/abfbd0372e369fbb.png)

Here’s an example response:  
 ![](https://sea2.discourse-cdn.com/flex026/uploads/particle/122/489ea2cc45c63753.png)

---

<div class="post-metadata">

**Author:** ![Delphy](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@Delphy](https://community.particle.io/u/Delphy)\
**Post date:** [December 26, 2013, 4:40pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/4 "2013-12-26T16:40:24Z")

</div>

@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. 😉 But thanks for all the tips!

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [December 26, 2013, 4:51pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/5 "2013-12-26T16:51:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [December 26, 2013, 5:17pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/6 "2013-12-26T17:17:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Delphy](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@Delphy](https://community.particle.io/u/Delphy)\
**Post date:** [December 26, 2013, 6:56pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/7 "2013-12-26T18:56:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [December 26, 2013, 7:33pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/8 "2013-12-26T19:33:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Delphy](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@Delphy](https://community.particle.io/u/Delphy)\
**Post date:** [December 26, 2013, 11:47pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/9 "2013-12-26T23:47:54Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![kidrock](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kidrock](https://community.particle.io/u/kidrock)\
**Post date:** [December 30, 2013, 7:34pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/10 "2013-12-30T19:34:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [December 31, 2013, 2:24am UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/11 "2013-12-31T02:24:18Z")

</div>

@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:

> **[Cloud API incorrectly parsing DOUBLE values](https://community.particle.io/t/cloud-api-incorrectly-parsing-double-values/1074/3)**
>
> It seems that strings work pretty well: char mystr\[6\]; void setup() {     Spark.variable(“mystr”, &mystr, STRING); } void loop() {     sprintf(mystr, “%.2f”, 12.34); }

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](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 😉

---

<div class="post-metadata">

**Author:** ![kidrock](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kidrock](https://community.particle.io/u/kidrock)\
**Post date:** [December 31, 2013, 9:11am UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/12 "2013-12-31T09:11:16Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![sierrasmith71](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@sierrasmith71](https://community.particle.io/u/sierrasmith71)\
**Post date:** [December 31, 2013, 4:51pm UTC](https://community.particle.io/t/function-doesnt-return-correct-values/1000/13 "2013-12-31T16:51:38Z")

</div>

> [@BDub](#):
>
> 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;

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

D.G.
