Skip to content

Instantly share code, notes, and snippets.

@AugustoCiuffoletti
Last active September 11, 2023 04:30
Show Gist options
  • Save AugustoCiuffoletti/e9287365d031da9b3003023fdd9e10a3 to your computer and use it in GitHub Desktop.
Save AugustoCiuffoletti/e9287365d031da9b3003023fdd9e10a3 to your computer and use it in GitHub Desktop.
How to configure a Raspberry Pi as a mobile hotspot using a UMTS dongle

Configuring a Raspberry as a UMTS concentrator for IoT

Browsing around I found the pieces of this solution, and I put them together in a guide.

I did not use the guide in the Raspberry site since I found it complicate to make it compatible with the mobile dongle, and I used instead the NetworkManager daemon, following many advices in that sense I found in technical blogs. The problem with this solution is that, as of now, it is incompatible with the native solution in the Raspian distribution, and so I removed some native network tools. This is relevant if you plan to alter the network configuraton of the Raspberry, but it is otherwise transparent.

The reason why I developed this solution is to have an autonomous IoT concentrator device; as succh, it does not provide Internet access to devices connected to the RPi through WiFi or wired Ethernet. In the past I extended this project to give Internet access to a small classroom (10 students) not served by the Internet through the RPi. This is obtained by enabling and configuring packet forwarding into the RPi, which is not covered in this gist. There are many guides in the Internet about that, but remember that the solution should preferably use the NetworkManager toolset (i.e., nmcli) in order to alter the RPi networking configuration with NAT capabilities.

To have an idea of the capabilities of the RPi as a concentrator for WiFi sensors, consider that the WiFi range, measured using a WEMOS D1 R1, is little more than 30 meters in line of sight (-70dB).

Now you have two altrernatives: to follow the three steps Fast track, or to go to the Step by step guide; excluding the time to download the image file, it will take about half an hour on the computer, with a lot of free time for you. In both cases, when you have finished proceed to the So what? final section.

Fast track

  • Flash an SD with "Raspbian Buster Lite"
  • Configure SSID, PSK and APN in the MobileModem.sh script
  • Run the MobileModem.sh script as root on the Raspberry
  • Skip to "So what?"

Step by step guide

Materials I used (many variants are possible):

  • a computer (Ubuntu 19.04) connected to LAN with Internet access
  • an additional Ethernet plug cable on the same LAN
  • the image for Raspbian 10 (from here)
  • a SD to USB adapter
  • a 4GB SD card
  • a Raspberry PI 3 Model B V1.2
  • a TP-LINK MA 260 Mobile Modem USB stick (3G HSPA+)
  • a SIM card with data traffic

Flash a SD card

I used a 4 GB SD card for the task, leaving more than half of it free for data or additional packages. Use the SD to USB adapter on your PC to flash the card using Etcher. Before unmounting the SD from your PC, open the "boot" filesystem in your file manager and create an empty file named "ssh".

Prepare the Raspberry

Insert the SD into the RPi and connect it to the LAN using the Ethernet plug.

Power the RPi with a suitable power supply: do not branch it to the computer USB, it is not sufficient.

Find the internet address of your RPi with the following command:

$ nmap -p 22 --open -sV xxx.xxx.xxx.xxx/xx | less 

where the xxx.xxx.xxx.xxx/xx stands for the Internet address of your LAN, that you should know. Just in case, you can find it with:

ip address | grep global

Find the sequence of lines that refer to your RPi in the output of the nmap command (it takes a while to complete, be patient). Look for the "Raspbian" word and find some lines like the following:

Nmap scan report for <RPi address>
Host is up (0.088s latency).

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 7.9p1 Raspbian 10 (protocol 2.0)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

and annotate the address that appears on the first line (here and in the following it is replaced with )

Now copy your public key on the RPi:

ssh-copy-id pi@<RPi address>

using the pi user password, which is "raspberry".

On your PC open the MobileModem.sh script with a text editor and configure the three variables SSID, PSK and APN. Next copy the MobileModem.sh script on the RPi:

scp MobileModem.sh pi@<RPi address>:

(mind the final ":" in the command).

Configure the Hotspot

Log into your RPi using its IP address:

ssh pi@<RPi address>

No password needed, but you should change the default "raspberry" password as soon as possible.

When logged into the RPi (please check the command prompt "pi@raspberrypi:~ $ ") run the script:

pi@raspberrypi:~ $ sudo bash MobileModem.sh

At the end the Raspberry will automatically reboot.

So what?

While the Raspberry is booting up, you prepare the Mobile Modem USB dongle with the SIM card and you plug it into a USB port of the RPi. Wait for the green light on the modem and then ssh into the RPi with the usual command:

me@home:~ $ ssh pi@<RPi address>

Check that routes are correct:

pi@raspberrypi:~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         10.40.87.189    0.0.0.0         UG    700    0        0 wwan0
10.40.87.188    0.0.0.0         255.255.255.252 U     700    0        0 wwan0
10.42.0.0       0.0.0.0         255.255.255.0   U     600    0        0 wlan0
192.168.113.0   0.0.0.0         255.255.255.0   U     100    0        0 eth0

Verify Internet connection:

pi@raspberrypi:~ $ ping -Iwwan0 www.example.com
PING www.example.com (93.184.216.34) from 10.40.87.190 wwan0: 56(84) bytes of data.
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=1 ttl=51 time=548 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=2 ttl=51 time=509 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=3 ttl=51 time=477 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=4 ttl=51 time=487 ms
^C
--- www.example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 5ms
rtt min/avg/max/mdev = 477.325/505.087/547.943/27.216 ms

Finally verify the presence of the new WiFI hotspot using a WiFi capable device (smartphone, PC etc.). Avoid joining a non-IoT device to the hotspot in case you use an IoT SIM in the modem, since such devices produce a lot of hidden traffic that might rapidly consume you credit. It's better to develop and test using an ordinary SIM.

At that point you can still ssh into the Raspberry through the LAN, but the connection to the Internet is through the mobile network: take this into account if you plan heavy traffic, since this would consume SIM credit. Instead, remove the mobile modem from the USB and enable the Ethernet port with the following command (on one line, do not split into two lines):

sudo dhclient -r eth0; sudo dhclient eth0

The effects disappear with a reboot.

Disclaimer

The script has been tested one (1) time. Take any precaution you deem appropriate.

Credits

  • The hotspot configuration is taken from here .

  • More about mobile modem utilization is found in an Ubuntu guidenetwork-manager/docs/configure-cellular-connections .

#!/bin/bash
#Configure the following as you like
SSID="my_ssid" # The SSID of your new Access Point
PSK="my_secret" # The password to join the Access Point
APN="my.provider.apn" # The APN of the provider of your SIM card
set -e
if (( $EUID != 0 )); then
echo "Please run as root"
exit
fi
echo -e "##### apt: update system"
apt -y update
apt -y upgrade
echo -e "##### apt: install NetworkManager package"
apt -y install network-manager
echo -e "##### apt: purge conflicting package"
apt -y purge openresolv dhcpcd5
# Hotspot creation
echo -en "##### nmcli: Create wifi connection... "
nmcli connection add type wifi ifname wlan0 con-name Hotspot autoconnect yes ssid $SSID
echo "done"
echo -en "##### nmcli: Configure connection as wifi access point... "
nmcli connection modify Hotspot 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared
echo "done"
echo -en "##### nmcli: Configure connection security... "
nmcli connection modify Hotspot wifi-sec.key-mgmt wpa-psk
echo "done"
echo -en "##### nmcli: Define access point password (psk)... "
nmcli connection modify Hotspot wifi-sec.psk $PSK
echo "done"
# Create mobile modem connection
echo -en "##### nmcli: create mobile modem connection... "
sudo nmcli connection add type gsm ifname cdc-wdm0 con-name mobile apn $APN
echo "done"
# Inibizione della porta ethernet come default gateway
echo -en "##### nmcli: inhibit eth0 as default gateway... "
nmcli connection mod eth0 ipv4.never-default true autoconnect yes
echo "done"
echo -en "\n##### ALL DONE: REBOOTING NOW\n"
reboot
@sibero80
Copy link

Thank you @AugustoCiuffoletti for your reply, I'll give it a try right away.

I must admit I'm not a very advanced Linux user, so my understanding of the different networking tools is something I have never excelled at.... so just out of curiosity...in your script, you have an NCMLI command that creates a Wifi Hotspot, and the first time I followed your script I did it by manually running the commands one by one.
Before creating the GSM connection, I created the hotspot, and when I connected to it with my Phone to test it, I was able to navigate online.
This changed when I activated the GSM connection and De-activated the IPV4 default settings on eth0 in the last command.

what is the difference of using the NCMLI command to create the hotspot vs the hostapd approach you suggest? I ask because NMCLI seems very simple and elegant, while I see the hostapd requires more configurations.

@AugustoCiuffoletti
Copy link
Author

  • In my use case (wireless IoT concentrator) the RPI is not connected to the Internet with the Ethernet port, but only wireless through the dongle; so I deactivate the eth0 as the default route.
  • As far as I remember nmcli is best suited to configure the dongle but is incompatible with the native Linux configuration, hostapd included. As you see, I need to purge openresolv and DHCP (line 21).

So, be careful, since the guide I suggested you in my previous reply uses such tools: you need to look for a nmcli-based solution to implement your AP.

@sibero80
Copy link

My use case is exactly the same as yours... that is what has me confused... I only use ethernet for the initial configuration, as I expect to connect to the Raspberry Pi wirelessly in the future.
GSM->RPI->WLAN ----> Smarthphone

I started with a vanilla Raspbian image and followed your script only adjusting the connection and interface names for my case:

  • In line 42, changed "cdc-wdm0" to "*"
  • In line 47, changed "eth0" to "Wired connection 1"

That is it! everything is exactly the same as you did but still, no internet when I connect via Wifi from my Smartphone.
I have tried with 2 different Wireless USB adapters on the RPI, just to rule out any driver or device issues.
Did you do any particular configurations in IPTABLES or in the IP configuration of the Hotspot?

@AugustoCiuffoletti
Copy link
Author

AugustoCiuffoletti commented Mar 24, 2020 via email

@AugustoCiuffoletti
Copy link
Author

I made the project target more explicit. I plan to add a note (or a new gist) for the AP configuration, as soon as I find the time for it.

@sibero80
Copy link

Thank you for your reply.
I managed to get it working by adjusting the routing configurations by doing:

sudo route del default
sudo route add default dev ppp0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment