Changing variable via Cloud API

Hello,

Please bear with me, as I’m new to Photon and my coding skills are limited, to say the least. I’ve built a Photon-based flash trigger which uses very simple code:

const int statusPin = D7; // status LED pin
const int flashPin = D1; // flash pin
const int sensorPin = A0; // select the input pin for the LDR
const int pwrPin = A2; // power pin
int sensorValue = 0; // variable to store the value coming from the LDR

void setup()
{
    pinMode(sensorPin,INPUT);
    pinMode(statusPin, OUTPUT);
    pinMode(flashPin, OUTPUT);
    pinMode(pwrPin, OUTPUT);

}

void loop()
{
    digitalWrite(pwrPin, HIGH);
    sensorValue = analogRead(sensorPin); // read the value from the LDR
    if(sensorValue>950){ // you might need to adjust the default 950 value for better results
        digitalWrite(statusPin, HIGH);
        digitalWrite(flashPin, HIGH);
        delay(10);
    } else {
        digitalWrite(statusPin, LOW);
        digitalWrite(flashPin, LOW);
    }
}

The code does the job, but quite often I need to adjust the sensorValue variable according to the current lighting conditions. As you can see, the value is hardwired into the code, so I use the Build IDE to modify it and push the changes to the Photon board. I wonder whether there is a way to change the value on the fly via an HTTP request using Cloud API? I hope my question makes sense, and thank you in advance for your help.

Best,
Dmitri

You’d usually go with a Particle.function()

In the respective function you’d parse the parameter and assign the parsed value to your local variable.

Thank you for your reply. At the risk of sounding stupid, is it possible to pass a variable in a function to the variable in void loop{}? If it’s possible, how is this done? Thanks!

I may just be stupid , but if you could wouldn’t be void loop (int x), and not void loop ();

@dmpop, in addition to what @ScruffR suggested, I would consider using some “intelligence” in how you deal with your sensor. For example when the sensor value is withing a certain range, the threshold is adjusted for that range.

@peekay123 This is actually a pretty good idea. Thanks!

Hmm, I can’t quite follow what you actually mean with that, but your code with Particle.function() could look like this (but without @peekay123’s valid point to add a sanity check):

const int statusPin = D7; // status LED pin
const int flashPin = D1; // flash pin
const int sensorPin = A0; // select the input pin for the LDR
const int pwrPin = A2; // power pin
int sensorValue = 0; // variable to store the value coming from the LDR
int sensorThreshold = 950;
int overrideValue = 0;


void setup()
{
    Particle.function("SetOverride", setSensorOverride);
    Particle.function("SetThreshold", setSensorThreshold);

    pinMode(sensorPin,INPUT);
    pinMode(statusPin, OUTPUT);
    pinMode(flashPin, OUTPUT);
    pinMode(pwrPin, OUTPUT);
}

void loop()
{
    digitalWrite(pwrPin, HIGH);

    if (overrideValue > 0)
        sensorValue = overrideValue;
    else
        sensorValue = analogRead(sensorPin); // read the value from the LDR

    if(sensorValue > sensorThreshold)
    { // you might need to adjust the default 950 value for better results
        digitalWrite(statusPin, HIGH);
        digitalWrite(flashPin, HIGH);
        delay(10);
    } else {
        digitalWrite(statusPin, LOW);
        digitalWrite(flashPin, LOW);
    }
}

int setSensorOverride(String param)
{
    overrideValue = param.toInt();

    return overrideValue;
}

int setSensorThreshold(String param)
{
    sensorThreshold = param.toInt();

    return sensorThreshold;
}

@ScruffR Fantastic! That was exactly what I was looking for, and it all makes sense now. Thank you so much for your help!

1 Like

@peter_a, void loop() is something like "keyword" in Wiring (the programming "framework" for Arduino, Particle and other platforms).
So you can't just change it as you wish and you could not pass the parameter anyhow, since in the code you usually won't find a statement that calls loop().

I know digging out old threads is not always welcome, but I thought this would be the right place to ask:

How to I set a DOUBLE variable instead of an INT? The functions do only take strings, and I can’t manage to figure out how I do set a variable with a function if it has to be a DOUBLE of FLOAT?

The idea is to set a target temperature, which I need to be able to set exactly. My code works perfectly fine when using integers. It is nowhere near finished, but I can’t continue without being able to set the target temperature over the API:

//Function for setting the targettemp1
int targettemp1(String targettemp1) {
  target1 = targettemp1.toInt();
}
//Function for setting the mode1
int setmode1(String setmode1) {
  mode1 = setmode1.toInt();
}
//Function for setting the targettemp2
int targettemp2(String targettemp2) {
  target2 = atoi(targettemp2);
}
//Function for setting the mode2
int setmode2(String setmode2) {
  mode2 = setmode2.toInt();
}

Hmm, the “problem” here isn’t really reviving an old thread, but that the requested info would acutally be available here
https://docs.particle.io/reference/firmware/photon/#string-class

If you search for toInt() in the docs you’ll hit a spot right next to your desired answer :wink:

1 Like

Oh no, I was soooo close. Sorry for that!

Works perfectly fine now, thank you so much for helping me out despite “the stupid” question.

1 Like

Hello all,

I have this use case where an Argon board is on battery, and wakes up when a rare event occurs. When it does, it should check online if at this moment it is allowed to perform some action, and the check itself must be as quick as possible.

Another, online process, regularly checks if the board is allowed to perform the action should it detect an event at the moment.

I intended to have the online process publish a variable (flag) to True or False, and have the board check this variable on wake-up. However it seems it is not possible to post a variable value via the cloud API, as it is read-only.
I could have the online process talk to another, main powered board that would be in charge of updating this variable but it seems overkill…

Do you know a way I could change a variable value via the cloud API or another pattern that would better quit this use-case ?

Thank you,

Delboye

This kind of use case would call for Particle.publish()/Particle.subscribe() but both devices need to be awake at the same time to communicate.
Currently there is no way of deferred communication.
If you need that you need an always on device/server that stores the value for later collection.

Thank you for your answer.
A pattern that better fits my need is to have the online process expose a TCP Server, having the Argon connect to it at startup with the TCPClient to know if it is allowed to perform the action.

Regards