Problems with bluetooh + mqtt

Hello!

I'm trying to work with serial bluetooth and wifi+mqtt, but I'm having problems:

#include "BluetoothSerial.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include <WiFiClient.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;
String msgB;
String usermqtt;
const char* mqtt_server = "XXX.XXX.XXX.123";

WiFiClient wifiClient;
PubSubClient client(wifiClient);

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  if (WiFi.begin("SSID", "pass")) {
    client.setServer(mqtt_server, 1883);
    client.setBufferSize(30000);
    client.setCallback(callback);
    client.setKeepAlive(90);
  }

  usermqtt = "esp32/user/777";
}

void loop() {
  verificaMqtt();
  client.loop();
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  /*if (SerialBT.available()) {
    Serial.write(SerialBT.read());
    }*/
  if (SerialBT.available() > 0) {
    msgB = "";
    while (SerialBT.available() > 0) {
      char ch = SerialBT.read();
      msgB += ch;
      delay(5);
    }
    Serial.println("Mensagem:");
    Serial.println(msgB);
    Serial.println("Tamanho:");
    Serial.println(msgB.length());

    if (msgB == "page0") {
      Serial.println("Enviando...");
      sendMsg("page0ok");
    }
    else if (msgB == "page1") {
      Serial.println("Enviando...");
      sendMsg("page1ok");
    }
  }

  if (!SerialBT.hasClient()) {
    Serial.println("NAO CONECTADO");
  }
  delay(20);
}


void sendMsg(String a)
{
  uint8_t buf[a.length()];
  memcpy(buf, a.c_str(), a.length());
  SerialBT.write(buf, a.length());
}

void verificaMqtt()
{
  if (!client.connected())
  {
    Serial.println(F("DESCONECTOU MQTT, CONECTANDO NOVAMENTE"));
    connectMqtt();
  }
}


void connectMqtt() {

  if (client.connect(usermqtt.c_str(), "user", "pass1234")) {
    //Serial.println(F("conectado!"));
    String sub = "topic1/lyme3";
    if (client.subscribe(sub.c_str())) {
      Serial.println(F("Se inscreveu no tópico"));
      Serial.println(sub);
    }
  } else {
    Serial.print("falha, erro =");
    Serial.print(client.state());
  }
}

void  callback(char* topic, unsigned char* message, unsigned int length) {
  Serial.print("Mensagem chegou no topico: ");
  Serial.print(topic);
  Serial.print(". Mensagem: ");
  String messageTemp;
  for (int i = 0; i < length; i++) {
    messageTemp += (char)message[i];
  }
  messageTemp = "";
}

When loading this sketch, the wifi, bluetooth and mqtt connections are made without error, but I don't receive anything from the topic I subscribe to. And after a while the mqtt connection starts to wobble, then it disconnects and connects again, then disconnects and so on. But in this same sketch, if I take the Bluetooth functionality out of the code, the mqtt works without any problem. The mqtt connection does not drop and I receive messages normally.

Can anyone help me?

I am going to guess your micro controller, like a ESP8266 or an ESP32 or some other thingy has only one transmitter and one receiver and has room in ram for only one stack. That the MCU only has one radio your program can do only BT or WiFi but not both. Your issue is having only one radio.

I'm using an ESP32 and in Arduino IDE I put the partition scheme as "Huge App"...

Cool.

Still you can only one ONE application that uses the radio built into the ESP32. Just one.

One means only BT OR WiFi not BT and WiFi.