Raspberry PI Portable Hotspot with Android USB tethering

Check my RPI articles in Best Raspberry PI projects article or peppe8o.com home page. Or subscribe my newsletter (top right in this page) to be notified when new projects are available! Also interested to start 3D printing with a cheap budget? Visit my cheap 3D printers list
0
(0)

Connecting devices from Android WiFi Hotspot fastly drains smartphones battery and can consume in a few days your mobile subscription traffic. Raspberry PI, connected to smartphones USB tethering, works greatly as WiFi hotspot and adds advanced features

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

A common Raspberry PI usage makes its ethernet port connected to old routers (without WiFi) to create an 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.

Raspberry PI Zero W

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

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

What We Need

As usual, I suggest adding from now to your favourite ecommerce shopping chart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from shopping chart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon raspberry pi Zero W box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box
Amazon Smartphone box

Wiring Diagram

Please find in following picture wiring diagram in case of Raspberry PI Zero W:

RPI USB tethering wiring

Step-by-Step Procedure

We’ll start preparing Operating system.

Then we’ll configure a static IP for wireless interface (wlan0), as it will be used from client devices as default gateway. All connection will be routed to usb0 interface, which is usb port connected to smartphone in tethering mode (you can see it with ipconfig terminal command when USB tethering is enabled).

Then routing and dhcp server will be configure to correctly drive traffic and assign dynamic IPs to clients. Finally, WiFI hotspot settings will complete our installation.

All of these setting can be performed with a classic headless installation, with Raspberry PI connected to your home WiFi by wpa_supplicant. Last command in this procedure will restart Raspberry PI telling it to not consider client wifi configuration, as it will become an Access Point.

Install OS and Packages

Start preparing Raspberry PI operating system. I suggest to install Raspberry PI OS Lite to have an headless, fast OS. You can also use Raspberry PI OS Desktop (in this case, you will use its internal terminal).

Make your OS up-t-date. From terminal:

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

Install required packages:

sudo apt install hostapd dnsmasq

Configure Routing

Start defining a static configuration for Raspberry PI WiFi interface. Even if it can lead to misunderstandings, in linux the dhcpcd service identify for RPI its client IP configuration. From terminal, edit dhcpd configuration 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

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

Before setting traffic routing in RPI, we need to enable IP forwarding process:

sudo nano /etc/sysctl.conf

Uncomment:

net.ipv4.ip_forward=1

All routing capabilities will be set with a single command line, thus allowing traffic from all Rasapberry PI external clients, which will consider usb0 interface as exit gateway:

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

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).

This routing command will run until reboot. To make it persistent, we need to export running iptables configuration to a file and configure RPI in restoring this ip routing settings at each reboot. “iptables-save” needs to be logged as root (sudo is not enought), so these steps will be performed with following:

sudo su -
iptables-save > /etc/iptables.rules
exit
sudo nano /etc/rc.local

Add following line before the final “Exit 0”:

iptables-restore < /etc/iptables.rules

so that your rc.local should appear similar following one:

....(omitted)....

_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

iptables-restore < /etc/iptables.rules
exit 0

Configure DHCP and DNS

Start creating a backup of your config:

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 192.168.2.1 IP address (your RPI) as gateway.

Configure access point:

sudo nano /etc/hostapd/hostapd.conf

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

country_code=US
interface=wlan0
ssid=RpiWiFi    
hw_mode=g
channel=8
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=wifipassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Connect Smartphone and Reboot Raspberry PI

If still not done, you can connect now your smartphone to Raspberry PI USB port. Enable USB tethering:

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

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

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

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 0 / 5. Vote count: 0

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?