Particle.function() with two variables?

Can I create a function that has two variables? In a Particle.function…

Something like: int ledToggle(String command, int blinkCount){

}

I cannot seem to get this to work…

1 Like

You can’t, but there’s nothing stopping you from parsing a single string that’s made up of more than one argument :wink:

4 Likes

Thanks… I will give that a shot. :slight_smile:

Not quite sure Moors7 said you can't. He had to have misunderstood the question.

You most certainly can create a function that has two variables. But you just cannot use "String" as the type.

Here is what you would want to try.

int ledToggle(char *command, int blinkCount)
{

Serial.printf(" %s - %u\r\n",command, blinkCount);
return 1;

}

void loop()
{
ledToggle("Test", 1);
delay(1000);
}

I am quite sure though, and don't think I misunderstood the question.

You most surely can make a function with more input arguments, no doubt about it. But that can't be your

Particle.function with two variables

:wink:

Not sure what you mean by:

because this compiles just fine:

void setup() {
    
}

void loop() {
    test("this", "compiles", "just", "fine");
}

int test(String are, String you, String really, String sure){
    
}
2 Likes

Moors7, Not arguing, just seeking clarification because you have me kinda confused here.

The OP asked “Can I create a function that has two variables?” to which you said "you can’t"
now you stated “You most surely can make a function with more input arguments, no doubt about it.”

@mikemoy, the OP's post title says it all:

Particle.function with two variables

:wink:

2 Likes

Or write two functions. One would just set the argument for the main function. When you send the 2nd value in the second function it would then use both variables. I’ll post some code in a bit. I need to wake up and have some :coffee:.

1 Like

You might want/need to get the information at the same time to act on it though. Also, in the spirit of “let’s not spam a free cloud service with an unnecessary amount of calls”, it’d be nicer to combine the data in a single call. Also, data savings for cellular devices could be a reason not to split everything out. Also, your code won’t get interrupted twice. (I’ve said ‘also’ too many times, I know).
In this day and age it’s very much accepted to just throw a bit more memory/data at problems, rather than optimizing for the situation at hand, but seeing as we’re working with embedded platforms it might not hurt to give that a shot after all.

That said, sure, two functions could work.

2 Likes

@Moors7 - I’ll withhold the two function solution unless someone asks. I do agree one function (argument) is better than two for those reasons you stated. Just to be aware that there is another solution should string validation and parsing become problematic.

1 Like

My bad... I should have clarified the question better... . I was referring to a Particle.function.

I have other functions i have created and it was no problem. So I was a bit confused as to what I could do in an actual Particle.function. I have edited the original post.

2 Likes

As a practical example of passing two variables into a Particle.function via 1 string variable that then gets broken up and assigned value to 2 variables:

int calvaltwo(String command) { //need the user to input what pH the device read when tested in 
//buffer solutions of pH 6.86 and pH 9.18 at 25c, separated by comma.
//e.g. "6.94,9.38"
float cal918 = 0;
float cal686 = 0;
char one[32], two[32]; //issue with float and sscanf see https://github.com/spark/firmware/issues/1124 
sscanf(command, "%[^,],%[^,]", one, two);
cal686 = String(one).toFloat();
cal918 = String(two).toFloat();
//rest of the code 
return 1;
}
1 Like

That works.

Alternatively, the Particle JSON library, JsonStreamingParser (1.0.5), can be used.

1 Like

you can use sscanf or just simple parsing of a string for spaces or commas:

int my_real_func(String command, int count) {
  // do stuff
  return res;
}

int my_helper_func(String command) {
  command=command.trim();
  int s=command.indexOf(" ");
  if (s<0) return -1;
  String cmd=command.substring(0,s);
  int count=command.substring(s+1).toInt();
  return my_real_func(cmd, count);
}

Particle.function("my_func", my_helper_func);

Personally I used to use this technique with a switch statement to get over the 4-function limitation :slight_smile:

1 Like

That one had been faded out silently it seems...
https://docs.particle.io/reference/firmware/photon/#particle-function-

I’m actually working on an aquarium controller and currently working on the calibration part… so the timing of your post is uncanny.

Thanks

1 Like

Awesome. Part of my project included manufacturing a heavy-duty pH probe. Probably more than you need for an aquarium but I’d love to get a few testers to give me feedback and possibly review on Amazon. PM if you’re willing to test it. And if you can use some KCL pH probe storage solution I have that too :wink:

Now if only I can get all the other parts in my system market ready!

Here is quick function I use to manually calibrate (e.g. you typically see a +0.2 difference in the range you’re measuring and don’t want to bother with buffers) WITH a simple check that the calibration level is within expected or wanted parameters:

int calibratePH(String command) {
  if((command.toFloat() >= -0.9) && (command.toFloat() <= 0.9)) { //don't allow manual adjustment greater than 0.9 either way
  manualAdjustment = command.toFloat(); //for user to manually adjust the pH not tied to voltage reading
  EEPROM.put(addressManual, manualAdjustment);
  Particle.publish("Manual pH Adjustment", String(manualAdjustment), 360, PRIVATE);

  return 1;
}  else return -1;

}

Hi sir,

Even I need to send 3 values using Particle.function().
My values are
Upper threshold
Lower threshold’
Update_time

right below here is my code.I have used System.reset() function so as to avoid Manual intervention of pressing RESET button on device.
And I have placed the Particle.function() prototypes under SETUP section.So I need to RESET the devive for updated values to be available since they are written in EEPROM.I tried by placing the Particle.function() under LOOP if so then no need to call System.reset() function or press RESET button on device.

So I have explained my coding method.If any suggestions please Sir,I am ready to learn and adopt the changes

int upperthreshold(String command)
{
   EEPROM.write(2003,command.toInt());
    return 1 ;
}  

int lowerthreshold(String command)
{
EEPROM.write(2004,command.toInt()); 
 System.reset();
    return 2;

}

int updatetime(String command)
   {
       EEPROM.write(2005,command.toInt());
       
       System.reset();
       return 3 ;
       
   }