SMS Gateway How-To

This how-to will cover how to set up an SMS (Short Message Service) gateway using your own hardware and software. The intended audience is people that are reasonably comfortable with Linux/Unix and some type of scripting language.

Disclaimer: Use these instructions at your own risk. I assume no responsibility for any damage resulting from information contained in this guide.

The following documentation describes how I have set up my own system which has been running for over 6 months and sent tens of thousands of messages. The server has not suffered from any downtime at all.

I have been working with SMS systems for several years now and I have used 3rd party providers and have used both commercial and open source tools. Below documents how to go about building a completely open source system that can be run with relatively inexpensive hardware and free software.

For some people 3rd party solutions may be the best option as some of them offer competitive deals and very sophisticated services. I’m also sure that there are numerous commercial solutions that could offer the same results, I however prefer to use open source. For some individuals, this may not be the best solution, for others, it is the only way to get the flexibility and control they require.

With the following set up you will be able to send and receive SMS messages, one common use is to use your SMS gateway to provide “alerts” from various software applications or relay those generated by email. I’ll also touch on how you can set up a fully functioning 2-way SMS service using PHP (you could just as easily use Perl, Python or shell scripts).

What You’ll Need

  • PC running Linux/Unix
  • Gnokii (http://www.gnokii.org)
  • Mobile phone or GSM Modem and valid SIM card
  • Data Cable (if using a mobile phone)

Warning

A good practice for testing (or even for live systems for that matter!) is to use a “prepaid” SIM card. This helps protect you from running an up an excessive phone bill in case the system malfunctions or it is abused or exploited by a malicious user. I strongly recommend monitoring your server, automated tools can make this job easier.

Installation and Configuration

The first task to complete is building the server and installing Gnokii (note the spelling, it’s not Gnokki). Gnokii is an open source driver for Nokia mobile phones, it does however support many other models of phone. Included in the Gnokii package are some other tools, we’re mainly interested in SENDSMS. It’s also worth mentioning that Gnokii does have a graphical interface called XGnokii, so you’ll need X if you want to run that. Personally for servers, my preference is to run the bare minimum of services. Gnokii (and sendsms) runs quite perfectly from the command line.

Installing and configuring the actual server operating system is beyond the scope of this document, however there are plenty of tutorials to help get you started. Keep in mind that if you are directly connecting this machine to the internet, make sure it’s properly secured!

You can install Gnokii in two ways:

  • Package
  • Source

– Installing from a package
You can install Gnokii using your favourite package manager such as apt-get, rpm, yum or ports. Simply query your package repository for gnokii and install it.

– Installing from source
Installing Gnokii from source is quite straightforward, and I personally prefer installing all my software this way as you can always use the latest release.

tar -xzvf gnokii-1-1-1.tar

cd gnokii

./configure

make

make install

Once Gnokii is installed, it’s time to connect your device. If you’re using a standard mobile phone, plug your data cable (you can pick these up for around $5 US from your local Radio Shack, or Dick Smith) into your serial port. You may see a message like “Data Connection Successful” or “Data Accessory Connected”.

Next you’ll have to edit the configuration file so Gnokii knows what to look for your device, and what device you are using. You may have to consult the Gnokii documentation if you get stuck.

The Gnokii config file must be copies to the home drive of the user running the process.

# This is a sample ~/.gnokiirc file. Copy it into your home directory and name it .gnokiirc.

Once copied launch your favourite text editor, I'm using vi.

vi .gnokiirc

It’s worth reading all the options in the config file, there are plenty of comments to explain things. The only options you must change are described below.

# Set model to the model number of your phone

model = 6210

# Set port to the physical serial port used to connect to your phone.
# Linux version is:
port = /dev/cuaa0

Simply set the above options to the appropriate model and serial port. Now you should be ready to be able to run some test commands to make sure everything is working ok.

A few gnokii commands to get you started are listed below. For a complete listing simply type in gnokii at the command prompt with no options.

gnokii –getnetworkinfo

gnokii –identify

gnokii –getdisplaystatus

gnokii –sendsms destination [–smsc message_center_number |
–smscno message_center_index] [-r] [-C n] [-v n]
[–long n] [-i]

gnokii –netmonitor {reset|off|field|devel|next|nr}

How It Works

Sendsms works by constantly checking an incoming queue. The queue can be in the form of a database, flat file or folder. Using a database is probably the most powerful and flexible method, both MySQL and Postgres are supported. With the database method, all incoming messages are stored in a table called INBOX. Each message has a status that is either 0 (not sent) or 1 (sent).

The February 2004 (Volume 3 Issue 2) issue of PHP Architect actually describes how to do this from start to finish. I highly recommend picking up a copy, you can order back issues (as PDF’s) online.

If all you want is simple email integration, the folder option is easy to set up and you can use tools such as fetchmail and procmail without the need for any additional programming.

2-Way SMS

A very common use for SMS systems is for alerts and notifications, however enabling processing of incoming SMS data is relatively simple. Since Gnokii can also read incoming SMS messages, the only limit is your imagination. You could use a wide variety of tools such as Python, Perl, PHP or shell scripts. If you follow some of the references at the end of this document, you’ll find some examples – many commercial SMS services use these same technologies to drive some very powerful SMS systems.

Further Reading

Gnokii
http://www.gnokii.org

SHARE THIS POST