More Pi Please
Posts: 50
Joined: Mon Jun 17, 2013 5:26 pm

Pi to Pi communication

Wed Jun 19, 2013 4:40 am

I would like 2 Pis in the same room to communicate with each other.

Being able to send a character or string from one to the other via Python.

It seems an ad hoc network would work but I'm having 2 problems.

1. Most posts and writings on using the Pi with ad hoc seem to always be about sharing an internet connection, becoming an access point, Pi to not a Pi communication, or something else.

For example this:

http://spin.atomicobject.com/2013/04/22 ... unication/

Goes into creating an ad hoc network, but then turns it into a server.


2. When I get the ad hoc working I'm not sure of the best way to get a character or string from one Pi to another.


Does anyone know how, or know a good tutorial, on just communicating Pi to Pi only?

I did a lot of searching but just can't find the answer so far.

Thank you for any help : )


.

User avatar
rpdom
Posts: 15214
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Pi to Pi communication

Wed Jun 19, 2013 5:31 am

There are many ways to do this sort of thing.

You could echo a command and character string through ssh to the other pi.

You could set up a client/server arrangement using the "sockets" network interface, so one program listens to a port on one Pi and another one connects to that port and sends a message.

Overview:
raspi1 has IP 192.168.1.31. It has a python program that listens on socket number 31415 for recognised messages.
raspi2 has IP 192.168.1.32. It has a python program that does stuff, and opens a connection to socket 31415 on 192.168.1.31 (or "raspi1"), sends a message and closes the socket. You can also make it wait for a response.

I chose 31415 as an example, as it isn't in the "privileged" range of sockets (0-1023), like 22 for ssh, 80 for www etc.

I can think of a few other methods, like use a serial connection (either USB adaptor, or cross-connect the UARTs on P1. Even connect a GPIO or two and implement your own simple serial port, or even (silly suggestion) in morse code ;)

I can't give you example code, as I don't use Python, but a quick go with a search engine on "python simple socket client server" should throw up some results. It doesn't matter that most examples you find won't be for the Pi, networking is fairly standard on all Linux systems.

evansg515
Posts: 7
Joined: Wed May 15, 2013 10:24 am

Re: Pi to Pi communication

Wed Jun 19, 2013 2:47 pm

I'm presuming you want to be wireless?

There are a few Serial Radio's available (Google: XRF Modules by Ciesco or Xbee Radios)

You simply wire up the small radio to the UART pins on the Pi and write a simple python serial reader / writer program.

I've done this and its really simple.

Also you could look into ncat (net cat). A really simple way of setting up a network link between any linux device.
Gareth Evans

@pukkapi
www.pukkapi.co.uk

6677
Posts: 383
Joined: Wed Mar 14, 2012 9:23 pm

Re: Pi to Pi communication

Fri Jun 21, 2013 7:15 pm

evansg515 wrote:I'm presuming you want to be wireless?

There are a few Serial Radio's available (Google: XRF Modules by Ciesco or Xbee Radios)

You simply wire up the small radio to the UART pins on the Pi and write a simple python serial reader / writer program.

I've done this and its really simple.

Also you could look into ncat (net cat). A really simple way of setting up a network link between any linux device.
or bluetooth (via USB or UART modules) or 433MHz RF modules or whatever... XBEE is very expensive in comparison.

Or if they are close enough, just get 3 female to female jumper wires. Plug one into any ground on Pi A and any ground on Pi B. Plug one onto the TX pin of Pi A and the RX pin of Pi B, and the 3rd on the TX of Pi B to the RX of Pi A.

Serial is dead easy to program for and dead easy to setup.




Oh, the reason you wont get any results looking for adhoc is terminology.

If you must do things via a network. Then look up TCP or UDP. Both are network protocols. Done.

peewee2
Posts: 61
Joined: Sun Apr 14, 2013 7:04 pm

Re: Pi to Pi communication

Mon Jun 24, 2013 11:24 pm

I assume you have both raspis on some form of network, wifi or cabled to a router.

if so, then the simplest way is to run a web server on one and a web client on the other then use ajax calls from the client to send messages to the server.

I am unsure if I would use python on the server,it's easier to build the server using nginx and php, you are then leveraging 20 years of web development.

a simple example could be a pi with a camera running motion detects movement and sends a simple message to the server via a wget http://myserver/intruder.php?frontdoor=opened call
intruder.php calls a script ( python or anything else ) which sets a pin on the GPIO that fires a gattling gun.

you can always write the whole thing in python, but it's a lot of roll your own stuff, whereas you get all the comms for free by using a web server and wget or similar.

6677
Posts: 383
Joined: Wed Mar 14, 2012 9:23 pm

Re: Pi to Pi communication

Tue Jun 25, 2013 4:19 pm

peewee2 wrote:I assume you have both raspis on some form of network, wifi or cabled to a router.

if so, then the simplest way is to run a web server on one and a web client on the other then use ajax calls from the client to send messages to the server.

I am unsure if I would use python on the server,it's easier to build the server using nginx and php, you are then leveraging 20 years of web development.

a simple example could be a pi with a camera running motion detects movement and sends a simple message to the server via a wget http://myserver/intruder.php?frontdoor=opened call
intruder.php calls a script ( python or anything else ) which sets a pin on the GPIO that fires a gattling gun.

you can always write the whole thing in python, but it's a lot of roll your own stuff, whereas you get all the comms for free by using a web server and wget or similar.
python is also used in that role

RepublicanSwag
Posts: 21
Joined: Tue Oct 28, 2014 9:15 pm

Re: Pi to Pi communication

Sat Nov 01, 2014 1:20 pm

How hard would it be to have 2 Raspberry Pis in different rooms communicate using encryption and decryption matrices?

User avatar
DougieLawson
Posts: 36159
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Pi to Pi communication

Sat Nov 01, 2014 1:27 pm

RepublicanSwag wrote:How hard would it be to have 2 Raspberry Pis in different rooms communicate using encryption and decryption matrices?
It's trivial with GnuPG and any sensible method of transferring the message from one pi to another. Each Pi needs to hold the public key of the other along with their private key.
https://www.digitalocean.com/community/ ... -12-04-vps

You can use email, http, MQTT or a locally written TCP/IP client/server to move the message from one Pi to the other.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

RepublicanSwag
Posts: 21
Joined: Tue Oct 28, 2014 9:15 pm

Re: Pi to Pi communication

Sat Nov 01, 2014 4:09 pm

Could this message be read aloud?

User avatar
DougieLawson
Posts: 36159
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Pi to Pi communication

Sat Nov 01, 2014 6:20 pm

RepublicanSwag wrote:Could this message be read aloud?
Sorry I don't understand. We're talking about encryption not text-to-speech.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Pi to Pi communication

Sun Nov 02, 2014 12:41 pm

It is a simple matter to send ad-hoc messages between Linux/Unix systems.

Create a shell script the acts as a listener and runs out of inetd. Then simply telnet to the port that the listener is on and send/receive the data. I have written http servers that are just a shell script.

You will need to add telnet to your system.

What about security? Secure it later. No sense in complicating the issue with encryption until you actually get it working first.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
DougieLawson
Posts: 36159
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Pi to Pi communication

Sun Nov 02, 2014 1:51 pm

Richard-TX wrote: Create a shell script the acts as a listener and runs out of inetd. Then simply telnet to the port that the listener is on and send/receive the data. I have written http servers that are just a shell script.
If you want really simple isn't it easier to install nc (aka netcat)?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Python”