Error 400 for Spark.function [Solved]

I’m having a weird bug where I have identical code referencing two Spark.function methods, and one of them works fine, and the other issues a Error 400 message. On the core, I have the following in setup():

Spark.function("setPosition", setPosition);
Spark.function("setDesiredTemp", setDesiredTemp);

And the functions are defined as:

int setPosition(String val) {
    pos = val.toInt(); // this is global
    if(pos > 160) { pos = 160; }
    servo1.write(pos);
    return 0;
}

int setDesiredTemp(String val) {
    desiredTemp = val.toInt(); // this is global
    return 0;
}

I’m using jQuery to send the post request as:

$.post( "https://api.spark.io/v1/devices/xxxx/setPosition/", { params: newValue, access_token: accessToken });

For the setPosition request, things work perfectly, as expected and the core executes the setPosition function, moving the servo.
But if I substitute “setPosition” for “setDesiredTemp”, I get:

{
  "code": 400,
  "error": "invalid_request",
  "error_description": "The access token was not found"
}

Am I missing something, here?

Hi @pickledgator

I would try debugging this using curl and then move back to jQuery. The first thing I would do is probe the device for all its functions and variables to make sure you are running the right version of the firmware, don’t have any typos in the name etc. One other note, you can only have 4 functions so if you have more than you show here, the last one will be ignored.

curl https://api.spark.io/v1/devices/<<device id goes here>>/?access_token=<<token goes here>>

This should list your functions and variables for that core.

Then I would try

curl https://api.spark.io/v1/devices/<<device id goes here>>/setDesiredTemp/?access_token=<<token goes here>>

Aha. It looks like there is a limit on the number of characters that you can use for a variables/function name. It was truncating my Spark.function to “setDesiredTe”, 12 characters. I tested this for both variables and functions and it appears to be the case.

It’s specified in the docs, but I had overlooked it
http://docs.spark.io/firmware/#data-and-control-spark-function

Thanks for the simple examples to poll general stats from the device.

2 Likes