ChipmunkNinja
Ninjas are deadly. Chipmunk Ninjas are just weird.
About this blog
Marc Travels
Marc on Twitter
JustLooking on Twitter

Marc Wandschneider is a professional software developer with well over fifteen years of industry experience (yes, he really is that old). He travels the globe working on interesting projects and gives talks at conferences and trade shows whenever possible.

My Publications:

My book, "Core Web Application Programming with PHP and MySQL" is now available everywhere, including Amazon.com

My "PHP and MySQL LiveLessons" DVD Series has just been published by Prentice-Hall, and can be purchased on Amazon, through Informit, or Safari


ABCHKMPRaRoSTVW
-x----xxx--x-xx
Apr 10, 2007 | 02:28:51
Setting up, Configuring, and Using Kannel to send/receive SMS messages
By marcwan

I recently had the oppportunity (necessity) to set up a web application that interacted with many of the users through SMS messages in addition to the more traditional HTML interface. While there are a number of possible software solutions for GSM modems on Windows, on Unix-like platforms the most commonly used one is Kannel. It also has the advantage of being open source and thus very, very free.

However, setting, configuring, and using Kannel tends to be a bit tricky. I’m writing this article (almost a HOWTO) in an attempt to help out anybody who’s undertaking the process themselves and might be able to get some tips and tricks from this. I expect this to not be a terribly popular article, but if I ever need to set this stuff up myself in the future, then I’ll have it written down somewhere at least!

Most of the instructions here will work on any Unix platform, such as Linux, FreeBSD, or Mac OS X. It’s worth noting that I got nearly everything working with OS X, only to be thwarted at the very end because Mac’s no longer have serial ports to use GSM modems. You could, however, easily use some of the more advaned HTTP based SMS services on a Mac server of some sort.

The Scenario

The web site written was a discounting service oriented around cell phones. Users would send the site a message, and then receive a discount at local venues. They could go to the site on the intarwebs to see their total savings and learn about more venues.

The usage pattern was always:

  • User sends us a message.
  • We might send them a reply after processing it.
  • We might also periodically need to be able to send the user a message for other purporses, such as party invitations, etc (based on their preferences).

Downloading and Compiling

Kannel is trivially easy to download and compile. You visit the Kannel.org website and download the latest and greatest gateway-1.X.Y.tar.gz file.

From there:


# mkdir src
# cd src
# tar xfz ../downloads/gateway-1.4.1.tar.gz
# cd gateway-1.4.1
# configure --prefix=/usr/local/kannel

I chose to install to /usr/local/kannel just because I’m that kind of guy who likes to keep everything reasonably separated and organised. You’re free to put it anywhere.

Compile and install.


# make
# sudo make install
password: **************

Configuration Files

You can be forgiven for thinking Kannel is trivially easy thus far—it really is that easy to download, compile, and install. Unfortunately, here is where things get tricky.

You now need to set up a configuration file. This file has a zillion options to support all of the possible and powerful ways in which Kannel can be used. I will be showing strictly how I set it up for the GSM modem we had in the office (It’s a Siemens GSM modem connected to a serial port, and works quite well).

The basic smskannel.conf (in the gw/ directory) has much of the information we want, but we’ll need to add a few things for our GSM modems and to interact with our web server correctly.

Configuration is divided into a few key groups, each representing the key parts of the kannel system, including the server that handles sending and receving the actual SMSes (bearerbox) and the system that handles the final dispatching to your scripts (smsbox).

The core Group

The first part of the file is the “core” group, and the default is pretty close to what we want:


group = core
admin-port = 13000
smsbox-port = 13001
admin-password = bar
#log-file = "/tmp/kannel.log"
#log-level = 0
box-deny-ip = "*.*.*.*"
box-allow-ip = "127.0.0.1"

You’ll want to change the password of course, but everything else is nearly standard. We are assuming that all communication to the kannel server will come from the same physical computer (127.0.0.1). You can set a log file if you are going to be running kannel as a service on your server, or you can just redirect stdout to some file.

Be aware that kannel has various log levels, ranging from 0, which displays information that is only of interest when you’re in the development and debugging phases, to 4, which only displays critical errors and problems. I tend to develop at level 0 and run live servers at level 1. Disk space is cheap.

The smsc Group

Kannel supports a pretty insane number of ways of sending and receiving SMSes, ranging from SMS services over HTTP, to a fake SMS centre for testing/development purposes, to GSM modems, which is what I have used and is the smsc module. These modems use AT-style modem commands and typically hook up over the serial port. To get this going, I set up the smsc group in the smskannel.conf file:


group = smsc
smsc = at
modemtype = auto
device=/dev/ttyS0
my-number = 123123123123
connect-allow-ip = 127.0.0.1
log-level = 0

The my-number field contains the number of your GSM modem’s SIM chip. Again, I only allow connections from my local server, and the Ubuntu Linux serial port is on /dev/ttyS0.

The smsbox Group

The smsbox group helps configure the part of the system that dispatches SMSes received by the core SMS or receives SMSes before they’re sent out. I honestly don’t fully understand what this group really does, but it’s necessary, and pretty trivial to set up.


group = smsbox
bearerbox-host = 127.0.0.1
sendsms-port = 13013
global-sender = 123123123123
log-level = 0

The global-sender field is the outgoing-number of your GSM modem, which for me is the same as the my-number field above.

The Sendsms Group

This group is what allows your web applications to send SMS messages using Kannel. They do this via simple HTTP requests, and configuration here basically requires a user name and password:


group = sendsms-user
username = kanneluser
password = df89asj89I23hvcxSDasdf3298jvkjc839
concatenation= true
max-messages = 10

Since the password is semi-plain and unprotected here, I tend to use one that is complicated and nearly impossible to remember, but quite different from any other passwords that I actualy use for login accounts and the like.

Getting the Messages to your application

The sms-service group configures how Kannel gets messages to your web application. You are allowed to specify a number of these groups, each of which can “catch” incoming messages based on various criteria. My application had all messages go to one processing script, so I just set up one group that caught all incoming messages.


group = sms-service
keyword =
keyword-regex = .*
catch-all = yes
max-messages = 0
get-url = "http://localhost/sms?phone=%p&text=%a"

This particular configuration has Kannel set up to use an HTTP GET request to send the message to my application. The param phone contains the phone number of the sender and the text parameter contains their entire message.

NOTE: The max-messages value was particularly tricky and critical for me: When I first set up Kannel and tested sending messages, I would always get back '<Empty reply from service provider>'. Setting max-messages to 0 tells Kannel to never send a reply directly from the incoming message (you can, of course, initiate your own response later, of course).

Finally, Setting up the modems

Kannel and smsc tends to be pretty good at figuring out everything about your modem by yourself, but you can help them out by including modems.conf in your smskannel.conf file as I did:


include = "/usr/local/kannel/modems.conf"

Running the Server

The hard part is done; all we have to do now is copy over the config files and start the service up:



# cd /usr/local/kannel
# cp ~/src/gateway-1.4.1/smskannel.conf .
# cp ~/src/gateway-1.4.1/gw/modems.conf .
# sbin/bearerbox -v 0 smskannel.conf &
# sbin/smsbox -v 0 smskannel.conf &


I tend to run the last two commands in two separate shell windows when developing/debugging so that I can see the output from the two programs clearly and use the information to help me figure out what’s going on (level 0 really tells you a lot).

Receiving Messages

Kannel will simply call the URL you told it to in the sms-service group and you can process this with whatever HTTP server environment you want. We’re using LAMP right now, but, again, any will do. The incoming phone number and message are in GET parameters. You can, if you want, configure the sms-service to send them as POST messages as well.

Sending Messages through Kannel

The final part our puzzle is to send outgoing SMS messages through Kannel, and has only one little twist. It is also done via an HTTP interface. It requires you to be a little careful about the character set you use. I found I had the most success by using the UCS-2 character set. In PHP5, you can easily use the iconv function to do this for you.

Since I send both English and Chinese messages, my PHP scripts and langugage string files are all UTF-8. Here is the code I use to send messages:



 function sendSmsMessage($in_phoneNumber, $in_msg)
 {
   $url = '/cgi-bin/sendsms?username=' . CONFIG_KANNEL_USER_NAME
          . '&password=' . CONFIG_KANNEL_PASSWORD
          . '&charset=UCS-2&coding=2'
          . "&to={$in_phoneNumber}"
          . '&text=' . urlencode(iconv('utf-8', 'ucs-2', $in_msg));

   $results = file('http://'
                   . CONFIG_KANNEL_HOST . ':'
                   . CONFIG_KANNEL_PORT . $url);
 }


To make this work, of course, you need to have allow_url_fopen set to On.

That’s It

That’s pretty much it. This has been a pretty dry article, but it does contain everything you need to get Kannel up and running and operational. The manual actually does contain everything you could possibly want to know, so keep digging in there if you’re stuck. Finally, there are mailing lists at kannel.org which tend to be quite helpful as well.

Good luck!

Comments (50) Add Comment | Tags: kannel sms text messaging setup installation howto linux unix
Congratulations and help
Hello Marc.
It is a simple, but illustrative article.
I am trying to use a mobile telephone nokia shape 3250, as modem for the service SMS, but on having executed smsbox, this part send the command AT+CNMI=1,2,0,1,0 and the telephone answers with "EROR". Might you say to me please that I am doing badly?
Other one asks, some guide of user exists of kannel better explain of the own one of kannel?
Thanks and regards
I wait for your response,please!!!!
Modem errors
Posted By: marcwan Apr 27, 2007 22:51:47
Hey Pablo!

I'm sorry, but I don't have a good answer for your question - I have only used a small Siemens GSM modem so far, and I *DID* have some errors when I started, but by choosing the right modem from modems.conf (in fact, I didn't even need to do that - just including modems.conf was okay) all my problems went away.

If you're seeing weird problems, it might be best to go and ask the users@kannel.org mailing list ....

Good luck!
Setup
Posted By: marcwan Jan 31, 2008 20:10:41
I'm sure there are some simple scripts you could find to do some SMSing once you get the entire system set up, yah. You could just take my above code, put it in a PHP file, put #!/usr/local/php5/bin/php at the top, and make it executable, as well. I do a lot of system scripting in PHP these days, just because it's so easy to do.

As for using things other than GSM modems, you most certainly can -- It's just that my experience thus far is entirely limited to GSM modems. Many people use actual cell phones as GSM modems, and others use service providers and other facilities to do the SMS work (some of them even provide their own programmable interfaces that obviate the need for kannel entirely ...).


Good luck!
Configuring Kannel on Ubuntu
Posted By: K- Sep 04, 2008 01:02:11
@Thabiso : you need to install a compiler, like gcc.
Mr
Posted By: Enstine Sep 23, 2008 11:50:04
Sir,
I get the following error within my PHP script

"Warning: file(http://192.168.1.2:10000/cgi-bin/sendsms?username=root&password=kokodi&to=99999&text=test msg) [function.file]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in D:\wamp\www\sms_trigger\incl\sms.php on line 27"

I tried file (http://192.168.1.2) and everything seemed fined. Could it be the port or the ":" ?

Please help

Thanks
send sms group from Mysql data base with KANNEL
Posted By: Nabil Oct 22, 2008 08:44:59
Sir,
can someone tell me how I send a SMS to a lot of users, located in Mysql data base, with KANNEL.
Storing in DB
Posted By: marcwan Nov 30, 2008 02:23:23
I think there -might- be automated ways of doing this, but we would just take the incoming message text, process it in PHP, and then put the result in the database that way. The total processing script couldn't have been more than 150 lines of PHP code, so nothing serious, and didn't appear to be too slow.
How to install kannel on ubuntu transcript
Posted By: tomer Jan 24, 2009 02:33:52
see in this url how to install kannel on ubuntu
vinestars
Can I compile the Kannel gateway with Cygwin? how do I do that. can someone help me with the Stpe-by Step with Cygwin

Thanks
re: GSM MODEM
Posted By: mark raymund tejero Apr 28, 2009 18:45:42
hi marc,

can you give me tips ;-)

im having a problem in setting up my kannel + modem ( itegno 3800 )

everything is okay in receiving sms

followed your tips from this page

works fine.

but, on the sending of msg, logs seems said to be okay.

but i didnt receive the actual msg on my phone

#bearerbox-log

2009-04-29 09:52:32 [14442] [11] DEBUG: boxc_receiver: sms received
2009-04-29 09:52:32 [14442] [11] DEBUG: send_msg: sending msg to box: <127.0.0.1>
2009-04-29 09:52:33 [14442] [6] DEBUG: AT2[wavecom]: TP-Validity-Period: 24.0 hours
2009-04-29 09:52:33 [14442] [6] DEBUG: AT2[wavecom]: --> AT+CMGS=18^M
2009-04-29 09:52:33 [14442] [6] DEBUG: AT2[wavecom]: <--
2009-04-29 09:52:33 [14442] [6] DEBUG: AT2[wavecom]: <-- >
2009-04-29 09:52:33 [14442] [6] DEBUG: AT2[wavecom]: send command status: 1
2009-04-29 09:52:33 [14442] [6] DEBUG: AT2[wavecom]: --> 0011000A9156187509850000A705E8329BFD06
2009-04-29 09:52:33 [14442] [6] DEBUG: AT2[wavecom]: --> ^Z
2009-04-29 09:52:54 [14442] [6] DEBUG: AT2[wavecom]: send command status: -1
2009-04-29 09:52:54 [14442] [6] DEBUG: AT2[wavecom]: --> AT+CMGS=18^M
2009-04-29 09:52:54 [14442] [6] DEBUG: AT2[wavecom]: <--
2009-04-29 09:52:54 [14442] [6] DEBUG: AT2[wavecom]: <-- OK
2009-04-29 09:52:54 [14442] [6] DEBUG: AT2[wavecom]: send command status: 0

@smsbox-log
2009-04-29 09:52:32 [14454] [3] INFO: smsbox: Got HTTP request </cgi-bin/sendsms> from <127.0.0.1>
2009-04-29 09:52:32 [14454] [3] INFO: sendsms used by <tester>
2009-04-29 09:52:32 [14454] [3] INFO: sendsms sender:<tester:+6598540029> (127.0.0.1) to:<+6581579058> msg:<hello>


hope you can help me

thanks,
mark

SMSC
Posted By: Charles Jun 15, 2009 12:44:24
Can someone list how do we connect to SMSC and send message directly to SMSC over a server with ever having a GSM modem to deliver the messages?
Chinese characters
Posted By: marcwan Jun 24, 2009 03:13:48
here in mainland china it's no problem: it just works. You have to encode and decode from UCS-2 (iconv() in PHP).

Other countries will be different. I bet many (most) countries will not support them and mangle them.
Add a Comment

Title:

Name:

URL:

Comment:

Copyright © 2005-2008 Marc Wandschneider All Rights Reserved.