Skip to content Skip to sidebar Skip to footer

How To Ensure Parallel Communication Between 3 Raspberry Pi?

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

Solution 1:

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

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

Install with:

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"

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


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.

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.

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:

enter image description here

Solution 2:

If you want parallel communication you could consider using a websocket. See: https://pypi.python.org/pypi/websockets

But also HTTP and TCP would be an option, it depends on your usecase...

Solution 3:

Another option might be to install Redis on your Raspberry Pis. It is a very fast "data structure server" - meaning you can use it to implement hashes, sets, queues etc.

You can install it on your Raspberry Pi with:

sudo apt-get install redis-server

If you want to use it among multiple Raspberry Pis, you need to comment out the line in the config file that binds to the local host only, so that it looks like this in /etc/redis/redis.conf and then restart it:

#bind 127.0.0.1

Then, you can test the server from any of your Raspberry Pis with the ping command it should respond with a PONG:

redis-cli -h <SERVER> ping
PONG

Now, on your pi0 client, you can do a blocking wait (BLPOP) on the pi0cmd queue to wait for a command:

redis-cli -h <SERVER> <<< "blpop pi0cmd 0"

and that will wait till your server sends a command on the pi0cmd queue:

redis-cli -h <SERVER> <<< "lpush pi0cmd 000"

So, those commands are implemented in a queue in the Redis data structure server.

You could implement the status in a simple string data structure. So the client updates his status in pi0status with:

redis-cli -h <SERVER> <<< "set pi0status HAPPY"

and the server can get the status at any time with:

redis-cli <<< "get pi0status""HAPPY"

Here is a little animation of it working as above:

enter image description here

All the Redis commands are listed here.

Again, there are Python versions of this.

Post a Comment for "How To Ensure Parallel Communication Between 3 Raspberry Pi?"