Raspberry PI as a wifi gateway, providing internet connection on board and below deck

Raspberry Pi as a gateway on board - wifi to wifi (and cable too)

While we are waiting for Starlink to come on-line.....

Elevator pitch : A travel router that accept connect to a remote wifi access point via a USB-wifi dongle with a high gain antenna and provide a local Wifi and cable network. The figure below is often presented in harbours, marinas and cafés where sailors congregate.

Hence a practical solution to extend the coverage from the café to below deck is needed, without spending too much on commercial solutions. A USB antenna, a Raspberry Pi3 and a 12V USB adapter is all that's needed.



The figure to the left is showing a connection to Internet using a long range USB wifi antenna to provide local coverage for both wireless and wire connections to the internet.

While my own version works well it is severely limited without a Web interface. The possibility to just open a web page after connecting to the access point and connect to the remote wifi shore station is a must have. Using the pen on a soft keyboard to enter password etc is not just user friendly. While it worked well I have been on the lookout for a Web interface. I recently came across a project called raspap-webgui. This is just what I need, working well from both a laptop and a phone. Very simple to change shore station and enter password.

While original out of the box it does not support using wlan1 as client (to connect to a shore station via a long range USB wifi antenna) and BOTH wlan0 (built in wifi interface) and eth0 (cable connection) as servers. Providing wifi and cabled internet access to all stations on board. Some of the computers on board require cabled network as there is no wifi connections available. Hence to be able to serve out internet connections on board BOTH via local wifi (192.168.1.x) range and local ethernet (172.16.10.x) range is vital. My old box could to this hence these services were also required with a new system. Having solved it before it was no big deal to include it in the rasap-webgui setup too. I've put all the files with relevant changes in the git repo. With some additional information to help other users setting up a similar system.

The antenna to the right is an example of a wifi long-range USB antenna, it plugs into the Raspberry Pi and even though the antenna draw a fair amount of power the USB port on the Raspberry 3B seem to cope with the load quite well. There are many models of such antennas on the market, but the most cost effective can be found in online bazars like aliexpress, like the one on the figure. With 13 dB gain (if the numbers are right) it should perform far better than smaller antennas. The famous "wifi bat" claim 8dB and is generally known to work very well. An old rule is that the more bulkier, longer or hard to place the better gain. While not always true with modern designs it still apply to a certain degree.

A little about antenna gain

While an omnidirectional antenna pick up signal all from all directions which is not much gain they do provide a narrow band in the vertical direction, hence they are very directional in the vertical direction. Looking at the upper figure to the left it's evident that it picks up signal from all directions but has a very narrow band like a pancake in the vertical direction, (a 10 dB gain antenna). Hence it is very important to place it vertical if the base station have the antenna vertical, which most of them do.

A more directional Yagi antenna would be even better, they have far higher gain some of the longer ones up to 25 dB. As that are very directional they are only useful in berths, longside etc where the boat is fixed. The lower figure on the left show the

strong directional gain for an antenna like the Yagi.

The question which antenna to choose is not trivial, but an omnidirectional antenna can be set up permanently and will pick up at any direction, at the cost of somewhat lower gain. If the signal is too weak to be of any practical use and the boat can remain fixed in direction a more directional antenna can be used. The difference between 25 dB and 13 dB can be dramatic, the 13 dB might not give a strong enough signal to be of any use while the 25 dB antenna will provide a nice strong signal. Many of the high gain antennas require a USB wifi dongle box. Some of these claim very high output power. While output power is nice if you want to send signal somewhere it does not help when the other station only transmit a very low power. It can actually be even counterproductive then your buddy hears your strong signal and think you are strong because you are close and reduce power. If both stations have high output power it's fine. However most cafes etc have just a normal router with small output power and then a really good antenna is the only solution as an antenna provide gain for both sending and receiving.

Open issues with Raspap:

    1. Simple connect like http://raspap instread of ip number, while raspap.local work from laptops, the mobiles still struggle using chrome.

    2. Using Eth0 as server connection and dhcp is not implemented in the distro.



Older version with 3" touch screen interface.

Most routers connect to the internet (WAN) via an Ethernet cable, this is fairly easy to set up and is covered in several tutorials. On board we do not normally have a cable connection. Most of us have access to a wireless network from the marina or a café. In most cases the signal is not very strong and to get good connection an external antenna is needed. Several options exist from the expensive ones to more cost effective models to the really high gain models (25 dB gain is equivalent to a gain of 10^2.5 = 316). A good antenna boost both sending and receiving, see this page or none commercial here. The antenna should also be high up to have free view of to the access point, not up in the mast but a bit higher than deck level, for this a long USB cable is needed and some vene come with an amplifier at half the cable length, mine is 15m and work well.

In order to forward the signal from the high gain wifi antenna to a local wifi on board a smart router setup is needed and as most of the ones rely on a cable connection I put together a version based on a Raspberry Pi. This is an alternative to the Redbox from Mailasail found in many yachts, the Redbox can do a few more things, like mail handling using the teleport software and with a much cooler web interface. It is also an alternative to small travel routers. While small travel routers are quite capable devices, they expect connection to the internet via cable connection (RJ45), a USB-4G modem or a tethered mobile phone. Without looking at the whole market I have not found any of the small ones that accept connection to the internet via a USB-wifi dongle. Hence this device and setup comes in handy.

My setup consist of a RPi with a 3.5 inch touch screen and a suitable distro with support for this type of screen. The rest is a range of packages and setup files. The task of forwarding from on wifi client to a wifi access point is not quite as easy as fist expected. Most access points assume the cable connection is to be forwarded and many of the examples on the net uses bridging (which let the local clients obtain ip-number from the remote provider). Other examples uses the same wifi device both as client and access point. It my case want a high gain antenna (USB) to be the client obtaining an ip address using dhcp from the remote wifi access point while at the same time use a local dhcp server to hand out local ip's to the local clients and use network address translation (NAT) to forward the packages correctly. The picture to the left show a small wifi dongle and not the high gain antenna. It's worth to mention here that it's vital to bind the devices (like wlan0) to the MAC address as the devices might change after different USBs wifi devices are connected. The wlan0 must be the internal wifi device for this to work. If not the whole point of using a high gain antenna would be lost.I have also set up a dhcp server on the eth0 so that other devices like the OpenPlotter computer, laptops etc can connect via Ethernet Cable. As the speed is only 100 Mbits/s a simple Ethernet Hub (no need for power supply) is enough for all connections. This is also an excellent connection to interact with the gateway, as it works nice with X11 forwarding.

Using a 4G USB dongle is another option I have not explored, I have always used an old phone as 4G router, set it up as an access point and it just works. It's no problem to connect this gateway to the access point provided by the phone, indeed all devices can connect to it. But only this gateway will provide cable connection, which is handy for none wifi units.

After some work a suitable setup was in place. All the files and some notes are available on github. When all parts came together it's works quite nice. No real programming was involved, just a range of setup and config files. It's just a question of connecting wlan1 to a suitable hot spot using the network icon on the screen and entering the password using a soft keyboard on the screen (or using a GUI remotely, see below) and connect your phone or laptop the the local gateway (which uses wlan0).

The pictures show how this looks in real life. The small screen is about the size of a mobile phone and using a pen from a gaming device (NDSi) it is very easy to select your favorite hotspot and also add the password. The soft

keyboard works nice, but it's nothing for entering any amount of serious text, like an email.

However, it might not always be that this method is practical or in some cases the Raspberry is operated without a screen. In these cases X11 comes to in handy. Just log into the Raspberry gateway, the ip number is the first in the range specified in the dhcp range, see the file dhcpcd.conf. In my case the dhcp server has the value 172.16.10.10.

Then issue the secure shell command with an option to allow X11 forwarding.

ssh 172.16.10.10 -l pi -Y

This will log you in and you can launch a GUI version of the wpa command. The user pi is ok, no need to be root.

Generate and copy ssh keys if you do not want to punch in the password every time. Then open the GUI:

wpa_gui -i wlan1 &

This will launch and run the application on the computer you launched it from, but any graphical output will be forwarded to the local computer on which the screen is attached. If you are familiar with X11 this is well known.

Below is an example of wpa_gui running on the RPi gateway, connected to the OpenPlotter RPi via an Ethernet RJ45 cable. The GUI is easy to use and all configuration of a client to a remote wifi hotspot can be done from the on the OpenPlotter computer, which benefits large screen, keyboard and mouse. Remember that the device to connect to the remote wifi hotspot is the wlan1, the internal wlan0 is the wlan device of the local access point. Don't mix up the two.

Web interface

In the future a web interface will be added so selection of remote access points can be done from a mobile phone, a pad or a laptop connected using wifi. It's trivial to set up a web server, but some more work is needed to set up a web service that can do the same as the wpa_gui application shown over. So far I have written a Python script to list and connect to a remote access point, while not in production quality something like this is needed for as a back end for the web interface.

Performance tests.

Tests have shown that the packet forwarding works quite nice and performance is ok for watching both youtube and Netflix. Packet forwarding and NAT-ing in software like a time

sharing OS like Linux is not optimal, but experience so far have been satisfactory. However, the bandwidth when transferring files seems to be a bit lower than when connecting directly to the wifi access point.

The power supply for the Raspberry Pi need to be a somewhat more powerful than normal as the high gain antenna need more power than just a small wifi dongle. The specs for the antenna claim a power of 1000mW

which is quite a lot. Using a 12V to USB power outlet a 2.1 A outlet is needed to be sure both the Rasperry Pi, the screen and the high gain antenna have enough power.

As the RPi has internet connection for most of its time running NTP should be fine and using applications like xclock or a more fancy clock like sunclock. It can also provide time for any of its clients so NTP should be mandatory. Unless you have a GPS based NTP server already on board.

On the downside is the wireless performance, the throughput bandwidth over wifi is not that great. While I can get speeds up to more than 5000 kB/s when transferring files from the mobile phone directly to the access point, I get only 200 to 300 kB/s when connecting to the RPi gateway when connecting over wifi. Part of the reason is the low performance of the built in wifi device. Using a client on the eth0 device yield 4000 kB/s, which suggest again that the built in wifi device have poor performance. The dongle connected to the USB port does not seem to be a bottleneck.

The illustration on the left show a simple test using a site for for network performance using a web page. Numbers are in Mbits/sec, downloading and uploading. There is about a factor of 10 in performance between the built in wifi (wlan0) and the cable conection (eth0). If the wifi performance is found to be inferior a small travel router might be a better option, another possibility would be to use a second wifi dongle. The latter introduces some more complexity, but might be the simplest solution, a wifi dongle is very cheap.

Older version, left for reference.

Setting up the Raspberry PI as a router/gateway - using eth0 as wan connection

The Pi can perform the task of a router and gateway just as good as a dedicated box. This saves yet another box. Setting up a Dynamic Host Configuration Protocol (DHCP) that provide IP addresses for cable Ethernet and Wireless Ethernet is a good start. This provide a local area network onboard the Yacht. Connection to the outside world can be done using a USB WiFi with a nice antenna. In this way all devices connected to the local network (typical addresses 192.168.1.x) can connect and use the Internet connection, one might even use a 3G/4G USB modem and use this a the internet connection. Some selected pages follow, Turn a Raspberry Pi into a WiFi router , raspberry-pi-and-routing-turning-a-pi-into-a-router . I have not yet set this up or tested as I have a router which provide DHCP etc in the office and the sensors are prioritized.

There are several project out there:

IoT-wifi

References

https://www.maketecheasier.com/set-up-raspberry-pi-as-wireless-access-point/

https://www.raspberrypi.org/forums/viewtopic.php?t=213968

https://www.waveshare.com/wiki/3.5inch_RPi_LCD_(A)

https://thepi.io/how-to-use-your-raspberry-pi-as-a-wireless-access-point/

https://lb.raspberrypi.org/forums/viewtopic.php?t=211542

https://www.raspberrypi.org/forums/viewtopic.php?t=191678

https://serverfault.com/questions/181094/how-do-i-delete-a-route-from-linux-routing-table

https://www.raspberrypi.org/forums/viewtopic.php?t=195216

https://www.raspberrypi.org/forums/viewtopic.php?t=64487

https://www.raspberrypi.org/forums/viewtopic.php?t=191453

https://www.karlrupp.net/en/computer/nat_tutorial

https://www.raspberrypi.org/forums/viewtopic.php?t=113702

http://w1.fi/cgit/hostap/plain/wpa_supplicant/README