gsmqueue.pl

What

I have used gnokii to send sms-messages with several programs. I found that while gnokii is useful for single messages, some sort of queue needed to be utilized when sending/receiving more messages. If queue is not utilized, serial port lockup could (and will) occur when multiple gnokii-programs try to ccess GSM-phone simultaneously. I found that Juraj Bednar has created nice queue for gnokii. However, I couldn't get it to work on my machine (problems with lock-files mostly) and decided to try on my own.

How

The language I choose was Perl. It's so obious language for small text-processing program like this that it hardly needs any explanation. So I just made a little script that reads queue directory and sends any messages in there. After sending them it copies messages to either "sent" or "fail" folders. I also created small program (sendsms.pl) for actually putting the files to queue directories for easy use from procmail and similar programs.

Basic operation is simple. One script handles outgoing messages (gsmqueue.pl) and another script (getsms.pl) handles incoming scripts. These are run by cron once in a while. In addition, there are couple scripts for ease of use and for controlling various things with GSM.


Workflow of send-SMS process

  1. User makes input by either hand or with
  2. sendsms.pl script. Input should be in format
    	NUMBER: 012987654
    	THIS IS A TEST SMS
    	
    This text should be then copied to /var/gsm/out - directory (sendsms.pl handles this). Multiple numbers should be separated by comma (,).
  3. when ever gsmqueue.pl is ran, it cheks /var/gsm/out for outgoing messages. If there is one or more messages, it sends them to phone by calling Gnokii-software.
  4. If sending is successful, sent message is copied to /var/gsm/sent-directory for easy access at later times. If sending failed, SMS is copied to /var/gsm/fail -directory and e-mail is sent to "fail-address" that's defined in gsmqueue.pl.


Workflow of get-SMS process

  1. Phone gets SMS
  2. getsms.pl is run by cron. It checks if phone has any incoming messages. If it has, it gets them and saves them to /var/gsm/in - directory. It also deletes messages from phone.
  3. gsmcontrol.pl is a script that handles possible SMS-commands to various things like gsm-controlled sauna or similar... So when ever I send message with "SAUNA TILA" in the text to my computer, it responds with temperature readings from outside and from our sauna. This is handled in gsmcontrol.pl that is also run by cron.
  4. If message does not contain valid keyword (defined in gsmcontrol.pl, it stays in /var/gsm/in
  5. If keyword is found, it's handled in appropriate subroutine in gsmcontrol.pl.


Installation

Installation is pretty straightforward.

  1. Make world-writable directories under /var/gsm:
    	total 28
    	drwxrwxrwx    2 henkka     henkka         4096 Dec  3 17:15 fail
    	drwxrwxrwx    2 henkka     henkka         4096 Dec 13 14:06 in
    	drwxrwxrwx    2 henkka     henkka         4096 Dec 13 14:06 in.handled
    	drwxrwxrwx    2 henkka     henkka         4096 Dec 13 14:07 out
    	drwxrwxrwx    2 henkka     henkka        12288 Dec 13 14:07 sent
    	
    mkdir /var/gsm
    mkdir /var/gsm/fail
    mkdir /var/gsm/in
    mkdir /var/gsm/in.handled
    mkdir /var/gsm/out
    mkdir /var/gsm/sent
    chmod -R a+rwx /var/gsm
  2. Copy scripts to some directory. I have used /usr/local/bin
  3. Make sure scripts are executable:
    chmod a+rx /usr/local/bin/sendsms.pl
    chmod a+rx /usr/local/bin/gsmqueue.pl
    chmod a+rx /usr/local/bin/gsmcontrol.pl
    chmod a+rx /usr/local/bin/getsms.pl
  4. Edit crontab with crontab -e (or edit directly /etc/crontab) and put following entries to it:
    	0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/local/bin/getsms.pl 2> /dev/null
    	2,7,13,17,22,27,32,37,42,47,52,57 * * * * /usr/local/bin/gsmqueue.pl
    	* * * * * /usr/local/bin/gsmcontrol.pl 2>/dev/null
    	
    So that means getsms.pl and gsmqueue.pl are run at 5 minute differences between each other. This is because only one script can call gnokii at a time (due the serial-port lockup).
  5. Test your setup by calling sendsms.pl:

    echo "First test SMS"|/usr/local/bin/sendsms.pl 0129876543

    (Of course, replace number with your own number...
    If you look to /var/gsm/out, there should be one message:

    	[henkka@kilpikonna gsmqueue]$ ls /var/gsm/out/
    	sendsms.out.FIOAys
    	[henkka@kilpikonna gsmqueue]$ cat /var/gsm/out/sendsms.out.FIOAys 
    	NUMBER: 0129876543
    	First test SMS
    	

    So that message is eventually read by gsmqueue.pl and sent to you ! After that, you can find message from /var/gsm/sent


Troubleshooting

If something goes wrong, try running programs from commandline. First your should be able to call sendsms.pl with

echo "First test SMS"|/usr/local/bin/sendsms.pl 0129876543

This should copy your message to /var/gsm/out. Then you can try running gsmqueue.pl by hand:

/usr/local/bin/gsmqueue.pl


Procmail-setup for sending sms by e-mail

Edit your ~/.procmailrc and put following line to it:

:0 c
* ^From.*myself
* ^Subject.*sendsms
|formail  -I "" | /usr/local/bin/sendsms.pl

That way, any messages sent from "myself" with subject containing "sendsms" are copied to sendsms.pl script. Messages should be in

NUMBER: 12309987645
TEXT
format.


Downloads


Examples

Simple script to send sms with web-interface. Simply create a file "sms.cgi" with following contents:

#!/usr/bin/perl use CGI qw/:standard/; my $q = new CGI; my $text = $q->param('text'); my $number = $q->param('number'); my $smssend = "/usr/local/bin/sendsms.pl"; system("echo $text|$smssend $number"); print header, start_html('SMS SENDING VIA WEB'); print "Message sent to $number."; print end_html;

Move the file to your cgi-directory (for example something like /var/www/cgi-bin/) and give it eXecute bit (chmod a+rx sms.cgi).
After that, create a web-page that calls that cgi-script. Create a file "sms.html" in your html-directory (for example something like /var/www/html/):
<HTML>
<form action="/cgi-bin/sms.cgi">

Text:<textarea name="text" rows="5" cols="20"></textarea>
Number:<input type="text" name="number"><br>
<input type="submit" value="send">
</form>
</HTML>


After that, you can try surfing "http://localhost/sms.html"...

Copyright

Released under GPL. (c) Henry Palonen, 2001, h@-nospam-.yty.net