I see, learn and rediscover… everyday!
 
Setting up a SMS server using Gnokii

Setting up a SMS server using Gnokii

I’ve used gnokii to set up a SMS server in our college as a part of Spider Project. Though I didn’t actually believe that a computer and a mobile phone can be connected when Manas suggested the idea, I coded much for this SMS server later. 😛
So here is the short manual about how to configure gnokii in a system. Let us assume that we have a basic Fedora or Ubuntu installed machine, a Nokia 7250i mobile and an USB data cable.

Gnokii Installation :
This can be done in two ways like any other open source software.
Installing from binary.
Installing from binary is very easy if you have yum (Fedora) or apt (Ubuntu) configured for your system. All you need to do is to execute the following command as root or as sudo

In Ubuntu the command is
hari@spider:~$ sudo apt-get install gnokii gnokii-smsd gnokii-smsd-mysql
In fedora the command is
root@spider:~# yum install gnokii gnokii-smsd gnokii-smsd-mysql
The above packages have lots of dependencies like libgnokii3, liblockfile1, timeout, dialog, mysql-common, libmysqlclient15off. So it is better to install gnokii using yum (Fedora) or apt (Ubuntu) instead of rpm (Fedora )or dpkg (Ubuntu).

Installing from Source:
This will be useful if you don’t have a yum or apt configured. Download the source package from http://www.gnokii.org/download/gnokii/. Then execute the following. I have used gnokii-0.6.9 for example. It might differ in your case.[root@spider ~]# tar -zxvf gnokii-0.6.9.tar.gz
[root@spider ~]# cd gnokii-0.6.9/
[root@spider gnokii-0.6.9]# ./configure
[root@spider gnokii-0.6.9]# make
[root@spider gnokii-0.6.9]# make install

Gnokii Configuration :
Once gnokii is installed, we need to change the configuration file (gnokiirc) and check whether the computer can identify the mobile phone. The configuration file can be located at two places. One in your home folder and the another in /etc.
If there is no file in your home folder, then gnokii uses the configuration file present in /etc.

The mobile phone i use is Nokia, model 7250i, type NHL-4JX.
For the example below, let us assume that you have permissions to edit the configuration file in /etc. Else copy the file from /etc and do the changes mentioned below.

1. Open gnokiirc file in any editor as root or sudo.
2. Since I’m using an USB cable to connect the mobile to computer, set the value of port as “/dev/ttyUSB0” (quotes for clarity).
This value may rarely change from computer to computer. You can from the output of the command “dmesg | tail” after you connect the USB cable to the computer. I get an output like this [ 325.740151] pl2303 2-1:1.0: pl2303 converter detected
[ 325.740847] usb 2-1: pl2303 converter now attached to ttyUSB0
[ 325.741245] usbcore: registered new interface driver pl2303
[ 325.741253] /build/buildd/linux-source-2.6.22-2.6.22/drivers/usb/serial/pl2303.c: Prolific PL2303 USB to serial adaptor driver

So in line 2, I know that that it is ttyUSB0.
3. Set the model as AT
4. Set the value of connection as serial

Then execute the command “gnokii –identify”. The above command shoud produce an output like
hari@spider:~$ gnokii --identify
GNOKII Version 0.6.18
IMEI : 352955008063785
Manufacturer : Nokia
Model : Nokia 7250i
Product name : Nokia 7250i
Revision : V 4.63

If the command takes more than 5 or 10 seconds, than it just means that the computer hasn’t identified your mob yet. Remove the data cable from mob and USB cable from computer. Connect them once again and try. This time it may work. This has happened so many times in my machine. Still not able to come up with a nice reason why this happens 🙂

Once the computer has identifed your mobile phone, you can do lots and lots after that. 🙂
man gnokii will give you a huge list of commands which you can use to control your mob. Some of them which will be useful for SMS interface is gnokii --getsms SM 1 end
Above command reads all the messages stored in SIM memory gnokii --getsms ME 1 end
Above command reads all the messages stored in phone memory.
See man gnokii to see more options for getsms.

To send SMS using gnokii, try this command echo "Spider SMS Server" | gnokii --sendsms 9940557688

Working with smsd
Once gnokii is configured, we can use smsd to send messages. This is more like a wrapper for gnokii. After you install gnokii-smsd, a database called SMS is created in your database.

To start the smsd server, you run the command
smsd -u -p -c -m mysql
smsd -h or smsd --help will be quite useful if you can’t remember the above options.

Once smsd is running, any SMS which comes to the mobile is taken by the computer, stored in the inbox table and then deleted. Similarly any entry added in outbox is sent to the destination. An very useful field in these two tables is “processed”. Any message which is added to the inbox has a default “processed” value of “0”. So once we are done with this message, we should change the value to 1. Any message which is inserted in the outbox has a default “processed” value of 0 and “error” value -1. If the message is sent successfully, the value of processed and error is changed to 1 and 0 respectively. If there is any error in sending the message, the value of processed and error is changed to 1 and some positive integer. Also note that changing the value of processed in outbox to 0 will result in resending the message again.

So any script which checks the inbox table every 5 seconds for messages with processed = 0 and does the required action when there is any new message will complete the SMS server model.
This PHP script sends a “Thank you” message to anyone who sends any message to the SMS server.
This PHP script sends mass message to all the numbers in a text file
This PHP script sends mass message to all the numbers in a mysql table
This is the PHP script I used for LEO poll.
You can download all the above scripts as a single zip file here.

55 Comments

  1. nuffi

    This is a fine guide, but I am wondering what to do when things go wrong. I get the right response from –identify, but when I try to send an sms it looks like this:

    echo “Testing message in a bottle!” | gnokii –sendsms 5550124
    GNOKII Version 0.6.14
    SMS Send failed (Unknown error – well better than nothing!!)

  2. Joko

    Btw.. I have found a work around for this one. We can use connect script to set the SMSC number into the phone.
    1. Create a file, say /etc/gnokii-smsc-init.chat
    =============================================
    #! /usr/sbin/chat -vEf
    TIMEOUT 10 “” “” ABORT “BUSY” ABORT “NO DIAL TONE” ABORT “ERROR” ABORT “VOICE” ”

    AT+CSCA=+xxxxxx “OK”
    ============================================================
    2. Modify the gnokiirc, set connect script
    connect_script = /etc/gnokii-smsc-init.chat

    +xxxxxx : The smsc number, it depends on your network carrier.

    This should work, if not, check the syslog.

  3. Shoaib

    Hi,
    i have configured gnokii smsd but when i issued command as stated above i.e
    smsd abcUser xyzPassword localhost mysql
    it gives error : wrong number of arguments
    can you please tell me how to use smsd command exactly with arguments
    suppose [db_username = abcUser], [db_password = xyzPassword], [db_hostname = localhost],[module = mysql].
    please help me as i am a newbie

    1. Hi,

      If you install gnokii from there, I think it will have a smsd folder inside that. I generally install it from apt. I’m not too sure about gnokii-smsd-mysql, but I _think_ it also comes with gnokii package. I’m not too sure. Can you check this up and update me? 🙂

  4. sheikh

    Hi… are u there…
    1. i already check in my linux machine and found smsd inside gnokii-6.11, but i don know how to configure.
    2. Why only one time sent sms by using command line echo “testing” | gnokii –send 012XXXXXXX then i tried one more time it failed. This error msg SMS Send failed (Unknown error – well better than nothing!!)

    Can you help me??!!!

  5. Hi
    thanx for providing such a useful tutorial

    i have installed gnokii suscessfully and was able to send sms using command linke
    but now tell me how to configure smsd properly and how to send sms via php .
    the following error i got while running gnokii-smsd command.
    plz give me brief answer to setup http interface
    /usr/lib/smsd/libsmsd_pq.so: cannot open shared object file: No such file or directory!
    Cannot load database module pq in directory /usr/lib/smsd!

  6. @tauseef hi,

    you will have a file called libsmsd_pq.so in your current directory. You can copy paste it to /usr/lib/smsd/ folder. Also by default, smsd starts pqsql. If you want to try connecting to mysql, try starting with mysql module ( -m mysql)

    Hope that helps

  7. DDF

    Dear all,
    I have the same configuration of this tutorial ( Ubuntu Server, gnokii GNOKII Version 0.6.22 , not .18, and the same nokia phone, 7250i). I follow all the steps and it’s all ok until I start the command “gnokii –identify”.
    Here the final report is the following string:
    Error in link initialisation: 11
    Time out
    Impossible to unlock the device
    Why?
    Thanks in advance for your reply

    DDF

    1. Hi ddf,

      I’m just guessing this. Is your mobile phone locked when you try this gnokii –identify?
      To be frank, it’s been a long time since I’ve worked with gnokii. But I’ll try my best to help you out. 🙂

  8. DDF

    Hi sp2hari,
    I unlocked my phone by its menu and I reloaded the default settings, but nothing:according to you, what do I still have to do to unlock my phone?
    About the cable, I’m using a compatible nokia cable DKU-5. It is detected from OS as “pl2303 converter now attached to ttyUSB0” and its ID is “067b:2303 Prolific Technology, Inc. PL2303 Serial Port.” and reading from Gnokii’s wiki ( http://wiki.gnokii.org/index.php/Cables) the modules seem to be right.
    In the config file “/etc/gnokiirc” I used:
    “1st trial”
    port = /dev/ttyUSB0
    model = AT
    connection = serial

    but gnokii is locked on the AT commands for the initialization of the device. About this configuration I tried to send AT commands to my 7250i by Hyper Terminal, but I looked that it didn’t respond to AT commands (In the last weeks I tried the same commands on the Nokia 6680, which responded to the handshake commands at least :-D. I had to dismiss 6680, because it don’t support the “send / receive SMS” by gnokii).
    “2nd trial”
    port = /dev/ttyUSB0
    model = series40
    connection = serial

    but gnokii is locked on the same handshake messages, but in a different format respect the AT ones. I used model = series40, because other users changed AT into series40 (http://www.nabble.com/Problem-connecting-to-Nokia-2610-td17298595.html).

    “3rd trial”
    port = /dev/ttyUSB0
    model = 6510
    connection = serial

    but gnokii reports the above results.
    I tried to change the connection option in “dku5”, but nothing.
    I am open to every tip.
    Thanks a lot

    Best Regards
    DDF

  9. DDF

    P.S.
    I’m forgotting that the time out is on the “Getting model” command:
    SM_Block: exiting the retry loop
    Message sent: 0x1b / 0x0006
    00 01 00 07 01 00
    ………………….
    Impossible unlock the device

    Cheers

    DDF

  10. DDF

    For Marcus:
    Hi Marcus,
    I tried your tip, but nothing.

    For All:
    today I have a news. Gnokii runned only yesterday.
    I setted:
    port = /dev/ttyUSB0
    model = AT-HW
    connection = serial
    I runned gnokii, but nothing.

    I installed xgnokii
    I runned xgnokii, but nothing.

    I inserted another SIM (It seems the same of mine …. same provider and same KB ….. )
    I runned xgnokii and SURPRISE: my GW sms runned.

    Today is a bad day, because Gnokii doesn’t work and I don’t have the working SIM to test it. Accordijng to you, is it a possible reason or it was a simple coincidence?
    Thanks in advance for your support

    Cheers

  11. Hi,
    I think by default gnokii sends a message of length 320 characters in 2 messages. I’m not too sure about it. Been a long time since I worked on this. But in case if it doesn’t do so, you can always change the logic which performs the mysql_insert to do two inserts instead of one.

    Hope it helps…

  12. tito

    Hi,
    The subject is nicely described…but I have a little problem while running the smsd.
    Whenever I try this:

    #smsd -u -p -d -c localhost -m mysql

    I got this error : lowlevel.c:461 error: 8, unknown error

    Anybody understand that? If yes, help me to fix it, it really does bother me

  13. Saket

    I am trying to setup gnokii on ubuntu 8.10 for Nokia 6030..
    my settings are

    model : 6510
    port: /dev/ttyUSB0
    connection : serial

    but when I run gnokii –identify
    the output is :

    GNOKII Version 0.6.26
    Gnokii serial_open: open: No such file or directory
    Couldn’t open ATBUS device: No such file or directory
    Telephone interface init failed: Command failed.
    Quitting.
    Cannot unlock device.
    Command failed.

    I tried earlier with Nokia E50 and was successful in sending sms but was unable to read them, hence I tried on 6030 but this one is not even identified by gnokii..please help me ..

  14. Hi there guys! I’m just a newbie for this things.. I just wanna ask if is there a way that we can use gnokii just like chatting by just using the terminal or command prompt? for example, I want to send a message to my friend using the command (echo “Hello…” | gnokii –sendsms 1234567) and when he replies, it just display in the terminal also or in the command prompt.

  15. I install the Xgnokii and it was successful but what happen is that I can only send sms, I can’t receive even though I receive the message in my celphone.. I’m using nokia 3120 classic via bluetooth.

    Another issue is that, when I used the command “gnokii –getsms ……” it also does not work for me…
    Can anyone help me on this? please….

    Thanks…

  16. Anup

    Hi dude. Cool work even i was trying out to the same thing i have installed gnokii and its working fine. Now the problem that i am facing is with smsd.I have created the database and all those required. Problem is that the sms are not going out from outbox. Can u please tell me where exactly we are going wrong.How we have to insert the values into database. Thank u.

  17. Dev

    @ Ryan Upgrade to Gnokii 0.6.27 or .28. It now supports all Non Symbian series40 mobiles. Its not available on the repositories yet, so you’d have download the official tarball and compile from source. Make sure to have all the -dev packages as mentioned in the install-readme file.

    Once compiled you should use:
    port: your bluetooth address in aa:bb:cc:dd: format. Use hcitool to find your mobile’s address.
    connection = bluetooth
    model = series40

    You should then be able to read received messages via Xgnokii or through smsd-mysql, besides sending as well.

    In case your having an issues setting up, add:
    logging to your gnoiiirc conf file and set it debug to on to read errors on the terminal.

  18. Jase

    @ Joko
    The workaround to set the message centre using the connect_script didn’t work too well.
    It set the message centre ok but when gnokii continued the initialisation one of the commands it sends is ATZ which clears the message centre number again.
    Does anyone know of a way to stop gnokii sending the ATZ or maybe getting a script to run after initialisation?

  19. I’m implementing my SMS gateway using Gammu. I’ve got everything from the phone working to the SMSD running. I have LAMP installed. I’ve dished out a PHP script similar to the Thank You script you have here. I’m kind of new to this so wanted to ask you how exactly are you going to run the PHP script every 5 seconds so that it polls the database for new messages? Do we have to start a cron job or probably try and run the PHP script as a daemon? I’m using Ubuntu and I’m a bit confused regarding this part because opening the PHP script in the browser and keeping it running yields no result. Any inputs?

  20. @rahul,
    hi, great to know that you have crossed the major hurdle.. Yes, cron is the way to solve this. Opening the page in the browser is not going to help, since you request for the php page only (when you open the link) and not every 5 seconds. Cron is the best way to do this quick and easy. 🙂

    On a different note, what model phone did you use?

  21. Thanks for the lightening fast reply! 🙂 Thanks a lot because I was having nightmares trying to make a PHP Daemon out of the script, it was just beyond me! If you don’t mind, could you just share the simplest approach to it? Say, the script is sp2hari_is_god.php ( 😉 ), how do I start the cron job for it? Further, would the cron be system dependent or would kill once the terminal is closed?

    I tried several phones during the past few weeks. Older Nokia’s like 3120 seem to work but since my phone was a bit rusty, it would keep losing the connection. I tried Samsung SGH-B520 as well. It recognises the phone over an AT connection but doesn’t provide all functionality since the SMSD can’t communicate with it. The best results I have gotten are from Sony Ericsson phones. I use AT connection and the phone model is C510. “gammu –monitor 1” yields a lot of good information from the device which shows that it’s supported. Further, the SMSD communicates effortlessly with it. So, any normal current generation SE phone is the best bet in my opinion because obtaining older Nokia’s is a pain. Gammu, I believe is essentially a fork of Gnokii and the author maintains a great database of supported phones here : http://wammu.eu/phones/

  22. Just one more thing, I’ve noticed that the SMSD just dies after a while… say 15 minutes. I have to restart it manually from the terminal. Is this normal with what you faced? I’m going to give a presentation in a week on this in college and I wouldn’t want the thing to die on me when I ask the audience to take the system for a spin!

  23. @rahul,
    Yeah, at times, smsd crashes with a segmentation fault. This is at times troublesome.. But the good thing is that when you start smsd when it is already running, it doesn’t start a second instance. So you can have a cron running which starts smsd every 2 or 3 mins..
    Hope that helps..

  24. Hi, set up gnokii and everything. I can send SMS from the terminal, getalarm time and all the gnokii functions. But I am not able to retrive the messages from my phone by the command u mentioned. I use a nokia 3110c phone. Any idea or workarounds?

  25. Pingback: DelicioBot

  26. Pingback: Spider SMS service !! « SAURABH GUPTA'S DIARY

  27. Nicolas

    I’m building my own voting system at the moment. Your post was really helpfull. I still have some practical questions. Can you send me an e-mail please?

    Thanks,
    Nicolas

Leave a Reply to Shoaib Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.