After restarting Blynk It does not restart from app settings

I am playing around with a Blynk project which will later become my new aquarium controller. Everything is working fine, but i have noticed 1 thing and was wondering how to resolve the issue.
Say you have a switch widget which controls a LED on your Arduino via an ESP. when i switch it on the LED comes on…so far good. Now i disconnect the arduino and ESP (simulating a power cut) and reconnect. The Blynk app on my phone still says the LED is ON, however LED on the Arduino is OFF. you have to go back to the app and toggle the switch OFF and then back ON, then the LED comes back on. Is there a way that when the Arduino + ESP part is rebooted it reverts back to the last setting before power off or better still actually read the info from the App and turn on accordingly?

Hopefully, you’re using a virtual pin on your switch widget, rather than a digital pin.
If that’s the case then you can use Blynk.syncVirtual(VPin):

https://docs.blynk.cc/#blynk-firmware-blynktimer-blynksyncvirtualvpin

If you’re using digital pins then it can still be done, by reading the value of the pin on your device, but you’re much better-off going down the virtual pin route.

Pete.

How can a virtual pin on a widget control an actual pin on the Arduino?

Like this…

  #define RelayPin 5 // GPIO pin that your relay is connected to
void setup()
{
  pinMode(RelayPin, OUTPUT);

// after you've connected to Blynk...
  Blynk.syncVirtual(V1); // This will cause the  BLYNK_WRITE(V1) function to be called
}
BLYNK_WRITE(V1)  // This function is called automatically every time the V1 widget changes
{
 int vpin_value = param.asInt(); // get the value from the switch widget (1=on, 0=off)

 if(vpin_value)  // if the switch widget is on(1)...
 {
   digitalWrite(RelayPin, HIGH);  // energise the relay -  assumes that the relay is active HIGH
 }
 else
 {
   digitalWrite(RelayPin, LOW); // de-energise the relay -  assumes that the relay is active HIGH
 }
}

Pete.

Thank you. work perfectly.

Mariano

1 Like

While @PeteKnight’s code is a great example, I would do it a little different. I prefer to run the Blynk.syncVirtual(VPin) in the BLYNK_CONNECTED() function. As this function will run every time the device connects to the server. This includes a complete power cycle of the device, but also if it just loses (and re-establishes) wifi. Expanding on PeteKnight’s example…

  #define RelayPin 5 // GPIO pin that your relay is connected to

BLYNK_CONNECTED() {
// after you've connected to Blynk Server...
  Blynk.syncVirtual(V1); // This will cause the  BLYNK_WRITE(V1) function to be called
}

void setup()
{
  pinMode(RelayPin, OUTPUT);

//plus other setup stuff

}


BLYNK_WRITE(V1)  // This function is called automatically every time the V1 widget changes
{
 int vpin_value = param.asInt(); // get the value from the switch widget (1=on, 0=off)

 if(vpin_value)  // if the switch widget is on(1)...
 {
   digitalWrite(RelayPin, HIGH);  // energise the relay -  assumes that the relay is active HIGH
 }
 else
 {
   digitalWrite(RelayPin, LOW); // de-energise the relay -  assumes that the relay is active HIGH
 }
}
1 Like

Thank you.
Very helpful.