Quantcast
Viewing all articles
Browse latest Browse all 4980

Troubleshooting • Bluetooth between Pico and Pi 5 issues with receiving

Hi Pi Community,
I'm in desperate help for an issue relating with bluetooth connection between a Raspberry Pi Pico W(MicroPython) and the Raspberry Pi 5(running OS Lite - terminal). Using fully python, I am trying to:
1. Connect both with each other with MAC address and characteristic UUID
2. Send message from Pico
3. Receive the message on the Pi 5 and print it on the REPL.

Pico Code:

Code:

import ubluetoothimport timeSERVICE_UUID = "afc061de-096a-41c0-92a1-0c0ad71d5686"CHAR_UUID = "a2ef971e-8c08-4bbf-9f77-ada7ca40c98e"_IRQ_CENTRAL_CONNECT = 1_IRQ_CENTRAL_DISCONNECT = 2class BLEPeripheral:    def __init__(self):        self.ble = ubluetooth.BLE()        self.ble.active(True)        self.ble.irq(self.ble_irq)        self.connected = False        self.conn_handle = None        self.char_handle = None    def ble_irq(self, event, data):        if event == _IRQ_CENTRAL_CONNECT:            self.connected = True            self.conn_handle = data[0]            print(f"Central connected with handle: {self.conn_handle}")            self.send_message("Hello World")        elif event == _IRQ_CENTRAL_DISCONNECT:            self.connected = False            self.conn_handle = None            print("Central disconnected")            self.start_advertising()    def start_advertising(self):        name = "PicoW-BLE"        name_bytes = name.encode("utf-8")        adv_data = bytearray([0x02, 0x01, 0x06]) + bytearray([len(name_bytes) + 1, 0x09]) + name_bytes        self.ble.gap_advertise(100000, adv_data)        print("Started advertising")    def register_services(self):        service = (ubluetooth.UUID(SERVICE_UUID), ((ubluetooth.UUID(CHAR_UUID), ubluetooth.FLAG_READ | ubluetooth.FLAG_NOTIFY),))        handles = self.ble.gatts_register_services((service,))        self.char_handle = handles[0][0]        print(f"Registered services with char_handle: {self.char_handle}")    def send_message(self, message):        if self.connected and self.conn_handle is not None:            print(f"Sending message: {message}")            try:                self.ble.gatts_notify(self.conn_handle, self.char_handle, message.encode("utf-8"))                print("Message sent successfully")            except Exception as e:                print(f"Error sending message: {e}")        else:            print("Not connected, cannot send message.")# Main programdef main():    ble_peripheral = BLEPeripheral()    ble_peripheral.register_services()    ble_peripheral.start_advertising()    try:        while True:            time.sleep(1)    except KeyboardInterrupt:        print("Stopping BLE...")        ble_peripheral.ble.active(False)# Run the programmain()
Output:

Code:

Registered services with char_handle: 9Started advertisingCentral connected with handle: 64Sending message: Hello WorldMessage sent successfullyCentral disconnected
Raspberry Pi 5 code:

Code:

                                                                                                import asynciofrom bleak import BleakClientPICO_MAC = "28:CD:C1:0B:6D:EA"  # Replace with your Pico W's MAC addressCHAR_UUID = "a2ef971e-8c08-4bbf-9f77-ada7ca40c98e"async def main():    def notification_handler(sender, data):        """Callback for handling incoming notifications."""        print(f"Received notification from {sender}: {data.decode('utf-8')}")    async with BleakClient(PICO_MAC) as client:        print("Connected to Pico W")        # Check if the characteristic exists and supports notifications        service = await client.get_services()        characteristic_found = False        for service in service:            for char in service.characteristics:                if char.uuid == CHAR_UUID:                    characteristic_found = True                    print(f"Characteristic {CHAR_UUID} found and supports: {char.properties}")        if not characteristic_found:            print(f"Characteristic {CHAR_UUID} not found. Please check the UUID.")            return        # Subscribe to notifications        print("Subscribing to notifications...")        await client.start_notify(CHAR_UUID, notification_handler)        # Keep connection alive and listen for notifications        try:            print("Waiting for notifications...")            await asyncio.sleep(30)  # Wait for 30 seconds to receive notifications        except asyncio.CancelledError:            print("Notification wait canceled.")        # Unsubscribe from notifications        print("Unsubscribing from notifications...")        await client.stop_notify(CHAR_UUID)    print("Disconnected from Pico W")asyncio.run(main())
Output:

Code:

Connected to Pico WCharacteristic a2ef971e-8c08-4bbf-9f77-ada7ca40c98e found and supports: ['read', 'notify']Subscribing to notifications...Waiting for notifications...Unsubscribing from notifications...Disconnected from Pico W
As you can see, the pi 5 is unable to receive the message, though the pico says it has already sent it.

I need help for this desperately. I have established the way to get the 2 to connect, but need help on getting the Pi 5 to get the message.

Thanks.

Statistics: Posted by sns123 — Thu Jan 02, 2025 2:24 am



Viewing all articles
Browse latest Browse all 4980

Trending Articles