Requesting the state of a PB widget from code on the controller

Greetings Blynk Community

I would like to know if its possible to call a the Blynk.write function such as this one:


BLYNK_WRITE(V14){                       //This function ONLY occurs when the widget for the 
                                                          //SDcard is activted i.e. it does not run continuously
    
    int SDOption = param.asInt();       
    
        if(SDOption==HIGH){
            
            CheckSDCard=1;
            
        }        
        else{
        SDActive=0;             
        }
}

Or maybe to request the state of a Push button (PB) from the firmware programmed into the controller. Here is why i am interested in this:
I have implemented a sleep mode on my controller and when i press a push button on the Blynk app i created, the change in PB state seems to be missed by the program i.e. since the blynk.write function only executes when the PB is pressed, if the PB is pressed while the controller is asleep, the controller seems to be unaware of the change. Is there a way for me to request the state of the PB (because when i press the PB it shows ‘ON’ on the app) once the controller awakes from sleep so that any changes in state can be ‘seen’ by the controller. Thanks in advance.

Try adding this function

//run this function when BLYNK connects to server
BLYNK_CONNECTED()  
{
Blynk.syncVirtual(V14);
}

You can find more information about this function here
https://docs.blynk.cc/#blynk-firmware-blynktimer-blynk_connected
and
https://docs.blynk.cc/#blynk-firmware-blynktimer-blynksyncvirtualvpin
https://docs.blynk.cc/#blynk-firmware-blynktimer-blynksyncall

1 Like

When in Sleep, there is NO code running on your device, thus no Blynk communication is possible. The device only awakens by a wake timer or specific physical pin interrupt, state shange, reset, etc.

You can search this forum for more info using key words like Sleep, Deep Sleep, etc.

Thank you for your response and advise. I have tried using the Blynk.syncVirtual(V14); command in my program before but it has not helped. My hardware is put in sleep mode for just a second but when it wakes up the state of the PB is not updated or does not seem to be requested by the program. I have also now tried the example you suggested:

BLYNK_CONNECTED()
{
Blynk.syncVirtual(V14);
}

But as i have pressed the PB whilst the hardware was asleep when it did wake up, during the 3 seconds that it was awake, it did not become ‘aware’ that the PB has changed state as nothing happened, even though the PB was switched ON. It still remains in the ON position on the app but the hardware is not receiving this change in state. It is as if this info is not on the server? The Blynk.syncVirtual(V14) should have pulled the state of the PB from the Blynk server and update my hardware right ?

Thanks Gunner. I do know that there is no Blynk communication when the device is asleep, that is why i am looking for a way to request the information of, in this case, the change in state of a PB widget AFTER the hardware has awoken from its sleep.

Thanks a lot. I will try those key words on the forum and see if someone else found a solution to this problem.

Since the App sends the state change to the server, then that is done by syncing the last state from the server (to the device) upon device reconnection… as already mentioned above by @Toro_Blanco

First of all, there’s absolutely no point in putting your device to sleep for one second.

Secondly, based on the tests that I’ve done, you’re unlikely to achieve a start-up, Blynk connection, Blynk sync and then do something with the data before going back to sleep; - in just 3 seconds.
5 seconds is a normally the lowest amount of time in my experience.

Personally, I wouldn’t bother with the BLYNK_CONNECTED callback, I’d just put the Blynk.syncVirtual(V14) in void setup after your Blynk connection code if you’re using Blynk.begin.

Unlike normal Blynk sketch structure, I’d put everything in your void loop, with the intention that it will run just once for each wake cycle. The last thing in your void loop should be a deepsleep command, followed by a delay to prevent any further code execution before the sleep kicks-in.

Pete.

Okay please clarify this for me: When i use the Blynk.syncVirtual(V14), does the function associated with it execute. i.e. the following function:

BLYNK_WRITE(V14){

int SDOption = param.asInt();       
    if(SDOption==HIGH){
        
        //requestTime();   
        SDActive=1; 
        CheckSDCard=1;
        
    }        
    else{
    SDActive=0;            

    }

}

Or is it just the param.asInt() that updates? If the function executes then i do not understand why the action associated with the PB changing to the ON position does not execute.

The Blynk.syncVirtual(V14) command causes the BLYNK_WRITE(V14) callback to fire and execute fully, in exactly the same way as if the (in this case) button widget attached to V14 had been pressed.

I’m assuming that the button widget on V14 is set to Switch rather than Push, otherwise it will always return a value of 0 (off).

Pete.

Thank you Pete for that explanation and advice.
Why do you say there’s no point to putting your device in sleep for one second? Implementing this 1 second sleep mode to reduce the power drain on my battery and have extended the operation of my device by 3 hours.

Okay i will extend the ‘awake’ time of operation to 5 seconds and see if this resolves the issue. failing which, i will look into restructuring my code the manner in which you suggested.

Yes the PB is set to switch. The function appears not be executing though. But i will change the awake time and see if this yields better results. Thanks again Pete.

The wake time shouldn’t be specified in a timer. It should end once the code has finished executing.

I think you’ll find that the power used to boot-up, successfully negotiate a Wi-Fi and Blynk connection (I don’t think that’s happening at the moment with the timed wake period) then execute some code is probably more than the power used to maintain a stable connection.

Pete.

I actually combined all the functions that i need to be run into one main function that is controlled by the timer. The last command in that main function is to put the microcontroller into sleep mode. This way all the code executes first. Also i am using the BLE medium of communication with Blynk so no Wi-FI negotiation was needed.

So how does your device talk to the Blynk server to get the latest value for V14 ?

I think the answer is that it doesn’t, as I don’t believe there is currently any pass-through between the BLE connection to the device and the Wi-Fi/Cellular data connection from your phone to the Blynk server.

Pete.

1 Like

Because using BT/BLE does not directly connect the device to the server, there are some server related functions that do not work as they would with WiFi, etc. I rarely use BT/BLE so untested as to whether sync is one of those non-functional… er… functions :stuck_out_tongue_winking_eye:

@PeteKnight @Gunner

So how does your device talk to the Blynk server to get the latest value for V14 ?

It seems it actually does not. I think Pete, you and Gunner are correct. I will have to try and find another way to resolve this. If you happen to figure out another way please update this post. Till then i will play around with code. Thank you gentleman, i really appreciate the fact that you took the time and effect to advise and assist me.

I think the solution is to use Wi-Fi rather than Bluetooth.

Also, a word of advice for the future…
When you start a new topic the template you are presented with asks you to include quite a bit of background information. If you’d mentioned Bluetooth in that first post the answer would have been forthcoming much quicker.

Pete.

That would make my life so much simpler but unfortunately the design criteria for this project requires me to use Bluetooth :sweat_smile:

@PeteKnight @Gunner

In an attempt to find an alternate solution this problem i would like to know if there a way for Blynk to continuously send a command, in this case the PB sends an ON or OFF state, to the hardware and only stops sending this command when an acknowledgment is sent back from the hardware ? This would be simple enough to program on the hardware side but i am not sure how to force this behavior on Blynk’s side.

I’m not aware of any way to do that.
However, I don’t see how this is a potential solution to your desire to use Bluetooth and sleep mode. Can you explain a bit more about what you’re thinking?

Pete.