I will try to be as precise as possible. Sorry for my bad english if i am not able to put in the scene properly -----
I have a relay that is controlled both over physical button as well as Blynk app. Which works well. But the issue i am facing is =
Scene 1
When the device boots for the 1st time it just sync with the previous state stored in the server.(Good) And lets say i have turned on the relay from the app.
Scene 2
But later when the connection drops(internet) i will no longer be able to control it over the app. So i use the physical button to say turn the relay off
Scene 3
Now when the connection is up again what’s happening is it will sync from the app side and will turn the relay back on (which i dont want), as i had that previously turned on from the app.
What i am trying to achieve is, after the connection drop the App must sync to the hardware. Not vice versa (hardware syncing to App)
But at the boot process let the hardware sync from the app(server).
Is there a way out ??
First connection (on device boot) get relay state from Blynk server
Subsequent connections, push relay state to Blynk server?
If so, then you’ll need a flag to track if this is the first connection, or do the first synchronisation from void setup (which only rinds once on boot-up) and all subsequent synchronisations from BLYNK_CONNECTED.
Posting snippets of your code doesn’t really help, because so much depends on what else you’re doing.
I would recommend just to sync the pins that you need to, not use Blynk.syncAll.
You haven’t answered my question about how you want this to work, but if you do want it to work the way that I described then after the first connection and synchronisation you need to be doing Blynk.virtualWrites to push your relay values to the Blynk server.
The if statement in your BLYNK_CONNECTED callback needs to have an else clause tagged on the end.
This will contain code that will be executed when Blynk connects and isFirstConnect is not true.
The code you place in this else clause will push the current relay values out to the Blynk server using Blynk.virtualWrite command(s).
Yessssssss !!! Got it woking. Blynk.virtualWrite is very powerful !!
And as i was doing this i was wondering like
What it the internet is down and i am out of home, and i want the device to be On, then even if i turn on the from the app and as soon as the internet is up the input from the app will be ignored as i have Blynk.virtualWrite running.
So, you’ve now just moved the goalposts and want different functionality!
I guess the simplest way would be to have a switch widget on the app that allows you to specify which is the master - the app or the device.
If its in device mode then it will work as it currently does, if it is in app mode then it will work as it did before.
However, I think that this is a messy solution, that will lead to misunderstanding of what the expected behaviour in various scenarios.
As you can expect the internet and home power to be working as expected the vast majority of the time, I’d work on the principal that you currently have, and know that if the device shows offline in the app then you need to check it later and activate the switch when the device is back online.
You could add a notification to the BLYNK_CONNECTED routine to let you know when a re-connection has taken place.