ahacking
Posts: 4
Joined: Mon Jul 25, 2016 7:58 pm

PyBluez and continuous data transfer

Fri Apr 20, 2018 3:15 pm

Hi,

I'm developing a device to send data from an absolute rotary encoder from a Pi (PiZeroW) to a RPi3 via a Bluetooth connection using PyBluez.

In a nutshell, I want to collect the data on one device and send it to be displayed by the other. Collection and display may persist for 20 mins.

I can establish a socket and send a simple message between each Pi via RFCOMM using an example https://people.csail.mit.edu/albert/blu ... /x232.html from PyBluez.

I'm wondering what the best (simplest, robust, fastest) approach would be to continuously deliver data over Bluetooth using this setup.

Thanks

Adam

User avatar
Douglas6
Posts: 4437
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: PyBluez and continuous data transfer

Fri Apr 20, 2018 3:43 pm

Put a 'while True:' loop around the 'sock.send()' function?

ahacking
Posts: 4
Joined: Mon Jul 25, 2016 7:58 pm

Re: PyBluez and continuous data transfer

Sat Apr 21, 2018 1:45 am

Thanks. The send is already has a loop.

#SEND SIDE

#Import Libraries
import time
import spidev
import bluetooth

# configure client BT
bd_addr = "B8:27:EB:F2:B3:7B"
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))

#https://pypi.org/project/spidev/
spi = spidev.SpiDev()

spi.open(0, 0) # Opens the SPI Slave State Port for communication
spi.mode = 0b11
spi.bits_per_word = 8
spi.max_speed_hz = 50000

def rotation_degrees(binary_value):
degree_of_rotation = round(int(binary_value, 2)/2.844, 1) # has 1024 positions/360 degrees = 2.844
degree_of_rotation = str(degree_of_rotation)
return degree_of_rotation

while True:
resp = spi.xfer2([0x10, 0x10]) #read 2 bytes
first_byte = '{0:08b}'.format(resp[0]) # format in binary
second_byte = '{0:08b}'.format(resp[1])# format in binary
extracted_data = first_byte[1:] + second_byte[:3] # remove first bit from first byte and keep first 3 bits from
extracted_data = str(extracted_data)
sock.send(extracted_data)
print("Degrees rotation = ", rotation_degrees(extracted_data))
time.sleep(.001)

spi.close()
sock.close()

#RECEIVE SIDE

import bluetooth

server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

port = 1
server_sock.bind(("",port))
server_sock.listen(1)

client_sock,address = server_sock.accept()
print("Accepted connection from ",address)

data = client_sock.recv(1024)
print("received [%s]" % data)

client_sock.close()
server_sock.close()

Return to “Python”

Who is online

Users browsing this forum: Baidu [Spider], buja, Galbi3000, przemof and 10 guests