Get value of Virutal Pin when connected with Blynk

Hello everyone, I’m using ESP-NOW beside Blynk. I only spend 5 seconds to connect with Blynk and 5 seconds to disconnect. So, I want to get value from Virutal pin (slider) and store it in EEPROM to send via ESP-NOW next seconds. I found BLYNK_WRITE() but it only print value when I changed it in Web or app. I also want to print this value when it unchanged. Does anyone know other functions to do that? Sorry about my English. Thank you very much.

If you add a function called BLYNK_CONNECTED() it will be triggered automatically each time that your device connects to Blynk.

You can then add Blynk.syncVirtual(vPin) commands inside that function, which tells the Blynk server to send the latest values for these virtual pins. This will cause the corresponding BLYNK_WRITE(vPin) function to be triggered.
Typical code might look like this…

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1); 
  Blynk.syncVirtual(V2); 
}

I’m not sure why you need to store the value in NVRAM, but you need to be very careful about how you use EEPROM.
If your board uses physical dedicated EEPROM then it normally has a lifespan of around 100,000 write operations, so if you were writing to EEPROM once every minute you callus expect to “burn out” that memory location after around 70 days.

Using LittleFS is a far better approach.

Pete.

1 Like

Thank you very much. And what code to print value in V1 to Serial Monitor. I tried this code but it have an error.

BLYNK_CONNECTED() {
    Blynk.syncVirtual(V0);
  float c = param.asInt();
  Serial.println(param.asInt());
  Serial.print("PWM : ");
   Serial.println(c);
}

@votanthanh2k3 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

1 Like

Take a look at your code compared to mine.

You appear to have moved some of the code from BLYNK_WRITE(V0) into your BLYNK_CONNECTED() function, which isn’t what I said you should do.

Pete.

1 Like

So what code to print value in V1 when I connected with Blynk and use function

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

Thank you for you help.

The code you had is fine…

but it belongs in BLYNK_WRITE(V1) not in BLYNK_CONNECTED()

You obviously didn’t understand this part…

Pete.

1 Like