Restore value from Blynk cloud ad assign it to a variable

Hi, I need to store value of a 24-position vector to save them in case of Arduino reset.
I choose to save them on virtual PINs from V100 to V123.
I planned to make a backup in the void Setup().
How I can re-assign values to the vector ?

I tried this but it doesn’t works: vector[0] = param.asInt(V100);
Also this, it doesn’t works: vector[0] = Blynk.syncVirtual(V100);

Thank you

This is probably a neater way to do what you’re looking for…

BLYNK_WRITE_DEFAULT()
{
  int widget_pin = request.pin;      // Which virtual pin triggered this
  int widget_value = param.asInt();
  vector[widget_pin -100] = widget_value;
  Serial.print("Stored ");
  Serial.print(widget_value);
  Serial.print("In array location ");
  Serial.println(widget_pin -100);
}
BLYNK_CONNECTED()
{
  Serial.println("Getting stored values from Blynk...");
  for(int loop=100;loop<=123;loop++) 
  {
    Blynk.syncVirtual(loop);
  }
}

Pete.

Hello Pete and thank you.
It is a bit hard to understand for me…
Can I put this
Serial.println("Getting stored values from Blynk..."); for(int loop=100;loop<=123;loop++) { Blynk.syncVirtual(loop); }
in void Setup() or do I have to use a BLYNK_CONNECTED named routine?

and

In the BLYNK_WRITE_DEFAULT() do I have to use a for cycle to re-assign all vector positions?

Thanks

They are both stand-alone functions, just pop the, on the end of your code if you wish.

BLYNK_CONNECTED() is a function that is called (as it’s name implies) every time your device connects to the Blynk server - which is normally after it’s started-up and Blynk.begin executes.

BLYNK_WRITE_DEFAULT() is a bit like BLYNK_WRITE(100), but instead of just being called whe wither the value of a widget connected to pin V100 changes, or when you call Blynk.syncVirtual(V100) it is triggered whenever any virtual pin value changes.

BTW, if you’re using other virtual pins in your project then you might want to add an iff statement in there so that this only writes the values into your array if the virtual PIN number is between 100 and 123.

Pete.

Ok, I understood!
So BLYNK_CONNECTED() run once at begin and “ask a syncronization” for selected pins by Blynk.syncVirtual(loop) command.

Then BLYNK_WRITE_DEFAULT() upload values every “query” done by BLYNK_CONNECTED().

Is it?

But… as I am using other almost 20 VPIN in my project and I am using mobile data, I prefer to limit data flow between the cloud and Arduino.

How I can say to BLYNK_WRITE_DEFAULT() run just one time as BLYNK_CONNECTED() does?

You can’t, and you need it to run 24 times anyway. As I said earlier, you can add an if statement something like this…

BLYNK_WRITE_DEFAULT()
{
  int widget_pin = request.pin;      // Which virtual pin triggered this
  if (widget_pin >=100 && widget_pin <=123)
  {
    int widget_value = param.asInt();
    vector[widget_pin -100] = widget_value;
    Serial.print("Stored ");
    Serial.print(widget_value);
    Serial.print("In array location ");
    Serial.println(widget_pin -100);
  }
}

Pete.

Ok, thank you.
I hope a command like Blynk.virtualRead(Vpin); will be soon implemented.
It will be simpler to assign stored value to a variable.
:slightly_smiling_face:

Hi Pete, I figured out that one active widget per each virtual pin is needed otherwise values can’t be restored. Do I have to use a Value Display widget or you could suggest another widget where more virtual pin can be saved at same time? Thank you