Hello everyone,
I have a project where I use a Switch widget linked to a bool variable in Arduino IoT Cloud. On change, the onPorteChange() function is called. So far, everything works fine while the board is online.
However, I’ve run into a problem when the WiFi connection is lost. I’ve written the code so the user can continue interacting with the device while it's offline. The issue is: when the internet connection comes back, the cloud overwrites the local variable with the last value it had before the disconnection.
For example:
// While connected:
bool porte = false; // synced with cloud
// Offline usage:
porte = true; // changed locally while offline
// After reconnection:
porte = false; // overwritten by the last cloud value
I would like to keep the locally updated value (in this case true) after reconnection and prevent the cloud from overriding it.
Does someone know how to handle that ?
Thanks in advance for your help!
Yes, you can configure the synchronization priority setting the last parameter in the addProperty method in thingProperties.h.
You can choose between:
MOST_RECENT_WINS
CLOUD_WINS
DEVICE_WINS
By default it's omitted and it's set as CLOUD_WINS this explain your behavior.
Try this
ArduinoCloud.addProperty(porte, READWRITE, ON_CHANGE, onPorteChange, 0.0, DEVICE_WINS);
Thank you very much, DEVICE_WINS have solved my issue.
May i ask you where did you get this information ? In the documentation i did not see anything related to that ArduinoIoTCloud - addProperty() (TCP) - Arduino Reference . What is fifth parameter "0.0" here? :
ArduinoCloud.addProperty(porte, READWRITE, ON_CHANGE, onPorteChange, 0.0, DEVICE_WINS);
Yeah you are right it's not documented, I know because I used it some times ago, but you can find the complete firm directly in the library here
The fifth parameter is the minimum delta. So the board automatically filters a value if it's below this threshold ex: I'm monitoring a temperature with a sensor but I want that cloud receives values with a granularity of 1 °C so I set this value as 1.0 and:
temp1 = 1 //sent
temp2 = 1.3 //not sent
temp3=2.1 //sent
May I ask you to flag the answer as solution if you solved ?
Thank you for your help! Yesterday, I selected your answer as the solution. Did you not see it?