Sending SMS notifications with Gnokii on CentOS 6

I had a couple of Huawei USB UMTS/HSPA sticks gathering dust, so I decided to use them for SMS notifications. Below is a quick set of notes I took during the experiment.

Configuration

My setup:

  • CentOS 6.3 (64-bit)
  • GNOKII 0.6.30 (available from EPEL)
  • Huawei E160G and Huawei E176
  • Valid SIM card, PIN entry disabled

Plug in the USB stick, watch /var/log/messages. You should see something like this:

Jan 19 23:23:47 hal kernel: usb 2-2: new high speed USB device number 13 using ehci_hcd
Jan 19 23:23:48 hal kernel: usb 2-2: New USB device found, idVendor=12d1, idProduct=1003
Jan 19 23:23:48 hal kernel: usb 2-2: New USB device strings: Mfr=2, Product=1, SerialNumber=0
Jan 19 23:23:48 hal kernel: usb 2-2: Product: HUAWEI Mobile
Jan 19 23:23:48 hal kernel: usb 2-2: Manufacturer: HUAWEI Technology
Jan 19 23:23:48 hal kernel: usb 2-2: configuration #1 chosen from 1 choice
Jan 19 23:23:48 hal kernel: scsi36 : SCSI emulation for USB Mass Storage devices
Jan 19 23:23:48 hal kernel: usb 2-2: USB disconnect, device number 13
Jan 19 23:23:54 hal kernel: usb 2-2: new high speed USB device number 14 using ehci_hcd
Jan 19 23:23:54 hal kernel: usb 2-2: New USB device found, idVendor=12d1, idProduct=1003
Jan 19 23:23:54 hal kernel: usb 2-2: New USB device strings: Mfr=2, Product=1, SerialNumber=0
Jan 19 23:23:54 hal kernel: usb 2-2: Product: HUAWEI Mobile
Jan 19 23:23:54 hal kernel: usb 2-2: Manufacturer: HUAWEI Technology
Jan 19 23:23:54 hal kernel: usb 2-2: configuration #1 chosen from 1 choice
Jan 19 23:23:54 hal kernel: option 2-2:1.0: GSM modem (1-port) converter detected
Jan 19 23:23:54 hal kernel: usb 2-2: GSM modem (1-port) converter now attached to ttyUSB0
Jan 19 23:23:54 hal kernel: option 2-2:1.1: GSM modem (1-port) converter detected
Jan 19 23:23:54 hal kernel: usb 2-2: GSM modem (1-port) converter now attached to ttyUSB1
Jan 19 23:23:54 hal kernel: scsi39 : SCSI emulation for USB Mass Storage devices
Jan 19 23:23:54 hal kernel: scsi40 : SCSI emulation for USB Mass Storage devices
Jan 19 23:23:55 hal kernel: scsi 39:0:0:0: CD-ROM            HUAWEI   Mass Storage     2.31 PQ: 0 ANSI: 2
Jan 19 23:23:55 hal kernel: scsi 40:0:0:0: Direct-Access     HUAWEI   MMC Storage      2.31 PQ: 0 ANSI: 2
Jan 19 23:23:55 hal kernel: sr1: scsi-1 drive
Jan 19 23:23:55 hal kernel: sr 39:0:0:0: Attached scsi generic sg3 type 5
Jan 19 23:23:55 hal kernel: sd 40:0:0:0: Attached scsi generic sg4 type 0
Jan 19 23:23:55 hal kernel: sd 40:0:0:0: [sdc] Attached SCSI removable disk

Each Huawei sticks presents itself as 2 separate USB modems: /dev/ttyUSB0 and /dev/ttyUSB1. I will use /dev/ttyUSB1 since SMS notifications are apparently only sent to the second port. The Micro-SD slot is reported as a SCSI device – not used here.

Now it’s time to configure Gnokii. I’ll send SMS as root, so I created directories under /root:

$ mkdir -p /root/.config/gnokii
$ mkdir -p /root/.cache/gnokii

Copy the default configuration file from /etc/gnokiirc to /root/.config/gnokii/config and add the following section:

# Huawei USB Stick
[phone_huawei]
model = AT
port = /dev/ttyUSB1
connection = serial

Issue a Gnokii command to verify that it works:

$ gnokii --phone huawei --identify
GNOKII Version 0.6.30
IMEI         : 333444555666777
Manufacturer : huawei
No flags section in the config file.
Model        : E176
Product name : E176
Revision     : 11.126.02.01.55

Sending SMS

OK, now for the real test – sending an SMS:

$ echo "SMS from Huawei" | gnokii --sendsms +31612341234 -r

If the SMS was sent correctly, gnokii exits with status 0. You can check that using the $? variable in your shell.

Receiving SMS

Incoming SMS are saved on the SIM-card memory, in memory slots starting at 0 (zero). To read the first (oldest) received message:

$ gnokii --phone huawei --getsms SM 0

The next one can be read using:

$ gnokii --phone huawei --getsms SM 1

… and so on.

Once you processed a message, you can delete it from the SIM-card:

$ gnokii --phone huawei --deletesms SM 1

There’s a lot of fun to be had with this setup – using simple SMS.

For interactive viewing of incoming SMS, use:

$ gnokii --phone huawei --smsreader

This will show new messages immediately.

Wrap-up

There’s lots more information to be found on the Gnokii Wiki.

My Huawei E160G turns out to have a SIM-lock on it. It would error out on most requests until I inserted a SIM of the correct network. Not all documented commands work:

$ gnokii --phone huawei --getlocksinfo
GNOKII Version 0.6.30
Error: Command called isn't implemented in model.

This makes troubleshooting quite a bit harder…

Nagios alerting

Next on my list is integration with Nagios – this is fairly simple; set up a Host Notification and Service Notification command that echoes a message to Gnokii. Voila: SMS alerting for Nagios ;-)

Process incoming SMS

Incoming SMS can be read using “gnokii --getsms“, but “gnokii-smsd” is a better option. It polls the USB modem regularly, and stores received messages in a database (PostgreSQL or MySQL). This makes it quite easy to use SMS from your own applications.

Have fun!