Connecting to RPI with Bluetooth

Summary

Hello, I am trying to send data from my streamlit app to a Raspberry Pi through Bluetooth. I tested the app on localhost and everything worked properly, the data was received by the RPI however, when I deployed the code and ran the app on the streamlit server, I get the error shown below. I have set up my client side code, shown below, on the streamlit app and the server side code on my RPI

Steps to reproduce

Client-Side Code:

import socket
import pickle

def sendToRPI(points):
    mac="B8:27:EB:8A:F5:2A"
    port = 1
    with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:
        s.connect((mac,port))
        print('connected to RPI')
        data=pickle.dumps(points)
        s.sendall(data)

Server-Side Code:

import socket
import pickle

hostMACAddress = "B8:27:EB:8A:F5:2A"
port = 1
backlog = 1
size = 1024
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)
client, address = s.accept()
while True:
    p=b""
    try:
        data = client.recv(size)
        if data:
            print(data)
            p+=data
    except:
        break
points=pickle.loads(p)
# Do Something

Expected behavior:

The points should be sent from client to server through the bluetooth connection from the streamlit app, not just when it is run locally.

Actual behavior:

2023-04-27 23:09:17.700 Uncaught app exception

Traceback (most recent call last):

  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script

    exec(code, module.__dict__)

  File "pages/Lissajous.py", line 33, in <module>

    getPoints('SVG/Lissajous.svg')

  File "/app/capstone/Backend/SendData.py", line 114, in getPoints

    sendToRPI(points)

  File "/app/capstone/Backend/SendData.py", line 20, in sendToRPI

    with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:

  File "/usr/local/lib/python3.9/socket.py", line 232, in __init__

    _socket.socket.__init__(self, family, type, proto, fileno)

OSError: [Errno 97] Address family not supported by protocol

Debug info

  • Streamlit version: 1.17.0
  • Python version: 3.10.11
  • OS version: Windows 11
  • Browser: Chrome

I guess the VM in Streamlit Cloud doesn’t have a Bluetooth adapter. It wouldn’t be close enough to your Raspberry PI anyway.

1 Like

Is there any way to access local bluetooth devices from the VM?
I’m still fairly new to web programming and python so I don’t know much about how hosting and Streamlit Cloud work.

There is a components API that allows your Streamlit APP to comunicate with Javascript running in the browser. Then there is an experimental web API that allows the Javascript code access Bluetooth devices, insofar as the browser supports it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.