Can I read data stored in the server?

Hi, is there a way to read the value of a virtual pin which is stored on the server?
I mean can a virtual pin of a label widget function as a small EEPROM, for example by writing a buffer (which is easily done with virtualWrite) and later (at a power on) fetch this buffer somehow?

Hello. Yes you can. Just use Blynk.virtualWrite(x) and Blynk.syncVirtual(x)

http://docs.blynk.cc/#blynk-main-operations-state-syncing

I am missing something, Or I did not explain myself well.
Look at this code:
//-------------------------
int counter = 0;

BLYNK_WRITE(V2) //Button Widget is writing to pin V2, incremnt counter and store it on V1
{
Blynk.virtualWrite(1,counter++);

}
//----------
This will increment the counter on every click on button (V2), and store it in V1.

Now, at reset, I would like to fetch the value from V1 into counter, so the next value on V1 will continue from the last value.
How its done with sync? Can I use V1 as a variable with data ? How?

Thanks

BLYNK_CONNECTED() {
    Blynk.syncVirtual(V1);
}

BLYNK_WRITE(V1) { 
    counter = param.asInt();
}
int counter = 0;   // defined as a global variable

BLYNK_WRITE(V1) // which means READ a pin value from server to MCU
{
   counter = param.asInt();
}

When you call either of these your counter value will be restored:

Blynk.syncVirtual(V1);  // just read V1 from server to MCU
Blynk.syncAll();           // read everything available on server back to MCU
1 Like

I can’t like your post @Dmitriy as the variable is counter not conter :slight_smile:

1 Like

Heh. Nice catch =)…

Thanks guys.
A bit confusing is that BLYNK_WRITE (which is a read…):thumbsup:

Methinks it was written by a server guy, in which case it makes sense. Write value from server to MCU.
It’s just that most Blynkers are MCU guys.

1 Like

I’m a little confused about the use of Blynk.syncVirtual…

In this instance if your code says

Blynk.syncVirtual(V1);

will that trigger the

BLYNK_WRITE(V1) // which means READ a pin value from server to MCU
{
   counter = param.asInt();
}

or does it return the value that server has stored for V1 (i.e…

int storedValue = Blynk.syncVirtual(V1);

@alangstein there is a new example on Github that perhaps explains it all a little better, see https://github.com/blynkkk/blynk-library/commit/ebf73be1361ece278728dc9d0193f7e506fe231e for details.

Basically the sync commands get stored data from the server i.e. the last known value. You obviously need to write the value to the server on a regular basis, with Blynk.virtualWrite(Vx, data), whilst the ESP is running to ensure the recovered (sync’d) value is correct.

If the virtual pin was say a button the button would return to the pre crash state once the sync is done.

But in addition to the sync you may need to obtain the value of a variable and that is where you WRITE the value from the server to your MCU with the BLYNK_WRITE() function.

As per the latest Github example the sync is best used with BLYNK_CONNECTED() and you can recover various data types from the server:

// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
  //get data stored in virtual pin V0 from server
  Blynk.syncVirtual(V0);
}

// restoring counter from server
BLYNK_WRITE(V0)
{
  uptimeCounter = param[0].asInt();
  someStaticData = param[1].asString();
}
1 Like

Hi @Costas, have you tried that?
It does not work for me ( the data is not restored after connect).
I can’t see what am I doing wrong.

@johanan try the latest Github sketch (raw text link) and report back with your findings.

Or this link.

Once you have that running compare the sketch with your own sketch.

1 Like

@johanan please show sketch that doesn’t work.

Moved my project to ESP8266 (was Arduino Deu + Eth. Shield) and it looks OK.
Strange.