Using Particle App to pass variable values

Hey all,

Myself and a team of other engineering students are trying to use Photon to collect temperature data from a sensor, and send push notifications when the temperature exceeds certain bounds. We have created successful code, now, we want to be able to set the temperature threshold values through the Particle App.

My initial idea was to create a particle.function that takes the threshold value as a string command, converts it to integer, and then uses it in an infinite while loop that contains our if statement to send push notifications if the value is exceeded. However, when I called the function through Particle, I received an error message, presumably because the function is an infinite loop.

Does anybody have any ideas on how we could send a value to a variable from the Particle App? We are trying not to involve other applications in order to keep our project as simple as possible, but if this isn’t possible, that would be good to know as well.

Thanks,
Chaz

Particle.function() is the way to go, but why do you use an infinite while() and not just let loop() do the looping?
If you trap the code flow in a prolonged loop you either need to regularly call Particle.process() or go with SYSTEM_THREAD(ENABLED). But as already said, avoiding prolonged loops completely would be better practice.

Also the callback function for your Particle.function() should return as quickly as possible (and return a useful integer value) to let the cloud know execution was successful.
So the intended way would be to parse the incoming string, store it in a global variable and then return control to loop() to act upon the updated value.



Hey ScruffR,

Thanks for your help. We are currently trying to implement the Particle.function, convert the incoming string to float, and assign it the global variable “upperbound”, and then use it within our if statement inside of our loop(). For some reason, the Particle function returns an error when we try to send it a value. Any ideas?

We know that the code for reading the sensor and sending push notifications works, as that was tested desperately. It’s only the Particle function that is giving us errors.

Before I look at your code, reading screenshots isn’t my favorite task and if I had to actually try out your code, I’d have to type it off (which I won’t do in any case).
So you could either post the code as text to copy/paste into a test project or you could even provide a shared link to a project snapshot as described here
https://docs.particle.io/guide/getting-started/build/photon/#sharing-your-app

We actually just figured out that there was a space in the particle function name, and that was causing the error. Our code works! Thanks for your help.

1 Like

I’m quite puzzled about that now, since I actually do see some other issue in your code.
The function signature of upperBoundF() is wrong. A Particle.function() callback must return an int not a float.
https://docs.particle.io/reference/firmware/photon/#particle-function-

BTW, you are still using the deprecated syntax for Particle.variable()
https://docs.particle.io/reference/firmware/photon/#particle-variable-

The current syntax does not use the address-of operator (&) and no type specifier.

Interesting. We updated our Particle.variable() syntax, thanks for pointing that out.