How can I override BLE MTU? (or alternative solution)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
dougconran
Posts: 27
Joined: Sat Jul 03, 2021 5:10 pm

How can I override BLE MTU? (or alternative solution)

Post by dougconran » Sat Jul 03, 2021 5:45 pm

I'm trying to develop a BLE central app to read OBDII readings using Micropython. I've started off by using the ble_simple_peripheral.py and ble_simple_central.py programs from the MP bluetooth examples on two ESP32 devices running MP v1.14 (because I'm using Russ Hughes ST7789 module). Those are working but I now need the peripheral to send larger chunks of data (about 150 bytes) and the central is only receiving 20 bytes, presumably limited by the MTU setting. After a fair amount of googling I've found that I can/should be able to use BLE.config - which I can do to change the 'rxbuf' size (which has had no affect) but I get an error (NameError: name 'mtu' isn't defined) when trying 'mtu' (BLE.config(mtu,150)).

Is there a way (other than building a new MP port which I'm not capable of) of changing the MTU size? Alternatively, is there another ESP32 port that has BLE plus the ability to display on a ST7899 screen (such as the TTGO)?

I would be very grateful for any help/advice/suggestions - many thanks.

efahl
Posts: 15
Joined: Sat Dec 23, 2017 2:02 pm

Re: How can I override BLE MTU? (or alternative solution)

Post by efahl » Sun Jul 04, 2021 2:42 pm

Shouldn't mtu be a literal?

Code: Select all

BLE.config('mtu', 150)

dougconran
Posts: 27
Joined: Sat Jul 03, 2021 5:10 pm

Re: How can I override BLE MTU? (or alternative solution)

Post by dougconran » Sun Jul 04, 2021 3:23 pm

Thanks for the reply, but that doesn't work either. It gives an error: TypeError: must query one param

If I do: ble.config(rxbuf=500) that works but ble.config(mtu=150) or any other variation of that gives an error of one sort or another.

From the various error messages I would guess that mtu is not defined (although it is specified in the ubluetooth docs), so, if that is true, the question is whether it is possible to implement it in some way (other than rebuilding Micropython which I'm not yet capable of doing) or whether there is some other ESP32 port of MP that does have that parameter (and the ability to write to a ST7789 display).

efahl
Posts: 15
Joined: Sat Dec 23, 2017 2:02 pm

Re: How can I override BLE MTU? (or alternative solution)

Post by efahl » Mon Jul 05, 2021 6:30 pm

Yeah, the docs pretty clearly state that it's read-write, just like 'rxbuf', but I wonder if it's read-only in the ESP32 port? Did you try print(BLE.config('mtu')) to query the value?

dougconran
Posts: 27
Joined: Sat Jul 03, 2021 5:10 pm

Re: How can I override BLE MTU? (or alternative solution)

Post by dougconran » Mon Jul 05, 2021 10:09 pm

Ha! it has worked - but only if I set 'mtu' AFTER I've called central = BLESimpleCentral(ble) using ble.config(mtu=500) (I set it to 500 because it is already set to 256 although I'm only getting 20 characters returned in notify).

Code: Select all

    ble = bluetooth.BLE()
#    ble.config(mtu=500)  <----------  gives error: OSError: [Errno 19] ENODEV
    central = BLESimpleCentral(ble)
    ble.config(mtu=500)
    print("MTU = ",ble.config('mtu'))  #  <------  works if after call to BLESimpleCentral"
I'll test whether that makes a material difference tomorrow.

Maybe one of the BLE gurus (@Jimmo?) can explain what is going on.

However, as of now (prior to further testing), that seems to have done the trick so many thanks for your suggestions.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How can I override BLE MTU? (or alternative solution)

Post by jimmo » Tue Jul 06, 2021 3:58 am

dougconran wrote:
Mon Jul 05, 2021 10:09 pm
Maybe one of the BLE gurus (@Jimmo?) can explain what is going on.
You can't set the mtu until BLE has been activated (i.e. `ble.active(True)`). BLESimpleCentral does this for you in the constructor.

FYI, if you're doing BLE stuff, you might find https://github.com/micropython/micropyt ... oth/aioble useful. Much easier than working with the low-level ubluetooth API.

dougconran
Posts: 27
Joined: Sat Jul 03, 2021 5:10 pm

Re: How can I override BLE MTU? (or alternative solution)

Post by dougconran » Tue Jul 06, 2021 7:24 am

Ah, many thanks, both for the clarification and the suggestion - I'll do just that.

dougconran
Posts: 27
Joined: Sat Jul 03, 2021 5:10 pm

Re: How can I override BLE MTU? (or alternative solution)

Post by dougconran » Tue Jul 06, 2021 4:09 pm

:( I'm still struggling with this (setting 'mtu' to anything other than 20 chrs). I have added the following code to both the peripheral and central but the peripheral is still only transmitting 20 chrs. The one other thing that I've done is to change one of the UART_TX flags from _FLAG_READ to _FLAG_INDICATE (because that is what the OBD2 reader I'm using advertises).

Code: Select all

    def __init__(self, ble, name="DLINK"):
        self._ble = ble
        self._ble.active(True)
        ble.config(mtu=500)
        self._ble.irq(self._irq)
        ...
I've not yet tried 'aioble' in large part because I'm using MP v1.14 (for screen display reasons) and that requires at least v1.15. Any help on where I go from here would be much appreciated. - TIA

Post Reply