Problem in receiving the data via bluetooth in python using esp32

im trying to print the values in bluetooth from esp32 and i want to receive the data in python by using bleak library.
the problem im facing with this whenever i run the program it shows address not found.
ill attach the code for the reference purpose.
ESP32 device details:
Address: 0C:B8:15:F6:78:4A
Minor Type: PDA
RSSI: -49
Services: 0x802000 < Braille ACL >

char *devicename = "ESP32";

#include "BluetoothSerial.h"

BluetoothSerial SerialBT; 


void setup() {
// initialize serial monitor
Serial.begin(115200);
SerialBT.begin(devicename); //Bluetooth device name

// get Bluetooth classic address and print to serial monitor
uint64_t btAddr = ESP.getEfuseMac();
SerialBT.print("Bluetooth classic address: ");
SerialBT.println(btAddr, HEX);
}

void loop() {
// code here will not be executed
}```

the python code:

import asyncio
import logging
from bleak import BleakClient
logging.basicConfig()
log = logging.getLogger(__name__)

async def run(address, loop):
    log.info("Connecting to %s", address)
    async with BleakClient(address, loop=loop) as client:
        log.info("Connected. Reading data...")
        while True:
            data = await client.read_gatt_char("<802000>")
            log.info("Received data: %s", data)

if __name__ == "__main__":
    # Replace <device address> with the address of your Bluetooth device
    address = "<0CB815F6784A>"
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(address, loop))
error:    raise BleakDeviceNotFoundError(
bleak.exc.BleakDeviceNotFoundError: Device with address <0CB815F6784A> was not found

So data-direction is ESP32------->--->---->------Computer_running_python

and you are trying to send the data by using the library BluetoothSerial.h

I'm not very familiar with bluetooth but to me the library bluetoothSerial.h
seems not belonging to the ESP32

the comment says

// get Bluetooth classic address and print to **serial** monitor

but what you are doing is printing to SerialBT

uint64_t btAddr = ESP.getEfuseMac();
SerialBT.print("Bluetooth classic address: ");
SerialBT.println(btAddr, HEX);

is this really your intention?

Did you test the code with really sending to the serial monitor?
which would be

// get Bluetooth classic address and print to serial monitor
uint64_t btAddr = ESP.getEfuseMac();
Serial.print("Bluetooth classic address: ");
Serial.println(btAddr, HEX);

For a successfull bluetooth connection your computer must have done pairing
Then you have to activate the connection on your computer
(I don't know if your python script is doing this)

I tested the following code with a android-app for bluetooth serial

After the initial steps like described above this democode below is working


char *devicename = "ESP32";

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;


void setup() {
  // initialize serial monitor
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
  SerialBT.begin(devicename); //Bluetooth device name
  // get Bluetooth classic address and print to serial monitor
  uint64_t btAddr = ESP.getEfuseMac();
  Serial.print("Bluetooth classic address: ");
  Serial.println(btAddr, HEX);
  Serial.print("start sending Hello World with bluetooth serial");
}

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );  
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

unsigned long MyTestTimer = 0;                   // Timer-variables MUST be of type unsigned long
const byte    OnBoard_LED = 2;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}

void loop() {
  BlinkHeartBeatLED(OnBoard_LED,250);

  if ( TimePeriodIsOver(MyTestTimer,2000) ) {
    SerialBT.println("Hello World");
    Serial.println("did call SerialBT.println(Hello World);" );
  }  
}

best regards Stefan

As you are using a ESP32 how about using WiFi and sending UDP-messages?

Using WiFi/UDP seems to be easier as the configuration-data you need is simply
SSID, Wifi-password, IP-adress of computer
and matching the portnumber in the python-script and the ESP32-sketch

best regards Stefan

Is that the right format? Try address = "0C:B8:15:F6:78:4A"

Are you on a PC or a Mac ?

(macOS does not provide access to the Bluetooth address for privacy/ security reasons. Instead it creates a UUID for each Bluetooth device which is used in place of the address on this platform)

hi, stefan thanks for the reply. in the above code im using Serial.BT for to test its printing in bluetooth terminal but the python code is compiling and im getting error : address not found.

im using udp with wifi and it works. my professor want me to do with reading from the serialBT from python

hi jackson, thanks for reply, i had tried with both the format still its not working im using the address from the esp32 which prints its value in hex. for uuid when i connect with the mac it's shows 802000 but when i tried with the windows it shows e0cbf06c-cd8b-4647-bb8a-263b43f0f974. i don't know what im doing wrong in python. or with the arduino

BTW - are you sure this chipID is even the BT "address" ?

your ESP will be advertised through its device name (in your case "ESP32")

The ESP32 part is working as shown with the democode of post # 2

This means the problem is the MAC-adress and / or the python side

@shawn03693

as it is more likely that this is a python problem:

did you test bluetooth receiving with your computer with some other device ?
If not you should do so

For the python-part you should do a search on stackoverflow and if found nothing helpful posting your question at stackoverflow.

@the users that are thinking "this is cross-posting"
well are you able to give advice about the python-part??
If so why haven't you posted yet???!
I guess you don't. So as the project is more than just arduino programming it is not cross-posting

@shawn03693
you should post on stackoverflow very detailed
if you are using python 2 or python 3
espescially the computer and the OS you are using is important.
and
including your ESP32-sketch and your python scripts

Another place to ask is a python forum and if there exists a python-subsection on dischord

beneath bleak there is pybluez as a package for bluetooth in python

best regards Stefan

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