Particle.function node - Rule engine

I’m intend to call an exposed particle function named “selection” and registered in Photon code.

  // Particle.function as register in void setup()
  Particle.function("selection", selection); 
    
int selection (String command) {
  if(command =="RF1") {
    refr=1;  Serial1.println(refr);// printout "refr" on terminal
    return 0;
  }
  else if(command =="RF2") {
    refr=2; Serial1.println(refr); // printout "refr" on terminal
    return 1;
  }
  else if(command =="RF3") {
    refr=3; Serial1.println(refr); // printout "refr" on terminal
    return 2;
  }
  else return -1 ;
}

The device’s code registers the function and everything works fine when I testing it from particle console.

Particle%20Console

The exposed function is expecting to receive 3 different string arguments : RF1-RF2-RF3.

On Rule engine I’m using drop down node and connect it to a function node called " Get argument"

Next that function is hooked up to a Particle.function node named “Ref”

From particle console I’m verifying that “selection” get called .
Also from the debug tab, from Rule engine, it can be seen that the function get called from the cloud( Rule engine), cause -1 is the value returned as payload.
The strings arguments are being sending as can be seen on the R. engine debug tab.
I should received printed out on terminals the numbers 1, 2 and 3 when the selection of RF1, RF2 and RF3 are chosen respectively, but does not happen.
Can I get some help on that?

If you need further info please let me know.

Thank you very much.

Aditional pic : Get argument function code

I did manage to get the function return value back into the Rules Engine, but I found another quirk that feels like a show stopper for function calls - I’ll get to the bottom of it tho’

Just one note on your dropdown node: If you set the string value (left input box) to 1, 2 and 3 instead of RF1, RF2 & RF3 and only keep the long labels, you wouldn’t need to do the string comparison inside your Particle.function() handler but could just use command.toInt().
I’m also not sure why your return values are always one less than the value you send in and store in refr.

With both these changes your function handler would simply look like this

int selection (String command) {
  int retVal = command.toInt();
  if (1 <= retVal && retVal <= 3) 
    Serial1.println(refr = retVal);
  else 
    retVal = -1;

  return retVal;
}

Thank you again.
Those weird return values were just for debugging.
Anyway the code you proposed me is more coherent regarding
the conversion form string to int though the particle.function node keeps
returning -1.
The console return the corrects values… 1, 2,3 .
I’d consult to @rickkas7 also.
Regards,

I was told that there is a bug in the outbound Particle nodes where they only ever send the first argument they receive after deployment.
So currently the Particle nodes (apart from Subscribe) are rather useless for meaningful work with dynamic data.

You can get around this via a standard function node containing this code

msg.payload = { access_token: "<yourAccessToken>", args: msg.payload };
msg.headers = {'content-type':'application/x-www-form-urlencoded'};
return msg;

and a standard http node setup like this

However, I’d much prefere to use the OAuth2 configuration instead of hardcoding an access token.

1 Like

@rickkas7

Hi,

I’m intend to call an exposed particle function named “selection” and registered in Photon code.

int selection (String command) {
  int retVal = command.toInt();
  if (1 <= retVal && retVal <= 3) 
    Serial1.println( retVal);
  else 
    retVal = -1;

  return retVal;
} 

The device’s code registers the function and everything works fine when I testing it from particle console. The return values 1,2 3 are obtained.

The exposed function is expecting to receive 3 different string arguments : RF1-RF2-RF3.

On Rule engine I’m using drop down node and connect it to a function node called " Get argument"

Next a function node to set up the msg.argument value

Next that function is hooked up to a Particle.function node named “Ref”

From particle console I’m verifying that “selection” get called .
Also from the debug tab, from Rule engine, it can be seen that the function get called from the cloud( Rule engine), cause -1 is the value returned as payload.

I should received printed out on the terminal the numbers 1, 2 and 3 when the selection of RF1, RF2 and RF3 are chosen respectively, but does not happen.
Can I get some help on that?

If you need further info please let me know.

Thank you very much.

I moved this re-post here since double posting is not appreciated.
You did tag Rick already in your earlier post here so there is no need to open a new thread which would merely scatter the discussion over multiple threads.

BTW, have you seen my proposal for a workaround above?

Also, have you seen this

1 Like

Thank you for your time and help.

Well, it was good to know the end of the beta program.
So I’ll be moving to other dashboard.
Regards,

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.