Trouble calling function from CLI

Hey all,
I am having trouble calling a function through cmd. I can get variables very easy however, when I call a function I always get this error- “Function call failed Timed Out”

I can do spark list and it shows the function there.

Im pretty sure my syntax is perfect, its also successfully verified from ide-

CMD - 
spark call mycontroller changeMessage "NEWMESSAGE"
Spark IDE - 
char msg[] = "DEFAULT";
void setup(){
     Spark.variable("mymessage", msg, STRING);
     Spark.function("changeMessage",changeMessage);
}
void loop(){}
int changeMessage(String newMessage){
     strcpy(msg, newMessage.c_str());
    return 1;
}

What should I look into on how to fix this problem?

Thanks!

Hi @flashyourface,

In future, please paste your code with the following syntax:

 ```cpp
 paste code here

Try commenting out ```  strcpy(msg, newMessage.c_str());``` and see what happens

I actually just found the solution, it was very dumb (probably because im so new to c).

This line -

 Spark.function("changeMessage",changeMessage);

had to be changed to this -

Spark.function("change",changeMessage);

It did not like having the same id name as the function name which is kind of silly.

Thanks @kennethlimcp for the tip on how to post code, ill do that from now on.

Was that the fix? That’s weird caused i used to do that with the same function name exposed with the corresponding function given the same name.

That was exactly the fix. I got the idea from someone who had the same exact problem in this post:

Though your rite in expecting it work, I would and have done the same in other languages. Maybe its this specific case?

@flashyourface, the function key should be less than 12 characters.

http://docs.spark.io/firmware/#spark-function

4 Likes

Just a heads up that this code could potentially corrupt memory. It only reserves 8 characters (7 chars plus the null character) for the array pointed to by msg in the line

char msg[] = "DEFAULT";

The function changeMessage copies the contents of newMessage into the array even if changeMessage contains more than 8 characters. This will lead to memory issues.

Refer to this excellent post (another post latter in the thread, by Zachary, gives a great intro to C strings):

@flashyourface I know you've already seen this, but I thought it would improve this thread to reference it.

AH @krvarma Thanks that makes so much sense!

Thanks @brett13 I actually learned the hard way and got the red light flashing for that exact problem then I updated to have char msg[100] and that kept me a bit safer!

1 Like