Apr 13, 2007

SMS Service with Free Software



Before you start I would say you get familiar with Kannel at http://kannel.org/
Go through the documentation.

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

then

modprobe usb-uhci

If all goes well, type the following

hciconfig

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
UP RUNNING PSCAN
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 = 127.0.0.1
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

# SMSC GSM
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

http://127.0.0.1:13013/cgi-bin/sendsms?username=tester&password=foobar&to=&text=hello 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
smsc-id="change"
denied-smsc-id="change"
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 haveeru.pl with the following lines.

#!/usr/bin/perl

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


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

# get RSS data
$raw = get('http://www.haveeru.com.mv/xml/index.php');

# parse RSS feed
$rss->parse($raw);
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/haveeru.pl"
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.


1 comment:

Anonymous said...

Thanks mate, that was a wonderful piece.