pathead
Posts: 21
Joined: Mon Dec 14, 2015 7:19 pm

How-To: Ethernet Bridge over serial(or xbee)

Tue May 31, 2016 1:43 am

Intro

The goal for this guide is first to create an 'ethernet bridge' between two Raspberry Pis by turning a serial connection into an ethernet connection. Then, using a router connected to Pi #1, internet access will be passed through to Pi #2 and anything connected to its Wifi or LAN, depending on how you set it up.

I used two Raspberry Pi 3s running the latest version of Rasbian. Haven't done any testing on earlier models so I can't comment on compatibility.... Pi #1 is connected to my home router and has a USB-to-TTL serial cable connected to the serial console of Pi #2(pins 8 and 10). Theoretically this should all work with a direct GPIO connection but I was unable to get it working so had to stick with the cable...

The main reason I thought this worth posting about is that it will work wirelessly using a pair of Xbees in transparent mode. For anyone unfamiliar, transparent mode simply creates a remote serial connection between 2 xbees. In effect this is creating an 'ethernet bridge', something Digi charges $1,000 per pair for. The only caveat is that the transfer speed sucks but it is more than enough for HTTP requests or ssh. I have tested using USB dongles and Series 2 Xbees but this should work with any xbee that has transparent mode I think. My goal is to use Xbee Pro 900HPs for short range indoor communication.

Anyway... lets get started.

First install three things:

Code: Select all

sudo apt-get install dnsmasq ppp hostapd
To get the console serial working on a Pi 3 add the following to /boot/config.txt and then restart.

Code: Select all

enable_uart=1
This may or not be required but its a good idea to update the firmware:

Code: Select all

sudo apt-get install rpi-update
sudo rpi-update
Follow the first part of Dave Honess's guide below to remove the console reference from /boot/cmdline.txt. Please note that the console serial on the Pi 3 uses /dev/ttyS0 or /dev/serial0 now instead of /dev/ttyAMA0.


Setup PPP

Credit for the PPP connection setup goes to Dave Honess, MagPi 41.

Pi #1 on /dev/ttyUSB0 (connected to router or modem)

Create a new file for the PPP options for USB0

Code: Select all

sudo nano /etc/ppp/options.ttyUSB0

Code: Select all

# /etc/ppp/options.ttyUSB0
noauth
nocrtscts
xonxoff
passive
local
maxfail 0
nodetach
192.168.5.100:192.168.5.101
persist
proxyarp
Create a new service to run ppp in the background

Code: Select all

sudo nano /etc/systemd/system/rpippp.service

Code: Select all

# /etc/systemd/system/rpippp.service
[Unit]
Description=PPP
[Service]
Type=idle
ExecStart=/usr/sbin/pppd -d /dev/ttyUSB0 115200
Restart=always
[Install]
WantedBy=multi-user.target
Alias=ppp.service
Then use the following to enable the new service:

Code: Select all

sudo systemctl daemon-reload
sudo systemctl enable rpippp.service

Pi #2 on /dev/ttyS0 (connected to something that wants internet access)
Create a new file for the PPP options for S0

Code: Select all

sudo nano /etc/ppp/options.ttyS0

Code: Select all

# /etc/ppp/options.ttyS0
noauth
nocrtscts
passive
local
maxfail 0
defaultroute
persist
nodetach
192.168.5.101:192.168.5.100
Create a new service to run ppp in the background

Code: Select all

sudo nano /etc/systemd/system/rpippp.service

Code: Select all

# /etc/systemd/system/rpippp.service
[Unit]
Description=PPP
[Service]
Type=idle
ExecStart=/usr/sbin/pppd -d /dev/ttyS0 115200
Restart=always
[Install]
WantedBy=multi-user.target
Alias=ppp.service
Then use the following to enable the new service:

Code: Select all

sudo systemctl daemon-reload
sudo systemctl enable rpippp.service
Restart both Pis. When you startup type the following command on either Pi to check if the PPP connection was successful:

Code: Select all

sudo systemctl status rpippp.service
If it works you will see something like this. If it times out then there is probably an issue with your serial connection.

Code: Select all

May 30 21:37:13 pitwo pppd[381]: local  IP address 192.168.5.101
May 30 21:37:13 pitwo pppd[381]: remote IP address 192.168.5.100
May 30 21:37:13 pitwo pppd[381]: Script /etc/ppp/ip-up started (pid 7411)
May 30 21:37:13 pitwo pppd[381]: local  IP address 192.168.5.101
May 30 21:37:13 pitwo pppd[381]: remote IP address 192.168.5.100
May 30 21:37:13 pitwo pppd[381]: Script /etc/ppp/ip-up started (pid 7411)
May 30 21:37:13 pitwo pppd[381]: Script /etc/ppp/ip-up finished (pid 7411), status = 0x0
May 30 21:37:13 pitwo pppd[381]: Script /etc/ppp/ip-up finished (pid 7411), status = 0x0
Setup DHCP on Pi #2

Pi #2 can provide access through either wifi or ethernet. To setup a static IP and dhcp service for wifi, follow the guide below but stop before setting up the iptables. For ethernet, do the same except skip the 'Configure hostapd' section. Make sure to use different IP addresses than the ones used for the PPP connection.
https://frillip.com/using-your-raspberr ... h-hostapd/


Setup IP Tables

Pi #1

Code: Select all

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE  
Pi #2

Code: Select all

sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

To have them load at startup, first save them to a file:

Code: Select all

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Then create a separate file that will restore the saved file at startup:

Code: Select all

sudo nano /lib/dhcpcd/dhcpcd-hooks/70-ipv4-nat

Code: Select all

# /lib/dhcpcd/dhcpcd-hooks/70-ipv4-nat
iptables-restore < /etc/iptables.ipv4.nat 
Wrapping up

From Pi #2 you should now be able to ping Pi #1 and vice versa:

Code: Select all

sudo ping 192.168.5.100
You should also be able to reach the internet but only through ppp0:

Code: Select all

sudo ping 8.8.8.8 -I ppp0
On Pi #2, type the following command to check the kernel IP routing table:

Code: Select all

netstat -rn

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.5.100   0.0.0.0         255.255.255.255 UH        0 0          0 ppp0
To route traffic through ppp0 we need to add a default route on Pi #2:

Code: Select all

sudo route add default gw 192.168.5.101 dev ppp0
Wait a few seconds and try a normal ping to 8.8.8.8 and it should work! Depending on which interface you set your DHCP service up on, anything that connects to Pi #2 should now be able to access the internet as well! If you are having trouble, try restarting the ppp service with:

Code: Select all

sudo systemctl restart rpippp.service
This was setup with Pi #1 on /dev/ttyUSB0 and Pi #2 on /dev/ttyS0. To change the serial port you need to change the reference in the service file

Code: Select all

sudo nano /etc/systemd/system/rpippp.service
Save the file and restart the service

Code: Select all

sudo systemctl restart rpippp.service
Then create the options file for the new device:

Code: Select all

# example to copy options for USB0 to USB1
sudo cp /etc/ppp/options.ttyUSB0 /etc/ppp/options.ttyUSB1
Final notes

The only aspect of this that is not automatic is adding the route to the IP routing table. I could not figure out how to set it to run at startup. Also anytime the ppp connection drops due to reboot, etc, the entry is cleared and needs to be re-added. It should be easy enough to write a script to run on Pi #2 to watch for that but I have not done so yet....

Any feedback is appreciated! I am definitely not an expert so if you see something that looks off please let me know.

Good luck!

akosin
Posts: 1
Joined: Tue Nov 15, 2016 2:57 am

Re: How-To: Ethernet Bridge over serial(or xbee)

Tue Dec 06, 2016 10:49 pm

I'm having some trouble with the DHCP part of it. Every time I plug my laptop into the Pi2, Windows auto-sets the IP and I can ping Pi2 but not Pi1(over XBee). Even manually setting the IP's doesn't seem to work. Any ideas?

Thanks!

Return to “Networking and servers”