How to setup a USB 3G Modem on Raspberry PI using usb_modeswitch and wvdial

Setting up a USB 3G/4G modem on a Raspberry Pi is not a simple task. There are many guides available using the now defunct sakis3g script, but getting my modem to work required a bit more understanding. 

The main problem is that most USB modems act as two devices - a USB storage device and a USB modem. When plugged in to the Raspberry PI the device is usually in USB storage mode. There is a program called usb_modeswitch that we can use to do the switching 

The other missing part is a way to connect to the cellular network with the USB modem from the Raspberry. For this we will use the classic ppp and wvdial programs.

Requirements

  • Raspberry PI computer
  • Raspbian OS installed
  • USB 3G Modem
  • 5V 1A power supply for the Raspberry PI 

1. Install required software

  • Connect your Raspberry PI to the internet via LAN or Wifi
  • Open a terminal window and enter:
sudo apt-get update
sudo apt-get install ppp usb-modeswitch wvdial

2. Get the USB switching codes

  • We need to get the codes of the USB device in USB storage mode and USB modem mode. 
  • Connect your USB modem and reboot the Raspberry PI without LAN or Wifi connection
  • Once rebooted open a terminal window and enter:
lsusb
  • The output will look similar to the screenshot below. Your modem should be listed as one of the USB devices. Note the numbers underlined. These are the device vendor and product codes.

  • Make a note of these numbers. In this case it is 19d2:2000
  • This value will later be used in the DefaultProduct value 
  • Then we need to soft reboot the Rapberry PI with the modem connected.
  • Open a terminal window and enter:
sudo reboot
  • Once rebooted open a terminal window and enter:
lsusb
  • The output will look similar to the screenshot below. Your modem should be listed and if you are lucky the second set of numbers would have changed. This is usb_modeswitch doing it's job and switching the device to USB modem mode.

  • Make a note of the new numbers. In this case it is 19d2:2002
  • This value will be used later in the TargetProduct value 

3. Create custom usb_modeswitch config file

  • We need to create a custom config file for usb_modeswitch on the Raspberry PI because on a cold boot the device is not always active yet when usb_modeswitch runs on startup and the device is left in USB storage mode.
  • We will extract some more information for our usb_modeswitch config file so that we can do the switching manually.
  • Open a terminal window and enter the following replacing the codes 19d2 and 2000 with the codes you noted in step 2. Also note the added backslash.
cd /tmp
tar -xzvf /usr/share/usb_modeswitch/configPack.tar.gz 19d2\:2000
  • Now open the extracted file with a text editor like leafpad replacing the codes noted in step 2.
leafpad 19d2:2000
  • The content of the file should look something similar to the screenshot below.
  • The parts we are interested in are shown in blue.

  • Now open the /etc/usb_modeswitch.conf file and add the information obtained above to the file.
  • Open a terminal window and enter:
sudo leafpad /etc/usb_modeswitch.conf
  • Then add the following replacing the codes and MessageContent values with those of your device and save.
DefaultVendor=0x19d2
DefaultProduct=0x2000

TargetVendor=0x19d2
TargetProduct=0x2002

MessageContent="5553424312345678000000000000061e000000000000000000000000000000"
MessageContent2="5553424312345679000000000000061b000000020000000000000000000000"
MessageContent3="55534243123456702000000080000c85010101180101010101000000000000"

4. Create the wvdial config file

  • The next step is to create a config file for wvdial so you can connect to your service provider.
  • Open a terminal window and enter:
sudo leafpad /etc/wvdial.conf
  • Replace the content of the file with the following. 
[Dialer 3gconnect]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=1,"IP","internet"
Stupid Mode = 1
Modem Type = Analog Modem
ISDN = 0
Phone = *99#
Modem = /dev/gsmmodem
Username = { }
Password = { }
Baud = 460800
  • Replace internet with your service provider's APN
  • Replace the Phone number if you need to dial a different code to connect.
  • Replace Username and Password if needed. To leave the username and password as blank use { }

5. Connect to the internet

  • To connect we need to make sure the device is in modem mode. 
  • Open a terminal and enter:
sudo usb_modeswitch -c /etc/usb_modeswitch.conf
  • Then connect to the internet with:
wvdial 3gconnect

 

Tags: 

Comments

Hi. Is there any way to

Hi. Is there any way to forward ports (22 in my case) on a 3g usb dongle (Huawei e3131) connected to the USB of an RaspberyPi. I want remote access to the shell of the Raspbery.

If your service provider

If your service provider allows your 3G USB modem a public IP you can access ssh directly with the IP. You will need a DDNS service like Duck DNS to point a domain name to the IP of the 3G modem as the public IP of the modem will change.

Hi, can this method be

Hi, can this method be applied for EVDO USB modem? Thank you.

EVDO is just another 3G

EVDO is just another 3G standard like HSPA - depending on the modem it should work. Let us know.

I'm planning to use Wifi or

I'm planning to use Wifi or LAN as main internet connection and USB 3G modem for sending SMS using gammu, but also able to switch to USB modem for internet connection if wifi or LAN failed. Do you think it's possible?

Yes it is. Almost anything is

Yes it is. Almost anything is possible with linux

E: Unable to locate package

E: Unable to locate package usb_modeswitch Correct : sudo apt-get install ppp usb-modeswitch wvdial

Corrected the instructions -

Corrected the instructions - thank you

Thanks for the tutorial - it

Thanks for the tutorial - it's certainly the best information source out there! I managed to get most of the way there, but now have the following error: ... CONNECT --> Carrier detected. Starting PPP immediately. --> Unable to run /usr/sbin/pppd. --> Check permissions, or specify a "PPPD Path" option in wvdial.conf. I've tried to chown, and chmod the appropriate files, but doesn't seem to work. Thanks!

The permissions on /usr/sbin

The permissions on /usr/sbin/pppd should not be changed. You can re-install ppp and try again, or use sudo wvdial 3gconnect .

unable to locate wvidal

What do I do if im unable to locate package wvidal?

unable to locate wvidal

Hi Kyle - check you spelling - it is wvdial NOT wvidal

Use to send sms instead?

Hi! I'm trying to send an SMS from my pi using a huawei e3131 dongle and I was wondering what changes I would need to make to the wvdial.conf file so that when I execute it, instead of connecting the dongle to 3G it just connects so that I can send an sms instead? Thank you!

SMS

Hi Andrew. Look at using Gammu for sending sms's, you do not need to connect to the 3G network with wvdial, just make sure the modem is in 3G mode.

Re SMS

Hi, thanks for replying! By putting the dongle in 3G mode I assume. Because I can get it into modem mode using usb modeswitch, like you have written, but then gammu will not work because there is nothing in the ttyUSB ports? Thanks again.

SMS

Hi Andrew, this article is not about SMS. Try and replace /dev/ttyUSB device with /dev/gsmmodem.

Step #2

Can you please clarify what you do in step 2. lsusb gives you two different numbers. For me I get the same numbers. What is the difference? All you do is a cold reboot vs a warm reboot?

Step #2

Hi Ocopus, it is confusing. Most modems have two modes, the USB mode and a modem mode. The modem is detected differently if plugged in on a soft reboot vs hard reboot. Try booting the Pi without the modem plugged in. Then plug it in, wait a 10 seconds and the do the lsusb. Usually this is the usb mode. Then with reboot with sudo reboot, this will make sure the modem is powered on the reboot and usb modeswitch should do the switching to modem mode. Check the numbers again now with lsusb.

Huawei E303c supported on Raspberrry Pi?

Can I setup the Huawei E303c as a 3G usb modem on Raspberry pi using usb_modeswitch and wvdial??

Sierra Wireless Modem

I am not able to find the codes from your file for 1199:68a3 Will something else work? or do I have to find the codes from the Sierra driver pack?

Thank

Work perfect!!!