Setting up your Raspberry Pi to work with a 3G dongle

Getting my Raspberry Pi to work with a 3G dongle was quite a mission.

I’m not a total newbie, but newbie enough to add the warning: Beware – you are entering crocodile infested waters. Don’t complain if the info below is wrong or gets you eaten 🙂

It appears as if there are quite a few myths floating around. Eliminating these myths and finding the real information made for quite a journey.

I have a need to connect my Raspberry Pi via 3G to the internet without any human intervention.

For this blog, I’ll be using a Raspberry Pi B Rev 2, running Raspian released 9 September 2014. I’ve connected a 4 Port USB hub to the Raspi, and in this is a keyboard, mouse and off course the 3G modem (Huawei E1820). This is a very old modem and I assumed that it would be supported.

This blog describes what was required to do after the initial configuration of the Raspberry Pi – all the raspi-config steps were completed as well as apt-get update/upgrade.

No additional packages were installed.

I configured the setup not to boot to X windows, as it adds lots of time on a reboot cycle during testing.   I used the text login screens available – switch between screens using alt-1 or alt-2 etc.

Ok, I did go into the GUI environment to capture the terminal screens 🙂

The 3G modem problem

3G modems are produced to be friendly to Microsoft Windows and to work without hassles in such an environment. To achieve this, such a modem has two modes. The first mode emulates a CD plugged into the USB port, and the second is the actual modem mode. When the modem is plugged into a Windows machine’s USB port, the CD mode is activated and if the drivers and supporting software is present on the computer, the mode is switched to function as a modem. If the drivers and software is not yet present, it is installed.

A very user friendly process – if one is using a Windows machine!

In the Linux environment this obviously won’t work. It is up to the master of the Raspberry Pi to do the configuration so all will work.

The solution

The core of the solution is to install a package usb-modeswitch.

More detail can be found at http://www.draisberghof.de/usb_modeswitch/

The key words from the documentation of usb-modeswitch is:

In most cases, you will be able to use your device without any interaction except plugging it in.

There is a lot of procedures where complicated configuration is required. From this single statement in the documentation, it is clear that this will seldom be required if a commonly available modem is used.

It seems as if the main problem is that the Raspberry Pi boots to slowly for the package to do its work. This could be solved quite easily by forcing the modem configuration step at the end of the boot sequence.

The process I followed involved a lot of Googling 🙂 And even more testing.

The amount of info below seems daunting – but I is not. Stripped to the bare bones, it is less than 10 minutes work to do the configuration. The only reason it looks so daunting, is because I added a lot of the testing steps I took to gain understanding of the process.

I learnt a lot during the process, and the testing approach taken below aims to transfer the same knowledge to you.

During the testing process I observed the following:

  • When booting from zero power, with the modem plugged into the USB-hub, it would not work.
  • Unplugging the modem, and plugging it again into the USB-hub caused the mode switch to happen (“Hot Plugging”)
  • Rebooting without getting the modem to switch resulted in the modem not to switch
  • Rebooting after the modem switching has happened, resulted in the modem being switched to the correct mode again after the reboot completed.

It is important to note that “Hot Plugging” works!

All of this are indicative that the usb-modeswitch package wants to do its work, but can’t at the moment it needs to because the boot process was not advanced sufficiently yet.

The process

Install the required packages

  • sudo apt-get install wvdial usb-modeswitch

(this requires an internet connection)

Now shut the Pi down with sudo shutdown -h now and when it is down, remove the power. It needs to be “cold”.

Boot you Pi with the modem plugged in

Login as user pi

Testing & setting the modem to modem mode

Now a few tests to establish the baseline:

Run the command lsusb – this will list the USB devices currently recognised

2014-11-11-105016_657x221_scrot

The line showing “Huawei” shows the modem is present – but in CD mode

Run the command ifconfig -a – this will show all the networking devices available

2014-11-11-105357_657x281_scrot

Note how few network interfaces are present!

For the 3G dial-up to work, a device link file /dev/gsmmodem is required. Check with ls -al /dev/gsm* if the file is present – it most probably is not!

2014-11-11-105622_657x86_scrot

Now unplug the modem from the USB-hub.

Do the tests again and note the differences against the above tests.

The key observation should be that the lsusb command does not list the modem in any way.

Plug the modem into the hub. Allow a few seconds for the modem to be recognised. Inserting a USB thingie while the computer is running is called Hot Plugging. When the system senses a new USB unit was plugged in, processes start to activate the unit and make it available.

Run the command lsusb – note that the entry for the modem is different.

2014-11-11-111617_657x296_scrot

The line showing “Huawei” shows the modem is present – but it should now be in modem mode.

Run the command ifconfig -a – this will show all the networking devices available

2014-11-11-111735_657x386_scrot

Note that a new network is available: wwan0

Check ls -al /dev/gsm* if the file is present – it should be there.

2014-11-11-111906_657x71_scrot

This proves that the modem can be activated with hot plugging.

Please play around with rebooting before and after hot plugging to see the effect – this is the way I realised how to solve the problem.

Dialing up

The dialup process is managed with wvdial

Edit /etc/wvdial.conf with sudo nano /etc/wvdial.conf

Mine looks like this:

[Dialer Defaults]

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

You many need to tweak this according to the settings of your service provider.

To test the dialup execute the command sudo wvdial

Quite a number of lines of informative text will scroll.

Once the command has completed (can take a few minutes), run ifconfig again.

Alternatively: Open another screen and run the ifconfig command while waiting for the above process to complete.

2014-11-11-115314_657x521_scrot

There should now be an entry for ppp0

In the ppp0 section, look for the inet entry – this is the IP number assigned by your service provider to your modem. For the fun, you should be able to log in with SSH from the outside to this IP number. WARNING: Your Raspberry Pi is now connected to the internet and a potential target from all the baddies outside! Make sure your login password is secure and strong!

In real life, one can’t hot plug a modem every time when required. Many applications require that a Raspberry Pi setup must be able to run unattended.

How does one trigger a hot plug event with software?

The answer is simple: The command sudo udevadm trigger

Read more about udev – the Linux Kernel device manager – at http://en.wikipedia.org/wiki/Udev

Using the udevadm trigger command as it is, triggers every device – a bit of a sledgehammer approach to target a single device. It can be refined to sudo udevadm trigger –subsystem-match=usb

Try sudo udevadm trigger –verbose –dry-run and sudo udevadm trigger –subsystem-match=usb –verbose –dry-run in the command line. The additional options are self explanatory.

Now that all is working, it needs to be configured to work at start-up.

For this, a small script file is required.

Do the following commands

sudo -i

cd /root

nano 3GModem.sh

(sudo -i makes you root! BEWARE -YOU HAVE SUPER POWERS!)

My script file contains the following:

#!/bin/bash

sleep 1m

udevadm trigger

sleep 1m

Wvdial

Save the file. Make it executable with chmod 755 3GModem.sh

A few notes on the script:

The first sleep is to ensure that the boot has completed. The second sleep is to allow time for the modem initiation processes to complete. Feel free to experiment and optimise!

Now edit the rc.local file:

nano /etc/rc.local

Add the following line before the exit 0 line:

/root/3GModem.sh &

And save.

The & at the end of the line is essential to allow the boot process to complete.

More info on the rc.local file is available at http://www.raspberrypi.org/documentation/linux/usage/rc-local.md

Now reboot, and you should have internet access via 3G after a few minutes.

The command ifconfig will confirm if the connection was successful or not.

Read more:

A word of thanks to those who assisted in this forum post:
Raspberry Pi Forum: Problems with usb_modeswitch http://www.raspberrypi.org/forums/viewtopic.php?f=29&t=91283

How to setup a USB 3G Modem on Raspberry PI using usb_modeswitch and wvdial https://www.thefanclub.co.za/how-to/how-setup-usb-3g-modem-raspberry-pi-using-usbmodeswitch-and-wvdial

Raspberry Pi’s documentation on RC.LOCAL

http://www.raspberrypi.org/documentation/linux/usage/rc-local.md

Raspberry Pi Forum: GPRS usb modem, Huawei E171 http://www.raspberrypi.org/forums/viewtopic.php?f=29&t=32307&p=624908&hilit=usb_modeswitch#p624908

This entry was posted in Computing and tagged , , . Bookmark the permalink.

5 Responses to Setting up your Raspberry Pi to work with a 3G dongle

  1. Ali says:

    Nice write up Nico. I’ve been trying numerous methods to get a pi to use a Huawei E3276s working correctly – finally followed this and it works fine. No more hair pulling, thanks!

  2. Nano says:

    Very good job Nico. And many thanks for that. It really helped me to make my Huawei E1752 work !

  3. Valentine says:

    Modeswitch is not working for me. Any idea why?

  4. Sanjay says:

    Hi , Nico
    Nicely presented .Thankx

  5. Pingback: Internet on the Raspberry Pi 2 with a 3G Dongle | Pradeep CE

Leave a comment