Sync physical & virtual pin status

Hi Blynk community,

I’ve looking in vary topics but I’ve don’t found the solution to my problem.
My project is very simple : I would like to drive (Virtual V3) a led (or a relais) with Esp8266_07 but in the same time I would like the status of the physical pin (5) connected to the led (Virtual V2). Also I would like to receive the voltage reading of my Esp8266 (Virtual V1).
Following the best working sketch; the problem is that sometimes the led status on the app it is not synchronized with the physical status of the pin. I’ve tried syncAll and synVirtual but it seems worse.
May someone give me some suggestion ?
thank you in advance

> include ESP8266WiFi.h
> include BlynkSimpleEsp8266.h
> include SimpleTimer.h

>  ADC_MODE(ADC_VCC);
>  int Led = 5 ;

>  char auth[] = "";
> WidgetLED led1(V2);
> SimpleTimer timer;  
>   
>   void setup() {                  
>   pinMode (Led, OUTPUT) ; 
>   digitalWrite (Led, HIGH);
>   Blynk.begin(auth, );
>  }

>  void loop() {
>    Blynk.run();
>     }
>   BLYNK_READ(V1)
> {
> Blynk.virtualWrite(1, ESP.getVcc());
> }
>   BLYNK_WRITE(V3)
> {
>  int pinValue = param.asInt();
>   if (pinValue==1) 
>  {
>   digitalWrite (Led, HIGH);
>   } else {
>   digitalWrite (Led, LOW);  
>   }
>  {
>  if (digitalRead(Led)) {
>     led1.on();
>     } else {
>       led1.off();
>   }
>   }
>   }

Edit your post and format your code like this:

```cpp
CODE
```

First, I would recommend changing your voltage read to a timer with these changes/additions to your code:

SimpleTimer timerVCC;  // Setup voltage reading timer with legible name

void setup() {
// Add this to your setup void
  timerVCC.setInterval(5000L, espVCC); // Run voltage timer every 5 seconds.
}

void loop() 
{
// Add this to your loop void
  timertimerVCC.run();
}

// And change existing BLYNK_READ(V1) function to this void function.
void espVCC()
{
  Blynk.virtualWrite(V1, ESP.getVcc()); // V1 is a display widget of some sort.
}

And a simpler way with the virtual LED is:

BLYNK_WRITE(V3)
{
  int pinValue = param.asInt();
  if (pinValue == 1)
  {
    digitalWrite (Led, HIGH);
    led1.on();
  } else
  {
    digitalWrite (Led, LOW);
    led1.off();
  }
}

This is all untested as I do not have a ESP8266 device and didn’t bother loading the libraries in order to compile.

Hi @Gunner,

thank you for your help. I’ve modified the sketch but it seems that the timer interfers with BLYNK_WRITE : I can have readings of espVCC but if I push on the button (V3) on the app three is no action on the physical pin (led connected to pin 5 on ESP8266_07).
Conversely, with the sketch I’ve posted I can drive the led but sometimes the espVCC reading on the app stops.

It is either their timer or one well tested with Blynk… but implementation can make a difference. As I said, it was just an untested quick n dirty example… sorry :slight_smile:

Change the button widget to switch mode and try again. I find there can be cases for a bounce effect (most likely due to timing in the link?). flags and additional code arrangements can resolve that, but I just kept it simple as for an on/off switch.

Hi @Gunner,

it seems I’ve improved stability changing frequency from 1 to 5 sec.

Thank you for your help.

Following my final sketch (I hope useful for community) :wink:

> // ESP8266-07 Standalone
> //
> // Esp voltage reading to Blynk
> // Pin physical drive on ESP8266
> // Low voltage setting for Deep Sleep
> // 
> // Deep sleep for 600 sec (GPIO16 connected to RST) 
> //
> // Arduino IDE parameters : 
> // - NodeMCU 0.9 (ESP-12 Module)
> // - 80 MHz
> // - Serial
> // - 115200
> // - 4M (3M SPIFFS) 
> //
> // Virtual PIN
> // V1 : EspVCC readings
> // V2 : Led physical pin status On/Off
> // V3 : Slide button for physical pin
> // V5 : Led for deep sleep mode status
> // V4 : Slider for deep sleep Vcc low limit value setting

>  
> // #define BLYNK_PRINT Serial 
> include ESP8266WiFi.h
> include BlynkSimpleEsp8266.h
> include SimpleTimer.h

> ADC_MODE(ADC_VCC);
> int Led = 5 ;
> int SliderValue;

> char auth[] = "";

> WidgetLED led1(V2);
> WidgetLED led2(V5);

> SimpleTimer timer;  

> // Keep this flag not to re-sync on every reconnection
> bool isFirstConnect = true;
> // This function will run every time Blynk connection is established
> BLYNK_CONNECTED() {
> if (isFirstConnect) {
> Blynk.syncAll();
> isFirstConnect = false;
> }

> led1.off(); 
> led2.off();
> }

>  
>  void setup() {                
>  led2.off();
>  // Serial.begin(115200);
>  
>  pinMode (Led, OUTPUT) ; 
>  digitalWrite (Led, LOW);

>  Blynk.begin(auth, "");
>  timer.setInterval(5000L, ReadSensor);
>  Blynk.notify("ESP8266-BLYNK Ready");
>  }
>  
>  
>   void loop() {
>    Blynk.run();
>    timer.run();
>   }


> //Function to read Esp voltage and check deep sleep low voltage value (to safe battery!!)
> void ReadSensor() {  
> Blynk.virtualWrite(V1, ESP.getVcc());

> // Serial.println(ESP.getVcc());
> // Serial.println(SliderValue);

> if (ESP.getVcc() < SliderValue)
> {
> led2.on();
> Blynk.notify("Deep Sleep");
> ESP.deepSleep(600000000);
> delay(100);
> }
> }


> //Switch button to drive the physical pin
> BLYNK_WRITE(V3)
> {
> int pinValue = param.asInt();
> if (pinValue==1) 
> {
> digitalWrite (Led, HIGH);
> led1.on();
> } else {
> digitalWrite (Led, LOW);
> led1.off(); 
> }
> }

> //Slider change values from 3000 to 3700 from Widget Slider
>  BLYNK_WRITE(V4)
>  {   
>    SliderValue = map(param.asInt(), 0, 1023, 3000, 3700);    
>  }