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/
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
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
$ ls -l /dev|grep usb_modem
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
$ 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
# 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/
error sending SMS: No SMSC number given
$ 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
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
Post a Comment