Raspberry PI WiFi Hotspot with Android USB tethering

3
(6)

Last Updated on 2nd September 2023 by peppe8o

In this tutorial, I’m going to show you how to configure a Raspberry PI WiFi Hotspot to share smartphone USB connectivity creating a WiFi Network.

Connecting devices from Android WiFi Hotspots fastly drains your smartphone’s battery and can waste it in a few months. Raspberry PI, connected to smartphone USB tethering, works great as a WiFi hotspot and adds advanced features

A common Raspberry PI usage makes its ethernet port connected to old routers (without WiFi) to create a home Access Point. This works with boards including ethernet ports (RPI models B), but can’t be applied to all those boards (like RPI Zero W and PI 3 Model A+) which have an ideal form factor for portability.

Moreover, with increasing smart working needs, our smartphone usage as a WiFi hotspot can drain mobile batteries while having WiFi the hotspot function always on.

Using a small Raspberry PI (like the Raspberry PI Zero 2 W) to share smartphone connectivity across a WiFi network (created in RPI) also increases your WLAN capabilities, as you can run on the same single-board computer also a traffic-blocking system (like Pi-Hole), Proxy capabilities (like Squid, Privoxy or TOR) or creating local services (like a git web portal or WordPress host).

Of course, you can use this tutorial with all the Raspberry PI computer boards.

What We’ll Learn

We’ll configure a static IP for the wireless interface (wlan0, with the last character being a zero), which will be used from client devices as the default gateway.

All connections will be routed to the usb0 (with the last character being a zero) interface, which is the USB port connected to your smartphone in tethering mode (you can see it with the “ipconfig” terminal command when the smartphone is connected and the USB tethering is enabled).

Then, we’ll configure routing and DHCP server to correctly drive traffic and assign dynamic IPs to clients.

Finally, WiFI hotspot settings will complete our installation.

Please note that you can use this tutorial also as WiFi hotspot with Raspberry PI sharing your home internet connection, by connecting your home router via the Ethernet cable and changing the “usb0” in the following configurations with “eth0” (also here, the last character is a zero).

What We Need

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

raspberry-pi-zero-2-w-board-500px
  • Raspberry PI Zero 2 W (including proper power supply or using a smartphone micro USB charger with at least 3A) or newer Raspberry PI Board. If you use an RPI Zero W, you could also need a USB to micro USB adapter to connect your smartphone with RPI Zero out port.
  • high speed micro SD card (at least 16 GB, at least class 10)
  • a Smartphone with USB tethering function (available in quite all recent smartphones)

Wiring Diagram

Please refer to the following picture for the wiring diagram. The picture shows a Raspberry PI Zero 2 W, but you can use the same to create a WiFi hotspot with any Raspberry PI computer board.

raspberry-pi-zero-2-w-hotspot-wiring-diagram

Step-by-Step Procedure

Prepare the Operating System for Raspberry PI WiFi Hotspot

Start preparing the Raspberry PI operating system. I suggest you to install Raspberry PI OS Lite to have a headless, fast OS. After this step, please make your OS up-to-date. From terminal:

sudo apt update -y && sudo apt upgrade -y

We also need to install additional packages:

sudo apt install hostapd dnsmasq -y

Check The nfttables Service

In the previous article version, the routing was managed by the old “iptables” service (still available only in legacy OSes). The “iptables” service has been replaced in Raspberry PI OS Lite with the newer “nftables” service, already available by default.

In order to check that the nftables service is installed, you can run the following terminal command:

nft -v

And nftables service should answer correctly the version query:

pi@raspberrypi:~ $ nft -v
nftables v0.9.8 (E.D.S.)

Disable WiFi Power Save

The Raspberry PI OS WiFi Power Save is a configuration that tries to reduce the WiFi chip power consumption. It is useful when the board works as a client of a network but, for hotspot usage, I suggest disabling it (in any case, this is optional). From the terminal, please open the rc.local file:

sudo nano /etc/rc.local

And append the following before the final “exit 0” line:

iw dev wlan0 set power_save off

Close and save.

Configure Static IP

In order to keep the Raspberry PI computer board always reachable from the WiFi network, we must give it a fixed IP address on that interface. Even if it can lead to misunderstandings, in Linux the dhcpcd configuration file manages the Raspberry PI’s IP configuration.

From the terminal, edit the dhcpcd.conf file:

sudo nano /etc/dhcpcd.conf

And append at the end the following:

interface wlan0
static ip_address=192.168.2.1/24
nohook wpa_supplicant

The “nohook wpa_supplicant” will disable the wpa_supplicat configuration file usage from the next reboot. “192.168.2.1” will become the IP address your Raspberry PI will use inside its WiFi LAN.

Save and close this file.

Connect Your Smartphone and enable USB Tethering

If still not done, you can connect now your smartphone to the Raspberry PI USB port. You have also to enable USB tethering from your smartphone, according to its OS user manual. Usually:

  • Android Smartphones: this function is usually available in Settings -> Connections -> Mobile Hotspot and Tethering
  • Apple Smartphones: this function is usually available in Settings -> Cellular (some iOS versions skip this step) -> Personal Hotspot

Enable the Routing

In this chapter, we’re going to set the Raspberry PI OS to redirect all the traffic entering from any port to the “usb0” port. The usb0 port is the interface automatically created by the Raspberry PI when it automatically detects a modem from one of its USB interfaces (you can check it by using the “ifconfig” terminal command on Raspberry PI once you have connected the smartphone and enabled here the USB Tethering).

Before setting traffic routing in RPI, we need to enable the IP forwarding process. From the Raspberry PI terminal:

sudo nano /etc/sysctl.conf

Find the following line and uncomment it (by removing the “#” char at the beginning of the line):

net.ipv4.ip_forward=1

With the following commands, we’ll allow traffic coming from all Raspberry PI WiFi Hotspot external clients to consider the usb0 interface as the exit gateway. This configuration is similar to the one used for a ethernet-to-wifi RPI Access Point, just changing usb0 with eth0 (only for boards with cable connection and only if you want to share router ethernet internet connection instead of USB connection).

Let’s create a folder that will include the nft configuration files (even for your possible future needs):

sudo mkdir /etc/nftables.d

Now, please create a new configuration file inside this folder:

sudo nano /etc/nftables.d/nat.conf

Use the following lines for the created file:

table ip nat {
        chain postrouting {
                type nat hook postrouting priority srcnat; policy accept;
                oif "usb0" masquerade
        }
}

Close and save.

In order to make this (and any “*.conf” file from this folder) loaded from the nft service, we have to include the folder files to the “/etc/nftables.conf” configuration file. The following command will do the job for you:

echo 'include "/etc/nftables.d/*.conf"' | sudo tee -a /etc/nftables.conf

Please note that the routing is still not active and, even when activated, it will disappear after every reboot. Moreover, this command works correctly only when it can detect a “usb0” interface connected, so we can’t run it automatically at the boot time, because if the smartphone is still not connected it will result in an internal error not directly visible to you.

So, the trick is loading the “usb0” routing only when the Raspberry PI OS detects that the usb0 interface is plugged. For this reason we’ll use a “dhcpcd hook”. From the terminal, please create a new file:

sudo nano /lib/dhcpcd/dhcpcd-hooks/99-setntw

And add the following lines

#!/bin/bash
if [ "$interface" = "usb0" ]
then
 nft -f /etc/nftables.conf
fi

the “nft -f /etc/nftables.conf” command is the one that loads the routing and it will be executed only when it will detect the interface.

Configure DHCP and DNS

Create a backup of your dnsmasq configuration file:

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.backup

Open /etc/dnsmasq.conf file with your terminal editor:

sudo nano /etc/dnsmasq.conf

and add the following lines:

interface=wlan0 
dhcp-range=192.168.2.2,192.168.2.100,255.255.255.0,24h
domain=wlan
address=/gw.wlan/192.168.2.1

This configuration will configure IP assigned to WiFi clients in a range in a local “192.168.2.x” subnet, where devices will get an IP ending with a number between 2 and 100. All devices will be advised to use the 192.168.2.1 IP address (your Raspberry PI) as the gateway address.

Configure the Raspberry PI WiFi Hotspot

Configure the access point network. Please create a hostapd configuration file:

sudo nano /etc/hostapd/hostapd.conf

Add the following lines (changing country_code with your one; SSID and password with your favourite one):

##### hostapd configuration file ##############################################
interface=wlan0

##### IEEE 802.11 related configuration #######################################
country_code=US
ssid=RpiWiFi
hw_mode=g
channel=7
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0

##### WPA/IEEE 802.11i configuration ##########################################
wpa=2
wpa_passphrase=wifipassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Configure Hosts

Before finishing the configuration and making the Raspberry PI WiFi hotspot running, I also suggest adding a few resolution names to your Raspberry PI (optional). It should improve the Hotspot performance and avoid Windows clients to release the WiFi network when your USB smartphone internet line is not really strong.

In the following lines, the “msftconnecttest.com” and “msftncsi.com” are the Microsoft service used by Microsoft Windows to detect if an internet network is connected to the web. When it can’t ping these services or the ping test is not performing really fine, your Windows PC will try to connect to a different network (from those already stored in your PC), in order to find a better internet connection.

So, open the hosts file from terminal:

sudo nano /etc/hosts

And append the following 3 lines, which include also the IP of our Raspberry PI from the Hotspot network:

127.0.0.1     www.msftconnecttest.com
127.0.0.1     www.msftncsi.com
192.168.2.1   localhost

Close and save.

Complete the Configuration and Reboot Raspberry PI

Last but not least, I experienced that sometimes the USB modem connection was fighting with the ModemManager service to use the same port. If you don’t really need this service in your Raspberry PI, you can stop and disable it:

sudo systemctl stop ModemManager.service
sudo systemctl disable ModemManager.service

Finally, enable the wireless access point and reboot Raspberry PI with the new configuration by using the following terminal command:

sudo systemctl unmask hostapd && sudo systemctl enable hostapd && sudo reboot

After the reboot finishes, you can plug the smartphone and turn on USB Tethering.

The RpiWiFi will appear in your WiFi list in a few time (give it time to appear after every boot) and you should be able to use it for your internet connection.

Next Steps

You can take a look at my Raspberry PI projects to take inspiration for your first projects with Raspberry PI Zero 2 W (and other Raspberry PI computer boards).

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 3 / 5. Vote count: 6

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?