MicroPython 蓝牙连接至 OBD BLE

问题描述 投票:0回答:1

我正在尝试使用

obd2
将 Raspberry Pico W 连接到
BLE
适配器。 至于检测和连接
obd
我应该已经成功了,但现在我陷入了如何向
OBD
发送命令的困境,例如
ATZ
ATD

对于我正在使用

micropython
aioble
库的项目。

如果我理解它如何正确工作,我就充当中心并尝试连接到外围设备。

对于发送AT命令,我使用的是写入功能,在下面的代码中更清楚我在做什么,但是我如何检查从

obd
收到的响应?

由于该服务使用两种不同的

UUID
进行写入和读取,我是否应该创建两种不同的特征,一种用于读取,一种用于写入?

import aioble
import bluetooth
import machine
import uasyncio as asyncio


_OBD_UUID_SERVICE = bluetooth.UUID(0xfff0)
_OBD_UUID_CHARACTERISTIC = bluetooth.UUID(0xfff1)

led = machine.Pin("LED", machine.Pin.OUT)
connected = False
alive = False


async def find_remote():
    async with aioble.scan(duration_ms=5000, interval_us=30000, window_us=30000, active=True) as scanner:
        async for result in scanner:
            if (result.name() == "OBDBLE"):
                print("Found OBDBLE")
                for item in result.services():
                    print(item)
                if _OBD_UUID_SERVICE in result.services():
                    print("Found OBDBLE service")
                    return result.device
    return None


async def blink_task():
    print("Blink task started")
    toggle = True

    while True and alive:
        blink = 250
        led.value(toggle)
        toggle = not toggle

        if connected:
            blink = 1000
        else:
            blink = 250
        await asyncio.sleep_ms(blink)

    print("Blink task stopped")


async def peripheral_task():

    global connected, alive
    connected = False
    device = await find_remote()

    if not device:
        print("OBD device not found")
        return

    try:
        print("Connecting to", device)
        connection = await device.connect()

    except asyncio.TimeoutError:
        print("Timeout during connection")
        return

    async with connection:
        print("Connected")
        alive = True
        connected = True

   
        try:

            obd_service = await connection.service(_OBD_UUID_SERVICE)
            print(obd_service)
            obd_characteristic = await obd_service.characteristic(_OBD_UUID_CHARACTERISTIC)
            print(obd_characteristic)

        except asyncio.TimeoutError:
            print("Timeout discovering services/characteristics")
            return

    
        try:
            if obd_service == None:
                print("Obd Disconnected")
                alive = False
                connected = False

            if obd_characteristic == None:
                print("No Control")
                alive = False
                connected = False

        except asyncio.TimeoutError:
            print("Timeout during discovery/service/characteristic")
            alive = False
        
        print("Sending ATZ")
        obd_characteristic.write(b"ATZ",response=True)
        obd_characteristic.notified()
        data = await obd_characteristic.read()
        print(data)


    await connection.disconnected()
    print("Disconnected")


async def main():
    tasks = []
    tasks = [
        asyncio.create_task(blink_task()),
        asyncio.create_task(peripheral_task())
    ]

    await asyncio.gather(*tasks)

while True:
    asyncio.run(main())

这是输出:

Blink task started
Found OBDBLE
Found OBDBLE
UUID(0xfff0)
Found OBDBLE service
Connecting to Device(ADDR_PUBLIC, aa:bb:cc:12:22:33)
Connected
Service: 19 24 UUID(0xfff0)
Characteristic: 22 21 18 UUID(0xfff1)
Sending ATZ
b''
Disconnected

我使用应用程序找到了

UUID
,我将附上图像以便您检查

odb_details

bluetooth-lowenergy micropython raspberry-pi-pico obd-ii aioble
1个回答
0
投票

这是一个非常广泛的问题。有些方面已经在 https://stackoverflow.com/a/52139765/415982 中得到了解答,特别是您需要探测要使用的特征数量。这是特定于供应商的。

对于实际的通信,您需要使用 API 编写一个队列抽象,该 API 允许您发送命令,等待命令被组装(您也可能只收到部分答案),然后将整个答案传递给来电者。

不要忘记 ELM327 派生协议要求命令以

\r
终止。

© www.soinside.com 2019 - 2024. All rights reserved.