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

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:


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.
<p>i get an error chmode command not found</p><p>what should i do ?</p>
<p>spell it chmod </p><p>Chmode is spelt incorrectly</p>
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. <br><br>It almost drove me crazy, since I'm a linux noob and I wasn't able to solve this... <br><br>Until I realized that there's an error in the Nat script. <br><br>You wrote:<br>sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE<br>sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT<br>sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEP<br><br>Problem is, that at least for me, using a 3G connection, you don't have to forward &quot;eth0&quot;(there is no cable plugged in anyway), but rather forward &quot;ppp0&quot;.<br><br>So all you got to do is to change <br>sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE<br>sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT<br>sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEP<br><br>In<br><br>sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE<br>sudo iptables -A FORWARD -i ppp0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT<br>sudo iptables -A FORWARD -i wlan0 -o ppp0 -j ACCEP<br><br><br>At least that's what helped me :)
<p>I also think it should be ppp0 (3G modem ppp connection) and not use eth0 (for Ethernet).</p>
<p>Hi,</p><p>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:</p><p>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-&gt; run it, otherwise wait x time.</p><p>What other solutions?</p>
<em>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</em><br> <br> Can you please tell why are you using the eth0 interface and not the wlan0 ? Thanks.<br> <br> <br>
<p>Have you enable forwarding in the kernel?</p><p>echo 1 &gt; /proc/sys/net/ipv4/ip_forward</p>
<p>Excelent! It works for me. Thank you for the post.</p>
<p>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:</p><p>#wireless adapter<br>allow-hotplug wlan0<br>iface wlan0 inet static<br>address<br>network<br>netmask</p><p>And then I changed the end of /etc/dnsmasq.conf to say:</p><p>interface=wlan0<br>dhcp-range=, ,,12h</p><p>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. </p><p>Still, I am quite happy with the results and want to say thank you. </p>
<p>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</p>
<p>1. With this setup, can one ssh to the raspberry pi?</p><p>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.</p>
<p>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?</p>
<p>Hey- I'm having the same problem. Are you losing power to your USB ports when you attach the Huawei? </p>
thanks for the nice tutorial!<br><br>I have a Problem with to get Clients connected to the eth0 to the Internet.<br><br>My iptables-restore file is like following<br><br># Generated by iptables-save v1.4.14 on Sun Feb 2 01:41:20 2014<br>*filter<br>:INPUT ACCEPT [105:18135]<br>:FORWARD ACCEPT [0:0]<br>:OUTPUT ACCEPT [99:22038]<br>-A FORWARD -i ppp0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT<br>-A FORWARD -i eth0 -o ppp0 -j ACCEPT<br>COMMIT<br># Completed on Sun Feb 2 01:41:20 2014<br># Generated by iptables-save v1.4.14 on Sun Feb 2 01:41:20 2014<br>*nat<br>:PREROUTING ACCEPT [39:3046]<br>:INPUT ACCEPT [23:1674]<br>:OUTPUT ACCEPT [28:2989]<br>:POSTROUTING ACCEPT [7:1539]<br>-A POSTROUTING -o ppp0 -j MASQUERADE<br>COMMIT<br># Completed on Sun Feb 2 01:41:20 2014<br><br>What ist wrong? DNS seems to work, clients are getting the IP from DNS but no ping, nothing is possible.<br><br>Any tipps?<br><br>Best regards!<br><br>Andy
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
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 =) <br>https://mega.co.nz/#!J9UFiBIZ!VsW4GWTcnqABHAAqG7UayUxz_aVHYlwSbvmowQXD2so
Nice tutorial! Had some real problems getting it to work on a Huawei E220 modem, but after updating the modem firmware, no problems =) <br>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?
There is also a typo regarding the script <br>sudo tar -xzvf sakis36.tar.gz <br>instead of <br>sudo tar -xzvf sakis3g.tar.gz
Small typo the command for unzip of utms keeper should be sudo tar -xzvf umtskeeper.tar.gz

About This Instructable


102 favorites

More by BigCowPi: Raspbery Pi Wireless Auto-Sorting NAS/Media Server using MiniDLNA and Samba Raspberry Pi as a 3g (Huawei E303) wireless (Edimax EW-7811Un) router
Add instructable to: