Problem with Blynk.virtualWrite(Vx, value)

This text will be hiddenHi, Blyk Team!
In my project sometime happens disconnection from blynk_cloud.
Below I show the simlified fragment of real code of project and output of terminal:

```
#define BLYNK_PRINT Serial

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

WidgetLED led1(V1); //shows state of GPIO5  of esp8266
WidgetLED led2(V2); //shows state of GPIO4  of esp8266
WidgetLED led3(V3); //shows state of GPIO12 of esp8266
WidgetLED led4(V4); //shows state of GPIO13 of esp8266

//Virtual pin V5 of Blynk App is linked with widget Value Display
//which is shows value of parameter state

//Virtual pin V0 of Blynk App is shows the current level
//of WiFi signal

SimpleTimer  timer;

char ssid[] = "klg";
char pass[] = "klgklgklg";
char auth[] = "5c774b8297a74ca6a06058c0e6c6b5ba";
const int      outputs_esp8266[4]        = {5, 4, 12, 13};
unsigned int   state = 0;  //argument of operator: switch (state)
int            rssi;

void setup() 
{
  Serial.begin(115200);
  WiFiManager wifi;

  //taking care of wifi connection
  wifi.autoConnect("Blynk");

  //config blynk
  Blynk.config(auth);
  while (Blynk.connect() == false)
  {
    ; // Wait until connected
  }
  Serial.println("Blynk connected");  
  timer.setInterval(1000L, _main);
  for (int i = 0; i < 4; i++)
  {
    pinMode(outputs_esp8266[i], OUTPUT);
    digitalWrite(outputs_esp8266[i], LOW);  //HIGH
    delay(10);
  }
}

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

void _main()
{
  rssi = WiFi.RSSI();  
  Blynk.virtualWrite(V0, rssi);
  Blynk.virtualWrite(V5, state);
  switch (state)
  {
case 0:
      digitalWrite(outputs_esp8266[0], HIGH);
      Blynk.virtualWrite(V1, 200);
      state=1;
      break;
case 1:
      digitalWrite(outputs_esp8266[1], HIGH);
      Blynk.virtualWrite(V2, 200);
      state=2;
      break;
case 2:
      digitalWrite(outputs_esp8266[2], HIGH);
      Blynk.virtualWrite(V3, 200);
      state=3;
      break;
case 3:
      digitalWrite(outputs_esp8266[3], HIGH);
      Blynk.virtualWrite(V4, 200);
      state=4;
      break;
case 4:
      digitalWrite(outputs_esp8266[0], LOW);
      Blynk.virtualWrite(V1, 0);
      state=5;
      break;
case 5:
      digitalWrite(outputs_esp8266[1], LOW);
      Blynk.virtualWrite(V2, 0);
      state=6;
      break;
case 6:
      digitalWrite(outputs_esp8266[2], LOW);
      Blynk.virtualWrite(V3, 0);
      state=7;
      break;
case 7:
      digitalWrite(outputs_esp8266[3], LOW);
      Blynk.virtualWrite(V4, 0);
      state = 0;
      break;
  }
}
```
Output of Terminal:
```
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Using last saved values, should be faster
*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.1.90
[3131] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.1 on NodeMCU

[5001] Connecting to blynk-cloud.com:8442
[5081] Ready (ping: 0ms).
[10082] Login timeout
[10082] Connecting to blynk-cloud.com:8442
[10173] Ready (ping: 1ms).
Blynk connected
[16221] Connecting to blynk-cloud.com:8442
[16304] Ready (ping: 0ms).
[23260] Connecting to blynk-cloud.com:8442
[23344] Ready (ping: 1ms).
[82260] Connecting to blynk-cloud.com:8442
[82361] Ready (ping: 1ms).
[88258] Connecting to blynk-cloud.com:8442
[88345] Ready (ping: 1ms).
[95371] Connecting to blynk-cloud.com:8442
[95458] Ready (ping: 2ms).
[146218] Connecting to blynk-cloud.com:8442
[146433] Ready (ping: 1ms).
[152331] Connecting to blynk-cloud.com:8442
[152416] Ready (ping: 1ms).
[157417] Login timeout
[157418] Connecting to blynk-cloud.com:8442
[157504] Ready (ping: 1ms).
[163488] Connecting to blynk-cloud.com:8442
[163578] Ready (ping: 1ms).
[168582] Login timeout
[168582] Connecting to blynk-cloud.com:8442
[168663] Ready (ping: 0ms).

```
As result disconnection on Blynk App customer sees not adequate state of device although device realy works O'k.   
I dont think that disconnetion is as result of bad code in this case.
But disconnectiom may be for other reasons - failures of network, load of network, and many others.
Now my questions:
1. Have the errors my code?
2. How can be verify that  Blynk.virtualWrite(V3, 0); done succssesful or not? If not then how can be avoid this in code of program?
Thank You.

@klg please read [README] Welcome to Blynk Community! and edit your post accordingly so we can see the sketch correctly formatted and so the invisible include statements become visible etc.

1 Like

Hello. You have 2 connections at same time?

Do You mean:

  • the first - wifi.autoConnect(“Blynk”);
  • the second - Blynk.config(auth); ?

Yeap. Is that necessary?

Perhaps this is a mistake. Now I will try to fix it.

This is not mistake however ESP not very good at handling 2 connections as I know. I could be wrong :slight_smile:.

I commented Blynk.config(auth); and in result got:

*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Using last saved values, should be faster
*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.1.90
[5001] Connecting to 0.0.0.0
[11194] Connecting to 0.0.0.0
[17444] Connecting to 0.0.0.0
[23718] Connecting to 0.0.0.0
[29968] Connecting to 0.0.0.0
[36242] Connecting to 0.0.0.0
[42492] Connecting to 0.0.0.0
[48742] Connecting to 0.0.0.0
[54991] Connecting to 0.0.0.0
[61266] Connecting to 0.0.0.0
[67515] Connecting to 0.0.0.0
[73790] Connecting to 0.0.0.0

It means that device cannot to connect to Blynk cloud. Is’nt it?

I mean

WiFiManager wifi;

and not

Blynk.config(auth)

Сan not remove WiFiManager wifi;, because this means, that wifi is variable with type WiFiManager (in code is: wifi.autoConnect(“Blynk”);).
I think that wifi.autoConnect(“Blynk”); is connection to the WiFi network and Blynk cloud.
Blynk.config(auth); is connection to the Blynk App.
Therefore in my sketch aren’t two connection.

Can anyone help me? What is not right in my code?
Thank You.

I compiled it on D1 mini and it is working. At first it disconnected continuously when I opened Serial Monitor and reset it manually it stopped disconnecting.
I think, no problem with your code.

Same here, all fine on a D1 Mini. LED’s responding and WiFi signal strength showing.

Do you have frequency widgets set as PUSH?

What ESP do you have and does it have a decent power source?

What means a frequence widget?
In this fragment I use 5 vidgets: 4 leds(V1-V4) and Display value (V0)
I use NodeMCU v3 (ESP8266-12E) which powered from external power sorce 12VDC 500mA.

Some widgets (like Value Display) have an update / refresh rate, marked as READING FREQUENCY in the app. By default these widgets are set to refresh every 250ms. Most experienced Blynkers disable the refresh rate by setting the frequency to PUSH i.e. controlled by the code in your sketch rather than the app / server.

In my case with 5 widgets only widget Display value have parameter Reading Frequency. I setted 3sec. But if I set Push nothing changes.

As variant can be to remove blynk.virtualWrite(Vx, value) from switch() and to call all these blynk.virtualWrite after switch(). Even if will be disconnection, then after when connection will restored again, blynk.virtualWrite(Vx, value) will transmits the correct data for the Blynk widgets. But in this case will increases traffic.

I strongly suggest you leave at PUSH, not 3s or 250ms.

1 Like

Thanks. I get this.
Wat You can say about my variant in my previous post?

Your code works fine without modification. You could try adding Blynk’s DEBUG and see if that highlights anything particular to your set up. Two Blynkers have your code running just fine.