BLYNK_WRITE and BLYNK_READ alternatives

Or quite some time, it is also possible to use alternative Blynk function names, like:
BLYNK_WRITE -> BLYNK_IN
BLYNK_READ -> BLYNK_OUT

Do you think this would make Blynk sketches more readable & understandable?
What do you think?
Maybe you have ideas how to improve current library API?

@vshymanskyy I think you’ve hit on something.

The confusing thing about those functions (at least for me) has always been that they’re written from the perspective of the Blynk cloud and app. I think we typically code from the perspective of the hardware, where a “read” describes the hardware receiving information, and a “write” describes the hardware sending information. Your IN / OUT functions are a better fit with that convention.

@chrome1000, you can try using alternative names already -> and tell us how it feels :wink:

BLYNK_IN is not compiling for me. Is it a direct swap for BLYNK_WRITE? As in:

BLYNK_IN(V1){
  if(param.asInt()){
    Blynk.virtualWrite(V2,HIGH);
  }
  else Blynk.virtualWrite(V2,LOW);
}

Using Blynk 3.8 and Arduino IDE 1.6.5.

I think we can make it BLYNK_INPUT() for BLYNK_WRITE and BLYNK_OUTPUT for BLYNK_READ in next version… I think it reads better.

2 Likes

very self explained using this way. I prefer it most!

I think I can support that, lol. After much consideration, with mostly myself, I tend to agree with you. And how I love readable code…

Yes, a more understandable API would be nice.
I would like to take it one step further though.
Why use the functions Blynk_write(V0) {your code here} when
it could be as simple as:
int mydata = Blynk.virtualRead(V5); //It might be necessary to cast the datatype somehow though…
Maybe like:
int mydata = Blynk.virtualRead(V5, int)

I love how you have done it when sending data to app:
Blynk.virtualWrite(V5, mydata); //This is so easy to understand. It should be same for reading back data.

The current way is a bit confusing.

1 Like

Sorry, we can’t provide this at hte moment, as it has far worse implications, that “a bit confusing” BLYNK_WRITE :wink:

BLYNK_WRITE can not place inside another function, right? than I can not get data from server as demand?

Blynk.syncVirtual(Vx) will get you the data from the server on demand.

2 Likes

you call this, and then get corresponding BLYNK_WRITE called shortly after

I know that, but why you have to call BLYNK_WRITE to use your data? Maybe Blynk.virtualRead is simpler to get just data, to widely use that data instead of have to BLYNK_WRITE to handle it.

hello!

my 2 cents on blynk_read / write:
it would be much compact / nicer / readable to use just one instance of the BLYNK_READ() function, and put all Blynk.virtualWrite(pin, value); stuff inside.

currently, i have to write something like this - i can’t even use a “for” cycle :frowning:

// send stuff to phone:

BLYNK_READ(BATTLOW) {
  Blynk.virtualWrite(BATTLOW, battLow);
}

BLYNK_READ(BATTERY) {
  Blynk.virtualWrite(BATTERY, battery);
}

BLYNK_READ(SPD) {
  Blynk.virtualWrite(SPD, spd);
}

BLYNK_READ(SPDAVG) {
  Blynk.virtualWrite(SPDAVG, spdAvg);
}

BLYNK_READ(SPDMAX) {
  Blynk.virtualWrite(SPDMAX, spdMax);
}

BLYNK_READ(RPM) {
  Blynk.virtualWrite(RPM, rpm);
}

BLYNK_READ(RPMAVG) {
  Blynk.virtualWrite(RPMAVG, rpmAvg);
}

BLYNK_READ(RPMMAX) {
  Blynk.virtualWrite(RPMMAX, rpmMax);
}

BLYNK_READ(TRIP) {
  Blynk.virtualWrite(TRIP, tripMM / 1000000.0);
}

BLYNK_READ(ODO) {
  Blynk.virtualWrite(ODO, odo);
}

BLYNK_READ(INCLINATION) {
  Blynk.virtualWrite(INCLINATION, inclination);
}

BLYNK_READ(AUTO_MODE) {
  Blynk.virtualWrite(AUTO_MODE, autoMode);
}

BLYNK_READ(TOTSHIFT) {
  Blynk.virtualWrite(TOTSHIFT, readEp(7));
}

BLYNK_READ(GEAR) {
  Blynk.virtualWrite(GEAR, EEPROM.read(2));
}

BLYNK_READ(UPTIME) {
  Blynk.virtualWrite(UPTIME, millis() / 1000);
}

…not really meets the “do not repeat yourself” principle…
this could be:

// send stuff to phone:
BLYNK_READ() 
{
  Blynk.virtualWrite(BATTLOW, battLow);
  Blynk.virtualWrite(BATTERY, battery);
  Blynk.virtualWrite(SPD, spd);
  Blynk.virtualWrite(SPDAVG, spdAvg);
  Blynk.virtualWrite(SPDMAX, spdMax);
  Blynk.virtualWrite(RPM, rpm);
  Blynk.virtualWrite(RPMAVG, rpmAvg);
  Blynk.virtualWrite(RPMMAX, rpmMax);
  Blynk.virtualWrite(TRIP, tripMM / 1000000);
  Blynk.virtualWrite(ODO, odo);
  Blynk.virtualWrite(INCLINATION, inclination);
  Blynk.virtualWrite(AUTO_MODE, autoMode);
  Blynk.virtualWrite(TOTSHIFT, readEp(7));
  Blynk.virtualWrite(GEAR, EEPROM.read(2));
  Blynk.virtualWrite(UPTIME, millis() / 1000);
}

than i could do something like this:

BLYNK_READ()
{
  byte virtualWriteBytes[8] = {battlow, battery, spd, spdAvg, spdMax, rpm, rpmAvg, rpmMax};
  int virtualWriteInts[6] = {tripMM / 1000000, odo, inclination, autoMode, readEp(7), millis() / 1000};
  
  for (byte i = 0; i < 8; i++) {
    Blynk.virtualWrite(i, vwBytes[i]);
  }

  for (byte i = 0; i < 6; i++) {
    Blynk.virtualWrite(i + 8, vwInts[i]);
  }
}

the same goes to BLYNK_WRITE(). it could be:

BLYNK_WRITE()
{
    autoMode = param.asInt(vpin);
    if (param.asInt(vpin) == 1 && EEPROM.read(2) < 14) shiftUp = true;
    if (param.asInt(vpin) == 1 && EEPROM.read(2) > 1) shiftDown = true;
    if (param.asInt(vpin) == 1) tripMM = 0, rpmAvg = 0, rpmMax = 0, spdAvg = 0, spdMax = 0;
    opRpm = param.asInt(vpin);
}

or there is a better way currently to do a lots of virtual writes / read without repeating all this lines?
think of it, if you have to use all the 128 virtual pins on a arduino mega?

@wanek already there - https://github.com/blynkkk/blynk-library/blob/master/examples/More/PrintAllVirtual/PrintAllVirtual.ino

sorry, i didn’t noticed. my bad!

hey Dmitriy,

How can I read virtual pins such as ( v1 , v2 ,…) and drag thier values in a void loop ,

the idea is :
two leds represnts two IR sensors whitch they give ( 0 , 1 ) , and two switches releted to these sensors , if the switch 1 is On; the led 1 should trun off and keep traking led 2 if the object is there or no to show it in a Blynk as Off or ON .
But i have for loop as a timer inside BLYNK_WRITE(V1) , so this makes a delay to read a values of other virtual pins because of for loop, so how can i avoid this delay by reading pins togather , shall i use timer instead of for loops , or can i read these pins togather inside a void loop witch is error to write a function inside a function. ?

i know that following code is for reading V1 from Blynk :

BLYNK_WRITE(V1)
{
int pinValue1 = param.asInt();
Serial.println(pinValue1);
}

void setup {
…
}
void loop {
…
}