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
Code: Select all
enable_uart=1
Code: Select all
sudo apt-get install rpi-update
sudo rpi-update
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
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
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
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
Code: Select all
sudo systemctl daemon-reload
sudo systemctl enable rpippp.service
Code: Select all
sudo systemctl status rpippp.service
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
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
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"
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
From Pi #2 you should now be able to ping Pi #1 and vice versa:
Code: Select all
sudo ping 192.168.5.100
Code: Select all
sudo ping 8.8.8.8 -I ppp0
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
Code: Select all
sudo route add default gw 192.168.5.101 dev ppp0
Code: Select all
sudo systemctl restart rpippp.service
Code: Select all
sudo nano /etc/systemd/system/rpippp.service
Code: Select all
sudo systemctl restart rpippp.service
Code: Select all
# example to copy options for USB0 to USB1
sudo cp /etc/ppp/options.ttyUSB0 /etc/ppp/options.ttyUSB1
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!