Using blynk read in loop()

I need to grab the status of a push button from BLYNK and use the variable inside the LOOP() to pass to other function calls.

I made a very short test program to duplicate what I’m getting in a much larger program as follows:

#include "blynk/blynk.h"
char BlynkToken[] = "70a54d197825483c9c00ee99999a234e";// bogus number
int pinData = 0; 

void setup()
{
        Serial.begin(9600);
        Blynk.begin(BlynkToken);   
        delay(5000); // Allow time to stabilize
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void loop()
{
    Blynk.run();
    BLYNK_READ(V0) // Get and store push button status 
    {
        int pinData = param.asInt(); // save status
    }
}

I get an error message "function definition is not allowed here before “{” token
void^ loop()

BLYNK_READ(V0) // Get and store push button status 
{
    int pinData = param.asInt(); // save status
}

This is a function definition (as the error message clearly states) that needs to be outside of loop() and will be called via Blynk.run() implicitly.

@DGG-KY, in addition to what @ScruffR pointed out, you also need to make pinData a global variable in order to access it in loop(). :wink:

1 Like

Yup, as @peekay123 said.
You’ve already got a global pinData, so drop the data type (int) inside the BLYNK_READ() implementation otherwise you just hide the global variable and will lose the assigned value once the local pinData goes out of scope.

1 Like
  1. i rewrote the test program as follows which compiles, but I’m not sure if I use Blynk.virtualWrite(v5, pinData) to GET and SET the status from/to the Android push button widget --CONFUSED
  2. Is my BLYNK_WRITE function definition the best way to retrieve and set pinData?
#include "blynk/blynk.h"
char BlynkToken[] = "70a54d197825483c9c00ee99999a234e";// bogus number
int pinData = 0; // global variable

void setup()
{
        Blynk.begin(BlynkToken);   
        delay(5000); // Allow time to stabilize
}

BLYNK_WRITE(V5) //Button Widget is writing to pin V5
{
  if (param.asInt())
  {
    pinData = 1;
  }
  else 
  {
    pinData = 0;
  }
}
void loop()
{
    Blynk.run();
    Blynk.virtualWrite(v5, pinData) // update the state of the Button Widget in the app 
}

Discovered, I’m not really using BLYNK_WRITE(V5), just passing the value of the global variable =0 every time.
If I try to use the function call inside loop() it fails. Again confused on how to used this function.

void loop()
{
    Blynk.run();
    BLYNK_WRITE();
    Blynk.virtualWrite(V5, pinData) // update the state of the Button Widget in the app 
}
junk1.cpp: In function 'void loop()':
junk1.cpp:32:1: error: expected ';' before '}' token
 void loop()
 ^

make[1]: *** [../build/target/user/platform-6junk1.o] Error 1
make: *** [user] Error 2

This is your code line

And this the error message

 expected ';' before '}' token

Any idea?

And with using Blynk.virtualWrite() you won’t need BLYNK_WRITE() at all.
But initially you were asking about BLYNK_READ() :confused:

Ah yes, I changed horses didn’t I? Okay I inserted the “;” and the test program compiled.

But, If I don’t use the BLYNK_WRITE function, how will I know the status of the push button on the Blynk App to be able to change pinData from 1 or 0 and notify the app of the changes I make?

This is why I started with BLYNK_READ. I thought I needed to first GET the status of the button, then I could do a BLYNK_WRITE to send back to the APP. Am I making this too difficult?

Blynk.virtualWrite() does the writing (from Photon to Blynk app), while BLYNK_WRITE() is the function that gets called (from inside Blynk.run()) when the Blynk app wants to set (write to) the virtual pin on the Photon.

This might also help
http://docs.blynk.cc/#blynk-main-operations-virtual-pins

The capitalised “functions” are some kind of callback function where you put your code what should happen when the phone app wants to READ or WRITE data from or to the Photon.
On the other hand, you’d use Blynk.virtualWrite() when your Photon code wants to write to the virtual pin on the phone app.
So you’d need to distinguish between the phone app view and the Photon view when choosing to read or write (READ on one side means write on the other side :wink: ).

Thank you for clarification, I have been studying BLYNK documentation and have yet to find a useful solution. Can you review this simple TEST program to help me understand how to work with BLYNK and the PHOTON as explained in my comments inside the code. THANK YOU

#include "blynk/blynk.h"
#define ON 255
#define OFF 0
int pinData = ON; // change this global variable to prove code is functioning
long prevMillis = 0;
long interval = 100; 
bool isFirstConnect = true;
char BlynkToken[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void setup()
{
    Blynk.begin(BlynkToken); 
    delay(5000); // give time to settle
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
BLYNK_CONNECTED() // runs every time Blynk connection is established
{
    if (isFirstConnect) 
    {
// Request server to re-send latest values for all pins
    Blynk.syncAll();
    isFirstConnect = false;
    }
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
BLYNK_READ(V0) //Function to get status of BLYNK widget from Android ??
{
    Blynk.virtualWrite(V0,pinData);  // is this the correct statement ??

// what code can be entered here to receive info from the push button?
// the following statement to is to prove the READ function wss executed
    Particle.publish("BLYNK_READ executed : ", String(pinData));
}

//xxxxxxxxxxxx[ Widget Button using virtual pin # 0 ]xxxxxxxxxxx
BLYNK_WRITE(V0) //Function to send status to BLYNK widget from PHOTON ??
{
    int state = param.asInt();
    if (state)
    {
        Blynk.virtualWrite(V5, ON);

// what code can be entered here to send or receive info from the push button?

// the following statement is to prove the WRITE function was executed
        Particle.publish("BLYNK_WRITE executed : ", String(ON));
    }
    else 
    {
        Blynk.virtualWrite(V5, OFF); 
// the following statement is to prove the WRITE function was executed
        Particle.publish("BLYNK_WRITE executed : ", String(OFF)); 
    }
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void loop()
{
    Blynk.run();
//    checkIfWorking(); // call to see if BLYNK is responding if needed
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void checkIfWorking() // this is just to comfort me that BLYNK is still responding
{
unsigned long curntMillis = millis();
   if( curntMillis - prevMillis > interval) 
    {
        prevMillis = curntMillis;        
        Blynk.virtualWrite(V3, pinData); // virtual LED 
        Particle.publish("Data value of pinData: ", String(pinData));
    }
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
BLYNK_READ(V0) //Function to get status of BLYNK widget from Android ??
{
    Blynk.virtualWrite(V0,pinData);  // is this the correct statement ??

// what code can be entered here to receive info from the push button?
// the following statement to is to prove the READ function wss executed
    Particle.publish("BLYNK_READ executed : ", String(pinData));
}

I’d not put a publish in there since this function will be called each time the blynk app wants to read the virtual pin state from Photon and this can be (depending on your V0 setting in the app) more frequently than allowed by the publishing limit (once per sec) - especially if you have multiple V-pins doing the same.
Otherwise you put all the code in here that’s required to gather the values toghter you’d need to send back to the mobile app via virtualWrite().

Not quite, this reads from the Photon to update the widget in the mobile app.

BLYNK_WRITE(V0) //Function to send status to BLYNK widget from PHOTON ??
{
    int state = param.asInt();
    ...
// what code can be entered here to send or receive info from the push button?
    ...
}

The same here, use publish with caution.
Otherwise you’d put the code here to catch the data sent from the mobile app (e.g. via param.as...()) and do whatever is required to control the Photon’s behaviour as intended.

Again, as with BLYNK_READ() and said earlier

not the other way round!

And as a rule of thumb, try to reduce the code in these functions to the required minimum and off-load more laborious tasks to loop() (e.g. by setting flags to be checked in loop()).

I have to leave for awhile, but will study your examples and attempt to implement them.

A big thank you for the great advice.

SOLVED! A great big THANK YOU to the Particle and Blynk Communities, I have a good working example showing how to use a BLYNK Push Button. The BLYNK push button shows its state,ON or OFF when used as a switch. I also write to a BLYNK LED to give show the subroutines executed by toggling a virtual LED. I publish the variable value for displaying the intensity of a virtual LED to the Particle Dashboard. When the PB is toggled is will display either 0 or 255 (255 = full on for virtual LED). I used #define for ON = 255 and OFF = 0. I’m not entirely sure if I’m flooding the servers, so I welcome comment on how to control it. Below is my TEST APP.

// if not displayed in this example, be sure to include '#' before include and define
#include "blynk/blynk.h"
#define ON 255
#define OFF 0
bool isFirstConnect = true;
char BlynkToken[] = "999999999999999999999999999999"; // insert your Blynk token here

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void setup()
{
    Blynk.begin(BlynkToken); 
    delay(5000); // give time to settle
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
BLYNK_CONNECTED() // runs every time Blynk connection is established
{
    if (isFirstConnect) 
    {
// Request server to re-send latest values for all pins
    Blynk.syncAll();
    isFirstConnect = false;
    }
}
//xxxxxxxxxxxx[ Push Button Widget using virtual pin V0 ]xxxxxxxxxxx
BLYNK_WRITE(V0) //Function to test status from BLYNK widget to PHOTON
{
    int state = param.asInt();
    if (state ==1)
    {
        TurnOn();
    }
    else if (state ==0)
    {
        TurnOff();
    }
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void loop()
{
    Blynk.run();
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void TurnOn()
{
        Blynk.virtualWrite(V7, ON); // virtual LED 
        Particle.publish("TurnOn state executed : ", String(ON));
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void TurnOff()
{
        Blynk.virtualWrite(V7, OFF); // virtual LED 
        Particle.publish("TurnOff state executed : ", String(OFF));
}