Virtual Pins
Virtual Pins is a way to exchange any data between your hardware and Blynk app. Think about Virtual Pins as channels for sending any data. Make sure you differentiate Virtual Pins from physical GPIO pins on your hardware. Virtual Pins have no physical representation.
Virtual Pins are commonly used to interface with other libraries (Servo, LCD and others) and implement custom logic. The device can send data to the App using Blynk.virtualWrite(pin, value) and receive data from the App using BLYNK_WRITE(vPIN). Read below

Virtual Pin data types

All Virtual Pin values are always sent as Strings and there are no practical limits on the data that can be sent. However, there are certain limitations on the hardware side when dealing with numbers. For example, the integer on Arduino is 16-bit, allowing range -32768 to 32767.
To interpret incoming data as Integers, Floats, Doubles and Strings use:
1
param.asInt();
2
param.asFloat();
3
param.asDouble();
4
param.asStr();
Copied!
You can also get the RAW data from the param buffer:
1
param.getBuffer()
2
param.getLength()
Copied!

Blynk.virtualWrite(vPin, value)

Use BlynkTimer when you use this command to send data. Otherwise your hardware may be disconnected from the server
Send data in various formats to Virtual Pins.
1
// Send string
2
Blynk.virtualWrite(pin, "abc");
3
4
// Send integer
5
Blynk.virtualWrite(pin, 123);
6
7
// Send float
8
Blynk.virtualWrite(pin, 12.34);
9
10
// Send multiple values as an array
11
Blynk.virtualWrite(pin, "hello", 123, 12.34);
12
13
// Send RAW data
14
Blynk.virtualWriteBinary(pin, buffer, length);
Copied!
Calling virtualWrite attempts to send the value to the network immediately.
Note: For virtual pins with numbers > 127, the V128 syntax is not available. Please use plain virtual pin number, for example:
1
Blynk.virtualWrite(128, "abc");
Copied!

BLYNK_WRITE(vPIN)

BLYNK_WRITE is a function called every time device gets an update of Virtual Pin value from the server (or app):
To read the received data use:
1
BLYNK_WRITE(V0)
2
{
3
int value = param.asInt(); // Get value as integer
4
5
// The param can contain multiple values, in such case:
6
int x = param[0].asInt();
7
int y = param[1].asInt();
8
}
Copied!
BLYNK_WRITE can't be used inside of any loop or function. It's a standalone function.
Note: For virtual pins with numbers > 127, please use BLYNK_WRITE_DEFAULT() API

BLYNK_WRITE_DEFAULT()

Redefines the handler for all pins that are not covered by custom BLYNK_WRITE functions.
1
BLYNK_WRITE_DEFAULT()
2
{
3
int pin = request.pin; // Which exactly pin is handled?
4
int value = param.asInt(); // Use param as usual.
5
}
Copied!

Blynk.syncAll()

Requests all stored on the server latest values for all widgets. All analog/digital/virtual pin values and states will be set to the latest stored value. Every virtual pin will generate BLYNK_WRITE() event.
1
BLYNK_CONNECTED() {
2
Blynk.syncAll();
3
}
Copied!

Blynk.syncVirtual(vPin)

This command updates individual Virtual Pin to the latest stored value on the server. When it's used, a corresponding BLYNK_WRITE handler is called.
1
Blynk.syncVirtual(V0);
Copied!
To update multiple pins, use:
1
Blynk.syncVirtual(V0, V1, V6, V9, V16);
Copied!

Blynk.setProperty(vPin, "property", value)

This command allows changing widget properties
Last modified 3mo ago