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
}
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 “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.