Virtual pin reading

Hallo,
I have conected button in app to V1pin.
In ARDUINO code is super easy to use the command " Blynk.virtualWrite(pin,value) ".

How can I read virtual pin? It would be more simple to use something like :" Blynk.virtualRead(pin) "

I need read value from app V1pin. In ARDUINO I need save this value (button pushed “HIGH”) to the “int pin= HIGH/LOW”.

Thank you for your support

This is how you can read data from virtual pin. For button you need to :

param.asInt() 

And you’ll get 1 or 0 (HIGH/LOW).

Im so sory, Im new here.
I was read some topic about that, but i dont understant it.
I can show you my test code as an example…

MY CODE:

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2,3); // RX, TX
 #define BLYNK_DEBUG
 #define BLYNK_PRINT SwSerial
 #include <BlynkSimpleSerial.h>
char auth[] = "171b6854e43146fxxxxxxxxxxxxxxx";
void setup() {
  
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Serial.begin(9600);
  SwSerial.begin(9600);
  Blynk.begin(auth);
  Serial.println("READY");
}

void loop() {


/*   WHAT I HAVE TO WRITE HERE if I want to read pin number V1 ?????
 I need:  
        if "virtual pin V1" =HIGH {
          pin_activated()
          }
          if "virtual pin V1" =LOW {
          pin_deactivated()
          } 
*/
 Blynk.run();
}

void pin_activated(){
    Blynk.virtualWrite(1, HIGH);  //LED 10 "HIGH"
    Blynk.virtualWrite(2, LOW);   //LED 11 "LOW"
    digitalWrite(13, LOW);        //relay is on  
    }

void pin_deactivated(){
    Blynk.virtualWrite(2, HIGH);   //LED "HIGH"
    Blynk.virtualWrite(1, LOW);    //LED "LOW"
    digitalWrite(13, HIGH);        //relay is off
    }
1 Like

@milos

BLYNK_WRITE(1) 
{
    if (param.asInt()) {
        //HIGH
        pin_activated();
    } else {
       //LOW
       pin_deactivated();
    }
}

void loop() {
...
}
3 Likes

@milos
Go to sketch examples and there you’ll find GetData and PushData - check them to see how to work with Virtual Pins

I read some examples, but I dont uderstand it.
I simplify my code and tried it again with part of your code. If i tried to compile my code IDE write me:

BLYNK_USB_button.ino: In function ‘void BlynkWidgetWrite1(BlynkReq&, const BlynkParam&)’:
BLYNK_USB_button.ino:20:20: error: cannot convert ‘BlynkParam::asInt’ from type ‘int (BlynkParam::)()const’ to type ‘bool’
compilation error.

Please check it:

 *#include <SoftwareSerial.h>*
 *SoftwareSerial SwSerial(2,3); // RX, TX*

 *#define BLYNK_DEBUG*
 *#define BLYNK_PRINT SwSerial*
 *#include <BlynkSimpleSerial.h>*

*char auth[] = "171b6854e43146f68a35cxxxxxxxxxxx";*

*void setup(){*
  *SwSerial.begin(9600);*
  *Blynk.begin(auth);*
  *pinMode (13,OUTPUT);*
*}*

*BLYNK_WRITE(1)* 
*{   if (param.asInt) {*        
        *pin_activated();    //HIGH*
    *} else {*
       *pin_deactivated(); //LOW*
    *}*
*}*
*void pin_activated(){digitalWrite(13, LOW);}*

*void pin_deactivated(){digitalWrite(13, HIGH);}*

*void loop()*
*{*
  *Blynk.run();*
  *delay(1000);*
*}*
  1. :exclamation:Read the posting rules and use code formatting so that it’s readable. I did it for you this time, but next time we will ignore such posts.
  2. You should be more attentive when coding and always double check your code :wink:

GetData Example clearly shows how to get data from Virtual Pin:

BLYNK_WRITE(1)
{
  BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use: asInt() and asDouble()
}

while yours is:

Spot the difference? :wink:

BTW, there is no need in complicating code so much:

BLYNK_WRITE(1){
    if (param.asInt()){       
        digitalWrite(pin, HIGH);
    } else {
        digitalWrite(pin, LOW);
    }
}

Also, avoid using delay() function, especially in void loop(). It can affect connection to Blynk Cloud and cause other errors. Read carefully instructions in SendData Example to see how to work with timers and avoid delays.

@milos

param.asInt

should be

param.asInt()

Thank you very much for your advice. It was helpful !!!

Wundered why the LED widget differs from other presentation widgest ,like value / graph.
e.g. with request or push.

1 Like

@knooien Does your question relate to this thread?

Think so,

Maybe just like a value widget:
e.g. V0 assigned to a LED.

request code:
BLYNK_READ(0)
{
int status = get_some_status();
Blynk.virtualWrite(0, status);
}

push code:
some_interrupt()
{
int status = get_some_status();
Blynk.virtualWrite(0, status);
}

A post was split to a new topic: Read from multiple virtual pins?