1

I connected 3 sensors via Bluetooth serial port to a Raspberry Pi 4 with the following Python program:

import socket

serverMACAddress1 = 'XX:XX:XX:XX:XX:XX'
serverMACAddress2 = 'XX:XX:XX:XX:XX:XX'
serverMACAddress3 = 'XX:XX:XX:XX:XX:XX'

port = 1

s1 = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s2 = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s3 = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)

s1.connect((serverMACAddress1,port))
s2.connect((serverMACAddress2,port))
s3.connect((serverMACAddress3,port))


while 1:
    text = input()
    if text == "quit":
        break
    s1.send(bytes(text, 'UTF-8'))
    data = client.recv(size) 
    if data: 
        print(data)
s1.close()

while 1:
    text = input()
    if text == "quit":
        break
    s2.send(bytes(text, 'UTF-8'))
    data = client.recv(size) 
    if data: 
        print(data)
s2.close()

while 1:
    text = input()
    if text == "quit":
        break
    s3.send(bytes(text, 'UTF-8'))
    data = client.recv(size) 
    if data: 
        print(data)
s3.close()

The sensors connected fine, but I haven't managed to see the data they are sending on the Shell of the IDE or on separate windows. What I would like is to open three separate windows (one for each sensor) to be able to see the data they are sending simultanously via seria port. Any ideas on how to do that?


Program for 1 sensor:

import socket

serverMACAddress = 'XX:XX:XX:XX:XX:XX'

port = 1

s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((serverMACAddress,port))

while 1:
    text = input()
    if text == "quit":
        break
    #data = s.send(bytes(text, 'UTF-8'))
    data = s.recv(1024)
    print(data)
    
s.close()

2
  • This is not actually a Pi based issue but a general programming question and should be in a different exchange. Say that I would look at PyQt5, tkinter and pygame for GUI output (RPT have a book / PDF magpi.raspberrypi.org/books/create-guis that may help).
    – user115418
    Nov 27 '20 at 15:12
  • @Anyroo thanks!
    – blt
    Dec 1 '20 at 8:43
1

If you want all of the sensors to be read in one script/window then the three recv statements need to be in the same while loop.

However if you want them to run in three different terminals, I would make your script take a parameter so you could specify on the command line which server that script will connect to. e.g:

python read_spp_server.py 11:27:EB:D1:14:7E

An example of what the script might look like is:

import argparse
import socket

PORT = 1
SIZE = 16

def create_socket():
    return socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)

def read_sensor(sensor_socket):
    data = sensor_socket.recv(SIZE)
    if data:
        print(data.decode('utf-8'))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('bd_addr', help='Address of device to connect to')
    args = parser.parse_args()

    sock = create_socket()
    print(f'Connecting to {args.bd_addr}')
    sock.connect((args.bd_addr, PORT))
    try:
        while True:
            read_sensor(sock)
    except KeyboardInterrupt:
        print('Closing socket')
    finally:
        sock.close()
6
  • hi,I'm trying to do this, but I have the problem that I'm not being able to see the data the sensor is supposed to be sending. The sensors do connect with the program, so they must be sending data, right? I'm trying the program for just one sensor and the while loop doesn't seem to do anything. I don't know if I'm using incorrectly the recv() function, I used it because I'm supposed to be receiving data, not sending, but I also tried the send() function in case I was mixing their definitions and doing the opposite, but it didn't work either. I added this program to the original question
    – blt
    Dec 1 '20 at 8:55
  • You are in the realm of device specific information and difficult for me to know the answer. There is no way I can tell if your sensor requires a command to start sending data. Did your previous way of doing this give the correct output without any input? Could it just be that text = input() is causing the code to halt while it is waiting for input?
    – ukBaz
    Dec 1 '20 at 9:55
  • yes, the previous way was automatic, without any input. But now the program works too. It was what you said, the text = input() was causing the code to halt. Thank you very much! The program to run it on three different terminals worked too (I just had to delete de "f" on the line print(f'Connecting to {args.bd_addr}'). Thanks again!
    – blt
    Dec 1 '20 at 12:22
  • P.D.: in the future I will have to group the data of the sensors in some way like a vector: (Sensor1_Data1, Sensor2_Data1, Sensor3_Data1), (Sensor1_Data2, Sensor2_Data2, Sensor3_Data2)... I've tried to do it with a list inside the while loop like this [data1, data2, data3], but sometimes it prints the data of the 3 sensors and sometimes just one or two, or incomplete data. Do you think it's possible to do something like this, or there would be some kind of latency between sensors due to the serial port connection which would make it impossible to group their data in perfect order?
    – blt
    Dec 1 '20 at 12:23
  • Sounds like a question for StackOverflow. :-) There is also more information around the issue at places like: realpython.com/python-sockets/#handling-multiple-connections
    – ukBaz
    Dec 2 '20 at 6:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.