如何通过蓝牙在两个覆盆子圆周率之间通信,并将传感器数据从一个圆周率发送到另一个圆周率? [英] How to communicate between two raspberry pi via Bluetooth and send sensor data from one pi to another?

查看:33
本文介绍了如何通过蓝牙在两个覆盆子圆周率之间通信,并将传感器数据从一个圆周率发送到另一个圆周率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为某个学校项目工作。我想通过蓝牙将温度传感器数据从一个覆盆子π4发送到另一个π4。 我搜索了很多教程,但没有找到任何相关的教程。请任何人帮忙,或者任何建议都会很有帮助。

推荐答案

我对蓝牙知之甚少,此答案可能过时或实践不佳,但至少它可以工作。如果有人知道得更清楚,请提供更好的答案,我将删除此内容。

因此,我有两个运行Raspbian的Raspberry PI 4。我在这两个服务器上安装了BlueDot和:

sudo pip3 install bluedot

然后我paired它们,在第一个上运行以下代码:

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

然后我在sudo下的第一个RasPI上运行此服务器代码(归因于here):

#!/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")

和第二个代码(归因于here),也在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 {} 
".format(str(datetime.now())))
        sleep(1)
finally:
    c.disconnect()
两个RasPI成功交换消息,直到中断。我测量了往返时间(RTT),两个相距约一米的RasPi4之间的平均时间约为30ms。

pi用户添加到dialoutLinux组(或其他某个组)可能比在sudo下运行更好。如果有人知道,请说。

这篇关于如何通过蓝牙在两个覆盆子圆周率之间通信,并将传感器数据从一个圆周率发送到另一个圆周率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆