BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Sending data from hardware to Blynk


#1

Hi,

In the below code, I am trying to send button value to a text display widget.
But the data is not being sent and BLYNK_READ function is not getting called in every iteration of loop.
I couldn’t find the issue.
Can any of you please help me in this regard?

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "XXXX";
char ssid[] = "XXX";
char pass[] = "XXX";

int button_val;
BLYNK_READ(V1)
{ 
  Blynk.virtualWrite(V1, button_val);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  pinMode(4, INPUT);
  Blynk.begin(auth, ssid, pass);
  
}

void loop()
{
  button_val=digitalRead(4);
  Serial.print("Button val");
  Serial.println(button_val);
  Blynk.run();
}

#2

Please read the Welcome Topic before posting unformatted code in the forum.

Then take a look at the Sketch Builder for a better way to poll a physical button and display it’s status.

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FSync%2FButtonPoll

Adjust for the pin # you are using (make sure it is the Arduino number that corresponds to your ESP’s GPIO pin… which will be labeled differently) and set your Widget to PUSH


#3

Thank you for your quick response.
I have selected the ESP266 device while creating the project. In the display widget, I have selected virtual pin V1. When I keep time frequency 1sec, values are being sent to the App. But I am not able to send with PUSH.

What might be the issue?


#4

I think that’s because you have mixed up BLYNK_READ and BLYNK_WRITE. 99% of the time it’s a WRITE you need not a READ i.e. the server WRITE’s the data to the MCU.


#5

Thank you

I didn’t use BLYNK_WRITE() function in the code. I only used BLYNK_READ(). Please check the above code.


#6

I know and it’s wrong.


#7

Sorry. I didn’t get you. I wrote the code by referring examples and blynk docs. Is it wrong?


#8

Yes, use BLYNK_WRITE().
Modify your loop().
And don’t use virtualWrite(x) in BLYNK_WRITE(x).


#9

BLYNK_READ(vPIN) happens when a Widget, set to a polling time, calls for data… only works when App is active (might also need to be open?).

BLYNK_WRITE(vPIN) happens when your program tells it to… sends the data to the server regardless if App is active or not. Widgets should be set to PUSH. This is the MOST commonly used method for App <—> MCU communication


#10

So we should not keep “PUSH” in “Rate of request” field in the widget when we use BLYNK_READ().
We can send and receive the data from the app using BLYNK_WRITE.Then how BLYNK_READ is unique from BLYNK_WRITE()?


#11

I believe they differ due to the Widget interaction method on the App side; One is automatic and the other requires user/script intervention.

  • BLYNK_WRITE(vPIN) works when the Widget is told via manual activation (PUSH) to submit its state or value update, usually asa control command, to the server/MCU. This activates a function() that can contain many commands and processes that can be determined by the incoming state/value. Regular display feedback is usually handled by timers or fuction() reaction via Blynk.virtualWrite().

  • BLYNK_READ(vPIN) works when the Widget is asking via pre-determined polling for its state change or value update, usually a simple display update to same widget, via Blynk.virtualWrite()… thus no need for device side timers in order to have regular feedback.

Most programmers use the WRITE method as it gives them a single point of timing and control for everything… their device’s scripting.


The documentation is admittedly written in geek, :stuck_out_tongue_winking_eye: so not always clear to mere mortals… but it says much the same thing…

BLYNK_WRITE(vPIN)
BLYNK_WRITE defines a function that is called when device receives an update of Virtual Pin value from the server:

BLYNK_READ(vPIN)
BLYNK_READ defines a function that is called when device is requested to send it’s current value of Virtual Pin to the server. Normally, this function should contain some Blynk.virtualWrite calls.


#12

Check out the various button examples in Sketch Builder.