Skip to main content

SMS messaging on Raspberry Pi with Gammu

Background

Ever wanted to be alerted when the power fails at home? I typically worry that food in the freezer will go off when I am on vacation! (Yep, that's how I tick).

I have recently installed a UPS system which I use to protect various IT and network devices from instant power loss, enabling them to shutdown gracefully. This is particulary important for my Raspberry Pi (Pi) devices, which are sensitive to this issue, as SD-Cards can be corrupted and then not re-boot. 

The UPS I have installed interfaces via USB with my Raspberry Pi server using the Linux Network UPS tools NUT so that I can monitor status changes (e.g. grid power loss) and this is further integrated into my openHAB-based home automation system. This setup will be covered in another post. 

I wanted to additionally be alerted when grid power fails via SMS to my mobile phone. To do this I have purchased a pre-owned USB Internet Surf Stick and a pay-as-you-go SIM card (zero cost) which I can load with credit. I am only using the card for sending SMS messages, so the costs are really low (9 ct per SMS) and I am not using it for internet network connection.


Installing the USB Surf Stick

Installing the USB Surf Stick is easy. Plug the Stick into a spare USB port (I use a USB extension cable as there wasn't much space left at the back of my Pi). Depending on your USB port power usage you may need to use an external USB hub.

Next we need to check that the USB Stick is recognised:

$ lsusb

My Pi shows the following:




The Stick is an HSDPA Modem from Huawei (Vodaphone branded). Note the Ids for the device (12d1:1001), we will use this later to set a udev rule. 

We now need to find out which serial ports are available for the Stick. Use the following command:

$ ls -la /dev/serial/by-id/
My Pi shows the following ports for the Huawei Stick and we can see that it offers three serial ports, ttyUSB[0,1,2]:




We will create a udev rule to map these ports to a symlink device we can reference. We do this as sometimes the USB ports map to a different number on reboot or removing the device and reinserting, and the Gammu software configuration may fail not recognising the device.

$ sudo nano /etc/udev/rules.d/99-usb-serial.rules 
Copy the following line into the file (it may be a new file if no udev rules have been created for usb devices already):

SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1001", SYMLINK+="usb_modem", GROUP="users"

Save the file and exit. To reload udev rules and ensure the new symlink is recognised type the following:

$  sudo udevadm control --reload-rules && sudo udevadm trigger 
The USB tty ports are now mapped to the port "usb_modem", which we will use for Gammu. You can check this as follows:
$ ls -l /dev|grep usb_modem 
The following should be displayed:

lrwxrwxrwx 1 root root           7 Feb 22 17:41 usb_modem -> ttyUSB2

Where the ttyUSB device may have a number 0-4.


Installing the SMS Software

First we need to remove the installed OS modemmanager, as it conflicts with the SMS software:
$ sudo apt-get remove modemmanager

Next we need to remove some dependency components left behind:

$ sudo apt-get autoremove

To send SMS messages from the Surf Stick I used Gammu and the the gammu-smsd tools (a component of Gammu) which can be installed as packages from the Pi command line:

$ sudo apt-get install gammu gammu-smsd

Once installed we need stop the software daemon:

$ sudo systemctl stop gammu-smsd

And then configure the software:

$ sudo nano /etc/gammu-smsdrc
The following changes should be made:
# Configuration file for Gammu SMS Daemon
# Gammu library configuration, see gammurc(5)
[gammu]
# Please configure this!
port = /dev/usb_modem <<<< the symlink port we created using udev
connection = at
# Debugging
logformat = textalldate
# SMSD configuration, see gammu-smsdrc(5)
[smsd]
pin = 0000 <<<<<<< replace with your SIM pin here >>>>>>>
smsc = +491234567  <<< replace with your sim phone number here >>> 
service = files
#logfile = syslog
logfile = /var/log/gammu-smsd
# Increase for debugging information
debuglevel = 1 <<<< increase to 3 or 4 to get more info if things ain't working
ReceiveFrequency = 300
# 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/
Note: the option "smsc" in section [smsd] may not need to be set. I had an issue that sms messages were not sent sue to unknown smsc (originating number) not found by the daemon. Check the file /var/log/gammu-smsd to check. If there is an issue the following error message will be displayed:

error sending SMS: No SMSC number given

Save the file and restart the SMS daemon: 
$ sudo /etc/init.d/gammu-smsd restart

You can check the log file in the directory /var/log/gammu-smsd to see if there are any issues.

I typically open a new ssh session and use the tail command to follow the log entries: 

$ tail -f /var/log/gammu-smsd


Note that an SMS message in the queue was sent using the device /dev/usb_modem.


Sending a test SMS

To send an SMS message use the following command from the command line:

echo "This is a Test SMS, sent from my Raspberry Pi" | sudo gammu-smsd-inject TEXT "<number>"

Note: replace <number> above with the destination mobile phone number you are sending the message to in the format +<country code><telephone number>, e.g. +491738546734 (German mobile number).


Setting up a template SMS message

So the next step is to setup a template message, which I can use to send various messages to one or more mobile phone numbers. The template message could be the example we used before, by copying the echo command above and pasting it as a shell script file (e.g. sendsms.sh) in your favourite text editor and then setting it to be executable using:

$ sudo chmod +x /usr/local/bin/sendsms.sh
This can then be executed as required.

However I want to parametrize this based on rules set in my openHAB system so that I can pass an appropriate message to the template. As shell scripts can be passed parameters with a space between the initial command and the parameters ($1, $2 ...) space delimited I can use a .sh message template as follows (note I am only using one parameter but you could pass further parameters e.g. for different phone number destinations):

echo "$@" | sudo gammu-smsd-inject TEXT "+49...."

Note: the use of "$@" in passing the sms text to gammu-smsd-inject ensures that spaces between the words of the text are preserved (took me a while to find this one out).

Now I can use the EXEC binding in openHAB to execute this script and pass the text parameter to it based on rules, but this will be covered in a further blog.

Note: I had a hard time trying to work out how to use gammu-smsd-inject without SUDO. 

gammu-smsd-inject[5088]: Cannot save OUTC20220220_180953_00_+49xxxxxx_sms99.smsbackup. No available file names
Failed to inject message: Can not open specified file

I finally tracked this down to an issue with the ownership of the files created when the sms message is created for the server to process. The following will solve the issue (based on file locations in the /etc/gammu-smsdrc file):

sudo chown -R gammu:gammu /var/spool/gammu 

   


  

Comments

Popular posts from this blog

Setting up NUT on a Raspberry Pi for Greencell USB UPS

  Background This article provides a guide for installing a USB UPS attached to a Raspberry Pi. In my case I had purchased a GreenCell 600 360W USB UPS. Integrating the USB UPS requires using NUT (Network UPS Tools). This UPS uses the blazer_usb driver. Installation NUT Plug-In the UPS USB-Connection to the host. My device showed up as Device 006 below: # lsusb   Bus 001 Device 006: ID 0001:0000 Fry's Electronics MEC0003 Bus 001 Device 005: ID 0bda:0309 Realtek Semiconductor Corp. USB3.0-CRW Bus 001 Device 004: ID 0658:0200 Sigma Designs, Inc. Aeotec Z-Stick Gen5 (ZW090) - UZB Bus 001 Device 003: ID 0424:ec00 Microchip Technology, Inc. (formerly SMSC) SMSC9512/9514 Fast Ethernet Adapter Bus 001 Device 002: ID 0424:9514 Microchip Technology, Inc. (formerly SMSC) SMC9514 Hub Install the NUT package with the apt package manager:   sudo apt-get install nut Make some configurations and changes in the nut directory /etc/nut: # cd /etc/nut drwxr-xr-x 2  root nut  4096   Jan 11 00:49 .

Sending SMS Messages from openHAB

Background So I setup sending SMS messages from my Raspberry Pi (see article I wrote here ) and have a bash script to automate this. The next step is to integrate this into my openHAB server, so I can send SMS messages to my mobile phone when my UPS system (managed by openHAB) is on battery power (e.g. due to a mains network failure). Installing the EXEC Binding to openHAB See here . Note especially the requirement to add commands to the whitelist. Adding a Thing to openHAB Once the EXEC binding is installed the next step is to setup a thing file to use the binding for sending bash shell commands:  $ nano /etc/openhab/things/exec.things Add the following line: Thing exec:command:sendsms [ command="/usr/local/bin/sendsms.sh %2$s", autorun=true, interval=0 ] Save the file. As you can see this thing is used to pass the command to run the bash shell script to send an SMS and pass the text sent as "%2$s" to the script. Adding Items to openHAB Two items are required, one