Sending and receiving an SMS with the Raspberry Pi

Using Gammu

The rights assigned to the device files above govern access to the UMTS hardware. Therefore, you should first make sure that the user account you are using belongs to the dialout group. If in doubt, you should use the command from the last line of Listing 1 to add the account to the corresponding group. Log out and then in again for the changes to take effect. The standard Raspbian user pi already belongs to the dialout group.

You can compile the configuration for Gammu either by individual user in the .gammurc file in the home directory or universally in /etc/gammurc. The output from the gammu-detect command lets you create a rudimentary configuration file in a terminal. Listing 2 shows an example of the corresponding result. As you can see, the software identifies the three modems for the stick. You will have to figure out which modem actually supports all of the SMS services. During testing we determined that it was the modem with the number 2. The first modem, /dev/ttyUSB0, did not support all of the commands. However even if it had done so, this modem should always be reserved for pure data connections.

Listing 2

Output .gammurc

; This configuration file was created with gammu-detect.
; Please read the Gammu manual for additional information.
device = /dev/ttyUSB0
name = Telephone is connected to serial USB interface
connection = at
device = /dev/ttyUSB1
name = Telephone is connected to serial USB interface.
connection = at
device = /dev/ttyUSB2
name = Telephone is connected to serial USB interface.
connection = at

The final configuration file will then look like Listing 3. The data for locking and logging are optional. The first 'locking' optionprevents more than one program from accessing the modem. The logging option assists with finding errors. To do this, the logformat option should be set to textalldate. You should be careful to keep in mind that under certain circumstances, the PIN will also land in the log.

Listing 3

Output gammu.log

device = /dev/ttyUSB2
name = Huawei UMTS USB-Stick
connection = at
use_locking = yes
logfile = gammu.log
logformat = nothing

Figure 2 shows a series of commands for Gammu. They always follow the same pattern. For the first argument, you should use the command, thenthe parameters. Find more information with the help command. It is possible to formulate this command so that it references the name of the relevant section from online support. In the absence of any parameters, it will list suitable chapters.

Figure 2: With just a few commands on the command line you can use Gammu to control communication with the UMTS modem.

The command identify reads out a small amount of information about the modem. You can request current security status for the modem with getsecuritystatus, and set the PIN, or PUK, via entersecuritycode. This last command causes the software to request the PIN via a standard entry so that sensitive information does not end up in the history of the relevant shell. If this were to happen, then a script could be used to read the PIN from a secure file.

The last command in Figure 2, getussd, sends a so-called USSD code, or Unstructured Supplementary Service Data code to the provider. These codes are specific to the provider. You will find them either in your UMTS documents or on the Internet, for example in the pre-paid Wiki [4]. Typically you would request information about the available balance on a voucher via the USSD, or add credit from a prepaid card.

Not every stick supports the entry of USSD codes in plain text. Some devices need the codes in so-called PDU format, in other words in the transport format. The info box entitled "Sending USSD Commands in PDU Format" describes how to deal with this.

Sending USSD Commands in PDU Format

PDU (Protocol Data Unit) format is an alternative way to encode SMS messages. Unlike standard text format, PDU format is not immediately human readable. This will not make a difference with computer supported processing. However, some mobile phones and UMTS sticks only support the PDU format for USSD codes. In this case, you will need to select from one of two choices. The first option consists of using an online PDU converter [7] where you select Clear all and then enter the code in the Text field. Then you can press Convert and read the results in the USSD Entry/Display field (Figure 3). The second option comes in the form of a small Python script.The Python interface in Gammu makes it possible to code and decode in PDU format.

Some USSD codes are interactive. They have a menu for making selections. Scripting is necessary here. You should either use Python API or you should move on to a corresponding Perl script [5]

Figure 3: The online PDU converter makes it possible to conveniently and easily create PDU codes. Alternatively, you can integrate the corresponding script from the Python interface for Gammu.

Sending and Receiving

You can test out the interaction between the UMTS stick and the provider network by using the commands from Figure 2. This will help you to determine which of the integrated modems actually function. No fee is charged for these tests. You can send the first test SMS with the command from line 1 of Listing 4.

Listing 4

Output of gammu getsms

$ gammu sendsms TEXT Number -text "Greetings from the Raspberry Pi to the telephone number Number"
$ gammu getallsms
$ gammu getsms 0 Number

If you leave out the -text option then the program reads the message text from the standard input. In addition to messages consisting of pure text, the software supports all available variations like MMS and Unicode text. Accordingly, there are a multitude of options. Detailed information about these options is found in Manpages.

The stick can receive an SMS all on its own as long as it is logged onto the network. However, the Raspberry Pi needs to be switched on to read an SMS. The command from the second line of Listing 4 takes care of this for all SMS messages. Theoretically, it is possible to call specific messages by using the command from the third line where Number stands for memory on the telephone or on the SIM card. However, discovering the numbers in advance poses a practical obstacle. Therefore, this option does not actually work. The first command is also only useful under certain conditions since it always issues all of the available SMS messages. This means that if you don't constantly delete messages that have been received, you will always be confronted with old ones.

In light of this, the Python interface is far more suitable for processing SMS. Listing 5 shows a minimal script. After initializing the Gammu environment it loops through all of the messages and then reads out only those SMS that do not show an "already read" status. As part of this process, the script assumes that only text messages are present.

Listing 5

01 #!/usr/bin/python
02 import gammu
04 # Gammu initialization
05 sm = gammu.StateMachine()
06 sm.ReadConfig()
07 sm.Init()
09 # SMS request
10 status = sm.GetSMSStatus()
11 count = status[,SIMUsed'] + status[,PhoneUsed'] + status[,TemplatesUsed']
12 if count == 0:
13   exit
15 sms = sm.GetNextSMS(0,True)
16 while True:
17   if sms[0][,State'] <> ,Read':
18     print ,-----------------------------------------------------------'
19     print ,Number:   ,, sms[0][,Number']
20     print ,Location: ,, sms[0][,Location']
21     print ,State:    ,, sms[0][,State']
22     print ,Text:     ,, sms[0][,Text']
23     print
24   count -= 1
25   if count > 0:
26     sms = sm.GetNextSMS(0,Location=sms[0][,Location'])
27   else:
28     break

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