Problem with ESP32 bluetooth

Hi everyone,

I am trying to connect ESP32 via bluetooth classic to my PC to transfer some data from a sensor.

As a first step, I am simply trying to send some random data over bluetooth serial. The code is at follows:

#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

int myData[8];
BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  Serial.println("Opening Serial Connection on COM4");
  SerialBT.begin("ESP32"); //Bluetooth device name
  SerialBT.println("Opening Serial Bluetooth Connection on COM6");

  
}

void loop() {

  for(int i = 0; i < 8; i++)
  {
    myData[i] = random(0,500);
  }
  if(Serial.available())
    {
 
     for(int i = 0; i < 8; i++)
  {
    SerialBT.write(myData[i]);
  }
   
      
    }
    delay(20);
}

In Windows bluetooth settings, I have paired the ESP32 with windows and connected it. I have set up incoming and outgoing COM ports for bluetooth where incoming port is COM6 and outgoing port is COM5.

However, when I open arduino serial monitor for COM6, it is empty and no data is being printed. Furthermore, on COM4, there is nothing printed despite the Serial.println("Opening Serial Connection on COM4"); command which I think should work regardless of bluetooth.

I have tried everything I can think of. I would be grateful if anyone can help me solve this problem as I have been stuck for days

The posted code will only send data via bluetooth if incoming data is available from Serial, so you need to send data to the ESP from the serial monitor before bluetooth starts to transmit. The incoming serial data is never cleared, so whenever data is received bluetooth will be spammed.

You should try to modify your code to send data via bluetooth once every second instead:

void loop() {

  for(int i = 0; i < 8; i++)
  {
    uint8_t data = random(0,255); //Range!
    SerialBT.write(data);
  }

  delay(1000);

}

Thank you for the reply. I replaced the loop code with the code you wrote but unfortunately there is till no data on the serial monitor. Any idea why?

Okay, thank you for the help. It is working now with the change you suggested and also when SerialBT.write(data) is replaced by SerialBT.print(data). I am not sure why this is the case. Any idea why?

write(data) writes the value of the data
print(data) prints the character representing the data

Thanks for the answer.
Yes, I understand that. However, for some reason nothing gets displayed on serial monitor when i use write(data). The values are only getting displayed when I use print(data)

Maybe 'data' is a non printable ASCII character ?

If you look at an ASCII-table, you will see that the letter 'A' has the decimal value 65.

char ch = 65; //or ch = 'A'
Serial.write(ch);
Serial.print(ch);

Should print 2 times 'A'. Another thing to mind is that the serial monitor may require a line-ending (can be send with "Serial.println();") before the received data is displayed.

Data is an integer number

Okay, thank you for the reply.
One more thing, could you refer me to a serial library capable of fast communication?
Currently, I am using SimpleSerial and it is too slow for my requirement

Serial.begin(2000000);
I2C at 1 Mbit/s
SPI at 60 Mbit/s

How fast do you want to go ?