Blynk interfering with Bluetooth Serial on ESP32

I just added a bluetooth serial connection to my ESP32 project to send data back to a laptop. About 13k/second is being sent 130 bytes every 10ms. The throughput very slow, it stutters with frequent pauses.

I wrote a simple test program and found out that when Blynk is not active, the data comes through full speed but once Blynk.begin is called the bluetooth serial connection pauses, stutters and doesn’t run full speed. You don’t have to be doing anything with Blynk, just starting it up is enough to cause problems. Doesn’t matter if you call Blynk.run or not, same problem either way.

Here is test code. Fill in an app key and wifi info. As is it should work fine, just load it on an ESP32, pair to a bluetooth capable PC and open the COM port with a terminal program like putty. If you uncomment the Blynk.begin line the output on the PC should be slower and stutter.

#include "BluetoothSerial.h"
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

unsigned long
  loop_start,
  next_loop;

int
  barber_pole=10;
  
uint8_t
buff[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz!@#$%^\n";
uint8_t newline[]="\n";
const char auth[] = "your key";
const char ssid[] = "your ssid";
const char pass[] = "your pass";

void setup() {
  Serial.begin(115200);
  SerialBT.begin("yourname"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  loop_start = micros();
  next_loop  = 0;
//    Blynk.begin(auth, ssid, pass);

}

void loop() {
  int i;
  
  if((loop_start=micros()) < next_loop)
    return;

  next_loop = loop_start + 10000; // every 10ms (100hz)

  if(barber_pole < 130)
    barber_pole++;
  else
    barber_pole = 10;
  i = SerialBT.write(buff,barber_pole);
  Serial.println(i);
  i = SerialBT.write(newline,1);

//    Blynk.run();

}```

First of all:

Thanks for the suggestions, but I don’t see what in this code isn’t “clean”?

The loop is guaranteed to run every 10ms (100hz) and Blynk.run is called every time through the main loop. There are no delays or other blocking calls.

But when the Blynk code is active, bluetooth serial starts stuttering. If you look at the code you can see Blynk isn’t even doing anything, it just connects. There is nothing coming in from the app or being sent out.

First off, you didn’t format your code when posting it here… so it is messy for us to look at… AKA ignored :stuck_out_tongue_winking_eye:

If you think about it, Blynk is actually doing lots of stuff in the background… it is in constant communication with the Server, so what you are trying to do is have simultaneous control, in the void loop(), of two conversations at the same time.

While not ideal as far as “traditional” Arduino type Serial processes, when combining IoT and Serial communication needs, you would be better to have your Serial process in a quick timer controlled loop and then play around with the timing to balance the communication needs.

I did try to format the code but I don’t see any options for indenting text in this editor, or doing raw text formatted with spaces. I tried the preformatted text option but it did not seem to work. Are there some markup commands I missed? An “advanced” mode that gives you more commands? A lot of forums have a “code” markup but I didn’t see that here, and it would not let me upload the file. Happy to fix the format of the code but I don’t see the formatting commands to do it.

Regarding blynk, I understand it’s doing stuff in the background, what I meant was that I wasn’t doing anything with it that would cause long delays like send data to/from the app. In this test Blynk is just idling.

Also, I’m not sure if it’s clear but I’m talking about BLUETOOTH serial, so this is more of a network thing. Not a wired serial port.

I have previously run TCP and UDP stuff simultaneously with Blynk and not had any problems so I didn’t expect Bluetooth to be dramatically different.

Also the ESP32 is pretty fast compared to most microcontrollers, my main development code has a lot of profiling timers in it so I know the main loop is running every 10ms but only 4ms is ever used in a pass through loop(), the rest is yielded back to the CPU. The above test code is so simple I doubt if it’s even taking 1ms before it returns.

I also tried this test with my main development code which IS doing a lot more, reading I2C, talking to a Blynk app, sending stuff out USB serial, managing PWM for motors and a bunch of other things. The stuttering looked exactly the same in this case where the processor was much more loaded, and the stuttering ONLY showed up on the bluetooth serial, the USB serial sending the same data was not effected, nor were any of the other functions.

Whatever is happening, it only seems to effect bluetooth. The difference in throughput appears to be a good 4x compared to sending the data by serial, TCP or UDP.

The main reason I asked this was that I saw Blynk has some beta bluetooth support, so I was wondering if it might be doing something with bluetooth even though I don’t use bluetooth for blynk?

Sorry… TL:DR :stuck_out_tongue_winking_eye:

You deleted the formatting directions when creating your first post…

And you didn’t read the Welcome Topic.

As for the rest, I will leave it for others to chip in… I am in the middle of a bunch of stuff on my side as well. But I might get back to this later…

Edited original post to fix code formatting. Sorry to have missed this the first time, I did read the welcome but it was awhile ago and had forgotten how code formatting worked in this board.

Please keep in mind this is tested with an ESP32 board using the ESP32’s on-chip bluetooth. No idea how it would perform on a different bluetooth solution.

Key points why I don’t think this stuttering should be happening.

Blynk is run every 10ms, according to my timers (in a more complicated piece of code) each pass through the loop only uses 4ms worst case and probably a lot less with this test code. After finishing, loop() returns yielding 6ms or more to any background tasks.

There are no blocking delays in the test code

The delays start when you call Blynk.begin, calling Blynk.run doesn’t change things (same delay either way)

My Blynk app has many buttons and other widgets (for the more complicated example) but none of the widgets are setup to send or receive data automatically. When I tested the above code, the app wasn’t running.

Similar size messages sent by TCP or UDP do not have this stuttering problem.

Other thoughts…

When you add bluetooth serial to an app, it sucks up 570k of program space on ESP32, hadn’t noticed it was this large but there is still 7% of code storage free and lots of dynamic RAM.

I don’t think the issue is in the PC because when Blynk is shut off the data comes over full speed.

It’s always possible this could be a problem in the ESP32 bluetooth implementation, but again it only shows up when Blynk is built.in.

This test app doesn’t have handlers for any of the virtual pins the app uses, but my more complicated app does and it still has this problem.

Hope you can help me with this, thanks in advance.

FYI, ESP32 BLE & Blynk support is relatively new…

First off, as mentioned before, you have code running in the void loop()… trying to run thousands of times a second… which with the Blynk library is NOT recommended as it can interfere with the App/Server/Device communication required for IoT. Use timed functions for all your Serial and sensor routines.

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

Other than that, I don’t know, as I do not use BT or BLE with Blynk for any long term projects, just WiFi.

OK, now that I can read your code better, I took a quick look… you are NOT trying to use the BLE for Blynk communication like I first thought (instead of WiFi)… So what you are effectively doing is trying to run two timing intensive communication protocols at the same time. Situation still stands, use timed routine for the Serial and try to balance the timer between both Blynk’s needs and the Serial read needs.

With the ESP32 you could also try splitting the cores between tasks… check this out and search for more dual core references with and without Blynk.

Hi Gunner, thanks for the support.

I think you are still missing a few things…

  1. The code in loop() is not being run “thousands of times a second” it is running exactly 100 times per second (every 10ms) and the code that does run takes less than 4ms each time including blynk.run()

  2. This means 6ms or more are yielded back to background tasks that happen outside of loop()

  3. You are right I am not using bluetooth for Blynk, that is on WiFi, I am using bluetooth serial separately to send telemetry back to a data logger on a PC. Also note I am using bluetooth classic, not BLE

P.S. I have been running Blynk using this same loop setup for months now and so far there have been no problems except the one with bluetooth stuttering.

After doing some more research I guess the best question I can ask is: Does Blynk on ESP32 make any use of FreeRTOS task controll/creation APIs that are exposed to Arduino?

If it doesn’t, then all the stuff blynk creates should land on core #1, where the user code is.

I could execute blynk.run on core 0 but I’m not sure there is any way to trick the stuff setup in the blynk constructor or blynk.begin to run on another core…unless maybe you run blynk.begin on core 0.

This would be easy to figure out but unfortunately the code that provides the ESP32 task list is disabled in the arduino build and so far it looks like a big deal to enable.

IMPORTANT: I am connecting to blink cloud by WIFI, NOT bluetooth.

I did a lot of research today on this problem. The only thing I have found is that Blynk.begin must be setting up something that really messes up bluetooth serial on ESP32. Without Blynk library sources I can’t even guess what the problem is.

All I know is that without a call to Blynk.begin/Blink.Run bluetooth runs very fast. Once Blynk.begin is called, it starts stuttering, slowing down and losing data immediately. With debug level VERBOSE there is no indication data is being lost.

I’ve done some messing around with ESP32 freertos calls to try putting blynk on one CPU and my bluetooth code on another, it does not help. Altering priorities does not help.

Here is the test code for ESP32, I hope someone will have a look at it.

// Blynk/Bluetooth Serial problem sample
// THIS USES THE CLOUD SERVER FOR BLYNK!
// Regardless of what CPU the loopmain and blinktask are on (same one, two different ones) the output starts and stops, losing data
// without logging an error.  On the receiving end is a Windows 10 laptop using PuTTY to open the bluetooth serial port.
//

#include "BluetoothSerial.h"
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <freertos/task.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#define BLYNK_NO_BUILTIN

BluetoothSerial SerialBT;

int
  barber_pole=10;
  
uint8_t buff[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz!@#$%^\n";
uint8_t newline[]="\n";
const char auth[] = "yourauth";
const char ssid[] = "yourSSID";
const char pass[] = "yourpass";

void loop()
{
  vTaskDelay(11);
}
void blynktask(void *pvParameters)
{
  Blynk.begin(auth, ssid, pass);
  while(1)
  {
    Blynk.run();
    vTaskDelay(6);
  }
}
void loopmain(void *pvParameters) 
{
  while(1)
  {
    if(barber_pole < 130)
      barber_pole++;
    else
      barber_pole = 10;
    SerialBT.write(buff,barber_pole);
    SerialBT.write(newline,1);
    vTaskDelay(9);
  }
}
void setup() {
  Serial.begin(115200);
  SerialBT.begin("BalanceBot02"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");

//                                                   priority v  CPU#  v
  xTaskCreatePinnedToCore(blynktask, "blynktask", 4096, NULL, 3, NULL, 1);
  xTaskCreatePinnedToCore(loopmain,   "loopmain", 8192, NULL, 3, NULL, 1);
}

Hi guys, just FYI I’ve been having some trouble using bluetooth serial for sending log data to a PC while using Blynk on WiFi. Having blynk in a program with bluetooth serial really messes up bluetooth serial, stuttering, dropped data…the works.

See Blynk interfering with Bluetooth Serial on ESP32 for details.

@greg.corson That topic is about using BLE & ESP32 as intended with Blynk… as a link between device and App.

Your issue is unrelated and not really Blynk issue specific as both WiFi and BT/BLE use the same 2.4GHz and may simply be interfering, or the usual timing issues when running two timing intensive communication protocols together.

I am moving your post back into your relevant topic.

Sorry if this is a bother, I made that small post in the other topic because it’s possible adding BLE support to Blynk might be related to the problem I see with Bluetooth Serial.

I do get your point about running multiple protocols at the same time, however in my experience it just doesn’t seem right that a 240mhz dual core CPU should have trouble with such a small amount of data on Bluetooth only when Blynk is loaded. I have had the same problem with Blynk and TCP also. Packets seem to get stalled for no reason, no error or congestion on the network, when Blynk is loaded, even with only “virtual” pins and no data being exchanged on Blynk (ie: no data pushed from ESP32 to the app and vice versa). No errors are reported either.

Please understand I think Blynk is a fantastic product. I am using it as a control panel for a 2 wheel balancing robot. Unfortunately the robot also needs to communicate with other things by WiFi or bluetooth and Blynk seems to be interfering with that so reluctantly I have to remove Blynk support.

Once it is done, my new setup still will be doing TCP and Bluetooth simultaneously, if this works then the problem obviously is something Blynk is doing internally. I’ll report back when I have some results.

Blynk’s BT/BLE support is only added when you add the required BT/BLE library, which is not possible when you are already using the Blynk WiFi library… so unrelated.

While I and others like yourself tend to use it as a GUI for non IoT control, Blynk is primarily intended for IoT application. Thus, as I understand it, Blynk does seem to have constant and prioritised communication to the Server/App… so sharing Blynk’s timing with other timing sensitive items is, and may always be, an issue.

Just the nature of the beast.

I just wanted to give you the final results of my tests.

I have an ESP32 setup to generate roughly 100 byte packages of data at a rate of 100hz.

The test sends this data out over bluetooth, tcp, udp and serial ALL AT THE SAME TIME so it is sending about 40k per second all together. It is doing this without any problems, no network congestion, no delay, no lost data, no stuttering. It’s received smoothly at my PC over all 4 interfaces at once.

However, if I build the same test with Blynk and try to use even one other network protocol the network traffic stutters and data is lost, the Blynk app is not being touched and no data is being pushed so it’s just doing keep-alive processing to the server over WiFi.

So the ESP32 can handle constant traffic over 4 interfaces at once with no problem, but when you compile in Blynk you can’t even use 1 interface without issues.

I think this shows that Blynk is doing something internally that messes up other network functions. I’m sure it’s probably unintentional, maybe it only effects ESP32?

I’m NOT trying to run-down Blynk, it’s a great tool and I would have liked to have kept it in my app, but I require at least 1 network connection and Blynk seems to disrupt all of them, so I have reluctantly had to remove Blynk.

It’s entirely your call, but if it was me I would at least have a look at it because even if your using Blynk just for IOT, you still might want to be able to use other networking without problems.

Anyway, I won’t bug you about it anymore, it’s up to you. Happy to answer questions about the tests.

Possibly… as I understand it Blynk is focused on their full IoT utilisation and less on just using the App as a GUI for other applications.

Again, possibly… I know linking Blynk App with BLE & ESP32 is still a work in progress… who knows what issues might be affecting things with the relative youth of the whole ESP32 infrastructure.

Nope :stuck_out_tongue_winking_eye: … I am just a fellow Blynk user, NOT a developer. I do tend to interact in these type of conversations as it often helps stimulate awareness and draw in others assistance.

Hi,

I think the problem is not related to Blynk. In fact, it is known that using Wifi and Bluetooth on ESP32 simultaneously is difficult. Wifi and Bluetooth/BLE are using the same RF module which are competing, so it’s not the CPU that is the bottleneck. Don’t try to run the code calling wifi on one core, and the code calling Bluetooth on another: they will fight for the RF module. When using BlynkTimer to send bluetooth messages will be probably better, as they will not try to use wifi & bluetooth at the same time.

But heap memory might also be a problem.

The ESP32 is actually started in dual mode BLE & BT classic:ESP_BT_MODE_BTDM, so it is in fact initialising Wifi, Bluetooth classic and BLE. And it takes too much memory to load all 3 stacks. Google “esp32 wifi ble simultaneously” and you will find a lot about the problem. I know that you are trying to combine Wifi & BT classic, but there are simply more people combining Wifi & BLE, so you will find more info about that problem.

Maybe you can try to use the following command:

esp_bt_mem_release(ESP_BT_MODE_BLE);

esp_bt_controller_mem_release(mode) should be called only before esp_bt_controller_init() , which is called by SerialBT.begin

If you succeed, please tell how, I think a lot of people are interested in it.

An alternative would be to use BLE instead of BT classic. For that you will probably have to combine UART over BLE (https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/Arduino/BLE_uart/BLE_uart.ino ) and use esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)

1 Like

Working like charm on IPhone 7plus + ESP32S + Blynk 0.6.1