Help with AIOBLE

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
dougconran
Posts: 27
Joined: Sat Jul 03, 2021 5:10 pm

Help with AIOBLE

Post by dougconran » Fri Jul 23, 2021 6:00 pm

I'm trying to develop a BLE client to get data from an OBDII reader. I have an ESP32 client derived from ble_simple_central.py working to an ESP32 peripheral derived from ble_simple_peripheral.py (but which I cannot get to work with the intended OBDII reader). I am now trying to use @jimmo's aioble library but, so far, with only limited success. I have got to the point where my aioble client can send commands to the ble emulator but, although the emulator is receiving those commands and returning some data, the client is not receiving that data almost certainly because I've got something wrong somewhere. I've looked at the examples and the readme at https://github.com/micropython/micropyt ... oth/aioble but am still struggling.

My code is

Code: Select all

_ENV_OBD_UUID = bluetooth.UUID(0xffe0)
_ENV_OBD_RWN_UUID = bluetooth.UUID(0xffe1)
_ENV_OBD_RW_UUID = bluetooth.UUID(0xffee)
...
...
    async with connection:
        print"Connected")
        try:
            obd_service = await connection.service(_ENV_OBD_UUID)
            obd_rwn = await obd_service.characteristic(_ENV_OBD_RWN_UUID)
            obd_rw = await obd_service.characteristic(_ENV_OBD_RW_UUID)
        except asyncio.TimeoutError:
            print("Timeout discovering services/characteristics")
            return

        cmds = ["ATZ\n","ATD\n","ATE1\n","ATL1\n","ATH1\n","ATS0\n","ATSTFF\n","ATFE\n","ATSP6\n"]

        for cmd in cmds:

            try:
                await obd_rwn.write(cmd, response=False, timeout_ms=2000)         #    <-------------  Tried response = both True & False
                print(cmd[:-1])
                rtn = await obd_rwn.read(timeout_ms=2000)                         #    <-------------  returns Write command
#                rtn = await obd_rwn.notified(timeout_ms=2000)                    #    <-------------  Times out
                print(cmd)
                utime.sleep(1)

            except asyncio.TimeoutError:
                print("Write timeout")
                return
What am I doing wrong?

EDIT - Do I need to subscribe to the notify characteristic for notifications? - And, if so, how and where?

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

Re: Help with AIOBLE

Post by jimmo » Wed Aug 04, 2021 12:15 pm

dougconran wrote:
Fri Jul 23, 2021 6:00 pm
EDIT - Do I need to subscribe to the notify characteristic for notifications? - And, if so, how and where?
Yes, if you're doing "await x.notified()" then you need to be subscribed.

Code: Select all

await obd_rwn.subscribe(notify=True)
I would do this immediately after characteristic discovery (i.e. after it's returned from await "obd_service.characteristic").

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

Re: Help with AIOBLE

Post by dougconran » Wed Aug 04, 2021 3:58 pm

Yes! Thanks. That and having

Code: Select all

await obd_rwn.written()
on the server have sorted the problem.

Post Reply