technology, amateur radio, scouting and me

How To connect a Raspberry Pi to a 3G network

Connect a Raspberry Pi to a Huawei 3G Modem to allow Internet access

Motivation

As part of the project to create a stand-alone Twitter Appliance, I needed to connect a Raspberry Pi to a 3G network.

System Design

The design is simple: An existing 3G modem is connected to the Raspberry via a USB connection. I already own a Huawei E5756s mobile broadband modem connected to Three UK, and as this also has a USB connection, it was the obvious one to use for this.

Installing the modem

Installing the modem required that the Raspberry Pi recognise the modem as such. Unfortunately, out of the box, the E5756 is detected as a hard disk by Raspbian. This is because the modem has a hidden disk partition that contains the Windows driver.

  1. Plug the modem into one of the USB ports.
  2. In a terminal session, run the command

    # lsusb

With a bit of luck, you’ll see something like:

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 005: ID 12d1:1506 Huawei Technologies Co., Ltd. E398 LTE/UMTS/GSM Modem/Networkcard

The key line is the last, showing that Raspbian has recognised the modem. Unfortunately, you may well see:

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 004: ID 12d1:14fe Huawei Technologies Co., Ltd.  

This indicates that Raspbian sees the Huawei device as a disk drive. To get around this, we need to installthe usb-modeswitch package, but we need to do things in a specific order for this to work (or at least I did anyway).

Open a terminal prompt and, with the modem unplugged:

  1. install the usb-modeswitch package

    # sudo apt-get install usb-modeswitch

  2. After this completes, reboot the Pi and then log in again and open a terminal session.

  3. Plug the modem in, leave it for a second or two for it to be recognised and then do

    # lsusb

  4. Hopefully, the output will now indicate the the modem has been recognised. Display the network interface list:

    # ifconfig -a

Hopefully, you will see:

eth0      Link encap:Ethernet  HWaddr b8:27:eb:04:64:df 
          inet addr:172.29.12.204  Bcast:172.29.12.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1191 errors:0 dropped:0 overruns:0 frame:0
          TX packets:633 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:383489 (374.5 KiB)  TX bytes:100065 (97.7 KiB)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wwan0     Link encap:Ethernet  HWaddr 0c:5b:8f:27:9a:64 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:5237 (5.1 KiB)  TX bytes:0 (0.0 B)

Good news, we have a new interface. To get this to acquire an IP address, we need to edit the file /etc/network/interfaces and add the lines:

allow-hotplug wwan0  
iface wwan0 inet dhcp

If you now reboot, you should see that the new interface has been initialised and configured:

eth0      Link encap:Ethernet  HWaddr b8:27:eb:04:64:df 
          inet addr:172.29.12.204  Bcast:172.29.12.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1191 errors:0 dropped:0 overruns:0 frame:0
          TX packets:633 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:383489 (374.5 KiB)  TX bytes:100065 (97.7 KiB)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wwan0     Link encap:Ethernet  HWaddr 0c:5b:8f:27:9a:64 
              inet addr:192.168.1.101  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:5237 (5.1 KiB)  TX bytes:0 (0.0 B)

Hoorah, the modem is connected and working.

After this bit of jiggery-pockery, the modem should continue to be recognised if it is connected at boot time.

3 Responses to How To connect a Raspberry Pi to a 3G network

  1. Thanks Gareth. Nice and simple tutorial.

    I’m able to see the dongle in network mode using “lsusb”, and the “wwan0” network interface using “ifconfig -a”. After following the steps, I can’t see any IP address assigned after reboot. I’ve tried using ifdown/ifup but it seems to never get an IP:

    pi@raspberrypi ~ $ sudo ifup wwan0
    Internet Systems Consortium DHCP Client 4.2.2
    Copyright 2004-2011 Internet Systems Consortium.
    All rights reserved.
    For info, please visit https://www.isc.org/software/dhcp/

    Listening on LPF/wwan0/02:50:f3:00:00:00
    Sending on LPF/wwan0/02:50:f3:00:00:00
    Sending on Socket/fallback
    DHCPDISCOVER on wwan0 to 255.255.255.255 port 67 interval 7
    DHCPDISCOVER on wwan0 to 255.255.255.255 port 67 interval 19
    DHCPDISCOVER on wwan0 to 255.255.255.255 port 67 interval 13
    DHCPDISCOVER on wwan0 to 255.255.255.255 port 67 interval 19

    Any help appreciated, thanks!

  2. I have the same problem.. the usb switch seem to work as the second parameter on lsusb changes and I can see /dev/ttyUSB0 to 2, and the wwan0 also appear in ifconfig -a, but no IP address. I tried to force the wwan0 to say 192.168.0.1, and was able to ping it.

    I found this and apply it to get the 3G ‘working’

    E – Install a 3G dongle
    This stage is only needed if the system is to be operated away from an existing available network.

    Install PPP to allow a data connection to be made:
    sudo apt-get install ppp

    Install sakis3g, which controls the 3G modem:
    sudo wget “http://www.sakis3g.org/versions/latest/armv4t/sakis3g.gz”
    sudo gunzip sakis3g.gz
    sudo chmod +x sakis3g
    If this fails because the sakis3g website is unavailable, try this alternative:
    sudo wget “http://sourceforge.net/projects/vim-n4n0/files/sakis3g.tar.gz”
    sudo gunzip sakis3g.tar.gz
    sudo tar -xvf sakis3g.tar
    sudo chmod +x sakis3g
    sudo apt-get install libusb-dev
    sudo ./sakis3g recompile
    The last step may take some time with nothing changing on the screen.

    Edit/create the sakis3g configuration file:
    sudo nano /etc/sakis3g.conf
    OTHER=”USBMODEM”
    USBMODEM=”12d1:1506″
    USBINTERFACE=”0″
    APN=”3internet”
    These settings are for a Huawei E353 dongle on Three. In the USBMODEM=”12d1:1506″ line 12d1:1506 is the dongle`s USB ID which can be found by listing all the currently connected USB devices using:
    lsusb
    Every device has a different code – the first part represents the manufacturer and the second the product line. Most 3G dongles have two USB IDs – one for the 3G modem and one for the memory card socket. The sakis3g package should ensure that the correct one is selected, but the USBINTERFACE line may help.

    Test the configuration by using:
    sudo ./sakis3g connect
    The 3G dongle light will stop flashing and remain lit once the connection is successfully made, which can take a little while.

    The only reliable way of automatically establishing the 3G connection has proved to be to add a line to the Cron table to activate sakis3g every couple of minutes. This also ensures that the connection is automatically remade if the dongle is unplugged/plugged or new credit is added to an expired provider account:
    sudo crontab -e
    */2 * * * * sudo /home/pi/sakis3g connect
    This line can be left in place – if the 3G dongle isn`t plugged in then it`s ignored.

    Once connected, the RPi will be issued with an IP address by the mobile broadband service. This can be found by using this command:
    ifconfig
    Look for the inet addr entry in the ppp0 section

    I used this following command to connect to MTN in South Africa
    ./sakis3g OTHER=”USBMODEM” USBMODEM=”12d1:1506″ USBINTERFACE=”0″ APN=”internet” as I could not get sakis3g to read the config file.
    This connects but was slow..

  3. Apologies for the slow response to this, Agustin. Thanks for the input, Gerrie.

    I’m going to have to explore this further as this is now a complete project and the Pi has been redeployed for another job.

    I saw the method Gerrie advocates, using sakis3g, but found that I didn’t need to follow it.

    With my Huawei 3G modem, the modem actually operates as a router/firewall and it is this that supplies the IP address rather than the network provider, as happens with the Vodafone dongle I use with my Mac. Perhaps your dongle is expecting to get an IP address from the network and your signal is not good enough. Are you getting a strong 3G signal?