Sending and receiving an SMS with the Raspberry Pi

Daniel Villeneuve,

Gabbin’ with Gammu

In today’s world, SMS seems like an outdated and expensive technology. Nonetheless, there are certain situations in which this technology acts as the go-to choice. The Raspberry Pi can easily use SMS by harnessing its onboard capabilities.

The Short Message Service or SMS as it is known, is one of the most robust technologies in the area of mobile communications. The service has many limitations including restrictions on the length of transmitted messages and asynchronous processing. In spite of such limitations, or rather precisely because of them, SMS continues to be bread and butterfor providers. There is good reason for this since the technology does not rely on the Internet, a channel of communication which is becoming ever more vulnerable.

With respect to the Raspberry Pi, there are a number of different areas of application which utilize SMS. For example, it is usually still possible to register a measurement module via SMS on a 2G network and send status updates even when there is no UMTS reception. Also, the mini-PC can warn of outages on the network. Likewise, the receipt of an SMS can be used to trigger a specific action.

In another scenario, SMS constitutes an independent channel for a so-called two factor authentification. For this process, the Raspberry Pi uses SMS to send a one-time valid password to a previously provided telephone number. The one time password can then be used together with the user name and a regular password for more secure login on sites containing sensitive information.

As described in a previous issue of Raspberry Pi Geek, it is also very easy to take a UMTS stick (Figure 1) and transform a Raspberry Pi into a 3G router [1]. Unfortunately, you often have to use SMS in order to check your available balance and data volume usage. The Pi can assume this task for you so you don't have take the card out of the stick and insert it into a mobile phone.

Figure 1: A cheap UMTS stick equips your Pi with cellular capabilities.

Below, we provide a brief description of how to install the hardware. Then we show how to use the Gammu service "Gammu All Mobile Management Utilities" for sending and receiving SMS messages via the command line. In addition, Gammu can be configured as a fully functioning SMS server without much effort. Users then have complete flexibility when it comes to the processing of ingoing and outgoing messages.

Minimal Hardware Requirements

All you will need for this project is one of the more commonly used UMTS sticks and a Raspberry Pi B+, Raspberry Pi 2 or higher. A first generation Pi requires a powered USB hub.

You will need two software packages (Listing 1 and 2) to operate the hardware. Once these packages are installed, the system should automatically recognize the UMTS stick when inserted. You can see if the stick has been recognised by checking for files ranging from /dev/ttyUSB0 to /dev/ttyUSB2 in the /dev/ directory. The exact number of corresponding files depends on the type of UMTS stick you are using. Typically, you will see three.

Listing 1


01 $ sudo apt-get update
02 $ sudo apt-get install usb-modeswitch usb-modeswitch-data
03 $ sudo apt-get install gammu python-gammu
04 $ sudo adduser Benutzername dialout

If none of the files in question exist, your stick may not be supported. However it is more likely that the Raspbian system is having problems. Try re-inserting the stick. If that does not work, your next step should be to run the command sudo udevadm trigger from the console. If this resolves the issue, and you wish toboot up the computer with the stick already inserted each time then you should also enter this command in the /etc/rc.local file.

If neither of these tricks has any effect, then you should read out the stick ID from the /var/log/messages log file and search for it on Google. There is a good chance that the system will automatically recognize the UMTS stick unless you are using a very new model..

Simple Installation

You also don't need to go through all kinds of gyrations during installation in order to actually be able to send and receive SMS messages. The command from the third line in Listing 1 takes care of this. You only need the Python package if you want to access the SMS functions.

The documentation process has its own package namedgammu-doc. However, there are a number other packages that accompany gammu-doc. 'Manpages' should provude sufficient documentation when you install the gammu package. Therefore it is a good idea to save yourself the space.

At first glance, Gammu [2] appears slightly antiquated. The software dates back to when the first intelligent mobile phones from Nokia and other makers first came out. As a result, the name "Nokia" still appears in 50 places in the Manpages. However first impressions can be deceptive. Work on this program has actively been pushed forward. Gammu has therefore kept pace with the times even if a number of the functions come across as old-fashioned in the current age of Android and iOS.

If you do Internet research on the topic of SMS transmissions under Linux, you may discover a number of additional packages. As is often the case with free software, there is more than one way to achieve the goal you have in mind. Specifically, we should mention here the SMS server tools that are available in the smstools package [3]. However if you don't have any need for a dedicated SMS server, then the leaner Gammu does a better job of meeting the requirements and also operates as a decent server.

However under certain circumstances, it pays to take a look at some of the other packages. For example, in addition to the Gammu server module, there is also Wammu, a simple graphical interface, and Libgammu which is a C library plus the Python interface referred to above. With respect to the latter, nothing prevents you from integrating your own scripts and applications. Nonetheless, we will be focusing in this article on the native, command line Gammu application since this is the best way to show basic principles. We address Python only in passing.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content