如何通过蓝牙在两个覆盆子圆周率之间通信,并将传感器数据从一个圆周率发送到另一个圆周率?

2024-08-11Python开发问题
10

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

问题描述

我在为某个学校项目工作。我想通过蓝牙将温度传感器数据从一个覆盆子π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下运行更好。如果有人知道,请说。

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

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11