Sending and receiving an SMS with the Raspberry Pi

SMSD in action

If you would like to use SMS to run specific commands on the Raspberry Pi, then self-made solutions will not suffice. Instead you should use a Linux "daemon".

Gammu has a server that can be configured to search at intervals via the UMTS stick for newly received SMS messages, then read them out and also initiate other actions. It is very easy to install and configure the server. Begin by running the command:.

$ sudo apt-get install gammu-smsd

SMSD expects to find its configuration information in the /etc/gammu-smsdrc file. As long as this does not exist, the server will not start under Raspbian. The file also exists in Windows INI format. An additional section is needed for the [gammu] section like in Listing 3. Listing 6 shows an example.

Listing 6

smsdrc

01 # Configuration file for Gammu SMS Daemon
02
03 # Gammu library configuration, see gammurc(5)
04 [gammu]
05 device = /dev/ttyUSB2
06 name = Huawei UMTS USB-Stick
07 connection = at
08 use_locking = yes
09 logformat = nothing
10
11 # SMSD configuration, see gammu-smsdrc(5)
12 [smsd]
13 service = files
14 logfile = syslog
15 PIN = 1234
16
17 # Increase for debugging information
18 debuglevel = 0
19
20 # Paths where messages are stored
21 inboxpath = /var/spool/gammu/inbox/
22 outboxpath = /var/spool/gammu/outbox/
23 sentsmspath = /var/spool/gammu/sent/
24 errorsmspath = /var/spool/gammu/error/
25
26 # process incoming messages
27 RunOnReceive = /usr/local/bin/smsin.sh

The daemon supports various backends for storing messages. The simplest among these is the file system. Alternative backends for more complex demands include database systems like MySQL, SQLite and databases with generic ODBC drivers. The file system suffices for our example. Listing 6 configures the system via the key service and also via the various paths for outgoing and incoming SMS messages (Lines 13 and 20 to 23). The configuration presented here only handles the bare necessities. The manual describes many options for modifying the properties as desired.

The CommTimeout key is especially noteworthy. If it does not find any outgoing messages in the outbox, then the daemon will take a break for the interval that has been specified before it looks again for new ones.. The default interval value is 30 seconds. The inspection occurs automatically after each SMS transmission.

SMS Transmission

In order to prevent Gammu and Gammu SMSD from getting tangled up during a transmission, you should not use Gammu to transmit the SMS by yourself when there is an active server. Instead you should forward the SMS to the server. Theoretically it's you could move a correctly formatted message to the outbox directory. For the sake of convenience however, you should let a support program do the messaging formatting.

$ gammu-smsd-inject TEXT Number -text "Greetings from the Raspberry Pi to the telephone number Number"

This variation also has the advantage that it works independently of the backend. However, transmitting USSD queries in this way will still fail. These queries are potentially interactive and therefore do not fit into the scheme of the server that is working away in the background.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content