0

Recently I used bluepy in ESP32 and RPI4 Bluetooth connection, ESP32 for server, RPI for clint, found that the data is collected about 5 to 50 times , the connection will be disconnected, after querying the google, I found that someone mentioned before, but it seems that the problem has not been solved, please ask 2022 Someone has found a solution? This is a question raised by others stackoverflow

This is the resource of code source

This is my code

from bluepy import btle
import time

MAC = "mymac"
SERVICE_UUID = "myID"
CHARACTERISTIC_UUID = "myID"

print("Connect to:" + MAC)
dev = btle.Peripheral(MAC)

print("\n--- dev ----------------------------")
print(type(dev))
print(dev)

print("\n--- dev.services -------------------")
for svc in dev.services:
    print(str(svc))
    
print("\n------------------------------------")
print("Get Serice By UUID: " + SERVICE_UUID)
service_uuid = btle.UUID(SERVICE_UUID)
service = dev.getServiceByUUID(service_uuid)

print(service)
print("\n--- service.getCharacteristics() ---")
print(type(service.getCharacteristics()))
print(service.getCharacteristics())

#----------------------------------------------
characteristics = dev.getCharacteristics()
print("\n--- dev.getCharacteristics() -------")
print(type(characteristics))
print(characteristics)
while True:
    for char in characteristics:
        if(char.uuid == CHARACTERISTIC_UUID ):
            print(char.read())
            time.sleep(0.5)
4
  • what is your specific question?
    – jsotola
    Apr 14, 2022 at 1:21
  • @jsotola OK, I have update
    – umr
    Apr 14, 2022 at 1:27
  • @jsotola I want to fix the problem of abnormal disconnection
    – umr
    Apr 14, 2022 at 1:27
  • You seem to be reading the data every half second from the ESP32. Typically if you want frequent updates like that you would have the ESP do BLE notifications that the RPi client would subscribe to. You haven't mentioned in your question what you have done to debug that this is a Bluepy issue. Have you tried an alternative library such as pypi.org/project/bleak or pypi.org/project/BLE-GATT
    – ukBaz
    Apr 14, 2022 at 6:55

1 Answer 1

0

Here's a (fairly) quick way of finding where the problem is. Assuming the problem is the same as your stackoverflow nRFconnect reference - I have tried this procedure with nRF, and can read a characteristic hundreds of times with no disconnection. I was doing the reads by hand, but faster than 2 per second.

1.To the Pi, download (just 4 files) and compile btferret, as described in section 2 of the documentation from here:

https://github.com/petzval/btferret

  1. Edit the devices.txt file "My other Pi" entry - insert the Pi's address so the entry is like this:
DEVICE = My Other Pi   TYPE=mesh NODE=2  ADDRESS = DC:A6:32:04:DB:56
  LECHAR = Control  PERMIT=06 SIZE=8   HANDLE=0005           
  LECHAR = Info     PERMIT=06 SIZE=1   HANDLE=0007            
  LECHAR = Name     PERMIT=06 SIZE=16  HANDLE=0009 UUID=2A00  
  1. Start sudo ./btferret
  2. Enter s for server
  3. Choose LE Server
  4. Set timer interval = 0
  5. The Pi is now listening as an LE server with the 3 LECHAR characteristics
  6. Connect from the client
  7. Read characteristics many times. A message will print on the Pi screen each time. It will also report if the client disconnects. There are two possible results:

It DISCONNECTS:

btferret will never initiate a disconnection unless it is told to, so the problem is with nRF or ESP32

No DISCONNECTION (that's what I found):

btferret does not use bluez - so the problem is with bluez/bluepy. If it's bluez, you are stuffed because the inner workings are undocumented and unfixable.

1
  • I think it's a concrete approach, thank you, I'll try it out.
    – umr
    Apr 15, 2022 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.