Issues with Bluetooth ESP32

Hello, it´s the firt time that I write here.

I am using the library "BluetoothSerial.h", and I have problems with the interruptions.

The first question that i have is: When I send a message by bluetooth, the interruption triggers more than ones, and I cannot understand why. I am the only one with this problem?

And the other question is (and the most important for me), does someone know how to disable the bluetooth? and also enable it.
I tried to find this commands on internet, but I don't find them.

Thank you so much for your time.

I forgot to say that I am programming in Arduino.

I think that .begin() and .end() are what you want.

I am using the library "BluetoothSerial.h", and I have problems with the interruptions.

You will need to explain more about your "problems" and what are the "interruptions"?

You will need to post some complete code which compiles and demonstrates the issue. Please use the code symbol at the top of the reply window, and post your code inside the marks.

THANK YOU!!!
For the idea of the SerialBT.end(), I didn't think about it, I have spent more than twelve hours trying to make this work.

Yes, you are right, I did't give enough information.

My problem is:
I am trying to send a message by bluetooth to the ESP32 (because I am working in my final degree project at the University), and the problem is that the ESP32 detect more than 1 message when I only send 1, I cannont understan why.

#include "BluetoothSerial.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;

void setup() {
  Serial.begin(9600);
  SerialBT.begin("ESP32_Invernadero"); //Bluetooth device name
}

void loop() {
    if (SerialBT.available()) { // THIS IS THE INTERRUPTION THAT TRIGGERS WHEN A MESSAGE IS DETECTED
      char Lectura=SerialBT.read();
      switch(Lectura){
      case '0':
        valor=0;
        Serial.println("111"); // TO KNOW IF THIS IS DETECTED
        // I ADD WHAT YOU TOLD ME, AND TURNING IT OFF AND THEN ON, I CANNOT DETECT ONE THAT I DIDN'T SEND
        SerialBT.end(); // ADDING THIS LINE IS THE ONLY WAY THAT O FIND TO MAKE IT WORK CORRECTLY
        SerialBT.begin("ESP32_Invernadero");
        // END OF THIS IDEA TURNING IT OFF AND THEN ON
        break;
      case '1':
        valor=1;
        Serial.println("222");
        break;
      default:
        Serial.println("INCORRECT");
        break;
      } // switch
  }//END interrupcion bluetooth

}

I wrote something so It could be easier to undertand it.

If I remove this two lines between '// //' It triggers more than ones.
Fist it detect well the interrupt, and prints "111", but then it prints "INCORRECT" two times, three times or maybe more times when I did't send any other message.

With your idea is the only whay that I can make it work triggering only ones.

Could someone could tell me if this code works well without this two lines, because maybe the problem is with my ESP32, or maybe not and I am not alone with this issue.

Thank you so much for your help :smile:

The issue is that your phone app (or whatever is communicating with the ESP32) is sending a new line and carriage return appended to the '0' or '1' character.

You should be able to set the app to send the command character without any terminations. I use Kai Morich's Serial Bluetooth Terminal app and there is a setting for line endings on send.

Alternatively, you can tell your program to ignore the '\n' or '\r' characters.

if (SerialBT.available())
  {  // THIS IS THE INTERRUPTION THAT TRIGGERS WHEN A MESSAGE IS DETECTED
    char Lectura = SerialBT.read();
    if (Lectura == '\n' or Lectura == '\r') return;
    else
    {
      switch (Lectura)
      {

With either of these changes you will not need to stop the Bluetooth. .begin() should be called once in setup().

Thank you so much for your ideas

I tried it, but it didn't work, maybe because I am reading with an String
Every time that the code detect a message sent by bluetooth, I print the numbers of the caractes of the thing that is read to see if it is correct, and the thing that is read.

It prints nothing, so I thought that it detect two spaces.
But no, because in the if that you wrote I added:

if (Lectura == "\n" or Lectura == "\r" or Lectura == "  "){  

// I didn't chang the rest of your code, only this part

}

Between " " there are two spaces

I also changed ' ' -> " " because I am reading String, not char.

And it continues triggernig more than onces :slightly_frowning_face:

I can only make it work If I turn off and then on the bluetooth, it the only way

There is no String class object in the code you posted. In the code you posted Lectura is typed as a single char.

Every time that the code detect a message sent by bluetooth, I print the numbers of the characters of the thing that is read to see if it is correct, and the thing that is read.

Again, there is nothing like this in the posted code.

I can not help you find a solution to your issue if you do not post the code you are actually running. The code of post #3works correctly with the changes shown in post #4.

I do not send you any code which I do not check out on my ESP32 and phone.

What is sending the command message to the ESP32?

I can only make it work If I turn off and then on the bluetooth, it the only way

You would be the first person in the history of this forum to need to do this. It is not a solution to your issue.

Sorry
I am working with two options, one that works with char and other with String

The one that works with char, it works perfectly with what you told me, one more time thank you for your time, seriously thank you :smile:

I want to apologise for not explaining myself correctly, i don't want you to think that i am an idiot, I am working with this project, the last subject of the university and my brain is exhausted.

Explaining the problem with the code that works this String:

I use the same app as you, I am going to post screenshots of the setting, because maybe there are something wrong there, but I didn't change anything.
And also what I send in the app.
I will also post a screenshot of what It's printed on my computer.

Explanation:
I also coment a lot of thing so it is easier to understand it

The maximun numbers send by bluetooth of charactes are going to be three ended with a "."
Example: 1000. this is how i wanted to be send by bluetooth, but i send a 10. to check it (you can see it in the screenshot below.

what does this code do?
two numbers are going to be send by bluetooth
First it is going to be sent one, is going to be checked if it is only numbers, and if this is correct is going to be transform from String->int, and finally it is going to be saved in an array called "Data_Base"

This two numbers are called: "Humedad" and "Lux"

As you can see in the screenshot from my computer ( the first one, the other one is from my phone) It reads my number correctly and then triggers when it shouldn't checking the second number (number that I didn't send), and then ask for this second number

I don't know if i provide all the information that is needed

I hope this works correctly on your ESP32, therefore I will ask my teacher for another one tomorrow Monday.
If not, I hope you could tell me how to make it work well.

One more time, thank you for your time.

#include "BluetoothSerial.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;


int Data_Base[2]={0,0};
int valor=1; // if valor=1 it will ask for the first value, valor=2 it will ask for the second value
int entero=0;
int primera=0;

void setup() {
  Serial.begin(9600);
  SerialBT.begin("ESP32_Invernadero"); //Bluetooth device name

}

int COMPROBAR(String Lectura, int Cantidad){ // this function "COMPROBAR" works correctly :) I check it in other exmple
  int correcto=0;
  int total=Lectura.length(); 
  
  Serial.println("Numero de caracteres: ");
  Serial.println(total);

  if(total==0)
    return 0;
  if(total>Cantidad)
    return 0;

   for (int i=0; i<total; i++){ //comoruebo que todo son nuermos
      if(Lectura.charAt(i)=='0' ||
         Lectura.charAt(i)=='1' ||
         Lectura.charAt(i)=='2' ||
         Lectura.charAt(i)=='3' ||
         Lectura.charAt(i)=='4' ||
         Lectura.charAt(i)=='5' ||
         Lectura.charAt(i)=='6' ||
         Lectura.charAt(i)=='7' ||
         Lectura.charAt(i)=='8' ||
         Lectura.charAt(i)=='9'   ){
          correcto++;
       }
    }// fin for 
    
    if(correcto == total){
        Serial.println("CORRECT, YOU ONLY WROTE NUMBRES");
        return 1;
     }else{
        Serial.println("INCORRECT");
        return 0;
     }
} // fin COMPROBAR(String Lectura, int Cantidad)
 
void loop() {

  if(valor==1){ // valor=1 because I want this to be repeated until the data that I send is correct, when it is correct I will change valor=2
    
    if(primera==0){ // because i am in a loop, i only want this to be printed ones until a message is send
      primera=1;
    Serial.println("Write humedad: "); // I ask for the first number
    }
    
    if (SerialBT.available()) {
      String Lectura=SerialBT.readStringUntil('.'); // I need to finish the numbers with a . to be correct

      if(Lectura == "\n" or Lectura == "\r" or Lectura == "  "){ // i also check is this are two spaces
        Serial.println("Cheking if I am here");
        return;
        }else{
          primera=0; // so "Mete dato humedad: " can be printed the next time
      Serial.println("Numero metido one: "); // to chek if I am here
      Serial.println(Lectura);
      entero=COMPROBAR(Lectura,3); // To know If all the characters are numbers, it will return a 1 if it is correct, I send the number read and the maximun number of characters
      if (entero==1){ // the number read is correct
        valor=2; // if the numeber is correct This code will ask for the next number
        int myInt = Lectura.toInt(); // change string to int
        Serial.println("YESS"); // print yesss to know i am here
        Serial.println(myInt);
        Data_Base[0]=myInt;
        Serial.print(Data_Base[0]); // and i print both numbers of the array
        Serial.print(" ");
        Serial.println(Data_Base[1]);
        }else{
          valor=1; // if the number is not correct I want to do this until is correct , so it is going to be repeated
          }
        }
  }//fin interrupcion bluetooth
}// fin valor 1

  if(valor==2){ // when valor=2 it will ask for the other value
    
    if(primera==0){
      primera=1;
    Serial.println("Write Lux: ");  // I ask for the second number
    }
    
    if (SerialBT.available()) {
      //primera=0;
      String Lectura=SerialBT.readStringUntil('.');

      if(Lectura == "\n" or Lectura == "\r" or Lectura == "  "){
        Serial.println("entre aqui dos");
        return;
        }else{
          primera=0;
      Serial.println("Numero metido 2: ");
      Serial.println(Lectura);
      entero=COMPROBAR(Lectura,3); // para saber que todos los caractares son numeros
      if (entero==1){
        valor=1; // it will ask for the first value
        int myInt = Lectura.toInt(); // lo transformo a int
        Serial.println("YESS");
        Serial.println(myInt);
        Data_Base[1]=myInt;
        Serial.print(Data_Base[0]);
        Serial.print(" ");
        Serial.println(Data_Base[1]);
        }else{
          valor=2;
          }
        }
  }//fin interrupcion bluetooth
}// fin valor 3



}

You are still having issues with the line terminations being sent by the phone app. Since you have complete control over the messages and the format, you are best off setting the app for no line endings on sending.

If you really want to keep line endings in the message, then you would do better to use .readStringUntil( '\n') instead of .readStringUntil('.').

Thank you so much!!!

It works perfectly, than you so so much for your time

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.