如何保证3个树莓派之间的并行通信

如何保证3个树莓派之间的并行通信

本文介绍了如何保证3个树莓派之间的并行通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与 3 个 Raspberry Pi 通信:一个作为服务器,其他作为客户端.服务器同时接收和发送信息给两个客户端.我正在寻找一种方法来确保这种沟通.请问有什么帮助吗!PS:我使用的是 Python3

I'm trying to communicate with 3 Raspberry Pi: one as server and the others are clients. The server receives and sends information to the two clients in the same time. I'm looking for a method to ensure that communication. Is there any help please !PS : I'm using Python3

推荐答案

MQTT 可能适用于这类事情,但您的问题非常模糊.

MQTT would probably work pretty well for that sort of thing but your question is very vague.

有可用的 Python 库,但您可以直接在终端的命令行中进行测试:

There are Python libraries available, but you could just test in Terminal at the command-line:

安装:

sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients python-mosquitto

首先以详细模式运行服务器(消息代理),以便我们可以看到发生了什么:

Run the server (message broker) first, in verbose mode so we can see what is happening:

mosquitto -v

现在,在一个新的终端中,运行一个监听调试"流的订阅者:

Now, in a new Terminal, run a subscriber that listens to the "debug" stream:

mosquitto_sub -h localhost -t debug

现在,在另一个新终端中,运行一个发送到调试"流的发布者:

Now, in another new Terminal, run a publisher that sends to the "debug" stream:

mosquitto_pub -h 127.0.0.1 -t debug -m "Hello"

显然更改周围的 IP 地址以匹配您的设置,以上是在一台机器上的一个非常简单的示例.

Obviously change the IP addresses around to match your setup, the above is a very simple example on one machine.

因此,具体而言,您的服务器可能对其两个客户端的状态感兴趣,因此它会订阅两个频道,即 client0statusclient1status 而 client0 会在频道 client0status 上发布,client1 将在频道 client1status 上发布.

So, in concrete terms, your server is probably interested in the status of its two clients so it would subscribe to two channels, namely client0status and client1status while client0 would publish on channel client0status and client1 would publish on channel client1status.

关于命令,client0 将订阅通道 client0cmd 以接收命令,client1 将订阅通道 client1cmd,而服务器将在这两个通道上发布命令.

As regards commands, client0 would subscribe to channel client0cmd to receive commands, and client1 would subscribe to channel client1cmd while the server would publish commands on both these channels.

这是一个关于这种设置工作的小视频.左上角的窗口正在运行 mosquitto 服务器.右上角的窗口订阅了两个 Raspberry Pi 的状态通道——这同样可以与服务器在同一台机器上.左下角的窗口是第一个树莓派,右下角的窗口是第二个树莓派:

Here is a little video of such a setup working. The top-left window is running the mosquitto server. The top-right window is subscribed to the status channels of both Raspberry Pis - this could equally be on the same machine as the server. The bottom-left window is the first Raspberry Pi, and the bottom-right window is the second Raspberry Pi:

这篇关于如何保证3个树莓派之间的并行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:49