Storing data on Blynk Server virtual pins

I will try to explain it better/differently. Maybe I am just going about it the wrong way.

What I am trying to accomplish is a battery powered ESP (for now just taking temp measurements), and have it send me ONE alert when the battery voltage/percentage gets below a certain value. I do not want it to send me an alert EVERY time it wakes up, just the one time when it drops below a set-point (continuous alerts will be sent once it reaches an even lower set-point). Normally, I would just use a “flag” that would change once the message was sent, and then change it back once the battery is recharged. The problem is that when the ESP sleep/wakes, the flag value is not stored. Since the ESP starts from setup when it wakes, it returns the flag to the initiating value.

This is where I thought Virtual Pins would come in handy. I could use the virtual pin as a “flag”, and access its stored value when the ESP wakes up. So after I get my battery voltage/percentage, and read the temp/humidity, I use a Blynk.syncVirtual (V3) to call the BLYNK_WRITE (V3). In this routine I want to check the battery voltage/percentage, and the Virtual Pin Value (flag), and then execute the proper portion of code depending on these values.

What I have found is that in my project I must have a Value Display widget assigned to Virtual Pin 3, and I must first make V3 a number. So I initially run my code with Blynk.virtualWrite(V3, 5); so that V3 becomes a number (int), I then comment out that command //Blynk.virtualWrite(V3, 5); and re-upload the program to the ESP. From there it seems to work as it should.

After watching V3s value change on the Display Widget, I thought why/is this extra widget really necessary. So I removed it from the project, and guess what, I stopped receiving the notifications. When I added the Value Display back to the project, no value was displayed in the widget (even with a reset/power cycle of ESP). I had to re-upload the code with Blynk.virtualWrite(V3, 5); , let it run one cycle (until the value reappeared on Value Display widget in project), then re-upload the code with //Blynk.virtualWrite(V3, 5);

I even added a serial.println(value) inside the BLYNK_WRITE (V3) and when the Value Display widget is removed from the project, the value stops being shown on the serial monitor.

1 Like

I see. Now that explains everything :).

You need to use either “no widget setup” or “setup with widget” but not both. When you put widget - the value is stored within the widget. When you remove widget - the value is removed with the widget. When you store value wihout widget it is stored. But when you add widget - widget will always override it. So in your case you just don’t need to add value display initally. You may check/set pin state via HTTP API in that case (in order to avoid adding value display).

So in order to not have to use the widget, I need to use HTTP API or just make sure the widget is not in the project upon initial startup?

Yeap. Or just do not remove it. Or use 2 pins - one for storage another one for display widget.

I will give it a try and report back.

So to clarify,

If you add a Blynk.virtualWrite(pin) the value will write to the app and the app will store the value, even if there is not a widget?

If @Toro_Blanco removes the widget the BLYNK_WRITE (V3) routine will be stranded and never execute?

Instead of BLYNK_WRITE (V3) could the same code be implemented in a new timer that runs on a long interval?

Sorry to interject, I’ve been trying to wrap my head around handling virtual pins. I plan on implementing something similar to OP (battery monitoring on a sensor bank), and will probably return to this post when I am ready.

Initial testing seems to confirm. If code is loaded/started without the widget, things appear to work as they should. I am waiting for the battery to drain a bit more to be certain.

correct. And we have example for that -


@ThisIsntFunny, the BLYNK_WRITE (V3) is executed due to Blynk.syncVirtual (V3);

The problem with not using the BLYNK_WRITE (V3), and instead using another timer, is that every time the ESP wakes, it runs from setup. So basically you cant store any values on the ESP. Additionally, it seems as if BLYNK_WRITE (V3) is where you get the stored valve off the server.

1 Like

you can, if you save to eeprom

all sketches run from setup(), just add a BLYNK_CONNECTED() function and put your V3 sync / virtualWrite() in there before you sleep.

1 Like

I will have to investigate this a bit more. I’ve never used eeprom.

I put the V3 sync at the end of my timed routine so that the values of the battery will be updated first (at least I think they will be).

What I was trying to say is that when the ESP wakes back up, it is like it is staring over. It doesn’t just pick up where it left off. All initial declarations will override any saved/changed values of pins/variables/etc. that took place before the board went to sleep.

@Toro_Blanco I think you are mixing up sync and virtualWrite. virtualWrite before you sleep and sync when you wake then you have the variables you require and you are not starting afresh.

Here is a good YouTube video on EEPROM and ESP8266.

1 Like