in https://docs.thinger.io/quick-sart/coding-guide
**
Summary
Show Input Resources State in Dashboards and API
**
The Dashboards or API works in a way that when you open them, they query the associated resources to correctly print its current state, i.e., the switch is on or off. In this way, when the API or a Dashboard is open, each associated input resource is called, receiving empty data in the call, as there is no intention to control the resource (the pson input will be empty).
So, how the Dashboards or the API knows what is the current state of an input resource? The resource must set its current state in the input parameter, if it is empty, or use the input value if there is one. This way, we can obtain three different things: query the current resource state (without modifying it), modify the current resource state, and obtain the expected input on the resource (this is how the API explorer on the device works).
Therefore, a correct input resource definition that actually allows to display the current state of the resource in a Dashboard or in the API, will be like this example code.
thing["resource"] << [](pson& in){
if(in.is_empty()){
in = currentState;
}
else{
currentState = in;
}
}
This sample code basically returns the current state (like a boolean, a number, etc) if there is no input control, or use the incoming data to update the current state. This can be easily adapted for controlling a led, while showing its current state in the dashboard once opened or updated.
thing["led"] << [](pson& in){
if(in.is_empty()){
in = (bool) digitalRead(pin);
}
else{
digitalWrite(pin, in ? HIGH : LOW);
}
}
Note: for controlling a digital pin just use the method explained in the Easier Resources Section.
In both examples given the final “;” is missing.