[SOLVED] VirtualWrite doesn't work :)

Hello EveryOne!

I’m using EPS8266 on WeMos D1 Mini Pro and trying to push values from my hardware to the Windget. Both ESP and my Smartphone are in the same network but behind strong NAT.
Widget at the smartphone can see wether ESP with Blynk works or not. That is good. I can send notifications from the widget to ESP with buttons. That is also good. I can request values from virtual pins from ESP by the widget. That is also good. But I barely can’t push values from ESP to thw widget. That is not good.

Connection is made via WiFi. Here is a number of code lines:
Blynk.config(auth);
Blynk.virtualWrite(11, 1); // Indicates that the display is turned on. V11 belongs to a SwitchButton. Doesn’t work
Blynk.virtualWrite(2, ppm_global); // V2 belongs to a value, set to push. Doesn’t work

Any suggestions are welcomed :slight_smile:

Are these lines consecutive in your sketch?
If they are try the regular while function after Blynk.config(auth) i.e. ensure you are connected to Blynk before trying to use virtualWrite.

Just use example sketches, provided with the library.

These two:
Blynk.config(auth);
Blynk.virtualWrite(11, 1);
are consecutive.

This one is inside a function:
Blynk.virtualWrite(2, ppm_global);

Sometimes it works, sometimes doesn’t.

I need to understand the reason why it sometimes works and sometimes doesn’t…

It should be connected to Blynk for sure. WiFi is connected and sketch posts data to ThingSpeak.

The examples are not the answer. The code was taken from them.

@Vladislav_Kravchenko I think what happens with your first virtualWrite is that is comes to quickly i.e. just before you are connected. So after config add:

while (Blynk.connect() == false)  {
// Wait until connected 
}

You probably have a bug in your code for the second virtualWrite as that is the main reason it doesn’t worl.
If you can’t find your bug you could post the function /sketch for others to take a look.

Also note that you use config, instead of begin… It’s a bit different.
It would be much easier if you post all your code.

You will laugth at me but this:
while (Blynk.connect() == false) {
Blynk.virtualWrite(11, 1); // Indicates that the display is turned on
}

Doesn’t work. The button remain unpressed at the widget :slight_smile: But code continued to work.

The overall code is huge and in several files. It includes OTA, WebServer, AdaFruit_GFX, AdaFruit_SSD1306, WiFiManager, SoftwareSerial libraries. I don’t think that it is good to post it here. I think we have a good example with
while (Blynk.connect() == false) {
Blynk.virtualWrite(11, 1); // Indicates that the display is turned on
}

That is located directly in Setup() routine after Blynk.config(auth);

Here is a clip from this code:
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
mySerial.begin(9600);
WiFiManager wifiManager;
wifiManager.autoConnect(“AutoConnectAP”);
getOTA();
getWEBServer();
Blynk.config(auth);
digitalWrite(ledPin, HIGH);
Serial.println(“Let us go…”);
delay(3000);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48)
while (Blynk.connect() == false) {
Blynk.virtualWrite(11, 1); // Indicates that the display is turned on
}
display.display();
delay(2000);
}

But this code:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48)
while (Blynk.connect() == false) {
delay(100);
}
Blynk.virtualWrite(11, 1); // Indicates that the display is turned on

Works. The button took its value :slight_smile:

according to your logic, you’re sending while disconnected.
Just read your code again.

Also, please use “V11” when referencing virtual pins.

Here is the answer:
Just after Blynk.begin(…) or Blynk.config(…), Blynk is not yet connected to the server. It will try to connect when it reaches first Blynk.run() or Blynk.connect()call.

But I didn’t get the difference between begin and config. Could you please point me to the right manual page?

Yes I will laugh at you, the code I provided didn’t include the virtualWrite inside the while loop.

Yes, it’s described in docs.
Anyway - world is changing too fast.
As I saw this thread, I decided to fix this issue - https://github.com/blynkkk/blynk-library/issues/154

On master branch, begin() is basically doing this:

  1. Connects to network (WiFi, Ethernet, …)
  2. Calls Blynk.config(...) - sets auth token, server address
  3. Connects to the server (blocks until connected)

Now, docs will have to be changed with next release.
Hope that helps :wink:

1 Like

Thanks! Now it is clear.