Reading a Blynk Virtual Pin

Hi,

I know this isn't a Blynk forum but I find the stuff on there utterly unreadable & that the forum isn't very helpful (unlike this one thankfully!)

Could someone kindly show me an example of code on how to read the digital state of (for example) virtual pin V7?

Many thanks,

Mick.

The Blynk nomenclature is kind of confusing. When you want to remotely monitor the state of a widget on your phone (like a button) with your Arduino sketch, you use the callback function BLYNK_WRITE(vPIN)

int value;
BLYNK_WRITE(V7)
{
  value = param.asInt(); // Get value as integer
}

This callback gets called every time the widget changes state.

If you want to send a value from your Arduino code to a widget on your phone (like a Value Display), you call the function Blynk.virtualWrite(vPin, value)

Blynk.virtualWrite(V7, 123);

The Blynk nomenclature is kind of confusing.

No sh*t. Having the function that gets called when the widgets changes state called WRITE was not something that the brightest crayon in the box came up with. Having the WRITE() method do nothing but read? Who's dumb idea was that?

gfvalvo:
The Blynk nomenclature is kind of confusing. When you want to remotely monitor the state of a widget on your phone (like a button) with your Arduino sketch, you use the callback function BLYNK_WRITE(vPIN)

int value;

BLYNK_WRITE(V7)
{
  value = param.asInt(); // Get value as integer
}



This callback gets called every time the widget changes state.

If you want to send a value from your Arduino code to a widget on your phone (like a Value Display), you call the function [Blynk.virtualWrite(vPin, value)](http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynkvirtualwritevpin-value)


Blynk.virtualWrite(V7, 123);

Thanks very much.

Once that code is written can I then use V7 in the same manner I would a digital input?

mickjbriggs:
Once that code is written can I then use V7 in the same manner I would a digital input?

No, you only use them in the manner I showed. One other subtlety is the way display widgets like "Value Display" and "Labeled Value" behave. When you add them to your phone, you set them up to either update periodically (the period can range from seconds to hours) or under control of your Arduino code (Push).

If set up for Push Mode, then you call the Blynk.virtualWrite() function in your code like I showed as often as you want (within reason).

However, if you set periodic updates in the Bylnk phone app, then you have to add the BLYNK_READ() callback function and call Blynk.virtualWrite() from within it. That's described here: Documentation for Blynk, the most popular IoT platform for businesses.

Update:
Like other terminology in the Blynk library, the term Virtual Pin a kind of misleading. These are really more like data pipes between the App Widgets on your phone and your Arduino code.

All that being said, once you get past the vocabulary's idiosyncrasies, the library does work very well.

Thanks for your replies gfvalvo, I really appreciate it.

I'm not getting this I'm afraid, I want to set the state of "overRide" to be true when a button on my phone changes the state of V7 from 0 to 1.

I can follow your code instruction but if i can't use V7 like I would a digital input how can I use it to activate "overRide"?

I can follow your code instruction but if i can't use V7 like I would a digital input how can I use it to activate "overRide"?

You CAN implement the BLYNK_WRITE(V7) method. Store the return value from the param.asInt() call into overRide.

loop() won't care how overRide got a value. It only cares about what the value is.

Sorry to go on folks but I can't get this to work.

When I try to compile the code as written above (having created an integer for "value" before Void Setup() it seems to be objecting to the lack of a semi-colon after BLYNK_WRITE(V7) giving the error " a function-definition is not allowed here before "{" token"

Post your complete code and complete error message.

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // control the LCD via Arduino pins 12, 11, 5, 4, 3 & 2
int timeState = 0; // ensure timer input is off on power up
int overRide = 0; // ensure the override input is off during power up
#include <ESP8266_Lib.h> // include the ESP8266 wifi chip library
#include <BlynkSimpleShieldEsp8266.h> include the Blinky wifi chip library
#include <TimeLib.h>
#include <WidgetRTC.h>
const int tempPin = A0; // create an integer called tempPin & asign it to Arduino analog input 1
int overRideValue;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "ghhjbjhlj"; // authorisation token from Bylnk app

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "!!!!!!!!"; // wifi network name
char pass[] = "!!!!!!!"; // wifi network password

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1 // remove initial // if communicating with wifi chip via hardwired serial

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h> // communicate with wifi chip via software serial
SoftwareSerial EspSerial(A4, A5); // communicate with wifi chip using Arduino pins A4 (Rx) & A5 (Tx)
#include<SPI.h> // include library SPI, WidgetLED commands below don't work without this library
WidgetLED led1(V2); // asign led1 to Blynk virtual pin 2
WidgetLED led2(V3); // asign led2 to Blynk virtual pin 3

// Your ESP8266 baud rate:
#define ESP8266_BAUD 19200 // set wifi chip baud rate to 19,200

ESP8266 wifi(&EspSerial); // 

BlynkTimer timer;
WidgetRTC rtc;


void clockDisplay()
{
  // you can call hour(), minute(), ... at any time
  // please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();
  // Send time to the App
  Blynk.virtualWrite(V4, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V5, currentDate);
}
void blinkLedWidget() //
{
  if (digitalRead(7) == LOW) { // if digital output 7 is off
    led1.off(); // turn off led1 on Blynk app display
  } else {
    led1.on(); // if digital output 7 is on turn on led1 on the Blynk app display
  }
{ if (digitalRead(8) == LOW) { // if digital output 8 is off
      led2.off(); // turn off led2 on Blynk app display
    } else {
      led2.on(); // if digital output 8 is on  turn on led 2 on the Blynk app display
    }
  }
}
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, millis() / 1000); // write to the Blynk app display every second via virtual pin 1
}
void temperature()
{
  int tempRaw = analogRead(tempPin);
  float temp = (tempRaw / 1024.0) * 5.0;
  int Temperature = (temp - .5) * 100; // convert the floating value (3 deciaml places) to an integer (0 decimal places) for the Blynk app display
  Blynk.virtualWrite(V0, Temperature); // write the value of Temperature to the Blynk app via virtual pin 0
}


void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
  //Begin synchronizing time
  rtc.begin();
  Blynk.run();
  // Setup a function to be called every second
  timer.setInterval(1000L, clockDisplay);
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(6000L, temperature);
  timer.setInterval(1000L, blinkLedWidget);
  pinMode(6, INPUT); // Pin 6 is a spare digital input (was for timer)
  pinMode(7, OUTPUT); // Pin 7 is output for heat lamp 1
  pinMode(8, OUTPUT); // Pin 8 is output for heat lamp 2
  pinMode(9, INPUT); // Pin 9 is for the override switch
  lcd.begin(16, 2);
  
 
}

void loop()
{
  BLYNK_WRITE(V7)
  {
    overRideValue = param.asInt()
  }
  int tempRaw = analogRead(tempPin); // tempRaw is unprocessed value of the analog input
  float temp = (tempRaw / 1024.0) * 5.0;
  int Temperature = (temp - .5) * 100; // convert the float value (3 decimal places) to an integer (0 decimal Places) for the LCD
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  overRide = digitalRead(V7); // if the override switch is on
  if (overRide == HIGH) {
    lcd.setCursor(0, 0);
    lcd.print("System is in      "); // display this message on the LCD
    lcd.setCursor(0, 1);
    lcd.print("override!     ");
    digitalWrite(7, HIGH); // turn on heat lamp 1
    digitalWrite(8, HIGH); // turn on heat lamp 2
  }
  else { // if the the override swich is off run this script on both serial & LCD & execute the program
    lcd.setCursor(0, 0);
    lcd.print("Temperature is: ");
    lcd.setCursor(0, 1);
    lcd.print(Temperature);
    lcd.setCursor(2, 1);
    lcd.print(" Degrees");
    timeState = (hour() == 5 && minute() >= 30)  || (hour() >= 6 && hour() < 18);
    if (timeState == false) { // if it's night time
      digitalWrite(7, LOW); // Turn off heat lamp 1
      digitalWrite(8, LOW);
    }// Turn off heat lamp 2
    else { // if it's day time
      if (Temperature >= 30) { // if the temperatue is above 5 degrees
        digitalWrite(7, LOW); // turn off heat lamp 1
        digitalWrite(8, LOW); // turn off heat lamp 2
      }
      if (Temperature < 28) { // if the temperature is below 5 degrees
        digitalWrite(7, HIGH); // turn on heat lamp 1
        digitalWrite(8, LOW); // turn off heat lamp 2
      }
      if (Temperature < 27) { // if the temperature is below 1 degree
        digitalWrite(7, HIGH); // turn on heat lamp 1
        digitalWrite(8, HIGH); // turn on heat lamp 2
      }
    }


  }
}

Arduino: 1.8.5 (Mac OS X), Board: “Arduino/Genuino Uno”

exit status 1
a function-definition is not allowed here before ‘{’ token

The C / C++ language does not allow nested functions. You're trying to define BLYNK_WRITE() inside of loop().

void loop()
{
  BLYNK_WRITE(V7)
  {
    overRideValue = param.asInt()
  }
  int tempRaw = analogRead(tempPin); // tempRaw is unprocessed value of the analog input
  float temp = (tempRaw / 1024.0) * 5.0;
.
.
.
.
.
}

The structure should be:

void loop() {
.
.
.
.
.
.
.
.
}

BLYNK_WRITE(V7)  {
    overRideValue = param.asInt()
}

Thank you very much.

Sorry about this but I could use some more help,

The code below now compiles but setting the state of V7 does not operate the overRide function.

Would some kind soul explain why this doesn’t work as I’m pulling my hair out!

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // control the LCD via Arduino pins 12, 11, 5, 4, 3 & 2
int timeState = 0; // ensure timer input is off on power up
int overRide = 0; // ensure the override input is off during power up
#include <ESP8266_Lib.h> // include the ESP8266 wifi chip library
#include <BlynkSimpleShieldEsp8266.h> include the Blinky wifi chip library
#include <TimeLib.h>
#include <WidgetRTC.h>
const int tempPin = A0; // create an integer called tempPin & asign it to Arduino analog input 1
int overRideValue;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "!!!!!"; // authorisation token from Bylnk app

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "!!!!!"; // wifi network name
char pass[] = "!!!!"; // wifi network password

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1 // remove initial // if communicating with wifi chip via hardwired serial

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h> // communicate with wifi chip via software serial
SoftwareSerial EspSerial(A4, A5); // communicate with wifi chip using Arduino pins A4 (Rx) & A5 (Tx)
#include<SPI.h> // include library SPI, WidgetLED commands below don't work without this library
WidgetLED led1(V2); // asign led1 to Blynk virtual pin 2
WidgetLED led2(V3); // asign led2 to Blynk virtual pin 3

// Your ESP8266 baud rate:
#define ESP8266_BAUD 19200 // set wifi chip baud rate to 19,200

ESP8266 wifi(&EspSerial); 

BlynkTimer timer;
WidgetRTC rtc;

void clockDisplay()
{
  // you can call hour(), minute(), ... at any time
  // please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();
  // Send time to the App
  Blynk.virtualWrite(V4, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V5, currentDate);
}
void blinkLedWidget() //
{
  if (digitalRead(7) == LOW) { // if digital output 7 is off
    led1.off(); // turn off led1 on Blynk app display
  } else {
    led1.on(); // if digital output 7 is on turn on led1 on the Blynk app display
  }
{ if (digitalRead(8) == LOW) { // if digital output 8 is off
      led2.off(); // turn off led2 on Blynk app display
    } else {
      led2.on(); // if digital output 8 is on  turn on led 2 on the Blynk app display
    }
  }
}
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, millis() / 1000); // write to the Blynk app display every second via virtual pin 1
}
void temperature()
{
  int tempRaw = analogRead(tempPin);
  float temp = (tempRaw / 1024.0) * 5.0;
  int Temperature = (temp - .5) * 100; // convert the floating value (3 deciaml places) to an integer (0 decimal places) for the Blynk app display
  Blynk.virtualWrite(V0, Temperature); // write the value of Temperature to the Blynk app via virtual pin 0
}




void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
  //Begin synchronizing time
  rtc.begin();
  Blynk.run();
  // Setup a function to be called every second
  timer.setInterval(1000L, clockDisplay);
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(6000L, temperature);
  timer.setInterval(1000L, blinkLedWidget);
  pinMode(6, INPUT); // Pin 6 is a spare digital input (was for timer)
  pinMode(7, OUTPUT); // Pin 7 is output for heat lamp 1
  pinMode(8, OUTPUT); // Pin 8 is output for heat lamp 2
  pinMode(9, INPUT); // Pin 9 is for the override switch
  lcd.begin(16, 2);
}

BLYNK_WRITE(V7)
{
  overRideValue = param.asInt();
  if (param.asInt() == 1) {
    overRide == HIGH;
  } else {
    overRide == LOW;
  }
}


void loop()
{
  int tempRaw = analogRead(tempPin); // tempRaw is unprocessed value of the analog input
  float temp = (tempRaw / 1024.0) * 5.0;
  int Temperature = (temp - .5) * 100; // convert the float value (3 decimal places) to an integer (0 decimal Places) for the LCD
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  //overRide = digitalRead(V7); // if the override switch is on
  if (overRide == HIGH) {
    lcd.setCursor(0, 0);
    lcd.print("System is in      "); // display this message on the LCD
    lcd.setCursor(0, 1);
    lcd.print("override!     ");
    digitalWrite(7, HIGH); // turn on heat lamp 1
    digitalWrite(8, HIGH); // turn on heat lamp 2
  }
  else { // if the the override swich is off run this script on both serial & LCD & execute the program
    lcd.setCursor(0, 0);
    lcd.print("Temperature is: ");
    lcd.setCursor(0, 1);
    lcd.print(Temperature);
    lcd.setCursor(2, 1);
    lcd.print(" Degrees");
    timeState = (hour() == 5 && minute() >= 30)  || (hour() >= 6 && hour() < 18);
    if (timeState == false) { // if it's night time
      digitalWrite(7, LOW); // Turn off heat lamp 1
      digitalWrite(8, LOW);
    }// Turn off heat lamp 2
    else { // if it's day time
      if (Temperature >= 30) { // if the temperatue is above 5 degrees
        digitalWrite(7, LOW); // turn off heat lamp 1
        digitalWrite(8, LOW); // turn off heat lamp 2
      }
      if (Temperature < 28) { // if the temperature is below 5 degrees
        digitalWrite(7, HIGH); // turn on heat lamp 1
        digitalWrite(8, LOW); // turn off heat lamp 2
      }
      if (Temperature < 27) { // if the temperature is below 1 degree
        digitalWrite(7, HIGH); // turn on heat lamp 1
        digitalWrite(8, HIGH); // turn on heat lamp 2
      }
    }


  }
}

but setting the state of V7 does not operate the overRide function.

I must be having difficulties finding the overRide() function. Can you tell me what line number it is on?

  if (param.asInt() == 1) {
    overRide == HIGH;
  } else {
    overRide == LOW;
  }

Why are you comparing the value of overRide to HIGH and LOW? Did you mean to assign HIGH or LOW to overRide, using =?

Hi Paul,

Thanks for your time.

I am trying to use V7 to turn on & off overRide which is used on line 170 but I've got completely lost, when I use a digital input overRide works as intended.

PaulS:
Why are you comparing the value of overRide to HIGH and LOW? Did you mean to assign HIGH or LOW to overRide, using =?

Doh! Thanks PaulS.

It works now.