Json post payload particle.function

Hello and Sorry, very much a newbie question

I have created a particle.function which I can happily post against causing various behaviours (leds on/off, play sound etc)

What I am wondering about is; is it possible to use the contents of the json post payload in the function? Essentially I am wondering if I could declare part of the particle function as a variable and pass the content of that variable as part of the Json payload?

E.g. in the payload example below YY = the action in the function, and ‘what’ would be the variable contents. So in this case enable color red against Led 6

{
“arg”: “yy”,
“what”: “6,255,0,0”
}

What I am finding is that the payload file is not really read beyond the first pair

Any advice?
Thanks
Tony

Functions are passed strings. What you do with that string afterwards is entirely up to you. Parse it as JSON, and you can use it as such.
If you show up what you’ve tried so far, we might be able to chime in.

// This #include statement was automatically added by the Particle IDE.
#include <InternetButton.h>
#include <application.h>
#include "InternetButton.h"

InternetButton b = InternetButton();
void setup()
{
    b.begin();
    b.allButtonsOff();
    Particle.function("led",ledToggle);
    Particle.variable("what",astring);
    what=="1,0,0,0";
}
    int ledToggle(String command) {
    if (command=="xx")
    {
       for (int i = 1; i<=(50); i=i+1)
            {
                b.allLedsOn(178,34,34);
                delay(100);                
                b.allLedsOff();
                delay(100);
            }
            b.allLedsOn(255,0,0);
    return 1;    
    }
    else if (command=="xy") {
       for (int i = 1; i<=(50); i=i+1)
            {
                b.allLedsOn(173,255,47);
                delay(100);                
                b.allLedsOff();
                delay(100);
            }
            b.allLedsOn(255,0,0);
    return 2;        
    }
    else if (command=="yy") {
       for (int i = 1; i<=(50); i=i+1)
            {
                b.ledOn("what");
                delay(100);                
                b.ledOff(2);
                delay(100);
            }
                b.ledOn(2,173,255,47);
    return 3;        
    }    
    else if (command=="off") {
    b.allLedsOff();
    return 0;
    }
    else {
    return -1;
    }
}

The if statement works fine (when I do not butcher the code) but thank you for any advice offered!

I suddenly thought I hadnt declared a variable to represent the passed payload so I was just blunderbus-style trying different things

Hmm, as a general rule of thumb, if you want help debugging logical issues, it’d be helpful to post code that actually compiles without errors.
Libraries included twice, “what” and “astring” are not declared.

I think you’re confusing the use of Particle.variable() with variables in general. Particle.variable() is meant to expose data to the outside from the device. You configure a variable on the device, which can then be read from the cloud side with an appropriate API call.

What you want to do is set a variable from the outside, something you’d use a function call for. You can have a single function argument contain all the information needed to set the variables. “[led],[state],[R],[G],[B]” would be a string you’d send, substituting the brackets with the actual data. In the function call, you’d then have to parse that data out, and act upon it.

ah. I see about about the particle.variable(). Ty.
To paraphase your response about variable from the outside for my understanding; I would need to create a second function, do an api post to that function to update the “variable” with my desired action and then an api post to the actual action function to get the desired result?

You could do that if that variable is some kind of semi-fixed setting you'd only change occasionally. But, you can set variables and activate in a single function call.

1 Like

Hello. not sure if there is a method for marking threads as closed. I have spent longer looking at this and understand that my original approach is not possible.
thank you for the assistance

for completeness sake, this is what I was trying to do when I asked the original question about passing additional information in the json payload.

void setup()
{

    b.begin();
    b.allButtonsOff();
    Particle.function("led",ledToggle);


}
    int ledToggle(String command) {
        
       String action=command.substring(0,1);
       String AAled=command.substring(1,3);       
       String red=command.substring(3,6);       
       String green=command.substring(6,9);
       String blue=command.substring(9);
       int lednum = 0;
       int rednum = 0;
       int grenum = 0;
       int blunum = 0;
       lednum = AAled.toInt();
       rednum = red.toInt();
       grenum = green.toInt();
       blunum = blue.toInt();

    if (action=="1")
    {
       for (int i = 1; i<=(50); i=i+1)
            {
                b.allLedsOn(255,0,0);
                delay(100);                
                b.allLedsOff();
                delay(100);
            }
            b.allLedsOn(255,0,0);
    return 1;    
    }
    else if (action=="2") {
       for (int i = 1; i<=(50); i=i+1)
            {
                b.ledOn(lednum,rednum,grenum,blunum);
                delay(100);                
                b.ledOff(lednum);
                delay(100);
            }
                b.ledOn(lednum,rednum,grenum,blunum);
    return 2;        
    }    
    else if (action=="3") {
    b.allLedsOff();
    return 3;
    }
    else {
    return -1;
    }
}
1 Like

Not sure that it is not possible or you just didn't understand what @Moors7 tried to explain.

You can send a JSON string to a Particle.function() and use libraries like JsonParserGeneratorRK to extract the individual fields.
If you then assign the respective values to some global variable you can also expose these values via Particle.variable().

As a side note:
Particle.function() implementations should not block for that long (50*200ms = 10seconds).