Send and receive data from device
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
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:
You can also get the RAW data from the param buffer:
Send data in various formats to Virtual Pins.
// Send string
// Send integer
// Send float
// Send multiple values as an array
Blynk.virtualWrite(pin, "hello", 123, 12.34);
// Send RAW data
Blynk.virtualWriteBinary(pin, buffer, length);
virtualWriteattempts to send the value to the network immediately.
Note: For virtual pins with numbers > 127, the
V128syntax is not available. Please use plain virtual pin number, for example:
BLYNK_WRITEis a function called every time device gets an update of Virtual Pin value from the server (or app):
To read the received data use:
int value = param.asInt(); // Get value as integer
// The param can contain multiple values, in such case:
int x = param.asInt();
int y = param.asInt();
BLYNK_WRITEcan't be used inside of any loop or function. It's a standalone function.
Note: For virtual pins with numbers > 127, please use
Redefines the handler for all pins that are not covered by custom
int pin = request.pin; // Which exactly pin is handled?
int value = param.asInt(); // Use param as usual.
Blynk.virtualWrite()is called, server stores the provided value along with a timestamp. If you make several consecutive commands, timestamps will be different for every datapoint. If you'd need to assign a single timestamp for a batch of values, you can group them together:
This approach can improve charts rendering and CSV reports when many datapoints should have similar timestamp
Blynk.beginGroup()will determine the timestamp for the commands that follow. Alternatively, you can specify the timestamp explicitly (using
uint64_t ts = (uint64_t)UTC.now() * 1000 + UTC.ms(LAST_READ);
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.
This command updates individual Virtual Pin to the latest stored value on the server. When it's used, a corresponding
BLYNK_WRITEhandler is called.
To update multiple pins, use:
Blynk.syncVirtual(V0, V1, V6, V9, V16);