Mysterious “Blynk.syncVirtual()” behaviour

Hi everyone,

I have a strange problem with my room controll that turns my lights on nearly everytime the esp disconnects from the internet.

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";


long unsigned int switchActivated;
long unsigned int switchActivated2;

BlynkTimer timer;
int light = 0;

void setup()
{
  // Debug console
  Serial.begin(9600);
  
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  Blynk.begin(auth, ssid, pass);

}


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


BLYNK_WRITE(V1) //Button Widget is writing to pin V
{
  if (param.asInt()){
    switchActivated2 = millis(); 
    digitalWrite(12, HIGH);
    Blynk.virtualWrite(V1, HIGH);
    Blynk.virtualWrite(V5, 1023);
    Blynk.virtualWrite(V4, 0);
  }
}

  BLYNK_WRITE(V2) //Button Widget is writing to pin V
{
  if (param.asInt()){
    switchActivated = millis(); 
    digitalWrite(14, HIGH);
    Blynk.virtualWrite(V2, HIGH);
    Blynk.virtualWrite(V4, 1023);
    Blynk.virtualWrite(V5, 0);
  }
}

BLYNK_WRITE(V3) //Button Widget is writing to pin V
{
  if (param.asInt()){

    if (light == 0) {
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       light = 1;
    }
     else  {
       digitalWrite(5, HIGH);
       digitalWrite(4, LOW);
       light = 0;
    }        
  }
}

void loop()
{
  Blynk.run();
  timer.run();

  
  if(switchActivated > 0 && switchActivated + 20000 < millis())
{
  switchActivated = 0;
  digitalWrite(14, LOW);
   Blynk.virtualWrite(V2, LOW);
   
}

 if(switchActivated2 > 0 && switchActivated2 + 20000 < millis())
{
  switchActivated2 = 0;
  digitalWrite(12, LOW);
   Blynk.virtualWrite(V1, LOW);
   
}
}

V3 is used to toggle the lights on and off, V1 and V2 are used for my rollershutters but they work just fine.
I use two pins for the light cause I built a toggle switch so I can either use my tablet on the wall or physical switch to turn the lights on and off.

What could cause this behaviour and how can I solve that?

thank you already,
Nick

Well… all I can point out is that the default boot state of ESP8266 GPIO pins 4 & 5 is LOW, so depending on how you have things wired… only you know if that means lights ON or OFF.

Then, your V3 sync should grab whatever was the last state stored in the server and update that function upon connection… which seems to flip-flop the pin states depending on vPin state… so again, who knows what you have wired.

I recommend adding in some form of debugging… either Serial or sending various vPin and GPIO state data back to a terminal widget or something… then follow the logic.

yeah that seems to make sense and in addition to the wiring there is also the problem with the toggle switch cause if I use that to toggle the lights the on/off wiring changes again.

I think with virtual pins I will not be able to come to a solution in this situation :pensive:

Virtual Pins are a tool for the App/sketch coding… GPIO and associated commands will always be required for the device side.

Now, if you are introducing a hardware based flip-flop in your wiring, then you need to add in some form of hardware based active circuit detection and use that to provide feedback to your sketch so that it can properly update the virtual pins.

Long topic about something similar is here…

thank you very much, will definitely try that out and give feedback if it worked!

When I first looked at your code I thought there was an issue with the structure of the if statements in your BLYNK_WRITE(V3) function, but as you’d said:

which made no sense to me. You now appear to be saying that the V3 button isn’t working as expected.

I think the problem is here:

This statement will only evaluate as true if the value coming from the toggle switch widget on V3 is a 1 (on).
You need an else statement that corresponds with the first if to turn your lights off when the value from V3 is 0 (off).

Pete.

1 Like

Hi Pete,

The V3 Button works fine but could that really be the problem?
I thought it has to have something to do with a wrong sync so at the reconnect the button value that is transmitted is wrong. With toggle switch I ment my physical toggle switch but the button in the app also is a toggle switch.

Will try to edit the code as you said…

Ok so I now did the following changes to V3:


BLYNK_WRITE(V3) //Button Widget is writing to pin V
{
  if (param.asInt()){

   // if (light == 0) {
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       //light = 1;
    
   }
     else  {
       digitalWrite(5, HIGH);
       digitalWrite(4, LOW);
      // light = 0;
     }        
}

I also had to change the button mode of V3 to switch, had it on push.
Hopefully this works now…