martinohanlon / BlueDot

A zero boiler plate bluetooth remote
MIT License
141 stars 43 forks source link

BLE communication between two Raspberry Pi 4 #167

Closed abhishek-v-pandey closed 3 years ago

abhishek-v-pandey commented 3 years ago

Hi, I need to send a pressure sensor data collected from an Arduino from one Raspberry Pi 4 to another Raspberry Pi 4. I used bluedot btcomm client server architecture. The connection is established but the client disconnects after sending two readings.

Raspberry Pi 4 client code:

import sys
import time
from bluepy import btle
from bluedot.btcomm import BluetoothClient
from signal import pause

def data_3():
    mac_adrs3 = 'f3:9a:9a:e8:54:5c'

    print("Connecting..")
    tp3_sense = btle.Peripheral(mac_adrs3)

    print("Discovering Services..")
    _=tp3_sense.services
    tire3_sensing_service = tp3_sense.getServiceByUUID("2b7537ab-8899-4359-a78a-096e076a4605")

    print("Discovering characteristics..")
    _= tire3_sensing_service.getCharacteristics()

    return tire3_sensing_service

def byte_array_to_int(value):
    value = bytearray(value)
    value = int.from_bytes(value, byteorder='little')
    return value

def byte_array_to_char(value):
    value = value.decode("utf-8")
    return value

def decimal_exponent_two(value):
    return value/100

def decimal_exponent_one(value):
    return value/10

def pascals_to_kilopascals(value):
    return value / 1000

def read_pressure3(service3):
    pres3_char = service3.getCharacteristics("9c30d6b2-e3d3-4eae-8641-425a36d550ec")[0]
    pres3 = pres3_char.read()
    pres3 = byte_array_to_int(pres3)
    #pres3 = decimal_exponent_one(pres3)
    #pres3 = pascals_to_kilopascals(pres3)
    print(f"Barometric pressure 3 : {round(pres3,2)} kPa")
    return pres3

def read_temperature3(service3):
    temp3_char = service3.getCharacteristics("e986145c-ead6-41a7-9718-d2b0b4834a11")[0]
    temp3 = temp3_char.read()
    temp3 = byte_array_to_int(temp3)
    #temp3 = decimal_exponent_two(temp3)
    print(f"temperature 3 : {round(temp3,2)}  C")
    return temp3

def loop():
    c = data_3()

    while True:
        time.sleep(1.0)
        print("\n")

        t3 = read_temperature3(c)
        p3 = read_pressure3(c)

        r = BluetoothClient("DC:A6:32:4C:9D:ED", data_received_callback=None, port=2)
        r.send(f"temp 3 : {t3} ")
        r.send("\n")
        r.send(f"pressure 3 : {p3} ")

def main():
    loop()

if __name__ == "__main__":
    main()

Raspberry Pi server code

from bluedot.btcomm import BluetoothServer
from signal import pause

def data_received(data):
    print(data)
    s.send(data)

s = BluetoothServer(data_received)
pause()

The error that i am facing.

`pi@raspberrypi:~/Desktop/rpible $ sudo python3 onearduino_senddata.py Connecting.. Discovering Services.. Discovering characteristics..

temperature 3 : 18 C Barometric pressure 3 : 100 kPa

temperature 3 : 19 C Barometric pressure 3 : 100 kPa Traceback (most recent call last): File "onearduino_senddata.py", line 83, in main() File "onearduino_senddata.py", line 80, in main loop() File "onearduino_senddata.py", line 70, in loop r = BluetoothClient("DC:A6:32:4C:9D:ED", data_received_callback=None, port=2) File "/usr/local/lib/python3.7/dist-packages/bluedot/btcomm.py", line 567, in init self.connect() File "/usr/local/lib/python3.7/dist-packages/bluedot/btcomm.py", line 660, in connect self._client_sock.connect((server_mac, self._port)) OSError: [Errno 16] Device or resource busy

`

please help me to solve since its showing related to bluedot. If not related to bluedot , m sorry , but please how to resolve it.

Thanks

Edited to format code correctly

martinohanlon commented 3 years ago

This is not a fault with btcomm.

You are creating a BluetoothClient (and therefore connection) each time around your loop. The error is telling you the resource is already in use.

You only need to create the client once.

Move this outside your while loop:

r = BluetoothClient("DC:A6:32:4C:9D:ED

I would encourage you to use the raspberrypi forum when asking for help. I am often the only person who monitors these issues.