Weird print behavior

Hi Everyone,

I’ve recently started using Blynk with an ESP8266 and I’ve noticed some odd behavior with Serial.print() while blynk is running.

In the example below, I attempt to sync a virtual pin with the server after a reset. While the functionality works (i.e. virtual pin is recalled and is incremented), I noticed that some of my debug print statements do not show up, but the code is seemingly executed. I’ve tried Serial.print(), Serial.printf(), BLYNK_LOG(), all with similar behavior.

Long story short, only the print inside the BLYNK_CONNECTED() callback shows up on the terminal. Prints in the loop() or the timerEvent() function don’t seem to work when blynk is running.

    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <SimpleTimer.h>

    SimpleTimer timer;

    volatile int isNotSynced = 1; 
    int counter = 0;

    BLYNK_CONNECTED() {
      if (isNotSynced) {
        Blynk.syncAll();
        Serial.println("synched"); // <-- I see this
        isNotSynced = 0;
      }
    }

    BLYNK_WRITE(V1) { 
        counter = param.asInt();
    }

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "...";
    char ssid[] = "...";
    char pass[] = "...";

    void timerEvent()
    {
      counter++;
      Serial.println(counter, DEC);
      Blynk.virtualWrite(V1, counter);
      Serial.println(counter, DEC); // < -- I do not see this
      //ESP.deepSleep(10000000L, WAKE_RF_DEFAULT);
    }

    void setup()
    {
      Serial.begin(115200);
      Blynk.begin(auth, ssid, pass);
      
      // Wait for connection
      while (false == Blynk.connect()){
        Serial.write('.'); // < -- I do not see this
        Serial.flush();
        
        }  
      // Once connected, start the timer task  
      timer.setInterval(1000L, timerEvent);
    }

    void loop()
    {
      Blynk.run(); // Initiates Blynk
      timer.run(); // Initiates SimpleTimer
      
    }

you could also post a screenshot of the serial monitor

What do you get when you use Serial.print’s throughout (not serial.write) and without the DEC suffix?

I cleaned up the code a bit and I have also included a screenshot.

  • The Poll loop for isNotConnected runs once and we see a “*”.
  • On the subsequent connection attempt we see “Ready”.
  • After that, "Blynk Connect " is printed after the Blynk.syncAll() and the terminal remains at that state.
  • Meanwhile V1 is incremented from some old non-0 value from the server as expected.

Note that the print at the end of the loop() never executes also.

    /**************************************************************
     * Blynk is a platform with iOS and Android apps to control
     * Arduino, Raspberry Pi and the likes over the Internet.
     * You can easily build graphic interfaces for all your
     * projects by simply dragging and dropping widgets.
     *
     *   Downloads, docs, tutorials: http://www.blynk.cc
     *   Blynk community:            http://community.blynk.cc
     *   Social networks:            http://www.fb.com/blynkapp
     *                               http://twitter.com/blynk_app
     *
     * Blynk library is licensed under MIT license
     * This example code is in public domain.
     *
     **************************************************************
     * This example runs directly on ESP8266 chip.
     *
     * You need to install this for ESP8266 development:
     *   https://github.com/esp8266/Arduino
     *
     * Please be sure to select the right ESP8266 module
     * in the Tools -> Board menu!
     *
     * Change WiFi ssid, pass, and Blynk auth token to run :)
     *
     **************************************************************/

    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <SimpleTimer.h>

    SimpleTimer timer;

    volatile int isNotSynced = 1; 
    int counter = 0;

    BLYNK_CONNECTED() {
      if (isNotSynced) {
        Blynk.syncAll();
        Serial.print("Blynk Connect Callback");
        isNotSynced = 0;
      }
    }

    BLYNK_WRITE(V1) { 
        counter = param.asInt();
    }

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "...";
    char ssid[] = "...";
    char pass[] = "...";

    void timerEvent()
    {
      counter++;
      Serial.print(counter);
      Blynk.virtualWrite(V1, counter);
      Serial.print("timerEvent");
      //ESP.deepSleep(10000000L, WAKE_RF_DEFAULT);
    }

    void setup()
    {
      Serial.begin(115200);
      Blynk.begin(auth, ssid, pass);
      
      // Wait for connection
      while (false == Blynk.connect()){
        Serial.print("*");
        }  
      // Once connected, start the timer task  
      timer.setInterval(1000L, timerEvent);
    }

    void loop()
    {
      Blynk.run(); // Initiates Blynk
      timer.run(); // Initiates SimpleTimer
      Serial.print("$");
    }

The $ prints fine in Serial Monitor but you don’t want a print statement in the main loop().
Any reason you are using Putty?

I prefer putty.

Similar behavior with Serial Monitor. It’s beginning to look like a hardware issue. Unfortunately I don’t have another serial-USB adapter to try.

Where did the dot and B come from?

Definitely try different hardware…

The “B” comes from the BLYNK_CONNECTED() callback after syncAll(). It doesn’t print the whole line. In my first screen cap it shows “Blynk Con”.

Not sure where the . comes from. Maybe from debug “Ready…” line.

Can one of the devs confirm if Blynk.run() and syncAll() are blocking calls? I still expect to see the timerEvent print as the virtual pin increases in the app.

syncAll - not. @vshymanskyy should know better.

tried your sketch (with out the print in the loop)

seems fine:

[2260] Connected to WiFi
[2261] IP: 192.168.0.19
[2261] Blynk v0.3.10 on Arduino
[5001] Connecting to 192.168.0.7
[5015] Ready (ping: 6ms).
Blynk Connect Callback
113timerEvent
114timerEvent
115timerEvent
116timerEvent
117timerEvent
118timerEvent
119timerEvent
120timerEvent
121timerEvent
122timerEvent
123timerEvent
124timerEvent
125timerEvent
126timerEvent
127timerEvent

i also used Serial.println(F(“timerEvent”)); not Serial.print(“timerEvent”);

Thanks,

I guess that means I have to try other hardware.