Here's how to connect your Raspberry Pi to the internet using a 3G USB modem.
First of all you'll need a compatible 3G USB modem and a SIM card with an appropriate data plan to start with.
Apart from that you'll need to make sure that your RPi's power supply has enough Amps to spare to power the 3G USB modem too.
A standard 700mA power supply, which is just enough for the RPi itself, is definitely not enough to supply the extra power for the 3G USB modem.
If you have one of the first Raspberry Pi's, the ones with only 256 MB of memory, you may have to use a powered USB hub to connect your 3G USB modem.
On these models the power output to each USB port is fused by a 140 mA poly fuse, which may not be enough for your modem.
The automatic dialer program we're going to use can get quite confused by SIM cards requiring PIN codes. In fact it can handle PIN codes if you run the dialer for the first time. However when you start the dialer again on a modem which is already activated by a PIN, it will loop to enter the PIN and will fail to complete the dial process.
The easiest solution to this is to disable the PIN request on the SIM altogether. Put the SIM in an old mobile phone and use the menu of the phone to disable the PIN request. If you don't have a phone in which your SIM will fit you can always follow this link, where you can find an explanation on how to disable the PIN using a serial terminal client to the 3G USB modem.
Installing the dialer is only a matter of entering the next command on the command shell:
sudo apt-get install wvdial
You'll need to have an internet connection in order to be able to install the dialer program of course. After installing the dialer you may wish to disconnect from your regular internet in order to be able to test your mobile connection.
Now it's time to connect your 3G USB modem to your RPi. You haven't forgotten to put your SIM into it, have you? Wait a few seconds after connecting your 3G USB modem and then type the command lsusb . This will produce a list of all the connected USB devices to your RPi. Among them should be a line which looks something like the one shown below. Needless to say that the exact content of this line depends on your specific 3G USB modem. (I have cut the line in two).
Bus 001 Device 006: ID 12d1:1003 Huawei Technologies Co., Ltd. E220 HSDPA Modem / E230/E270/E870 HSDPA/HSUPA Modem
One nice thing about the dialer we're going to use is that it can create a basic configuration file which is tailored to your specific 3G USB modem. Simply type the next command in the terminal:
sudo wvdialconf
This will create a file called /etc/wvdial.conf which will contain something similar to:
[Dialer Defaults] Init1 = ATZ Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0 Modem Type = Analog Modem Baud = 9600 New PPPD = yes Modem = /dev/ttyUSB0 ISDN = 0 ; Phone = <Target Phone Number> ; Password = <Your Password> ; Username = <Your Login Name>
This file only needs some minor changes to make it complete. Below you'll see my config file. The changes can be found from the line containing the word Phone onward.
[Dialer Defaults] Init1 = ATZ Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0 Modem Type = Analog Modem Baud = 9600 New PPPD = yes Modem = /dev/ttyUSB0 ISDN = 0 Phone = *99# Password = xs4all Username = xs4all Stupid Mode = 1 Init3 = AT+CGDCONT=1,"IP","umts.xs4all.nl"
Don't be fooled by the low baud rate.
Even with this setting I could download at full speed, which was only limited by the maximum speed of my data plan.
Most mobile operators don't really care what Password and Username you use.
Mine recommends to put in xs4all for both.
Finally the line starting with Init3 sets the Access Point name (or APN for short) to umts.xs4all.nl.
You'll have to find out what the APN for your mobile operator is.
Warning: Setting a wrong APN might still work.
However you may be presented with a very large bill at the end of the month if you don't use the correct one.
Therefore it is highly recommended to double check if you've got it right.
Now it's time to dial in. Type the next command:
sudo wvdial &
You'll see some messages fly over your screen. And if everything is all right it will connect you to the internet.
The dialer program will continue running in the background (because of the trailing &). You can stop the dialer by typing the command fg , which brings the background process to the foreground again. Then you can press Ctrl-C to quit the program.
Finally let's automate the connection process. Add the next few lines to the end of the file /etc/network/interface .
auto ppp0 iface ppp0 inet wvdial
The next time you boot your RPi it will automatically connect to the internet when your 3G USB modem is connected to it. Disconnecting from the internet is done by typing sudo ifdown ppp0 . Reconnecting, for instance when you connect your 3G USB dongle to an already running RPi, can by done by typing the command sudo ifup ppp0 .
Many mobile operators directly connect you to the internet.
Your RPi will probably get a public IP address, with no router/firewall to protect it from the evil internet.
This is not desirable, especially when you intend to leave it connected for extended periods of time.
Therefore you may want to configure a firewall to restrict access from the outside.
Setting up a firewall is a whole different story though.