Introduction
We'll be using a spare PC, free open source software and an old Nokia handset to create a gateway for sending and receiving SMS text messages. If you have some basic knowledge of installing Linux (I'll be concentrating on the Debian distribution) and MySQL, then this should be a relatively trivial procedure.
[edit]
Ingredients
This is what I used, you may alter according to taste, however some of my instructions are specific to Debian...
[edit]
Server
You're going to need a computer to run this on, preferably one running the latest stable release of Debian (3.1 at the time of writing) as most of the instructions here will be Debian specific.
This machine doesn't have to be dedicated to being an SMS gateway, nor does it even have to be very powerful. A low-end pentium should suffice. It will, however, require a serial port.
[edit]
Cellphone Interface
We're going to be using SMSD from the Gnokii project so you can use pretty much any GSM interface they support, I personally use a Nokia 5110 or 6110 series handset with a Nokia DAU-9P serial cable. A list of devices supported by Gnokii is available here.
You can usually find these items on EBay, the serial cables are better known as the Nokia Data Suite. If you want to try making your own cable, there are plans here and the Gnokii site has a more technical information about the cables here.
Beware of 3rd party no-name cables, they may not work as expected.
[edit]
Getting Started
I'm going to assume you have a basic Debian installation already as installing Debian is far beyond the scope of this little document. If you've chosen to use another Linux distribution then there are probably packages available for gnokii-smsd.
[edit]
Installing and configuring
Running the following should install all the required packages and dependencies we'll require for our basic SMS gateway:
aptitude install gnokii-smsd-mysql mysql-server
Once those are installed we'll login to MySQL (note: no MySQL root password has been set yet, see the security section below):
mysql -u root
Create a database (we'll call ours 'smsgw'):
create database smsgw;
Quit the MySQL client, and run the following to populate the smsgw database with the requisite tables and fields:
mysql -u root smsgw < /usr/share/doc/gnokii-smsd-mysql/sms.tables.mysql.sql
Now edit /etc/gnokiirc and change the port and model settings to match your setup.
Now connect the cellphone to the PC using the Nokia cable and run the following:
/usr/sbin/smsd -u root -d smsgw -c localhost -m mysql
This should work, however at the time of writing, there exists an issue with libmysql.so being in the wrong place. If you get an error saying:
dlopen error: /usr/lib/smsd/libmysql.so: cannot open shared object file: no such file or directory!
Creating the following symlink should fix it:
ln -s /usr/share/smsd /usr/lib/smsd
Now hopefully you are running smsd (which won't output anything to the screen)... it's time to test :)
Send a text message to the handset connected to the SMS gateway... you should see the SMS notification display on the handset's screen quickly before disappearing as smsd reads and deletes it.
Hit CTRL-C to stop smsd, log back into MySQL (mysql -u root smsgw) and run select * from inbox;, you should see something similar to the following:
+----+---------------+---------------------+----------------+-------------+-------+-----------+
| id | number | smsdate | insertdate | text | phone | processed |
+----+---------------+---------------------+----------------+-------------+-------+-----------+
| 1 | +447970123555 | 2005-11-20 16:03:15 | 20051120150343 | Testing 123 | NULL | 0 |
+----+---------------+---------------------+----------------+-------------+-------+-----------+
1 row in set (0.00 sec)
And that is pretty much it... you now have a functional SMS Gateway (I did say it was easy!). I'd suggest wrapping the smsd application in a script to ensure it runs smoothly. There are some pitfalls you should be aware of that are outlined below and please read the security considerations before deploying a home-made SMS gateway.
All that remains is to write an application to take the messages out of the database and do whatever it was you wanted to do with them, I suggest that if you intend to have multiple applications reading the messages that you write a single "SMS processor" to read the messages from the database and hand them off to individual applications.
[edit]
Sending messages
Simply insert your message into the outbox table and SMSD will take care of the sending. While this is fine for one or two status messages or for things like system monitoring it should be avoided for sending bulk messages as there are far cheaper options available, see "For fun, not profit" below.
[edit]
Pitfalls and oddities
The following are issues I've run into that you should probably be aware of...
[edit]
Duplicates
From time to time SMSD likes to put duplicate messages in the database. You might want to write some duplicate checking into your SMS processor to mark duplicates as such or even remove them from the database.
[edit]
libmysql.so
As mentioned above, there is at the time of writing an issue with libmysql.so being in the wrong place. If you get an error saying:
dlopen error: /usr/lib/smsd/libmysql.so: cannot open shared object file: no such file or directory!
Create a symlink as folows:
ln -s /usr/share/smsd /usr/lib/smsd
This will probably be fixed by the Debian maintainer for gnokii-smsd-mysql in future versions.
[edit]
Security
[edit]
Basic security
You'll notice that, if you followed my instructions above, SMSD runs and connects to the database as root... this is generally considered a Bad Thing. Create an SMS user on both the system and MySQL and use that instead. While you are adding an SMS user to MySQL you will probably want to create a password for the root user.
If this machine is to be connected to the Internet or any publicly accessible network, please look into firewalling it else you may find one day that someone has accessed your machine, and not only stolen any user data, but also used your SMS Gateway to send messages that you'll end up paying for.
[edit]
Consider your users' privacy
It's all very well using this for your own personal use but as soon as you start offering services for other users you instantly have an obligation to those users to protect the details you gather from them. Primarily this is their phone number but in certain circumstances (such as my own SMS_To_LJ service) you may be exposed to other sensitive information.
Ask yourself, do you really need to keep every message? If the answer is yes, do you need to store more than the last few digits of their phone number? If you handle sensitive information inside of the messages, can you obfuscate it once you no longer need it?
[edit]
For fun, not profit
While this project is perfect for home use or small projects it really shouldn't be relied on for business use, it has absolutely no built-in redundancy and can, at times, be quite unstable. Not only this, but sending messages using a regular consumer cellphone account can be quite expensive.
If you need a gateway with guaranteed uptime or intend to send a lot of text messages you'll probably find you are better off talking to an SMS aggregator, these are large companies with direct connections to the major cellphone networks and can offer big discounts on bulk messages.
[edit]
...but I want to make money!
One of the limitations of this gateway that seriously hampers it's use in any commercial money-making application is that, due to being based on consumer equipment/services, it is completely incapable of providing premium rate SMS services.
[edit]
Uses for an SMS gateway
I started this document as an extension to a few discussions I had with people at the MobileActive conference about how easy it is to setup a simple SMS gateway for use in activist campaigns. The uses for this kind of gateway are pretty much left to your imagination from home automation (hook this into an X10 module and turn things on/off from a text message) and security (hook it into your alarm system) to providing access to existing web-based services... let me know if you used this document to create something interesting :)
[edit]
About the author
Dan Lane is Chief Technical Officer of Howler Technologies Ltd based in London, in his spare time he messes around with disruptive communications technology and a number of other geeky projects. He can be found on the web at http://invalid.name or e-mailed at null@invalid.name (yes, really).
Dan runs the free SMS To LiveJournal gateway and was technical architect for the ARC project.

This comment has been removed by the author.
ReplyDelete