How to Use Virtual Pins in Blynk

43,061

52

16

Posted

Introduction: How to Use Virtual Pins in Blynk

Hello Everyone. I was tinkering with the blynk app and wanted to find a way to use the virtual pins.. Here is what I came up with so far...
Essentially this project uses and LDR attached to adc pin to switch on a virtual LED in the blynk app as a notification. I am hoping that when further widgets and more features such as MQTT, Thingspeak, IFTTT are added, this can come in handy to make easy projects such as alarms, irrigation timers, home automation etc.

Step 1: Tools

Hardware:

ESP8266 development board. I am using ESP-12 with and LDR attached to ADC pin.

Software:

Arduino IDE with required libraries(blynk, esp8266com etc)

Here is a step- by step guide how to set up https://www.instructables.com/id/ESP8266-ESP-12Stan...

Blynk app

Step 2: Virtual Pins

Virtual pins are used to interface with libraries (Servo, LCD and others), and implement custom functionality.The device may send data to the widget on a virtual pin like this:

* Blynk.virtualWrite(pin, "abc");
* Blynk.virtualWrite(pin, 123);
* Blynk.virtualWrite(pin, 12.34);

Also, virtual pins can react to value updates and requests. For example, this function will be called every time App Widget requests data for Virtual Pin 5:
* BLYNK_READ(5) { // Usually, you will need to respond with a virtual pin value. Blynk.virtualWrite(5, some_value); }

This function will be called every time App Widget writes value to Virtual Pin 1:
* BLYNK_WRITE(1) { BLYNK_LOG("Got a value: %s", param.asStr()); // You can also use: param.asInt() and param.asDouble() }

BLYNK_READ/BLYNK_WRITE functions are effectively "getters/setters" of the Virtual Pins if you're familiar with this concept in other programming languages. Please also take into account that these functions should take minimal time to execute, so avoid using sleep/delay inside of them.
N.B: taken from readme doc https://github.com/blynkkk/blynk-library/blob/mast...

Essentially i wanted to make an example to illustrate the BASIC use of the Blynk.virtualWrite() command
I made a sketch in Arduino:
the value of adc pin is read and when value <100, a HIGH is written to virtual pin 5.
the magic then happens in the blynk app where I can also visualise the adc analog value and light an LED when ldr analog value <100

Step 3: Results

As you can see in the pictures:
when LDR value is >100 the Burglar LED pin is switched off
When I shine a light on LDR, ldrvalue<100 LED is swtiched on

This is just a proof of concept.. With more tinkering I guess this can come in handy..

2 People Made This Project!

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
  • Gluten Free Challenge

    Gluten Free Challenge
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

11 Comments

This code is wrong. It doesn't work. Please correct it like this. I wrote this program to door sensor. Thanx saurabh47 for correct code. Below code is working.

int doorSensor = 16;

void setup(){

Serial.begin(9600);

Blynk.begin(auth, "Gayan", "xxxxxxxxxx");

}

void loop() {

int val=digitalRead(doorSensor);

if(val==HIGH){

Blynk.virtualWrite(V2, 255); //LED 10 "HIGH"

}

else {

Blynk.virtualWrite(V2, 0); //LED "LOW"

}

Blynk.run();

}

}

Can you post full working code?

It doesn't works i have usb connectivity only.please give any solution .i just want led indication. i have connected led to digital pin 6.and pin 6 also connected to analog pin a0.i have using same code just editing that
analog=anlogRead(irec)
if(anlog= HIGH)
{
VirtualWrite(5, HIGH);
}
else
{
virtualwrite(5, LOW);
}
when program is running then led is on /off by Blynk it doesn't show indication of led on blynk.

You are right it doesn't work ! Fix it please

char auth[] = "auth token";
int prev = -1;
int B1V;
int BUTTON1_P = 6;
void setup()
{
SwSerial.begin(9600);
Blynk.begin(auth);

// Default baud rate is 9600. You could specify it like this:
//Blynk.begin(auth, 57600);
}

void loop()
{
Blynk.run();
B1V = digitalRead(BUTTON1_P);
if (B1V != prev) {
if (B1V) {
Blynk.virtualWrite(V2, 255);
} else {
Blynk.virtualWrite(V2, 0);
}
}

}

this is pragram realy works .
here digital pin is 6.and virtual pin is v2.

I can't understand, did you manage to solve the problem?

yes i am able to solve

how about your setup in blynk app? did you link 5 to led indicator in app?virtual pin

yes, i link led to virtual pin 5

Thanks, hope it was helpful