Void virtualWrite -> boolean virtualWrite, time for a change?

@vshymanskyy as sole contributor to BlynkApi.h according to Github ill go ahead and tag you.

I’m noticing that some of my Blynk.virtualWrite’s don´t seem to make it through to my terminal. I can tell that my labeled widgets got updated by looking at the temp label.

Below is my output from Serial monitor for 2 cycles of my ESP device
For each of theese prints a string is concatenated and a virtualWrite to terminal is performed at the last line.
Yet as seen in the image only the first cycle printed to terminal

Perhaps if virtualWrite could return a boolean value based on success of write to server, theese type of issues could be coded around? As some of my writes make it through I could then just add a loop that atempts extra virtualWrites in case of failure.

---------------- 19:30:41 ----------------
Wake Reason: Door open
Chip RstReason: Deep-Sleep Wake
Battery voltage: 0.04
Battery raw: 9.00
Sleep time: 15 minutes
Recheck time: 30 seconds
Voltage factor (977): 0.977
Temperature: 22.62 °C
Door is closed
Door is unlocked
WiFi signal at 50 %
Sleeping until: 19:31:12

---------------- 19:31:18 ----------------
Wake Reason: Timer
Chip RstReason: Deep-Sleep Wake
Battery voltage: 0.04
Battery raw: 9.00
Sleep time: 15 minutes
Recheck time: 30 seconds
Voltage factor (977): 0.977
Temperature: 22.62 °C
Door is closed
Door is unlocked
WiFi signal at 47 %
Sleeping until: 19:45:00

There’s no way to tell exactly if command is delivered to server or not, anyway…

Ah to bad very well at least I got an answer :slight_smile:

@Fettkeewl is it a random virtualWrite that you are losing or the last virtualWrite in a sketch / function?

I prefer virtualWrite for Terminal rather than flush but in your case you might want to try the print and flush method. It could be that the vw is waiting for the next blynk.run() cycle and missing it before you sleep.

If you are 5.0 perhaps try 4.10.

If neither of these work let me know as I might have a hack that could help.

1 Like

There is a way, however, it is a bit expensive and yeah, doesn’t worth the efforts.

2 Likes

I’ll get back to you once I’ve tried doing a “finishing” blynk.run.

No random writes, I concat a string then print to terminal before esp goes to sleep!
I’ve used flush before but it didn’t work that well for me, lost information there as well!
Actually not even the last write that’s lost but all writes for that cycle. I do

  • one write for the start time
  • Concat many strings and make a write at the end
  • And do a last write for sleep time

When this error occurs none of the writes make it to terminal

######### separate write 

---------------- 19:31:18 ----------------

######### separate write 

Wake Reason: Timer
Chip RstReason: Deep-Sleep Wake
Battery voltage: 0.04
Battery raw: 9.00
Sleep time: 15 minutes
Recheck time: 30 seconds
Voltage factor (977): 0.977
Temperature: 22.62 °C
Door is closed
Door is unlocked
WiFi signal at 47 %

######### separate write 

Sleeping until: 19:45:00

You could also try to disconnect Blynk before sleeping…

How will that help? :slight_smile:

… Read about how TCP sockets are being closed.

1 Like

So I’ve tried adding a
Blynk.run
Blynk.disconnect
Prior to setting my device to sleep.
No dice there on the whole not writing to terminal. I’ve noticed that the device does come online and connect to Blynk server. Just no terminal updates. Also the sleep was the default one hour even if I had set something else in the app, when this error occurred.

I think RTC is not working as it should, will investigate further with blynk debug :slight_smile:

Below is a copy from Serial monitor when having blynk debug enabled and noticing that I do not get a print to terminal (I do see the device connected to blynk from phone app). I even went as far as rebuilding the project leaving the “Device tiles” for a simpler setup with tabs. This way atleast terminal history is retained.

Using ESP core v 2.4.0
Up to date on everything blynk related

RTC sync not showing any error SO I do not know where to go from here to get any closer to an answer as to why my last virtualWrite does not seem to make it through SOMETIMES.
@Costas any ideas?
@vshymanskyy Do you see anything off in regards to Blynk debug that might explain the lack of virtualWrites?

I’ve included the important bits of code since my code is long.

This is from blynk.log

06:27:39.606 INFO - adiiss_@hotmail.com hardware joined.`
*WM: 
*WM: Adding parameter
*WM: static IP
*WM: Adding parameter
*WM: blynk-token
*WM: Adding parameter
*WM: blynk-server
*WM: Adding parameter
*WM: blynk-port
*WM: 
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Using last saved values, should be faster
*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.1.10
[2507] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.0 on Arduino

[2514] Connecting to 192.168.1.3:8442
[2553] <[02|00|01|00] *************AUTH REMOVED *****************
[2557] >[00|00|01|00|C8]
[2557] Ready (ping: 3ms).
[2558] Free RAM: 37144
[2624] <[11|00|02|00]Hver[00]0.5.0[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]Arduino[00]build[00]Jan 25 2018 20:23:25[00]
[2634] >[00|00|02|00|C8]
[2691] <[11|00|03|00|08]rtc[00]sync
[2693] >[11|00|03|00|0E]
[2693] >rtc[00]1516948059
[2693] Time sync: OK

---------------- 06:27:39 ----------------
[2760] <[10|00|04|00|05]vr[00]16
[2765] >[14|00|04|00|07]
[2765] >vw[00]16[00]0
[2827] <[10|00|05|00|0D]vr[00]10[00]12[00]2[00]14
Wake Reason: Door open
Chip RstReason: Deep-Sleep Wake
Battery voltage: 0.04
Battery raw: 9.00
[2839] >[14|00|05|00|07]
[2839] >vw[00]10[00]4
Sleep time: 15 minutes
[2906] <[14|00|05|00|08]vw[00]11[00]15
[2906] >[14|00|05|00|08]
[2906] >vw[00]12[00]30
Recheck time: 30 seconds
[2973] <[14|00|05|00|08]vw[00]13[00]30
[2973] >[14|00|05|00|08]
[2973] >vw[00]2[00]977
VoltageFactor  (977): 0.977
[2974] >[14|00|05|00|07]
[2974] >vw[00]14[00]1
[2976] <[14|00|06|00|0B]vw[00]0[00]9.0000
[3047] <[14|00|07|00|0A]vw[00]1[00]0.037
Temperature: 21.81 °C
[3569] <[14|00|08|00|0B]vw[00]5[00]21.812
Door is closed
Door is unlocked
[3636] <[14|00|09|00|10]vw[00]3[00]LOCKED [F0|9F|94|92]
WiFi signal at 47 %
[3703] <[14|00|0A|00|08]vw[00]15[00]47
Sleeping until: 06:28:10
[3770] <[14|00|0B|01]Avw[00]20[00|0A]---------------- 06:27:39 ----------------[0A]Wake Reason: Door open[0A]Chip RstReason: Deep-Sleep Wake[0A]Battery voltage: 0.04[0A]Battery raw: 9.00[0A]Sleep time: 15 minutes[0A]Recheck time: 30 seconds[0A]VoltageFactor (977): 0.977[0A]Temperature: 21.81 [C2|B0]C[0A]Door is closed[0A]Door is unlocked[0A]WiFi signal at 47 %[0A]Sleeping until: 06:28:10[0A]
[3860] <[0E|00|0C|00|1F]Notice![0A|0A|E2|9A|AB] Battery low: 0.04V
[3860] Disconnected

Image for compare, wake time was stamped as 06:27:39 and should have been between those two

This is the code for printing to monitor and concating strings, nothing advanced

void logSave(const String& txt)
{
  // logEvents is a global string
  logEvents += txt + "\n";
  Serial.println(txt);  
}
void logSend()
{
  Blynk.virtualWrite(vPin_terminal, logEvents);
  terminal.flush();
  logEvents ="";
}

This part covers the sleep. And it is in here where I send all text to terminal

void GoToSleep()
{ 
  if (!doorBoot) // Woken on door open, shoud sleep short, recheck lock
  {
    sleepTime = getShortSleepTime();
  }
  else  
    sleepTime = getLongSleepTime();
  logSave("Sleeping until: " + getTimeFromNow(sleepTime));
  logSend();
  
  sendNotify();  // Notify user of states 
  
  Blynk.run();
  Blynk.disconnect();
  
  delay(200);  
  // ESP sleep ~5% off on accuracy, adding 2.5% (testing) on long sleeps
  ESP.deepSleep(sleepTime * ((!doorBoot || sleepTime < 10 * 60e6) ? 1.00 : 1.025));
  delay(1000);  
}


unsigned long getLongSleepTime()
{
  // sleepInterval 1-4 ->  60 30 20 15 min
  uint8_t  minutes      = 60;
  uint8_t  seconds      = 0;
  uint8_t  interval     = 60 / sleepInterval;
  uint8_t  minMinutes   = 3;

  if (Blynk.connected()) {
    minutes = minute();
    seconds = second();
  }
  
  minutes = interval - minutes % interval;
  if (minutes <= minMinutes)  
    minutes += interval;

  return ((unsigned long) minutes * 60 - (unsigned long) seconds) * 1e6;
}

unsigned long getShortSleepTime()
{  
  // recheckTime 30-180, sec  
  return recheckTime * 1e6;
}

@Fettkeewl does RTC return the time when you get the error? Do you have a check for 1970? Perhaps you could add a short blocking routine to check for 1970, say try several times for X seconds.

Yes it does as per my output to Serial monitor I can tell everything is OK
My rtc sync code, trying without modifying the interval

if(Blynk.connected()) {
    rtc.begin();
    //setSyncInterval(1);
    while(year() == 1970) { Blynk.run();}
    //setSyncInterval(300);
  }

Explain.

As I am printing the same information to serial monitor as to Blynk terminal, the serial monitor log i pasted above contains information that should have been readable in the terminal. Hence my issue.

Everything in the background seems to work when my error occurs this from the fact that I get the expected output in Serial monitor

Explaining further every

------------- time stamp - - - - - - - - - -
Wall of text...
**... 
Sleep until: time stamp

That you see in my terminal in the image signifies one wake cycle. There is one wake cycle missing, but I have that cycles data from the serial monitor

Paste Serial Monitor and same Terminal for “error situation” even if Terminal is " ".

That is what I have done, sorry if I failed to communicate it properly :stuck_out_tongue:

I wanted you to make it easy for me like this:

SM shows:

“hfhhhhhhhhhhhhhhh”

Terminal shows:

" "

As you wish altough its a repeat of the above :slight_smile:
I’ll remove the Blynk debug text for easier read since it is posted above

Serial Monitor Shows (for the part that is missing in terminal)

*WM: 
*WM: Adding parameter
*WM: static IP
*WM: Adding parameter
*WM: blynk-token
*WM: Adding parameter
*WM: blynk-server
*WM: Adding parameter
*WM: blynk-port
*WM: 
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Using last saved values, should be faster
*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.1.10

Connecting to 192.168.1.3:8442
Ready (ping: 3ms).
Free RAM: 37144
Time sync: OK

---------------- 06:27:39 ----------------
Wake Reason: Door open
Chip RstReason: Deep-Sleep Wake
Battery voltage: 0.04
Battery raw: 9.00
Sleep time: 15 minutes
Recheck time: 30 seconds
Voltage factor  (977): 0.977
Temperature: 21.81 °C
Door is closed
Door is unlocked
WiFi signal at 47 %
Sleeping until: 06:28:10

Terminal shows:

So terminal SHOULD have shown like this if it worked properly
The bolded is MISSING in terminal but appears in Serial monitor as per text above :slight_smile:

---------------- 06:14:53 ----------------
Wake Reason: Door open
Chip RstReason: Deep-Sleep Wake
Battery voltage: 0.04
Battery raw: 9.00
Sleep time: 15 minutes
Recheck time: 30 seconds
Voltage factor (977): 0.977
Temperature: 21.69 °C
Door is closed
Door is unlocked
WiFi signal at 53 %
Sleeping until: 06:30:00

---------------- 06:27:39 ----------------
Wake Reason: Door open
Chip RstReason: Deep-Sleep Wake
Battery voltage: 0.04
Battery raw: 9.00
Sleep time: 15 minutes
Recheck time: 30 seconds
Voltage factor (977): 0.977
Temperature: 21.81 °C
Door is closed
Door is unlocked
WiFi signal at 47 %
Sleeping until: 06:28:10

---------------- 06:28:13 ----------------
Wake Reason: Door open
Chip RstReason: Deep-Sleep Wake
Battery voltage: 0.04
Battery raw: 9.00