Using Multi-Byte Character Sets in PHP (Unicode, UTF-8, etc)
Native Linux Space Warfare: Freespace 2
Symfony 2 Crash Course
Development Resource Project
Enforce Coding Standards with PHP_CodeSniffer and Eclipse IDE on Ubuntu Linux
Visualising Website Performance with Flame Graphs

GSM Internet on Raspberry Pi using Huawei E8372 WiFi Dongle

Wednesday, 25 December 19, 5:02 pm
The Huawei E8372 WiFi dongle (aka "wingle") is a pretty cool device. Like any WiFi dongle, it acts as a GSM modem allowing you to connect a PC to the internet wherever you have mobile signal. It supports 4G, allowing downloads at up to 5 Mb/sec. In addition to this however, the Huawei also contains a fairly well-specified WiFi router. This makes the dongle a standalone WiFi hotspot - you can plug the dongle into one PC which connects via USB, and then connect other devices (such as phones or other PCs) to the dongle over WiFi. The dongle also offers an API that you can use to not only programmatically control the router, but also to send and receive SMS messages.

All these features make it a pretty great choice for remote internet connectivity, especially at a location without hard-wired internet. My plan is to use this dongle to provide the network for an in-car system that will include security cameras, a dashcam and rearview camera, plus other sensors such as movement detectors, door switches and the like. The first thing I need to do is get the dongle running with my Raspberry Pi 3. I run Arch Linux ARM on my RPi, and I use netctl for networking.

Huawei E8372 netctl profile

The first thing to be aware of with the Huawei E8372 is that it has two basic modes of USB operation: firstly as a GSM modem, and secondly as a flash drive, thanks to its built-in microSD slot. When first plugged in, it will be in the flash drive mode, and when you list USB devices with lsusb, you'll see the words Mass Storage Device at the end of the Huawei's entry. We can use the usb_modeswitch tool to switch it into modem mode like so:
usb_modeswitch -v 12d1 -p 1f01 -M "55534243123456780000000000000a11062000000000000100000000000000"
Here, we specify the vendor ID with the -v switch, and the product ID with -p, which are shown in the output of lsusb. As far as I can tell, you only need to do this once, as the Huawei seems to remember the mode next time.

Now the dongle is in the right mode, we can create a netctl profile for it:
Description="Huawei USB static IP connection" Interface=eth1 Connection=ethernet IP=static Address=('192.168.8.50/24') Gateway='192.168.8.1' DNS=('192.168.8.1')
I'm setting my RPi up with a static IP because that way I know what IP it's on, which is mostly useful in order to connect to it over SSH. However the Huawei does also provide DHCP, and if you're not concerned about having a static IP, you can replace all the last four lines of this profile with IP=dhcp.

Auto-connecting to the Dongle on Plug-in/Power-up

For my application, it's important for the system to work unattended and/or without a directly connected monitor and keyboard. In other words, I want the RPi to connect to the Huawei as soon as it powers on. This can be accomplished using udev, the userspace device management system. We can create rules that are applied to specific devices when certain events occur: in our case, the event we're looking for is the Huawei being plugged in. A rule consists of one or more match keys and one or more assignment keys. The match keys identify the device that the rule applies to, and the assignment keys specify what should happen when all the match keys are satisfied.

Most commonly, the product and vendor IDs are used to identify a device, and that's what we will do here. Create a file at /etc/udev/rules.d/10-huawei.rules containing just the following line:
ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="14db", SUBSYSTEM=="net", ACTION=="add", RUN+="/root/dongle.sh"
Be sure to use a double-equals when writing your match keys, and to separate each key from the next with a comma. The vendor and product IDs match our device, as we can see from the output of lsusb. Notice that the Huawei has a different product ID (although the vendor ID is the same) when the dongle is in modem mode compared to when it's in mass storage mode. When plugged in, the Huawei actually registers multiple subsystem devices - the USB controller, the SD-card reader, and of course the network adapter. Each one of these fire off their own add action when the device is plugged in. The SUBSYSTEM=="net" match rule means our script will only be triggered once, when the network device is registered. Lastly, the ACTION=="add" means the rule will only match when our dongle is plugged in. When this happens, the rule will run the script called /root/dongle.sh. In this script, we can start our netctl profile and perform any other actions we may require.

Renaming the USB device

A common use of udev rules is to give a predictable name to a device, using a NAME assignment. This is less of an issue on the RPi as the Huawei dongle will always be named eth1. Anyway, add the following to your rules file if you want the dongle to always be loaded as /dev/huawei:
ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="14db", NAME="huawei"
Don't forget to use just a single equals sign in assignment rules! Once done, we need to update the netctl profile to use this name as the interface: Interface=huawei.

Side note: the vendor and product IDs of a device work fine in our case, but there are times when these are not available - most notably when the device has been unplugged. So if you want to create a udev rule to match when a device is unplugged (ACTION=="remove"), you will need to use something else to identify the device. Luckily various other details of the device are available as environment variables, which can be tested with the ENV{env_var} syntax.

Please enter your comment in the box below. Comments will be moderated before going live. Thanks for your feedback!

Cancel Post

/xkcd/ Kedging Cannon