How to update a device parameter from the app?


#1

How would one go about reading and/or updating a device parameter from the app? From what I read in the documentation, it should be possible to do it like this:

thing[“minimum”] = [](pson& in, pson& out){
if (in > 0) { minimum = in }
out = minimum;
};

However, when I tried that, the device value was not shown by the app, and a POST operation from the app to the device never ended on the app (even though it got executed by the device).

So then I had to revert to something more complicated, using a separate attribute name for setting the value, and one for reading the value. That works but it’s not as straightforward.

These resources that have both input and output are not selectable for the dashboard, is that by design? I would think that the one from the example above should be visible, as there are no subitems.

Then another question, is there a way to hide certain device resources from the app?

Thanks!


#2

If is just one value you can use this code

thing["Tag"] << inputValue(variable);

If is more than one value in the same “thing”, you can use:

thing["Tags"] << [](pson& in)
{
    if(in["Tag1"].is_empty()) in["Tag1"] = var1;
    else var1 = in["Tag1"];

    if(in["Tag2"].is_empty()) in["Tag2"] = var2;
    else var2= in["Tag2"];
};

You can select wich resources can a token access, when you generate the “Device Token” at device’s page.

Hope this helps


#3

Just two more pieces of advice.

When making (in>0) You are trying to evaluate a Pson as a numerical variable, and this might not work properly. By another side, could be useful to include an additional comparison to know if the input pson is empty:

thing["minimum"] = [](pson& in, pson& out){
    if (in.is_empty()){
      in=minimum ; //we are taking the last value of minimum in our program
      out=minimum; //if you want to show this value too when de pson is empty
    }else{ 
      minimum = in ;
      out = minimum;
    }
};

with this resource, the sistem will create two boxes, one for input and one for output data:


hope you find this tips useful

best regards!


#4

Another solution is to use the easier resources… that covers all recommendations described before:

http://docs.thinger.io/arduino/#coding-easier-resources-modify-sketch-variables

Modify Sketch Variables

Our sketch usually defines some parameters or variables that are used inside the loop code. This kind of resources are normally used to handle or control the execution behaviour. With this kind of resources we can modify any parameter we want to expose, like a float, an integer, a boolean, etc.
In this example it is possible to remotelly modify the boolean sdLogging variable defined as a global variable.

thing["logging"] << inputValue(sdLogging);

It is also possible to define a callback function to know when the variable has changed, so we can perform any other action. For this use case, define the resource as the following to have some code executed when the hysteresisVar changes.

thing["hysteresis"] << inputValue(hysteresisVar, {
    // execute some code when the value change
    Serial.println("Hystereis changed to: ");
    Serial.print(hysteresisVar);
});