Setting Up An SMS Gateway With Ubuntu 8.04, Kannel and Huawei E220 GSM Modem
Setting Up An SMS Gateway With Ubuntu 8.04, Kannel and Huawei E220 GSM Modem
Setting Up An SMS Gateway With Ubuntu 8.04, Kannel and Huawei E220 GSM Modem
Life
Googling a bit I found out that Huawei E220 GSM modem works well with a GNU/Linux
system.
Fortunately, the device was available in my area. So, I ordered one.
I took the Kannel SMS gateway setting from someone named Lasantha who posted it on
http://www.kannel.org/pipermail/users/2008-August/005342.html.
After putting user `kannel' under group `dialout' to get read-write access to /dev/ttyUSB*, the
configuration worked like a charm.
But, when I sent a single SMS to the modem, the following conversation ensued:
and the same conversation kept repeating in an endless loop filling up my harddisk space.
While waiting for the answer, I tried to google with `kannel huawei cms error 321' that returned
http://www.developershome.com/sms/resultCodes2.asp.
It said that 321 means: `The memory/message storage index assigned to the AT command is
invalid.'
Seeing the word index made me conclude that perhaps while Kannel addressed the SMS slots
from 1 to 40 inclusive, the modem addressed the SMS slots from 0 to 39 inclusive just like when
addressing an array.
I confirmed that this was true by sending another SMS that Kannel happily fetched and replied
successfully although it still could not fetch the old message as follows:
Now this is the benefit of buying a free software (are you confused with the word `buying'? See
http://gnu.org for the answer), you have the advantage to be able to fix this kind of corner cases
quickly.
So, I went on by downloading the source package as described in
http://www.cyberciti.biz/faq/rebuilding-ubuntu-debian-linux-binary-package/.
Adding
--message_number;
While trying to make a better patch to be posted to kannel-devel mailing list, I noticed an
interesting line in kannel-1.4.1/gw/smsc/smsc_at.c as follows:
if (!at2_read_delete_message(privdata, location)) {
error(1, "AT2[%s]: CMTI notification received, but no message
found in memory!",
octstr_get_cstr(privdata->name));
}
So, the next step was to google for `Huawei CMTI' that returned
http://forum.huawei.com/jive4/thread.jspa?threadID=325681&tstart=0&order....
There was no answer on the page, but I found an interesting fact: ttyUSB1 should be used instead
of ttyUSB0.
Besides that, it also mentioned that CNMI is the AT command to set the modem to issue CMTI.
So, after googling for a specification that describes CNMI and fiddling with the modem using
minicom, I found out that it was true that ttyUSB1 should be used instead of ttyUSB0 since the
modem does not send the notification through ttyUSB0.
This article found via Google the other day also confirmed this:
http://oozie.fm.interia.pl/pro/huawei-e220/
After a while, I came out with the following final configuration that makes the modem notifies
Kannel whenever an SMS is received:
group = smsc
smsc = at
port = 10000
modemtype = huawei_e220_00
device = /dev/ttyUSB1
sms-center = [EUS: Sorry, the number is private]
my-number = [EUS: Sorry, the number is private]
connect-allow-ip = "127.0.0.1"
group = modems
id = huawei_e220_00
name = "Huawei E220"
detect-string = "huawei"
init-string = "AT+CNMI=2,1,2,2,0"
message-storage = sm
speed = 460800
Besides that, my hypothesis is proven to be correct since Kannel is able to fetch an SMS from
location/index zero as supplied in the CMTI notification message. Therefore, there is no need to
work on my patch any further.
You may find that /dev/ttyUSB1 is not the right one for you. For example, in another server I use
/dev/ttyUSB3.
In that server, Kannel can successfully open /dev/ttyUSB0, but it does not receive the
notification.
Using /dev/ttyUSB1, /var/log/kannel/bearerbox.log got the following error message:
To conclude, setting up an SMS gateway with GNU/Linux Ubuntu 8.04, Kannel 1.4.1, and
Huawei E220 GSM modem has been proven to work successfully by using the right SMSC
configuration and USB-serial device node (e.g., /dev/ttyUSB1).
Eus's blog
Add new comment
eus...eus....Ei pake mayo
reply
Eus
on
April 22, 2009 - 3:33am
Hi Ho!
It turned out that not all SIM cards can be used. So, beware!
For a complete story, read `SIM Card Does Matter when Setting Up an SMS Gateway'
(http://kerneltrap.org/node/17071).
Best regards,
Eus (FSF member #4445)
Join: http://www.fsf.org/jf?referrer=4445
reply
Applications/tools
One day when I tried another SIM card with the Huawei E220 modem that I told about in
http://kerneltrap.org/node/17017, I got the following error message on bearerbox.log:
Googling for a solution, I needed to change the keywords several times before finally I got:
http://wiki.openmoko.org/index.php?title=FreeRunner_unable_to_work_with_...
Yes, it looks like that the new internal wiring of 3G SIM cards prevents the GSM modem to
work with it. What's the solution?
BTW, since I live in Indonesia, I use the available SIM cards sold in the country. Curiously
enough, I couldn't find another Indonesian experiencing this problem. I guess they are lucky to
get SIM cards with the old wiring. Anyway, in case anyone experiencing this problem in
Indonesia, I attached an image showing the working and non-working SIM cards. All cards on
the right works. All cards on the left doesn't.
If anyone can help me with this, I'd really appreciate it since 3G SIM cards are becoming more
common.
Attachment Size
Attachment Size
Working vs. Non-working SIM Cards.jpg 65.76 KB
Eus's blog
Add new comment
I am not alone
Eus
on
April 13, 2009 - 1:59am
Hi Ho!
This guy also experienced the same but finds no solution as well:
http://forum.huawei.com/jive4/thread.jspa?forumID=130&threadID=325628&me...
Best regards,
Eus (FSF member #4445)
In this digital era, where computing technology is pervasive,
your freedom depends on the software controlling those computing devices.
Join: http://www.fsf.org/jf?referrer=4445
reply
Filing complain
Eus
on
April 14, 2009 - 1:59am
Hi Ho!
Best regards,
Eus (FSF member #4445)
Join: http://www.fsf.org/jf?referrer=4445
reply
Eus
on
May 15, 2009 - 5:28am
Hi Ho!
Last weekend a friend of mine lent me a locked Huawei E220 modem. Out of curiosity, I
inserted the top-left non-working SIM card, and guess, it works well with the modem!
Particularly, I can send and receive SMS through Kannel. However, the modem cannot access
the bottom-left non-working SIM card just like the last modem.
Still another friend of mine lent me another non-locked Huawei E220 modem. This modem also
experiences the same thing; it can access the top-left non-working SIM card but not the bottom-
left one.
Altering the firmwares to both old and new do not have any effect at all. BTW, I get a bunch of
firmwares here: http://forum.telefonino.net/showthread.php?t=366707
So, I conclude that the first Huawei E220 modem is a bit defective for not being able to access
the top-left non-working SIM card.
Now, let me digress a bit. Regarding the locked modem, it turned out that the combination of the
modem and its firmware itself are not locked since I can use another SIM card to get connected
to the Internet using another SIM card. It seems that the locking is provided by the combination
of the included SIM card as well as the software included in the modem that will be installed on
Windows platform upon the insertion of the modem. It is because I cannot get connected to the
Internet by using a combination of an unlocked modem and the SIM card included with the
locked modem.
In all cases, when buying a modem for an SMS/WAP gateway, the best shot is to find out
whether or not the modem can work with a designated SIM card before finalizing the
procurement.
Best regards,
Eus (FSF member #4445)
Join: http://www.fsf.org/jf?referrer=4445
reply
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.
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.
# 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 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.
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 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.
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.
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).
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"
# 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:
$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!