Raspberry Pi As a 3g (Huawei E303) Wireless (Edimax EW-7811Un) Router

259,466

133

21

Introduction: Raspberry Pi As a 3g (Huawei E303) Wireless (Edimax EW-7811Un) Router

Aim:

Set up the Raspberry Pi as a wireless router using the Raspbian OS. The internet connection will be provided by a Huawei E303 USB 3g dongle on the safaricom network in Kenya, though the setup should be similar on most Huawei dongles and 3g networks. The wireless access point will be provided by an Edimax Nano USB Wifi adapter.

Power Source -------->RPI ----> Powered USB HUB -----> 3g Dongle
                                           |
                                           |         
                                     Edimax
                                        Wifi


The USB ports of the Pi seem to struggle to provide the power for a 3g dongle, so a powered USB hub is necessary.

After a lot of reading and messing around, I've come up with a step by step guide for setting up my Pi as a wireless internet router. I use this every time I make a fresh setup of Raspbian, which is a fairly regular event. Stumbling round my latest install like a drunken elephant, it never takes me long to screw something up so badly that it's easier just to start from scratch.

As such, this guide assumes that the Raspbian operating system is 'fresh'. It has been tested with the 2012-12-16 release - as soon as I have the time free, I'll update the guide for the newest release. So, the steps I use to set up the 'Pifi' Router are as follows:

This guide and more like it can be found on my blog.

Step 1: Make Sure the Pi Can See the 3g Dongle

Gather Information

To get more information about the USB devices connected to the Pi, we use the command 'lsusb'. Open a terminal window and type:

lsusb

should give an output similar to this:

Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 008: ID 050d:0234 Belkin Components F5U234 USB 2.0 4-Port Hub
Bus 001 Device 005: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 010: ID 12d1:1506 Huawei Technologies Co., Ltd. E398 LTE/UMTS/GSM Modem/Networkcard

lsusb lists the usb devices connected to the Pi, either directly or through the hub. I've highlighted the parts of the output that indicates the 3g dongle can be seen by the Pi. Yours should look similar to this, although it will vary from model to model - most 3g dongles on the market seem to be a Huawei of one flavour of another at the moment. So long as there is a mention of 'modem' or 'UMTS', you should be laughing. What you don't want to see is this:

Bus 001 Device 010: ID 12d1:14d1 Huawei Technologies Co., Ltd.

This is an issue that arises because your 3g dongle has a small partition on it containing the 'dashboard' software for windows. Your Pi has detected this partition, and labeled the dongle as a storage device before it had chance to notice the 3g modem waving its arms frantically in the background.

This happened occasionally with my 3g dongle - I would suggest following the next step even if you are feeling smug about your lsusb output at the moment. The next time you reboot your Pi it could detect your dongle as a storage device, throwing you into a fit of depression. Dangerous times

This can all be overcome with the use of usb_modeswitch. To install, in the terminal window type:

sudo apt-get install usb-modeswitch

The clue is in the name - its going to switch the mode of the usb device from 'storage' to 'modem. Yaaaaay. You need to give it some direction though, which will require some googling. The hexidecimal values highlighted here:

Good:   Bus 001 Device 010: ID 12d1:1506 Huawei Technologies Co., Ltd. E398 LTE/UMTS/GSM Modem/Networkcard

Bad:     Bus 001 Device 010: ID 12d1:14d1 Huawei Technologies Co., Ltd.

tell the operating system the vendor ID and device ID. More information on USB identification can be found on the debian wiki. Notice that the first 4 characters of the device ID (vendor ID) are the same in both examples, but the last 4 (device ID) have changed. Usb-modeswitch forces the operating system to use the combination that corresponds to the 3g modem.
usb-modeswitch usage

After usb-modeswitch has installed, an entry for your 3g dongle has to be made in /etc/usb_modeswitch.conf. If your device has already been detected, you can find the Device ID in the output from 'lsusb' - the portion highlighted in the examples above. If, however, you have the 'bad' example in your output, you need to find the correct Device ID (second part) for your 3g dongle - a good bet is a google search for '3g dongle model number usb_modeswitch'. Failing that, the search function of the usb-modeswitch forums. Your model number can normally be found somewhere on the device - try popping the cover off and looking for a sticker near where you insert the sim if you cant find it on the outside. We can assume the first part (vendor ID) is correct in both situations.

After a bit of research, the information I had to amend to the config file amounted to this:

DefaultVendor = 0x12d1
DefaultProduct = 0x1506
MessageEndPoint = "0x01"
MessageContent = "55534243000000000000000000000011060000000000000000 000000000000"

Once you have found the correct device ID, copy and paste the information above into a text editor. Amend the information to suit your own setup, deleting the bottom two lines if they are not needed - I'm not going to claim to understand the MessageEndPoint or MessageContent fields: They were part of the solution I found here - credit goes to krishnamohan of the fedoraforum.org site. Your device may or may not require them. Make sure to include the '0x' before the vendor and device IDs - this lets the Pi know that they are hexidecimal values.

To add this information to the usb-modeswitch config file, open the file for editing by issuing the following command in the terminal window:

sudo nano /etc/usb_modeswitch.conf

Paste the Device ID information from the text editor to the bottom of the config file you just opened in the terminal window. Hit Ctrl+X, y, Enter to exit and save changes.

Reboot your Pi and run 'lsusb', observing the output - if you found the right Device ID, you should see something to the 'good' output example shown above.

Step 2: Download and Setup Ppp, UMTSKeeper and Sakis3g

pppd


The ppp package will install the point to point protocol daemon, which will manage the connection between you and your 3g provider. The package can be installed via the terminal with the command:

sudo apt-get install ppp

Nothing further needs to happen with pppd, it should just sit there in the background, as a daemon is supposed to, waiting for Sakis3g to poke it in the ribs. Want to learn more? Here is a link to the Wikipedia page on daemons in linux.

UMTSKeeper

UMTSkeeper is used to automatically reconnect the 3g dongle using the Sakis3g script should the connection drop, which actually happens with monotonous regularity in Kenya.

Download and unpack UMTSkeeper by issuing the following commands from the terminal window:

sudo mkdir umtskeeper
cd umtskeeper
sudo wget "http://zool33.uni-graz.at/petz/umtskeeper/src/umtskeeper.tar.gz"
sudo tar -xzvf umtskeeper.gz
sudo chmod +x umtskeeper

The first command created a folder called 'umtskeeper' in the current directory you are in. To confirm what this directory is, type 'pwd'. Make note of the output, as we will need the full path to automate UMTS keeper later. Now we will download Sakis3g into the same directory to keep things simple.
Sakis3g

Sakis3g is a script used to make a 3g connection. You need to be able to provide a few details: your APN, the PIN for your sim card and your username and password if your provider requires them. This information can (hopefully) be found on your providers website or in the documentation that came with your simcard/dongle. If not, a search on google for 'your provider APN settings' should unearth them.

The sakis-3g.org website has been down for some time. Fortunately somebody has uploaded a copy of the script to sourceforge. To download and unpack it, issue the following commands from the terminal window:

sudo wget "http://downloads.sourceforge.net/project/vim-n4n0/sakis3g.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fvim-n4n0%2Ffiles%2F&ts=1363537696&use_mirror=tene~t" -O sakis3g.tar.gz
sudo tar -xzvf sakis36.tar.gz
sudo chmod +x sakis3g

Before going further, you should attempt to make a 3g connection using the sakis3g script alone, proving everything you've done up till now works. Sakis3g has an interactive mode, which will prompt you for information regarding your 3g connection. In the terminal window, type:

./sakis3g --interactive

and follow the prompts on screen. If you have no APN user or password, enter '0'. Once a connection has been made, make sure you can browse the internet, or issue a ping from the terminal window:

ping -c 4 google.com

Now test UMTSkeeper. The command at first may look a bit confusing (the details are for my connection):

./umtskeeper --sakisoperators "USBINTERFACE='0' OTHER='USBMODEM' USBMODEM='12d1:1506' APN='CUSTOM_APN' CUSTOM_APN='safaricom' SIM_PIN='1234' APN_USER='saf' APN_PASS='data'" --sakisswitches "--sudo --console" --devicename 'Huawei' --log --silent --monthstart 8 --nat 'no'

Breaking it down a little, these are the areas that you will need to change:

USBMODEM: The Device ID we found using the lsusb command earlier

CUSTOM_APN, APN_USER, APN_PASS, SIM_PIN: Information about your sim card and your providers data network.

A full breakdown of the paramaters can be found on the UMTSKeeper site..

Once you are satisfied UMTSKeeper is working in harmony with Sakis3g, we can edit /etc/rc.local so that it starts when the operating system boots. In the terminal window, type:

sudo nano /etc/rc.local

and add the following single line, edited to show your path to the umtsfolder you found with the 'pwd' command earlier:

PATH GOES HERE/umtskeeper --sakisoperators "USBINTERFACE='0' OTHER='USBMODEM' USBMODEM='12d1:1506' APN='CUSTOM_APN' CUSTOM_APN='safaricom' SIM_PIN='1234' APN_USER='saf' APN_PASS='data'" --sakisswitches "--sudo --console" --devicename 'Huawei' --log --silent --monthstart 8 --nat 'no' &

Exit, saving changes.

Step 3: Setup Wireless Access Point

The final hurdle involves setting up the wireless access point.

The chiipset in the Edimax Wifi adapter does not work 'out of the box' as an access point, so the process is a little disjointed. One step at a time:

Install 'hostapd' specifically built for Edimax adapter

hostapd manages the authentication on your wireless network. The version in the debian repositories in not compatible with the Edimax adapter - thankfully someone has put together a version which includes the correct drivers. Follow this guide to install the updated version of hostapd.

The guide mentions creating a configuration file. This should contain the following:

interface=wlan0
ssid=NAME OF YOUR WIRELESS NETWORK GOES HERE
channel=10
driver=rtl871xdrv
ieee80211n=1
hw_mode=g
device_name=RTL8188CUS
manufacturer=Realtek
wpa=2 # This sets the security settings to WPA2
wpa_passphrase=PASSWORD FOR NETWORK GOES HERE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP

# Other settings
beacon_int=100
auth_algs=3
wmm_enabled=1

Step 4: Configure NAT

NAT allows many clients on your private network to use the single public (internet side) ip address of the 3g dongle. The config for this part is stolen from the elinux.org guide.

First, edit the file /etc/sysctl.conf and add the following line to the bottom of the file:

net.ipv4.ip_forward=1

This will enable NAT in the kernel. Second, run the following commands:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

Your Pi is now NAT-ing. To make this permanent so you don't have to run the commands after each reboot, run the following command:

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Now edit the file /etc/network/interfaces and add the following line to the bottom of the file:

up iptables-restore < /etc/iptables.ipv4.nat

Step 5: Configure Wireless Interface and DHCP

The Edimax adapter needs to be given a static IP address. In the terminal window, type:

sudo nano /etc/network/interfaces

Amend the wlan0 entry so it looks like this:

#allow-hotplug wlan0
iface wlan0 inet static
address 10.0.0.1
network 10.0.0.0
netmask 255.255.255.0
To turn our Pi into a DHCP server, we will install dnsmasq from the terminal:

sudo apt-get install dnsmasq

Once dnsmasq has been installed, we need to edit its config file. As usual, from the terminal:

sudo nano /etc/dnsmasq.conf

amend these 2 lines to the end of the config file:

interface=wlan0
dhcp-range=10.0.0.2,10.0.0.10,255.255.255.0,12h

These lines set the DHCP server to listen on wlan0, and hand out a 12 hour lease to a maximum of 9 clients. If you need more clients, increase the number highlighted.

Fingers crossed!

Your Pi has now been configured to connect automatically to your providers 3g network via the USB dongle, and set the Edimax adapter as a wireless access point for up to 9 clients on the 10.0.0.0 network.

Now cross your fingers, reboot your Pi and try to connect from one of the client machines! If it doesn't work or you receive any errors during any of this guide, please leave a comment and I'll do my best to help you.

Be the First to Share

    Recommendations

    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • For the Home Contest

      For the Home Contest
    • Make It Bridge

      Make It Bridge

    21 Comments

    0
    olacomes
    olacomes

    5 years ago

    Hi,

    Thanks for this tutorial. but my raspberry pi 3 freezes
    after 5 seconds when HUWAEI E303 usb 3g dongle is plugged in. It does
    not freeze with ZTE usb dongle i have with me. Kindly assist me to
    resolve this issue.

    Thanks in Advance.

    0
    RobynC8
    RobynC8

    7 years ago

    Hello I get "/etc/rc.local: 15: /etc/rc.local: /home/pi/umtskeeper: Persmission Denied" on boot. I assume I need root persmission to run this?

    0
    nijeeshj
    nijeeshj

    7 years ago on Step 2

    i get an error chmode command not found

    what should i do ?

    0
    DavidA126
    DavidA126

    Reply 7 years ago on Introduction

    spell it chmod

    Chmode is spelt incorrectly

    0
    Happz0r
    Happz0r

    9 years ago

    Thanks for the tutorial, but it didnt seem to work for me. I got the AP up and running but my clients were not able to use the Internet connection.

    It almost drove me crazy, since I'm a linux noob and I wasn't able to solve this...

    Until I realized that there's an error in the Nat script.

    You wrote:
    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEP

    Problem is, that at least for me, using a 3G connection, you don't have to forward "eth0"(there is no cable plugged in anyway), but rather forward "ppp0".

    So all you got to do is to change
    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEP

    In

    sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
    sudo iptables -A FORWARD -i ppp0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i wlan0 -o ppp0 -j ACCEP


    At least that's what helped me :)

    0
    M.Y.L
    M.Y.L

    Reply 7 years ago on Introduction

    I also think it should be ppp0 (3G modem ppp connection) and not use eth0 (for Ethernet).

    0
    MübinI
    MübinI

    8 years ago

    Hi,

    I wonder how to access to RPi over 3G modem, since port 80 (http) or port 22(ssh) are not forwarded by operator. Is there any solution for that? One idea came in my mind is that:

    One private server hosts a special (password protected) text file, this text file includes commands. RPi check every x time this file over 3G, if there is something to run-> run it, otherwise wait x time.

    What other solutions?

    0
    dexxster
    dexxster

    9 years ago on Introduction

    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

    Can you please tell why are you using the eth0 interface and not the wlan0 ? Thanks.


    0
    Juan LuisP
    Juan LuisP

    Reply 8 years ago

    Have you enable forwarding in the kernel?

    echo 1 > /proc/sys/net/ipv4/ip_forward

    0
    JoséE2
    JoséE2

    8 years ago on Step 1

    Excelent! It works for me. Thank you for the post.

    0
    kunfannen
    kunfannen

    8 years ago on Introduction

    Thanks for the helpful instructable. Now I can connect my devices wirelessly to the internet here in Guinea. I had a bit of trouble getting things to work. Finally, I realized that I had a static address on the RPi that wasn't in the range you gave here. I was connecting headless via WiFi and needed the set IP address to do that. What I did was change the range of IP addresses in step 5. I edited /etc/network/interfaces so it now has:

    #wireless adapter
    allow-hotplug wlan0
    iface wlan0 inet static
    address 169.254.0.1
    network 169.254.0.0
    netmask 255.255.255.0

    And then I changed the end of /etc/dnsmasq.conf to say:

    interface=wlan0
    dhcp-range=169.254.0.3,169.254.0.13 ,255.255.255.0,12h

    So, now everything is working. A huge thank you for this instructable. The only problem I'm still having is that it seems that usb_modeswitch isn't working as it doesn't always boot up in the right mode and so doesn't always connect on boot. I'm still working on that though.

    Still, I am quite happy with the results and want to say thank you.

    0
    ндогонят
    ндогонят

    Reply 8 years ago on Introduction

    And... did you search what type of addresses were these 169.254.x you were getting? It's possibly working for 1 client, but you still have the same problem, except that the failure ends up assigning an address (to be seen as a failure to get the DHCP address) as a static address right in the DHCP range... Don't do that a second time unless you're ready to deal with random IP address conflicts :-S

    0
    SifisoS
    SifisoS

    8 years ago on Step 5

    1. With this setup, can one ssh to the raspberry pi?

    2. I'm trying to use a 3g modem to host a basic website, could you please help. ive looked so much and most people are using routers and then can access their websites on the internet. please help i also wanna Access my raspberry pi (website) via internet just using the 3g on the raspberry pi.

    Do you have any idea why this wouldn't work on a raspberry pi b+? i've got it all working on a model a, flashed the image to a micro sd and it works for the a but not the b+, any thoughts?

    0
    ManueleC
    ManueleC

    Reply 8 years ago on Introduction

    Hey- I'm having the same problem. Are you losing power to your USB ports when you attach the Huawei?

    0
    raceface2nd
    raceface2nd

    9 years ago

    thanks for the nice tutorial!

    I have a Problem with to get Clients connected to the eth0 to the Internet.

    My iptables-restore file is like following

    # Generated by iptables-save v1.4.14 on Sun Feb 2 01:41:20 2014
    *filter
    :INPUT ACCEPT [105:18135]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [99:22038]
    -A FORWARD -i ppp0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A FORWARD -i eth0 -o ppp0 -j ACCEPT
    COMMIT
    # Completed on Sun Feb 2 01:41:20 2014
    # Generated by iptables-save v1.4.14 on Sun Feb 2 01:41:20 2014
    *nat
    :PREROUTING ACCEPT [39:3046]
    :INPUT ACCEPT [23:1674]
    :OUTPUT ACCEPT [28:2989]
    :POSTROUTING ACCEPT [7:1539]
    -A POSTROUTING -o ppp0 -j MASQUERADE
    COMMIT
    # Completed on Sun Feb 2 01:41:20 2014

    What ist wrong? DNS seems to work, clients are getting the IP from DNS but no ping, nothing is possible.

    Any tipps?

    Best regards!

    Andy

    0
    svazquez6
    svazquez6

    9 years ago on Introduction

    Hi i have another version to install 3G Modem with Raspberry Pi, it is in Spanish but could help to some who wants to use Google Translate, also if you don't want to use Google Translate it could be easy 'cause it has images to guide... http://www.webonactivo.blogspot.mx/2013/09/raspberry-pi-con-banda-ancha.html

    0
    ismorenburg aguado
    ismorenburg aguado

    9 years ago on Step 2

    I made this script so you can execute (need internet connection first time to download stuff) and then will configure umtskeeper and 3gsakis, it will make the connection, is working with my carrier Tuenti and huawei k3772 dongle, so you have to modify the line 51 to connect with your setting hope it will be helpfull =)
    https://mega.co.nz/#!J9UFiBIZ!VsW4GWTcnqABHAAqG7UayUxz_aVHYlwSbvmowQXD2so

    0
    goofygutt
    goofygutt

    9 years ago on Step 2

    Nice tutorial! Had some real problems getting it to work on a Huawei E220 modem, but after updating the modem firmware, no problems =)
    Now I want to connect to the pc with the 3g Modem using remote desktop, but this was not so easy as it works a little different from usual DSL internet connections, I guess my operator has some restrictions. Have you tried this?

    0
    hadrianus
    hadrianus

    9 years ago on Step 2

    There is also a typo regarding the script
    sudo tar -xzvf sakis36.tar.gz
    instead of
    sudo tar -xzvf sakis3g.tar.gz