Send SMS using a USB Modem and Ubuntu 12.04

I developed a Java ERP system for a customer and he asked me if the system could send SMS messages. My first thought was to pay a SMS Gateway, the internet has thousands of them, easy to use, relatively cheap and fast to integrate.

But, there is always a but, I discovered that my customer has several Huawei E173 GSM USB modems with a SMS plan! They have the hardware, they already pay for SMS, so why not use these modems?!.

So here it is, a simple way to send SMS using a Huawei E173 GSM USB modem in a Ubuntu Server 12.04.

The E173 modem that I’m using, when plugged in, will emulate a CD-ROM driver containing the software needed by this modem. The modem device will not be available unless we “switch mode” the USB device, this is called a “flip flop” USB device. To handle that we need to install the usb_modeswitch(usb_modeswitch) if it isn’t already.

This command will install the usb-modeswitch and the gsm-utils that will do the magic:

apt-get install usb-modeswitch gsm-utils

Before connecting you modem to the USB port, run this command:

tail -f /var/log/syslog

Go ahead, connect the modem, wait some seconds(+5) and you will see something like this:

Apr 12 11:26:00 server kernel: [ 1862.731001] usb 1-1: new high-speed USB device number 3 using ehci_hcd
Apr 12 11:26:01 server kernel: [ 1863.381742] option 1-1:1.0: GSM modem (1-port) converter detected
Apr 12 11:26:01 server kernel: [ 1863.382231] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB0
Apr 12 11:26:01 server kernel: [ 1863.382279] option 1-1:1.1: GSM modem (1-port) converter detected
Apr 12 11:26:01 server kernel: [ 1863.382321] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB1
Apr 12 11:26:01 server kernel: [ 1863.385510] scsi4 : usb-storage 1-1:1.2
Apr 12 11:26:02 server kernel: [ 1864.386423] scsi 4:0:0:0: CD-ROM            HUAWEI   Mass Storage     2.31 PQ: 0 ANSI: 2
Apr 12 11:26:02 server kernel: [ 1864.398748] sr1: scsi-1 drive
Apr 12 11:26:02 server kernel: [ 1864.399381] sr 4:0:0:0: Attached scsi CD-ROM sr1
Apr 12 11:26:02 server kernel: [ 1864.400494] sr 4:0:0:0: Attached scsi generic sg2 type 5

Press Ctrl+C to exit tail.

If you see a line like this "GSM modem (1-port) converter now attached to ttyUSBX" means that your modem is ready to use.

And finally, how to send a SMS message(In my case my modem is in /dev/ttyUSB0 and the baudrate is 19200, change according to your setup):

echo "SMS Test Message!" | gsmsendsms -d /dev/ttyUSB0 -b 19200 PHONE_NUMBER

There should be no output. The exit code is 0 if everything is OK, greater than 0 if fail.

Great, I already can send SMS using the console, but I need to integrate into my application. The fastest and easiest way is to call the same command I ran in the console.

	public static boolean sendSmsSynchronous(final String phone, final String message)
		final ProcessBuilder processBuilder = new ProcessBuilder().command("/usr/bin/gsmsendsms", "-d", "/dev/ttyUSB0", "-b", "19200", phone, message);
		Process gsmsendsmsProcess;
			gsmsendsmsProcess = processBuilder.start();
		catch (final Exception e)
			return false;

		return gsmsendsmsProcess.exitValue() == 0;

That’s it, have fun!

Ps.: All the commands above must be run by root, if you aren’t root, just add sudo before.