Building an SMS gateway for openHAB (on Linux)

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):

  1. 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.

  2. Of course get a suitable SIM card, e.g. giffgaff in the UK.

  3. 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

  1. 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

  1. 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)

  1. edit the gammu config file /etc/gammu-smsdrc to look like this :

# Configuration file for Gammu SMS Daemon

# Gammu library configuration, see gammurc(5)
# Please configure this!
port = /dev/usb_modem
connection = at115200
# Debugging
logformat = textall
logfile = /var/log/gammu.log

# SMSD configuration, see gammu-smsdrc(5)
service = files

logfile = /var/log/gammu-smsd.log
RunOnReceive = /etc/openhab2/utils/
# 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/
  1. 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]

  1. Start the gammu daemon

sudo service gammu-smsd start

It should restart automatically when the system boots

  1. 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)

  1. Assuming that works, you can now create the /etc/openhab2/utils/ 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:

    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
  2. Then create openHAB items:

    Switch 	SMSreceived
    String	SMStext
    String	SMSnumber
  3. And a basic rule to process received SMSs:

    rule "SMS processor"
    	Item SMSreceived received command ON
    	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")
    //		etc etc
    //	Tidy up
    	logInfo("SMS", "Done processing SMS")
    1. 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!


(edited to deal with modemmanager uninstalling and to integrate symlink explanation)



Currently I send SMS messages using the API of a network provider.

I used to do that, but of course doesn’t help if internet goes down…

That is indeed a weak point. :slight_smile:


Would it make sense to have an old Android phone at home with Tasker that sends and receives the SMS messages?

1 Like

Please be aware of SMS spoofing. I for sure would not unlock my door based on the originating number. It’s not safe!

Good point - real rule has password plus “open”. Any hacker that can intercept that is going to be too busy hacking the CIA to bother with my house.

1 Like

Interesting suggestion re. a phone - if you can remove the battery then presumably it could run 24/7 without any problems?

@dan12345 thanks a lot for the detailed tutorial !

At the moment, I can send sms by the command line with my raspberry. But if I use the executeCommandLine command placed in a rule I do not receive any sms.
The log viewer says that everything is fine and that the commandline was excuted but nothing.

Is it possible that the PIN code of the SIM card is stopping openhab from sending an sms ?

More likely the openHAB user is not in the dialout or tty group.


sudo adduser openhab dialout
sudo adduser openhab tty   # ?


I tried and openhab was already a user of both groups…

have you tried running openHAB as root, to eliminate permissions as being a problem?

No didn’t try this.
And I cannot find the way to do it with openhab2. Found with openhab but the given files for the USER_AND_GROUP permissions is not located at the same place.
Could you direct me with this? Thanks again

edit /etc/default/openhab2 and add: USER_AND_GROUP=“root:root”

I tried to edit the openhab2 file, but as openhabian user I have no write permission.

sorry, not familiar with openhabian, but can’t you sudo?

I use this kind of method since 3 years, bu I use this gateway:

in order to avoid spoofing I setup into the inbound script a simple rule that accept only command from a specifica number, other message are send to my mailbox, to avoid to loose carrier related sms

good to know there are other options.

I may be naive, but seems to me that it would take serious hacking to get past (1) nobody else knowing the SMS number, (2) only accepting commands from designated numbers; (3) commands requiring a password. Local burglars are more likely to be drug-addicted teenagers than Tom Cruise from Mission Impossible.


I’m a total OpenHAB newbie and I too need two-way sms integration without involving an internet-based gateway, so this will be a great start. I’ve ordered one of those cheap gsm modules that uses a plain serial port to talk to the host using standard GSM AT-commands, so the host integration should be simple enough.

However, I’m thinking that it would be rather easy to create an add-on for this - the point being that it would enable a slicker integration (or at least, so I like to think :-)). I’m primarily thinking about the way incoming commands are handled. It would be nice if I could have a generic interface without having to write a ton of rules. For example:
"{password} GET {item ID}" and it would respond with the current value, and
"{password} SET {item ID} {Value}" and it would respond with a confirmation message.

But I don’t know if the OpenHAB add-on API would allow for this kind of integration (?)
I suppose it would act as a kind of user interface rather than anything else?
Of course, it would also need to provide a “send” action to send sms’s from rules.

Anyway, if anyone wants to collaborate on this, please post your thoughts, ideas, requirements and use-cases, etc.

Thank you @dan12345 for your help.

I finally found the solution to my issue.
In the rules, with the exec command the following code needs to be used:

	val results = executeCommandLine("gammu@@sendsms@@TEXT@@12345678@@-text@@\"This is an example message\"")

With the addition of the “val results” command all is working fine now and I can send sms.