Oisin_DIT
Posts: 4
Joined: Tue Feb 18, 2014 4:56 pm

RPi to RPi via ethernet

Tue Feb 18, 2014 5:19 pm

Hello,

A colleague and I are trying to connect two Raspberry Pis directly via ethernet. The reason is that one Pi is simulating a sensor we don't have yet but we know communicates over ethernet. However we are struggling to get a connection between them.

We've managed to get a wifi link to work but we can't get anything over the ethernet, we have tried pinging one pi's ip address from the other but it doesn't work. Below is the code we have come up with from different sources on the net and the changes we have made to the network interfaces file. We are totally new to the Pi and python so hopefully someone more experienced can point out our rookie mistakes?

Our code on the first Pi is

Code: Select all

import socket
import time
HOST = ''
PORT = 50007
x = 1

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST,PORT))
s.listen(1)

conn, addr = s.accept()
print 'Connected by', addr
conn.sendall('Test') 

while 1:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close()
and the interfaces file is

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet static

address 172.16.1.21
netmask 255.255.255.0
gateway 192.168.1.2
The code for the second Pi is

Code: Select all

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print('Socket Created')

port = 50007

remote_ip = '172.16.1.21'


s.connect((remote_ip , port))

s.sendall('Test Message')

s.close()
and its interface file is

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet static

address 172.16.0.1
netmask 255.255.255.0
gateway 172.16.0.2
We have tried both a normal ethernet and a crossover ethernet cable.

Thanks in advance

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5645
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: RPi to RPi via ethernet

Tue Feb 18, 2014 6:04 pm

AFAICT, you seem to have two different subnets. IIRC the "valid" one for a LAN (or local) connection is the 192.168.x.y one . I may be wrong, but I think the 172.16.a.b addresses are for WAN (internet not intranet) use. My Pi's use "pseudo-static"** addresses in the 192.168.x.y subnet (ie. fixed "x" but "y" changes) albeit on a router configured LAN. Although I haven't needed to configure a direct connection "Pi to Pi" I've done a similar thing for USB networking "Pi to netbook":
http://www.cpmspectrepi.webspace.virgin ... tworkCable
Trev.
**Fixed addresses associated with their MAC addresses by DHCP by the router.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Oisin_DIT
Posts: 4
Joined: Tue Feb 18, 2014 4:56 pm

Re: RPi to RPi via ethernet

Wed Feb 19, 2014 10:25 am

Thanks for your help, you got us thinking in the right area. In case it helps anybody else we changed the interfaces file for the first pi to

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet static

address 192.168.5.1
netmask 255.255.255.0
gateway 192.168.5.0
and the second to

Code: Select all

auto lo

iface lo inet loopback
iface eth0 inet static

address 192.168.5.2
netmask 255.255.255.0
gateway 192.168.5.0
and we are now able to send a message to the second pi.

Thanks again :D

Return to “Interfacing (DSI, CSI, I2C, etc.)”