Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

cant transfer data between bluetooth on raspberry pi pi w and raspberry pi zero 2w

$
0
0

I try to connect the two devices via Bluetooth Low Energy (BLE) and transfer data between them, but after the two devices connect to each other, they immediately disconnect. Does anyone have an idea what the problem could be?

Raspberry Pi Pico W code (server):

import sysimport aiobleimport bluetoothimport machineimport uasyncio as asynciofrom micropython import constdef uid():""" Return the unique id of the device as a string """    return "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format(        *machine.unique_id())MANUFACTURER_ID = const(0x02A29)MODEL_NUMBER_ID = const(0x2A24)SERIAL_NUMBER_ID = const(0x2A25)HARDWARE_REVISION_ID = const(0x2A26)BLE_VERSION_ID = const(0x2A28)led = machine.Pin("LED", machine.Pin.OUT)_DEVICE_INFO_UUID = bluetooth.UUID(0x180A) # Device Information_GENERIC = bluetooth.UUID(0x1848)_BUTTON_UUID = bluetooth.UUID(0x2A6E)_BLE_APPEARANCE_GENERIC_REMOTE_CONTROL = const(384)ADV_INTERVAL_MS = 250_000device_info = aioble.Service(_DEVICE_INFO_UUID)connection = None# Create Characteristic for device infoaioble.Characteristic(device_info, bluetooth.UUID(MANUFACTURER_ID), read=True, initial="KevsRobots.com")aioble.Characteristic(device_info, bluetooth.UUID(MODEL_NUMBER_ID), read=True, initial="1.0")aioble.Characteristic(device_info, bluetooth.UUID(SERIAL_NUMBER_ID), read=True, initial=uid())aioble.Characteristic(device_info, bluetooth.UUID(HARDWARE_REVISION_ID), read=True, initial=sys.version)aioble.Characteristic(device_info, bluetooth.UUID(BLE_VERSION_ID), read=True, initial="1.0")remote_service = aioble.Service(_GENERIC)button_characteristic = aioble.Characteristic(remote_service, _BUTTON_UUID, read=True, notify=True)print("Registering services")aioble.register_services(remote_service, device_info)connected = Falseasync def remote_task():""" Task to handle remote control """    while True:        if not connected:            print("Not Connected")            await asyncio.sleep_ms(1000)            continue        button_characteristic.write(b"data")        button_characteristic.notify(connection, b"data")        await asyncio.sleep_ms(10)async def peripheral_task():""" Task to handle peripheral """    global connected, connection    while True:        connected = False        connection = await aioble.advertise(ADV_INTERVAL_MS,                                            name="KevsRobots",                                            appearance=_BLE_APPEARANCE_GENERIC_REMOTE_CONTROL,                                            services=[_GENERIC])        print("Connection from", connection.device)        connected = True        print("connected")        await connection.disconnected()        print("disconnected")async def main():    tasks = [        asyncio.create_task(peripheral_task()),        asyncio.create_task(remote_task())        ]    await asyncio.gather(*tasks)asyncio.run(main())

Raspberry Pi Zero 2W code (client):

import pygattCHARACTERISTIC = "00002a6e-0000-1000-8000-00805f9b34fb"def connect_to_ble_device(device_address):    print('start')    adapter = pygatt.GATTToolBackend()    adapter.start()    try:        print("Connecting to BLE device with address: {}".format(device_address))        device = adapter.connect(device_address)        while True:            value = device.char_read(CHARACTERISTIC, timeout=10)            print('value is', value)    finally:        adapter.stop()if __name__ == "__main__":    connect_to_ble_device('28:CD:C1:0C:39:65')      # Pico MAC Address

After running the code I can see the Zero has successfully connected to the Pico (the Pico's console shows info about the connection), but I get pygatt.exceptions.NotificationTimeout: None from the zero.


Viewing all articles
Browse latest Browse all 23131

Trending Articles