3G USB Modem

Linux is a fun, frustrating, exciting, head scratching operating system for computers. Unfortunately its not well supported by commercial entities. Most equipment suppliers will provide help or even software drivers for Windows or Apple computers but very little for the Linux user. So we have to search for answers that other Linux users have put on web forums or personal blogs like this one.

When I have a task to accomplish with Linux I can usually find some way to do it and in the process my skill level improves. So lately I've been working on using the ubiquitous little Raspberry Pi for various tasks. Using a version of the Debian Linux system called 'Raspbian' it has some amazing features.

One thing I hadn't tackled with the Raspberry Pi was using a 3G USB modem on it. I've used telephone dial-up modem software on Linux years ago but not a 3G modem. Needing an Internet connection on a remote Science Station it seemed like a natural way to go.

I started with a USB modem from Optus, the Huawei E3131 and looked for articles on how to get it running. When the modem is first plugged in, the USB subsystem in Linux talks to it to find out what device it is. The modem responds with a manufacturer code and hardware code, in this case 12d1:14fe and the Raspberry Pi assigns it as an Optical Disk or CD Drive (run 'dmesg' and you should see a scsi device 'sr0'). Now of course these devices are configured to connect to a Windows PC mostly. So when plugged into a Windows computer it mounts as a CD which contains a startup program which installs the Dial-up program for Windows. Not much good to us.

I found an article on the Raspberry Pi Forum that gave me a lot of answers for this device. Huawei E3131 on Wheezy. So I needed to tell the modem to stop being a CD drive and change to being a modem. By running a line of code I could do this, according to the article. I needed to install a program called sg3-utils so I could talk directly to the modem.

On the command line i ran 'sudo apt-get install sg3-utils'

I could now send the modem a command to change mode. So on the command line again I ran
'sudo /usr/bin/sg_raw /dev/sr0 11 06 20 00 00 00 00 00 01 00'
and a few seconds later the modem responded with its buffer ready. Awesome. Thats step one. So now how do I get it to connect to the Internet?

Once the device had changed mode to a modem, Linux realized it was a serial device and assigned it ttyUSB0, ttyUSB1 and ttyUSB2. I could now talk to it using modem 'AT' commands. The AT command set is an old message and control format used with RS232 serial ports to talk to dial-up modems and was just what I needed.

A Linux program I'd used years ago to talk to modems, WVDial, seemed perfect for the job so I installed it on the Raspberry Pi using
'sudo apt-get install wvdial'
and configured the dialler to talk to the modem. When installed, WVDial has a configuration file that needs to be edited to set up the dialling parameters. The 'pre-paid' settings for Optus Australia turned out to be 'preconnect' for the APN (access point name) and the dialled number is *99#. After setting up the wvdial.conf file I added a shell script to the startup rc.d boot list so the script would execute after everything else during bootup. The script needed to look for a device called 'sr0' since this is the CD Drive the modem first came up as.

So, now I'm at the point where on boot-up, if the USB modem is plugged in it gets mode changed by the script to be a serial device. Sure enough - I had three tty devices. To run the modem dialler I had to type in 'sudo wvdial optus &' (the '&' at the end tells Linux to drop out of the shell script and return to command line). Up came the modem response on the screen as it talked to Optus. The Raspberry Pi was assigned an IP address and also the addresses of two DNS Servers. Great! I was online.

I tried a 'ping' to the outside world. It was working. Now at this point I was still in command line mode. I was itching to try a web browser so I started XWindows on the Raspberry Pi. So I just typed 'startx' and up came the very fetching Pi Desktop. I opened the web browser 'Midori' and of course opened this site first!

The response was quick, only a few seconds and there was my website. All I needed was to run 'sudo wvdial optus &' to connect to the Internet and to run 'sudo pkill -f wvdial' to stop wvdial and hang up. For my task that I needed an Internet connection for I just needed a script to call when I needed the Internet, then hang up afterwards. In this case, a file is getting uploaded using the Ethernet port from elsewhere to the FTP server running on the Raspberry Pi. The script gets called every minute using Cron and looks for the file to upload. If the file exists then wvdial is called, and a few lines in the script starts the Linux FTP client. The file get sent and the modem is then disconnected ready for next time.

Well thats it. There should be enough information for the next person looking for a way to connect their Huawei E3131 to a Raspberry Pi and get on the Internet. Thats what we do. We make something and add to it and write about it for the next person.

I love Linux.

06.07.2014. 01:02