Receive SMS on a Raspberry Pi

Acting upon Received SMS Messages

Creating our SMS Script

Before we install any more software, let’s create a script that will run each time a new SMS message is received by your Raspberry Pi containing a particular word or phrase. I would recommend creating a folder called sms inside your ~/bin/ folder and putting all of your scripts in that folder, it keeps everything together that way and things don’t get lost. If the ~/bin/ doesn’t exist just create it with mkdir ~/bin/ and you’re golden.

Creating the Action Script

Just to demonstrate what is possible we’re going to play a little game of slap and tickle with our Raspberry Pi, to do this we need to create a script called slap-tickle.sh in your sms folder with the command:

Creating Slap & Tickle Script

touch ~/bin/sms/slap-tickle.sh

To make sure your script can be executed as a little program rather than your Raspberry Pi treating it as a simple text file, we need to make it executable with this command:

Make the Script Executable

chmod u+rwx ~/bin/sms/slap-tickle.sh

This command assumes you’ve taken my advice in created the sms folder and used the same file name I did.

You can make sure your new script is executable by looking at its permissions with:

ls -l ~/bin/sms/slap-tickle.sh

The response you get back in your Terminal will contain the same elements as the example below, as you can see it has the name of the file at the end of the line as well as the date, time and the current user and group that own our new script. The important bit we are looking for is right at the beginning of the line, there should be the letter “x” (meaning executable) somewhere near the beginning of the line like this: -rwxr--r--

-rwxrw-r-- 1 user user 0 Jan 27 15:18 /home/user/bin/sms/slap-tickle.sh

Don’t worry if you don’t understand the whole permissions thing, I don’t think anybody does really. I did however find this website enormously helpful in understanding permissions.

Editing the Slap & Tickle Script

nano ~/bin/sms/slap-tickle.sh

This will open up a blank text editor in your Terminal, into which you are going to paste the following script. I will need to take note of a giant pitfall that I fell into and it was only crying fits and shouting that got me out of it. I don’t know the specifics but apparently Gammu can not send and receive on the same device or something like that, anyway you need to use a tool which comes bundled with everything was installed called gammu-smsd-inject, which worked beautifully for me.

Everyone’s got a much better explanation as to why this works please let me know!

Slap & Tickle Script

#!/bin/sh

from=$SMS_1_NUMBER
message=$SMS_1_TEXT
reply=""

if [ "$message" = "Slap" ] ; then
    reply="Tickle!"
else
    reply="What, you don't like tickles?"
fi

echo "$reply" | gammu-smsd-inject TEXT "$from"

That done, exit the Nano text editor with the key combination Control X, then press the letter Y and finally press Enter.

Installing Gammu-SMSD

Now let’s install the software that will run the script we just created, that software is called gammu-smsd. We install the software with:

Installing gammu-smsd

sudo apt install gammu-smsd

Configuring Gammu-smsd

We need to tell gammu-smsd where our script is located on our machine, so let’s update the configuration file with:

Updating gammu-smsd Configuration File

sudo nano /etc/gammu-smsdrc

After the file has opened either modify or create the line RunOnReceive = PATH_TO_SCRIPT in the configuration file under the [smsd] section, this is what my file ends up looking like:

Path to Script

# Gammu library configuration, see gammurc(5)
[gammu]
# Please configure this!
port = /dev/ttyUSB0
connection = at19200
# Debugging
#logformat = textall

# SMSD configuration, see gammu-smsdrc(5)
[smsd]
RunOnReceive = PATH_TO_SCRIPT
service = files
logfile = syslog
# Increase for debugging information
debuglevel = 0

# Paths where messages are stored
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/

Where PATH_TO_SCRIPT is the slap and tickle script we’ve just created and you should also make sure that the [gammu] section is filled in correctly with the right Port information; which in my case is /dev/ttyUSB0.

Once that’s all done, you can temporarily start the gammu-smsd program with this command, just so you can test sending and receiving the script above.

Temporarily Starting gammu-smsd

sudo gammu-smsd

You won’t get returned back to your command prompt when you run the above command, this is because the command is running in the foreground. We are going to use a process called Systemd to cause gammu-smsd to start on every reboot through the use of its Unit Files.

But before we do that we need to make sure everything is set up properly. We do that by sending a text message to the phone number connected to your Raspberry Pi containing the word "Slap" (without the quotes) and within a couple of minutes you should get the word "tickle" back to your mobile phone. If you don’t, then go back through the tutorial to see if you’ve made any mistakes and if that doesn’t work, cry.

Starting Gammu-SMSd Automatically

Now that that’s all hopefully working we need to make sure that gammu-smsd starts each time we reboot, because if we didn’t create a file to automatically start gammu-smsd on boot, we would have to start it manually. And that would be rubbish so we’re going to need a Systemd Unit File which I mentioned earlier.

We used to have to create this file ourselves when using Gammu but that’s not necessarily the case anymore, so before we go creating things we should check if we have a file already with this command:

ls /lib/systemd/system | grep gammu

And if you are ready have a SystemD Unit File you will get a response that looks like this:

gammu-smsd.service

If you do see the above response you can move on to the Enabling gammu-smsd.service, if you don’t see the above then just move to the next section.

Creating a Systemd Unit File

Make sure Gammu-SMSd Is Not Running

To create the Unit File for systemd we need to first stop the gammu-smsd daemon we started earlier. To do this, use the key combination Control C to stop the Daemon and be dropped back to a normal command prompt in your Terminal.

Systemd Unit File

We will use the following commands to start the Daemon at boot:

cd /lib/systemd/system

Create and open a new config file with your favourite text editor with the command:

Create the new Unit File

sudo nano gammu-smsd.service

Paste in the following code:

Unit File Code


[Unit]
Description=SMS daemon for Gammu
Documentation=man:gammu-smsd(1)
After=mysql.service postgresql.service

[Service]
EnvironmentFile=-/etc/sysconfig/gammu-smsd
# Run daemon as root user
ExecStart=/usr/bin/gammu-smsd --pid=/var/run/gammu-smsd.pid --daemon
# Run daemon as non-root user (set user/group in /etc/sysconfig/gammu-smsd)
#ExecStart=/usr/bin/gammu-smsd --user=${GAMMU_USER} --group=${GAMMU_GROUP} --pid=/var/run/gammu-smsd.pid --daemon
ExecReload=/bin/kill -HUP $MAINPID
ExecStopPost=/bin/rm -f /var/run/gammu-smsd.pid
Type=forking
PIDFile=/var/run/gammu-smsd.pid

[Install]
WantedBy=multi-user.target

Writing these files is not my area of expertise but in my testing the above service worked, obviously your mileage may vary. Save and close the file as above, which will drop you back into your normal Terminal prompt.

Enabling gammu-smsd.service

The following tells our Raspberry Pi to use our Unit File to start gammu-smsdon each boot:

Reload System Daemons

sudo systemctl --system daemon-reload

Enable our Unit File

sudo systemctl enable gammu-smsd.service

You can also start the start SMS Daemon immediately with:

Start gammu-smsd Immediately

sudo systemctl start gammu-smsd.service

Now let’s check the status of gammu-smsd with:

Checking the Status of gammu-smsd

sudo systemctl status gammu-smsd.service | grep running

And if everything is okay you should see a line that look something like this:

Active: active (running) since Mon 2019-01-28 12:47:38 GMT; 1h 26min ago

All Done!

And that’s it, you’ve now got everything you need installed to send and receive SMS messages from your Raspberry Pi. Obviously the example I’ve given above is extremely simple, but because it’s inside a Shell Script you can use any other application process that can trigger Shell Scripts to run gammu and thereby send SMS messages. For instance I use the very very awesome HomeAssistant to send one of my PAs a message when my iPhone battery gets below 20%, awesome sauce indeed.

If you get stuck, just drop me a line and I’ll be happy to help. I’m @robotsandcakes On Twitter. And all my other details are up on keybase/robotsandcake.