![](/img/trans.png)
[英]How to send data to multiple raspberry pi's at a time from raspberry pi 4 via Bluetooth?
[英]How to communicate between two raspberry pi via Bluetooth and send sensor data from one pi to another?
我正在为一些学校项目工作。 我想通过蓝牙将温度传感器数据从一个树莓派 4 发送到另一个 pi 4。 我搜索了很多教程,但我没有找到任何相关的教程。 请任何人对此提供帮助,或者任何建议都会非常有帮助。
我对蓝牙知之甚少,这个答案可能已经过时或实践不佳,但至少它有效。 如果有人知道更好,请提供更好的答案,我将删除此内容。
所以,我有两个运行 Raspbian 的 Raspberry Pi 4。 我在两者上都安装了BlueDot
:
sudo pip3 install bluedot
然后我将它们配对,首先运行它:
sudo bluetoothctl
[bluetooth]# discoverable on
[bluetooth]# pairable on
[bluetooth]# agent on
[bluetooth]# default-agent
这是第二个:
sudo bluetoothctl
[bluetooth]# discoverable on
[bluetooth]# pairable on
[bluetooth]# agent on
[bluetooth]# default-agent
[bluetooth]# scan on
当第一个 RasPi (hostname=pi4) 出现时,我通过在第二个 RasPi 中键入以下内容与之配对:
[bluetooth]# pair DC:A6:32:03:0C:1B
然后我在两者上都退出了bluetoothctl
。
然后我在第一个 RasPi UNDER sudo
上运行了这个服务器代码(归属于这里):
#!/usr/bin/env python3
from bluedot.btcomm import BluetoothServer
from time import sleep
from signal import pause
def data_received(data):
print("recv - {}".format(data))
server.send(data)
def client_connected():
print("client connected")
def client_disconnected():
print("client disconnected")
print("init")
server = BluetoothServer(
data_received,
auto_start = False,
when_client_connects = client_connected,
when_client_disconnects = client_disconnected)
print("starting")
server.start()
print(server.server_address)
print("waiting for connection")
try:
pause()
except KeyboardInterrupt as e:
print("cancelled by user")
finally:
print("stopping")
server.stop()
print("stopped")
第二个代码(归属于此处)也在sudo
下:
#!/usr/bin/env python3
from bluedot.btcomm import BluetoothClient
from datetime import datetime
from time import sleep
from signal import pause
def data_received(data):
print("recv - {}".format(data))
print("Connecting")
c = BluetoothClient("pi4", data_received)
print("Sending")
try:
while True:
c.send("hi {} \n".format(str(datetime.now())))
sleep(1)
finally:
c.disconnect()
两个 RasPi 成功交换消息直到被中断。
最好将pi
用户添加到dialout
Linux 组(或其他某个组),而不是在sudo
下运行。 如果有人知道,请说。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.