sparkie777
Posts: 281
Joined: Tue Nov 27, 2012 4:37 am

HOWTO: establish an IP connection between 2 PIs via the serial interface (3 wires)

Sun Sep 26, 2021 4:21 pm

[UPDATE as of 2021_09_29]
there already exists and older posting doing the same:
Connect Model A to Model B via serial lines - Raspberry Pi Forums
[/UPDATE as of 2021_09_29]

in this context one PI is called 'server' the other one 'client'


0. first connect the serial GPIO pins between both PIs:

Code: Select all

server:                         client:
GPIO  6 (ground)   connect to   GPIO  6 (ground)
GPIO  8 (TXD)      connect to   GPIO 10 (RXD)
GPIO 10 (RXD)      connect to   GPIO  8 (TXD)

1. install some packages on both PIs:

Code: Select all

apt install ppp lsof iftop netcat-openbsd

2. make sure the serial port is not used by any application on any PI:

Code: Select all

lsof | grep ttyAMA0
should show empty results. Eventually remove serial console related parts (only those :-) ) out of '/boot/cmdline.txt'.
In particular remove (if any):

/boot/cmdline.txt:

Code: Select all

console=serial0,115200 kgdboc=serial0,115200

2.5 enable the serial interface on both PIs in /boot/config.txt (if not already done):

/boot/config.txt:

Code: Select all

[...]
enable_uart=1
dtoverlay=disable-bt
[...]

3. replace the file '/etc/ppp/options' on the server with this file content:

/etc/ppp/options:

Code: Select all

asyncmap 0
xonxoff
local
lock
/dev/ttyAMA0
nodetach
115200
192.168.1.1:192.168.0.1

4. similarly replace the file '/etc/ppp/options' on the client with this file content:

/etc/ppp/options:

Code: Select all

asyncmap 0
xonxoff
local
lock
/dev/ttyAMA0
nodetach
115200
192.168.0.1:192.168.1.1

5. now start 'pppd' on both machines:

Code: Select all

# /usr/sbin/pppd

6. on both machines you now should see something like:

Code: Select all

Using interface ppp0
Connect: ppp0 <--> /dev/ttyAMA0
Deflate (15) compression enabled
local  IP address 192.168.0.1
remote IP address 192.168.1.1
local  LL address fe80::4901:832b:7e2e:3472
remote LL address fe80::3044:39b7:cd7f:2393

7. the connection now is up and running :-)

to test the connection speed start

Code: Select all

# nc -l -p 9000 > /dev/zero
on the server and

Code: Select all

nc 192.168.1.1 9000 < /dev/zero
on the client

To display the bandwidth start 'iftop'

Code: Select all

iftop -Bi ppp0
on either the server or the client. With a baudrate of 115200 this will give you amazing

760KB/s running over 3 wires:

Code: Select all

                 23.8MB           47.7MB           71.5MB           95.4MB       119MB
└────────────────┴────────────────┴────────────────┴────────────────┴─────────────────
192.168.0.1                   => 192.168.1.1                    760KB   731KB   685KB
                              <=                               12.4KB  12.5KB  12.0KB












──────────────────────────────────────────────────────────────────────────────────────
TX:             cum:   30.3MB   peak:    762KB        rates:    760KB   731KB   685KB
RX:                     539KB           13.1KB                 12.4KB  12.5KB  12.0KB
TOTAL:                 30.8MB            775KB                  773KB   743KB   697KB
Last edited by sparkie777 on Wed Sep 29, 2021 2:13 pm, edited 2 times in total.

cleverca22
Posts: 4735
Joined: Sat Aug 18, 2012 2:33 pm

Re: HOWTO: establish an IP connection between 2 PIs via the serial interface (3 wires)

Sun Sep 26, 2021 4:45 pm

sparkie777 wrote:
Sun Sep 26, 2021 4:21 pm
should show empty results. Eventually remove serial console related parts (only those ) out of '/boot/cmdline.txt'.
something ive thought about, but havent gotten around to testing

tools like wvdial can detect a login prompt on a serial port, and punch in a configured name/pw

what if you left the serial port enabled on one system, and added a user with pppd configured as the shell
when you run wvdial on the other machine, it can login over the serial port, causing pppd to startup

but you now have the choice to get a plain text console instead, if you dont run wvdial
sparkie777 wrote:
Sun Sep 26, 2021 4:21 pm
dtoverlay=pi3-disable-bt
that overlay has been renamed to just disable-bt, since it now also applies to the pi4 and pi0 as well

sparkie777
Posts: 281
Joined: Tue Nov 27, 2012 4:37 am

Re: HOWTO: establish an IP connection between 2 PIs via the serial interface (3 wires)

Sun Sep 26, 2021 4:54 pm

thanks for the updated overlay naming. I corrected this. The 'wvdial' options sound interesting. When I have some spare time I will test it.

GlowInTheDark
Posts: 2040
Joined: Sat Nov 09, 2019 12:14 pm

Re: HOWTO: establish an IP connection between 2 PIs via the serial interface (3 wires)

Sun Sep 26, 2021 6:21 pm

I'm (constructively) curious as to what the actual, underlying goal is here. One application I could see is to get Internet access on a Pi0, without using up the one and only USB port (and without using additional hardware, such as a hub with ethernet). All you would have to do is run 3 jumpers between the necessary GPIO pins on each board.

That said, I think I would solve this overall problem by using "slirp" (instead of ppp) - a really nifty package.

I actually ran slirp as my Internet connection for many years, when all I had was a simple dialup (no ppp or anything fancy like that - just a simple dialup that allowed me to login to one machine). But slirp turns that one serial connection into a regular Internet connection and doesn't require any fancy setup stuff. It "just works".
Poster of inconvenient truths.

Back from a short, unplanned vacation. Did you miss me?

sparkie777
Posts: 281
Joined: Tue Nov 27, 2012 4:37 am

Re: HOWTO: establish an IP connection between 2 PIs via the serial interface (3 wires)

Sun Sep 26, 2021 6:40 pm

GlowInTheDark wrote:
Sun Sep 26, 2021 6:21 pm
I'm (constructively) curious as to what the actual, underlying goal is here.
I had to connect 2 PIs. Unfortunately there are only 4 simple wires (non twisted) available between the locations. I didn't dare to run fast ethernet directly. With ppp the serial connection is transparent to the rest of the network. I spent some time to find a trivial ppp setup for this purpose. Finally I used the forum as scratchpad for this :-)
One application I could see is to get Internet access on a Pi0, without using up the one and only USB port (and without using additional hardware, such as a hub with ethernet). All you would have to do is run 3 jumpers between the necessary GPIO pins on each board.
quite right

I don't know "slirp". So I can't tell if if could replace ppp in this use case. I like the idea of having regular interfaces as ppp0. I can handle these just like eth0 in firewall/NAT rules.

Return to “Other projects”