I can trigger a particle function but it never receives my argument

Please let this be one of those times where I am missing something obvious. First things first, here’s my code:

Node:

var Particle = require('particle-api-js');
var particle = new Particle();

var token = '###abc'; //verified
var deviceId = '###'; //verified

var fnPr = particle.callFunction({ deviceId: deviceId, name: 'myfunction', argument: 'myData', auth: token });

fnPr.then(
    function(data) {
        console.log('Function called succesfully:', data);
    }, function(err) {
        console.log('An error occurred:', err);
    });

Particle:

void setup() {
    Particle.function("myfunction",myFunction);
}

void loop() {}

int myFunction(String command) {
    if (command == NULL) {
        return 1;
    }
}

When running the node script, I get the following back:

Function called succesfully: { body: 
    { id: '##########',
        last_app: '',
        connected: true,
        return_value: 1 },
    statusCode: 200 }

Just to make sure I wasn’t crazy I also flashed my particle with this:

void setup() {
    Particle.function("myfunction",myFunction);
}

void loop() {}

int myFunction(String command) {
    return 123;
}

And sure enough, got this back:

Function called succesfully: { body: 
     { id: '###########',
         last_app: '',
         connected: true,
         return_value: 123 },
     statusCode: 200 }

Any idea on what in the heck is going on?

1 Like

@dadschool
I wouldn’t recommend using NULL, as it is not possible for command to be NULL due to the int myFunction(String command) making myFunction require a String argument. Also Particle.function will pass an empty string to myFunction if “myfunction” is called with no arguments in JSON. Try something like if (command == "") to test if the string is empty. I would also recommend trying to debug over serial to be 100% sure it is working. Put Serial.begin(9600) in your setup, and Serial.println(command) in myFunction.

Your code works correctly for me when I call it from the CLI; I’m not able to reproduce your odd result. BTW, you really should have an “else” clause, so something is still returned from the function if the “if” isn’t executed.

2 Likes

@Ric

Great point. If a cloud function doesn't return anything it can get ugly.

Edit:
What worked? The first one or the second?

I doubt the first would work correctly.

The first one did work correctly. If I pass nothing, or an empty string, it returns 1, if I pass an argument, it returns 0 (or some other number if I put in an else clause).

Weird, I wonder why @dadschool is having problems then.

Unfortunately (command == “”) yields the same results :pensive: I am working to configure connection over serial.

Ric I didn’t include an “else” because I wanted to keep the code posted here pretty spartan.

Did you try calling the function from the CLI to see if you get the same results
?

Spartan is good for posting but the code should still be fully valid.
I might even suggest not necessarily to add an else branch, but make it a habit to have an unconditional return statement in each and every function that is supposed to return a value.

A possible useful statement for your problem might be this statement return command.length();, since it always has something to do with your input too.
And for your NULL, you might consider -1 as return value to distinguish between a one-char string and an empty one :wink:

And, as @Ric said, I'd guess your problem is in your calling code and not the firmware.

Adding Serial.println(command); in your function (or setting up a Particle.variable()) to see what data you actually get posted - as @nrobinson2000 suggested - is a good idea for debugging too.


@nrobinson2000, while it is true that the String parameter will never be NULL, there is an overload for the == operator that takes care of that. It will implicitly compare the internal C string against an empty string when you compare against NULL.

unsigned char operator == (const char *cstr) const {return equals(cstr);}

unsigned char String::equals(const char *cstr) const
{
	if (len == 0) return (cstr == NULL || *cstr == 0);
	if (cstr == NULL) return buffer[0] == 0;
	return strcmp(buffer, cstr) == 0;
}

Whoops, forgot to mention, when watching the console while submitting a function call via javascript, an empty line is added to the console, and the “test” command comes over fine.

EDIT: Forgotten important details

I just read today that another member had issues with particle-js-api 6.0.0.
Can you try the latest 5.x.x?

1 Like

Haha, I was doing that right now. Yes, that solved it.

There was also issue with particle.login method earlier as well, but I had chalked it up to bad copy/paste at the time.

Well that was a fun couple hours. Thanks again everyone who answered late on a Saturday night!

2 Likes

This was the post I had earlier above, I removed it because my edit history contained some uncensored info. This would have been directly below ScruffR’s first comment.

Here is my current code

Particle

void setup() {
    Particle.function("myfunction",myFunction);
    Serial.begin(9600);
}

void loop() {}

int myFunction(String command) {
    Serial.println(command);
    Serial.println("test");
    if (command == "") {
        return 1;
    } else {
        return 2;
    }
}

Node

var Particle = require('particle-api-js');
var particle = new Particle();

var token = '###abc'; //verified  
var deviceId = '###'; //verified

var fnPr = particle.callFunction({ deviceId: deviceId, name: 'myfunction', argument: 'myData', auth: token });

fnPr.then(
    function(data) {
        console.log('Function called succesfully:', data);
}, function(err) {
        console.log('An error occurred:', err);
});

CLI

particle call ############ myfunction "frustration"

Running CLI Returns:

2

Running via Javascript Returns:

Function called succesfully: { body: 
    { id: '##########',
        last_app: '',
        connected: true,
        return_value: 1 },
    statusCode: 200 }

Extra Stuff

particle-js-api version: 6.0.0

There’s a version 6.0.1 released now that fixes the login issue and this issue as well. Sorry for causing some frustration on a Saturday :bow:

1 Like