How to Use Virtual Pins in Blynk

71,566

56

14

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

  • Make it Glow Contest

    Make it Glow Contest
  • First Time Author Contest

    First Time Author Contest
  • PCB Challenge

    PCB Challenge

14 Discussions

0
chuvlx
chuvlx

Question 6 weeks ago on Introduction

Hello,
I am working on a door sensor in python and I am trying to figure out how to read the pin values into blynk. Here is my code:
import time
import blynklib
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)
auth_token = '4JR18qzs1AWkedrhF65mjwuASpBMpeGs'
# Initialize Blynk
blynk = blynklib.Blynk(auth_token)
@blynk.handle_event('read V2')
def read_virtual_pin_handler(pin):
p_val = sensor()
print(p_val)
a = blynk.virtual_write(vpin, p_val)
def sensor():
val1 = int(GPIO.input(8))
time.sleep(0.01)
val2 = int(GPIO.input(8))
if val1 and not val2:
#door is closed
return 0
elif not val1 and val2:
#door is open
return 1
try:
while True:
blynk.run()
except KeyboardInterrupt:
print("Quit")
0
kitty-visser
kitty-visser

Question 5 months ago on Step 3

can you also set a digital pin value with a virtual pin? Mine stays low after doing that.

0
roshantha08
roshantha08

2 years ago

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();

}

}

0
John Trzon
John Trzon

5 years ago

Can you post full working code?

0
saurabh47
saurabh47

5 years ago

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.

0
John Trzon
John Trzon

Reply 5 years ago

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

0
saurabh47
saurabh47

Reply 5 years ago

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.

0
Rahul27
Rahul27

Reply 5 years ago

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

0
saurabh47
saurabh47

Reply 5 years ago

yes i am able to solve

0
Rahul27
Rahul27

5 years ago

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

0
saurabh47
saurabh47

Reply 5 years ago

yes, i link led to virtual pin 5

0
Rahul27
Rahul27

5 years ago

Thanks, hope it was helpful