Why Blynk Terminal prints codes 3 line at a time

Hi, I’m new at Blynk and trying to test it by printing on Terminal in my phone. I’m using Adafruit Feather Huzzah ESP8266. It supposed to print “Setup is Processed” just before jumping to loop function.

Then every second it should print “Huzzah is in loop” into Terminal. It works, but the interesting thing is it is not refreshing Terminal screen every time something is printed but it refreshes every 3 seconds and prints 3 lines of code at once.

I thought this is a delay of communication between Huzzah and my laptop through wifi, but it is not. If I increase the delay and print “Huzzah is in loop” every 5 seconds, then Terminal prints 3 lines of codes every 15 seconds.

So somehow Terminal waits for 3 lines of codes until it is refreshed.

Additionally the lines are not complete, some characters of the last line is printed together with next print cycle. Below is an example of how I see the code in Terminal.

Setup is Processed
Huzzah is in loop
Huzzah is in loop
Huzzah is
-----(waits 3 seconds)-----
in loop
Huzzah is in loop
Huzzah is in loop
Huzzah is in loo
-----(waits 3 seconds)-----
p
Huzzah is in loop
Huzzah is in loop
Huzzah is in loop
-----(waits 3 seconds)-----
Huzza


.

And below is the code in Huzzah. Why do you think this happens?

#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial

WidgetTerminal terminal(V1);

char auth[] = "MyAuth";

const char *ssid     = "MySSID";
const char *password = "MyPass";
String data;


BlynkTimer timer;

void setup() {
 Serial.begin(115200);
 Blynk.begin(auth, ssid, password);
 WiFi.begin(ssid, password);
 pinMode(2, OUTPUT);
 
 timer.setInterval(1000L, repeat);  
 
  
 digitalWrite(2, HIGH);   //Turn off blue led
 terminal.println("Setup Processed");
}

void loop() {
 Blynk.run();
 timer.run();
}

void repeat(){

 terminal.println("Huzzah is in loop");
     
}

Add a terminal.flush();after the prints if you want to Ensure everything is sent. Otherwise it gets buffered probably

Dear J-M-L, you could be right if I get 3 codes every seconds. However I'm getting 3 codes every 3 seconds.

The expected behavior is to get 1 code every second. So There is no extra codes that are send from buffer.

Anyway, I'll try your suggestion when I'm back home in the evening, however I belive this is not the solution.

my guess is: there is a local buffer on the arduino side. until it's full or some sort of timing trigger happens, nothing gets sent and calling flush() forces the buffer to be sent.

but just wild guess

Confirmed: a 64-byte buffer is used.

WidgetTerminal.h

class WidgetTerminal
    : public BlynkWidgetBase
#ifdef BLYNK_USE_PRINT_CLASS
    , public Print
#endif
{
...
    virtual size_t write(uint8_t byte) {
        mOutBuf[mOutQty++] = byte;
        if (mOutQty >= sizeof(mOutBuf)) {
            flush();
        }
        return 1;
    }
...
private:
    uint8_t mOutBuf[64];
    uint8_t mOutQty;
};

Dear J-M-L, my bad, you were completely right. I’ve tested and confirmed that terminal.flush() should be used to make sure that code is sent to device. Thank you very much for solving the problem.

About my second question, why do you think it happens? I’ve updated to code by including a counter to better observe the new coming codes. I get the codes once a second in terminal as it should be. Now I wonder why some part of the code is in the second line even though terminal screen is wide enough to print almost twice more character in single line.

The code looks like this:

Setup is processed
Huzzah is in loop
1
Huzzah is in loop
2
Huzzah is in loop
3
Huzzah is in loop
4
Huzzah is in loop
5
Huzzah is in loop
6
Huzzah is in loop
7

Here is the updated code:

#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial

WidgetTerminal terminal(V1);

char auth[] = "auth";

const char *ssid     = "SSID";
const char *password = "Pass";
String data;
int i = 0;

BlynkTimer timer;

void setup() {
 Serial.begin(115200);
 Blynk.begin(auth, ssid, password);
 WiFi.begin(ssid, password);
 pinMode(2, OUTPUT);
 
 timer.setInterval(5000L, repeat);  

 digitalWrite(2, HIGH);
 terminal.println("Setup Processed");
 terminal.flush();
}

void loop() {
 Blynk.run();
 timer.run();
}

void repeat(){

 terminal.println("Huzzah is in loop");
 terminal.println(i);
 terminal.flush();
 i = i+1;
}

gunaygurer:
About my second question, why do you think it happens?

Serial.print is local, so gets to the console right away, as fast as your Serial port work

as @oqibidipo posted above, if you look at the code in the WidgetTerminal, there is a 64 byte buffer and when you print the data actually goes into this buffer and stays there until you call flush() or you fill up the buffer with 64 bytes - then the 64 bytes are sent and it starts over again.

 virtual size_t write(uint8_t byte) {
        mOutBuf[mOutQty++] = byte;
        if (mOutQty >= sizeof(mOutBuf)) {
            flush();  // <<=== CALL FLUSH ONLY WHEN BUFFER IS FULL
        }
        return 1;
    }

→ the behavior is very different than local Serial print.

if you wonder why there is a carriage return in the terminal window, that’s because you use println(). The ‘ln’ means after printing, go to next line. Use print if you want everything on the same line.