2

I've been trying to get a bluetooth LE IoT set up to work with multiple bluetooth LE devices that periodically turn on and transmit. I'm using code similar to https://engineersportal.com/raspberry-pi-ble-code to manage the connections:

from bluepy import btle
import struct, os
from concurrent import futures
import time

addr_var = ['d8:a9:8b:b0:d0:49', 'd8:a9:8b:b0:da:dd']

class MyDelegate(btle.DefaultDelegate):

    def __init__(self,params):
        btle.DefaultDelegate.__init__(self)

    def handleNotification(self,cHandle,data):
        global addr_var
        global delegate_global
        print('got data: ', data)
        try:
            data_decoded = struct.unpack("b",data)
            print("Address: "+addr_var[ii])
            print(data_decoded)
            return
        except:
            pass


def perif_loop(perif):
     try:
        if perif.waitForNotifications(8.0):
            print("waiting for notifications...")
     except Exception as e:
        pass
     finally:
        print('disconnecting!')
        try:
            perif.disconnect()
            time.sleep(4)
        except Exception as e:
            print('failed to disconnect!', e)
            pass

def establish_connection(addr):
    while True:
        try:
            print("Attempting to connect with "+addr)
            p = btle.Peripheral(addr)
            p_delegate = MyDelegate(addr)
            p.withDelegate(p_delegate)
            print("Connected to "+addr)
            perif_loop(p)
        except Exception as e:
            print("failed to connect to "+addr, e)
            time.sleep(1.0)
            continue
ex = futures.ProcessPoolExecutor(max_workers = os.cpu_count())
results = ex.map(establish_connection,addr_var)

This works for a bit, but throws connection errors (Failed to connect to peripheral d8:a9:8b:b0:da:dd, addr type: public), and eventually gets into bad state where no device will connect and bluetooth has to be restarted with sudo systemctl stop bluetooth, sudo systemctl start bluetooth.

Looking further with strace, the error received is "Connection refused (111)" in the bluepy-helper executable.

Has anyone managed to get this or something similar working in a reliable manner (does not miss broadcasts) for long periods of time?

2
  • Hi and welcome! Please add the configuration or code you used to connect to these Bluetooth devices, not just a link to the instructions. Please also add the errors that you're getting.
    – David
    Commented Apr 7, 2019 at 21:47
  • I posted my code, but it doesn't make a difference compared to the code in the link
    – RyanCheu
    Commented Apr 7, 2019 at 23:24

1 Answer 1

1

bluetooth LE devices that periodically turn on and transmit, ... eventually gets into bad state where no device will connect and bluetooth has to be restarted with sudo systemctl stop bluetooth, sudo systemctl start bluetooth.

Yes, I have 10+ Bluetooth serial modules and have the same problem. It is annoying to often hardware power off/on Rpi to POR (Power On Reset) the devices.

My way to get around is to use separate PSU (Power Supply Units) and power switches for individual or individual groups of Bluetooth modules.

Then I can manually by hand, pusing a button, or software by Rpi GPIO to hardware reset any individual or individual groups of Bluetooth modules, without resetting the Rpi.

I modified LM2596 PSU modules and LM2941 through hole devices and found them working smoothly, saving me a lot of time resetting and restarting devices.

The pictures of my manual/auto Bluetooth device hardware reset things are displayed below.

Software Resettable PSU and power switches

LM2941 power switch

LM2596 3A Voltage Regulator - TI

LM2941 1A Voltage Regulator - Nat Semi

Software resettable PSU

2
  • While this technically solves the issue, I was ideally looking for some way to avoid the crashing in the first place
    – RyanCheu
    Commented Apr 16, 2019 at 20:45
  • @RyanCheu I understand. But Bluetooh communication using Rpi these days (Rpi3B+ and stretch) is disappointingly not mature, and unreliable and so it is very difficult to avoid crashing and intermittent disconnection.
    – tlfong01
    Commented Jun 14, 2019 at 9:49

Your Answer

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

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