Read and Set values from/to V0 virtual "push button switch"

I cannot find an example explaining how to read the value of a push button widget (as a switch) for a Photon. I want to use a value (0 or 1) to perform a ‘code section bypass’ when the push button is “ON” and execute the code when the push button is “OFF” and toggle the setting from the Photon to the Widget.

“blynk/blynk.h” is included in my code and I have successfully turned ON/OFF virtual LED’s.

If a very simple and functional code example could be provided it would be very helpful to me and many others trying to use BLYNK. The potential seems fantastic, but getting there is very difficult for NON-Professional coders. Thank you so much for your help.

1 Like

This feature is called Syncing - http://docs.blynk.cc/#blynk-main-operations-state-syncing

I have been playing with this feature for hours and I included your SYNCing example in a test file. I have included “Publish” statements that will display in the Particle Dashboard when code is executed. I never see any of the publish statement from either READ or WRITE calls. Could you please review the code and give me your opinion on what is wrong?

// This #include statement was automatically added by the Particle IDE.
#include “blynk/blynk.h”
#define ON 255
#define OFF 0
int pinData = OFF; // change this global variable to prove code is functioning

bool isFirstConnect = true;
char BlynkToken[] = “70a54d197825483c9c00ee26652a153e”;

//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 set status of BLYNK widget on Android
{
Blynk.virtualWrite(V0,pinData);
Particle.publish("BLYNK_READ executed : ", String(pinData));
}

//xxxxxxxxxxxx[ Widget Button using virtual pin # 0 ]xxxxxxxxxxx
BLYNK_WRITE(V0) //Function to get status from BLYNK widget to PHOTON
{
int state = param.asInt();
if (state)
{
Blynk.virtualWrite(V5, ON);
Particle.publish("BLYNK_WRITE executed : ", String(ON));
}
else
{
Blynk.virtualWrite(V5, OFF);
Particle.publish("BLYNK_WRITE executed : ", String(OFF));
}
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void loop()
{
Blynk.run();
// BLYNK_WRITE(V0);
Blynk.virtualWrite(V3, pinData); // virtual LED
Blynk.virtualWrite(V4, pinData); // virtual LED
Blynk.virtualWrite(V5, pinData); // virtual LED
Blynk.virtualWrite(V6, pinData); // virtual LED
Particle.publish("Data value of pinData: ", String(pinData));
Particle.publish("Data value of ON : ", String(ON));
Particle.publish("Data value of OFF: ", String(OFF));
}

You can’t user virtualWrite in loop. http://docs.blynk.cc/#troubleshooting-flood-error

1 Like

That test can be completely eliminated and has nothing to do with getting the status of a BLYNK push button, it was just for my assurance. My question/need is “how to receive the status of a push button from the app to make a decision in the Photon code?”

The function “checkIfWorking()” is now optional, and I can call it in loop() to limit flooding if I need assurance BLYNK is still responding.

Here is revised code for the TEST. PLEASE SHOW ME how to retrieve the Push Button status. 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[] = “9999999999999999999999999999999”;

//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 set status of BLYNK widget on Android
{
Blynk.virtualWrite(V0,pinData);

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

Particle.publish("BLYNK_READ executed : ", String(pinData));

}

//xxxxxxxxxxxx[ Widget Button using virtual pin # 0 ]xxxxxxxxxxx
BLYNK_WRITE(V0) //Function to get status from BLYNK widget to 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?

    Particle.publish("BLYNK_WRITE executed : ", String(ON));
}
else 
{
    Blynk.virtualWrite(V5, OFF); 
    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));
}
}

What is your use case? Why would you need to read state of button? Doesn’t react on button click not enough?

I am NOT using the PB to turn on a physical LED. I want the button to provide a 0 or 1 (from the BLYNK APP on the Android phone) to my Photon code. Then I can use 0 or 1 to skip a section of code.

Example: IF I AM HOME

Receive ‘1’ from the push button, SKIP CODE that sends a TXT message indicating the door is open and a PIR is active.

Example: IF I AM NOT HOME

Receive ‘0’ from the push button, EXECUTE CODE to send a TXT message that the door is open and a PIR shows motion in the room.

Got it. Who changes this

IF I AM HOME
IF I AM NOT HOME

?

Not sure what you mean by “Who change this”, but I assume you are asking who will select the SKIP or EXECUTE selection.

That would be on my Android, so I can use this as a type of silent alarm system. So the PB would be on my smart phone.

@DGG-KY from what I can gather you need something like this extract:

void ifiamhome(){
  // functions for when you are home
}

void ifiamnothome(){
  // functions for when you are not home
}

BLYNK_WRITE(V1)  // button attached to Virtual Pin 1 in SWITCH mode
{
  int i = param.asInt();
  if(i == 1){
    ifiamhome();  
  }
  else{
    ifiamnothome();  
  }
}

@DGG-KY yeap, in that case see @Costas example. Also for that case Menu widget will be better.

I think I am beginning to understand that Blynk.run is designed to look for BLYNK_WRITE(V0). This must be a custom and dedicated function specifically defined for use by Blynk.

Therefore my code to be tested and executed for a virtual PB should then make another function call which can keep from flooding the servers.

The example provided by Costas looks like what I am looking for and I am looking forward to implementing and testing it.

Is this the case that BLYNK_WRITE() is called when the Blynk app wants to write to the virtual pin on the Photon?

@DGG-KY despite the indication from the name BLYNK_WRITE() reads the status of a pin rather than writing to it.

To set virtual pin 1 HIGH you would use:

Blynk.virtualWrite(V1, 1);

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