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
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.
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.
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
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).