It’s useful to have an SMS gateway for openHAB in case the home internet goes down, or you’re travelling somewhere with mobile phone reception but no internet. Aim is to have a system which can provide SMS notifications and respond to SMS requests.
Steps (tested on Ubuntu 14.04):
-
This is the most difficult bit. Buy the right kind of USB modem. Important you get an old one which behaves as a modem from the get go, e.g. Huawei E169. The newer ones all include flash memory with drivers on it, and it’s difficult to switch them into modem mode on Ubuntu 14 (there’s a utility called usb_modeswitch but me and many others have found it won’t work with newer versions of Ubuntu). I wasted hours on the newer models, and strongly recommend you go to ebay or somewhere and pick up an E169.
-
Of course get a suitable SIM card, e.g. giffgaff in the UK.
-
Install the gammu and gammu daemon SMS software:
sudo apt-get install gammu gammu-smsd
if your Ubuntu is using ModemManager then uninstall it to avoid conflicts:
sudo apt-get uninstall modemmanager
- Plug in the modem and lsub. You should see a line like:
Bus 002 Device 008: ID 12d1:1001 Huawei Technologies Co., Ltd. E169/E620/E800 HSDPA Modem
- Create a symlink so that gammu always finds your modem, regardless of which USB port it’s plugged into.
create/edit a udev file - /etc/udev/rules.d/99-usb-serial.rules - to add the following line:
SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1001", SYMLINK+="usb_modem", GROUP="users"
(of course using the vendor and product ids you saw in lsusb)
- edit the gammu config file /etc/gammu-smsdrc to look like this :
# Configuration file for Gammu SMS Daemon
# Gammu library configuration, see gammurc(5)
[gammu]
# Please configure this!
port = /dev/usb_modem
connection = at115200
# Debugging
logformat = textall
logfile = /var/log/gammu.log
# SMSD configuration, see gammu-smsdrc(5)
[smsd]
PIN=9999
service = files
logfile = /var/log/gammu-smsd.log
RunOnReceive = /etc/openhab2/utils/receivesms.sh
# Increase for debugging information
debuglevel = 1
# 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/
- Test this - make sure the daemon isn’t running and check the status:
sudo service gammu-smsd stop
sudo gammu identify
You should see something like:
Device : /dev/usb_modem
Manufacturer : Huawei
Model : E169G (E169G)
Firmware : 11.314.10.51.156
IMEI : [number]
SIM IMSI : [number]
- Start the gammu daemon
sudo service gammu-smsd start
It should restart automatically when the system boots
- Check you can send SMSs:
gammu-smsd-inject TEXT 0044999999999 -text "Test message"
(note you will need to provide the 00, then the country code, then the number)
-
Assuming that works, you can now create the
/etc/openhab2/utils/receivesms.sh
script that will pass incoming SMSs to openhab. To make life simple, my script converts all incoming messages to lower case and strips out spaces. It also provides very basic logging:#!/bin/sh from=$SMS_1_NUMBER message=$SMS_1_TEXT echo "`date`: SMS from $from reading $message" >> /etc/openhab2/utils/SMS.log #put message into lower case message=`perl -e "print lc('$message');"` #delete blank spaces message="$(echo "${message}" | tr -d '[[:space:]]')" #delete initial + from number and add two zeros from="00$(echo "${from}" | cut -c 2-)" wget -qO- http://localhost:8080/classicui/CMD?SMStext=$message &> /dev/null wget -qO- http://localhost:8080/classicui/CMD?SMSnumber=$from &> /dev/null wget -qO- http://localhost:8080/classicui/CMD?SMSreceived=ON &> /dev/null
-
Then create openHAB items:
Switch SMSreceived String SMStext String SMSnumber
-
And a basic rule to process received SMSs:
rule "SMS processor" when Item SMSreceived received command ON then logInfo("SMS", "SMS received - pausing before processing") Thread::sleep (1000) logInfo("SMS", "SMS received from {}:{}",SMSnumber.state,SMStext.state) // Text from either of us if ((SMSnumber.state =="0044123456789]") || (SMSnumber.state =="0044123456789")) { logInfo("SMS", "Confirmed text from one of us") if (SMStext.state=="open") { logInfo("SMS", "Opening door") DoorSwitch.sendCommand(ON) } // etc etc } // Tidy up SMSnumber.sendCommand("") SMStext.sendCommand("") SMSreceived.sendCommand(OFF) logInfo("SMS", "Done processing SMS") end
- Sending SMSs from within a rule is easy (using the impenetrable executecommandline format):
executeCommandLine("gammu-smsd-inject@@TEXT@@0044123456789@@-text@@\"This is an example message\"")
That’s it!
Dan
(edited to deal with modemmanager uninstalling and to integrate symlink explanation)