Thursday, November 30, 2006

Writing a SMS Service with Free Software

So what we will do is set up a simple SMS based service using Kannel and Linux. Kannel is a famous, powerful, open source and free SMS gateway and WAP gateway. You can use it to connect to a mobile operator's SMS center (SMSC), or using a GSM mobile phone or GSM modem as a virtual SMS center. Kannel can handle multiple SMS centers and virtual SMS centers.

Kannel supports the use of AT-capable mobile phones as virtual SMS centers. Most GSM mobile phones are capable to work with AT commands these days. A small list of mobile phones and GSM modems that are known to work as virtual SMS centers with Kannel is available on its web site.

The SMSC access protocols supported by Kannel include SMPP, CIMD, UCP / EMI and SMS2000 / OIS. In addition, Kannel has an HTTP / HTTPS interface that you can use to send and receive SMS messages.

I use Fedora Core 6 as my OS (desktop). A few methods to setup kannel using FC is, log in as root. Then execute the following command on your console

yum -y install kannel

or download the tarball from Kannel web site and compile it

This will get kannel installed on your box. Next is if you are using a GSM mobile, you need to make it work with your linux box. One easy way is setup the mobile as a GSM modem, via bluetooth. In my case I use the Bluez library for bluetooth connectivity. If you don't have it get it installed. All from FC3 up come preconfigured with Bluez libraries and all. In case you are using other distributions please get Bluez libraries and install them. On FC do the following. In any case your box needs to have bluetooth support.

yum -y install bluez-libs bluez-pin bluez-utils bluez-hcidump bluez-utils-cup

That should get you all fixed up. If you are using a bluetooth USB dongle you need to do the following

modprobe hci_usb


modprobe usb-uhci

If all goes well, type the following


then your output should be something like the following

hci0: Type: USB
BD Address: 00:0F:B3:90:86:46 ACL MTU: 192:8 SCO MTU: 64:8
RX bytes:5159 acl:5 sco:0 events:129 errors:0
TX bytes:1599 acl:5 sco:0 commands:83 errors:0

This means youe bluetooth device is all set to go. Now you just need to configure it. To test if your bluetooth is working, you can do a simple scan of all devices as follows. type

hcitool scan

which should give you an output simmiler to

Scanning ...
00:0F:B3:90:7B:6E WTM--LAP-07
00:0E:6D:CC:04:7F Dark
00:10:C6:82:A8:6F WTM-ITLAP-03

Anyway, the configuration is very simple and straight forward. If you want to bind to your mobile you need to configure Bluez.The configuration of blues will be located in /etc/bluetooth/ edit the following files and do medications as per your requirements. Add the following lines to rfcomm.conf

rfcomm0 {
bind yes;
device 00:00:0E:6D:CC:04:7F
channel 1;
comment "darkumoonu";

here 00:00:0E:6D:CC:04:7F is the mac address of my mobile. You need to change that accordingly . When you do a “hcitool scan” you'd get all the devices that your box finds. To make sure you are able to reach it you can even ping the device. Type l2ping mac; example

[root@darkmoon bluetooth]# l2ping 00:0E:6D:CC:04:7F
Ping: 00:0E:6D:CC:04:7F from 00:0F:B3:90:86:46 (data size 44) ...
0 bytes from 00:0E:6D:CC:04:7F id 0 time 58.93ms
0 bytes from 00:0E:6D:CC:04:7F id 1 time 17.34ms
0 bytes from 00:0E:6D:CC:04:7F id 2 time 17.31ms
0 bytes from 00:0E:6D:CC:04:7F id 3 time 16.31ms
4 sent, 4 received, 0% loss
Next you need to start the service. Do the following, type “etc/rc.d/init.d/bluetooth restart” or simply “servic bluetooth restart”

Now to talk the mobiles modem, type the following command.

rfcomm bind 00:0E:6D:CC:04:7F 1

This will essentially bind the Phones Modem to /dev/rfcomm0. Now we can check by issuing rfcomm.

[root@darkmoon bluetooth]# rfcomm
rfcomm0: 00:00:0E:6D:CC:04 channel 1 clean

You should get something like that. Ok, that about all you need to do to get your GSM device working with Linux. There is other configurations that you need to do if needed.

Ok, lets get back to Kannel. The Kannel configuration file will be localed in /etc/ edit /etc/kannel.conf

remove all the lines in the file and add the following

group = core
admin-port = 13000
smsbox-port = 13001
admin-password = bar
log-file = "/tmp/kannel.log"
log-level = 0

group = smsbox
bearerbox-host =
sendsms-port = 13013
global-sender = 13013
sendsms-chars = "0123456789 +-"
log-file = "/tmp/smsbox.log"
log-level = 0
access-log = "/tmp/smsaccess.log"

group = sendsms-user
username = tester
password = foobar

#Nokia Modem

group = smsc
smsc = at
smsc-id = nokia_smsc
modemtype = nokiaphone
device = /dev/rfcomm0
speed = 9600
pin = BlueZ

group = modems
id = nokiaphone
name = "Nokia Phone"
detect-string = "Nokia Mobile Phone"
init-string = "AT+CNMI=1,2,0,0,0"
speed = 115200
enable-hwhs = "AT+IFC=2,2"
need-sleep = false
no-pin = true
no-smsc = false
sendline-sleep = 100
keepalive-cmd = "AT+CBC;+CSQ"
broken = true
message-storage = "ME"
enable-mms = true

Now do the following

[root@darkmoon etc]# service kannel start
Starting kannel bearer box: [ OK ]
Starting kannel sms box: [ OK ]

That will get Kannel started as a service. If you fail look into the logs at /tmp/ /tmp/kannel.log and tmp/smsbox.log. That will help you to get started to debug and solve the problem.

Ok, So now if kannel is working you should be able to send SMS using the kannel web interface. Example from kannel

Type that in your browser. Again. If it gives any problems get back to logs and if needed google around. There is not much help for kannel out on the web, except the kannel documentation and the mailing list. But its easy to figure out most of the issues.

If you have access to a SMSC (if you are a telecom operator) you will need the following to connect to the SMSC. Add the lines to the kannel.conf and bring the needed changes.

group = smsc
smsc = smpp
host = "ip of your SMSC"
port = "port"
receive-port = "port"
smsc-username = "change"
smsc-password = "change"
system-type = ""
address-range = ""
interface-version = 33
source-addr-ton = 1
source-addr-npi = 1

You will need to have routes added to access the SMSC from your network. In my case, I have a FC5 box setup a SMSGW running Kannel and using SMPP to communicate to the SMSC.

So what do we do next. A simple service we can setup is one like Dhiraagu has to offer. Example the Haveeru news service. If someone sends you a SMS with the text “haveeru” we want to SMS him/her the new headlines. To do this we need to write a simple script to fatch the news from haveeru. For this perl is a good choice. So you need to have perl installed on your system. You would also need a cpan library to read the RSS feeds. Execute the following command first

cpan install XML::RSS

This will get the library installed. Next create a perl script called with the following lines.


# import packages
use XML::RSS;
use LWP::Simple;
use LWP::UserAgent;

# initialize object
$rss = new XML::RSS();

# get RSS data
$raw = get('');

# parse RSS feed
my $news='(*) ';

foreach my $item (@{$rss->{'items'}}) {
$title = $item->{'title'};
$url = $item->{'link'};
$news = $news . $title .' (*) ';
print("$title\n"); # we will only display the news titles

So now to add the service, you will need to add the following lines to the kannel.conf file.

group = sms-service
keyword = haveeru
exec = "/usr/bin/perl /home/darkmoon/perl/"
accepted-smsc="change as needed"

When the subscriber sends a SMS to a set short code, with the text "haveeru", it will execute the script we have. Then the output will be sent back to the subscriber. So there you go. Now you have a SMS based service up and running. There is a lot more you can do with kannel and others; just play around. Simple and easy.


Inash said...

This is an excellent article for people who want to setup and integrate a cost effective open source solution for any inter-communication with SMS. It was a good read.

I have a small question though. If I want to setup a system with Kannal with a bluetooth mobile phone as a SMS gateway, will your instructions work? I mean how does the routing work?

If the mobile phone recieves a SMS, how will it route that to Kannal for further processing of it?

chopey said...

Yeah! It should.. you have to define in your kannel config file the GSM(mobile) device as the smsc; like in the example... something like

group = smsc
smsc = at
smsc-id = nokia_smsc
modemtype = nokiaphone
device = /dev/rfcomm0

Inash said...

Alright. I get the picture. This is going to be excellent information for alot of us. Infact this will come very handy at home and even at office.

It will enable a lot of us to setup a SMS alert platform based on the systems that we maintain and monitor at office, and even other personal stuff.

I'm going to try this out. And if it happens to be very efficient, and not to mention convenient, I will definitely do a follow up article on this topic.

Let's see how many of us actually require a system like this and if this turns out good for them, it is a real consideration for medium to even large business to embrace this open source platform.


chopey said...

inn, actually kannel can help a lot. Example I've read abt mobile baking. One good example is here... lets say BML sets up a kannel box, connects to the telco's smsc. thats all they need. Everytime BML need to introduce a service they don't need to talk with the telco's.

Example. you want to setup a method for the customers to chk the bank balance. you pre register the customers and auth them via SMS. then if a sms is received from that MSISDN (mobile number) with the set pin code and a request like "BAL PIN" where "PIN" can be the pin given PIN number. you get the MSISDN and the corresponding account to which its registered to. Then run a script with the parameters and do a DB lookup, and give the balance. Simple as that. qprld

kevin Peter said...
This comment has been removed by the author.