vrijdag 11 januari 2013

Raspberry Pi as SMS gateway

We were driving home one winter evening. In the morning before we left, we had turned down the heater. It was about 10 below zero outside and we still had an hour to drive. The house would be really cold when we got home. 'Wouldn't it be nice', my wife said, 'if we could send an SMS to turn the heater on?'
It seemed like a really good idea. And like all truly good ideas, it kept lingering. (If ever you get an idea and you wonder if it's any good, just leave it. A good idea will come back on its own.)

You'd need a device that can receive an SMS message and do something according to what's in it. Apparently, such a device is called an SMS Gateway.
February 2011, Elektor magazine had a really useful article by Hans Henrik Skovgaard, which outlined how to set up an SMS gateway on a PC with Damn Small Linux (DSL). I'd been curious to try that, but the old PC I had destined for it was kind of a power hog. Then Raspberry Pi came along. It has a much smaller footprint, both ecological and in centimeters squared. So I figured the SMS Gateway would make a nice Pi project.

Some steps in this article are derived from Skovgaard's. Some steps were added that I felt were missing i.e. took me quite some time to figure out. The bits about the Huawei GSM modem were also added (22 January 2013).

Ingredients

  1. GSM modem.
    1. The first thing I tried (and got working) was an old Siemens M35 cell phone, which I still had lying about. I can't throw away anything that works. I got it for free back in 2001, from my bank. There are lots of cheap cell phones available second hand that support serial communication. "Real" GSM modem hardware may cost upwards of 180 euro. So watch out.
      If you go this path, you may also need:
      1. RS-232 data cable for the M35 via eBay, which I modified so that the plug into the phone also carries the
      2. power supply wired to it. This is because the M35 may need to be always on.
      3. RS-232 to USB converter, commonly made by FTDI (or at least their converter chip is more or less the industry standard). I got mine from here.
        Note: if your cell phone has a data cable with a USB connection, you won't need the converter. You can also skip step 2.2
    2. If you Google "Raspberry Pi SMS", you're bound to see the term 'Huawei GSM modem' pop up here and there. At first, I dismissed the idea because they do more than needed: they also support wireless internet. And they're at least 50 euro (new).
      It turned out, however, that my local internet provider used to sell "internet dongles", which are Huawei GSM (and UMTS) modems. They look like big ol' memory sticks with a USB connector; their purpose is to connect your laptop onto the internet in the middle of nowhere. Which is rather uncool in the age of the smartphone. Hence, people dump their Huawei dongles on eBay. You can easily get one for less than 10 euro. The advantage is that you won't need any of the items listed above under a. And yes, you can send and receive SMS messages with them too!
  2. a valid SIM-card. I got a prepaid Simyo card for 5 euro which is valid forever, on the condition that I send at least 1 sms every 6 months.
  3. Raspberry Pi with Raspbian Wheezy. Other distro's may work, I haven't tried any.

Step 1: Connect the cell phone or dongle.

That's easy. Just hook up all the ingredients. It'll fit only one way.

Step 2: Test the cell phone modem.

That's easier said than done, you'll see below. But if you chose the Huawei option, you can skip section 2, except for step 2.3.

Step 3: install Gnokii and use it.

Configure it, monitor the phone, send and receive some SMS messages. That's all. Detailed below also.

Step 2.1. (GSM phone modem only) Find out your modem's signalling properties.

Signalling properties? Yes, baud rate, data bits, parity, stop bits. 19200 8N1 is common. Google them for your model e.g. "modem settings Nokia 6130". Don't make the mistake I made, by Googling for "Linux modem driver Nokia 6130". Modem settings are independent of OS. How silly of me.

Step 2.2. (GSM phone modem with RS-232/USB converter only) Verify your FTDI driver is loaded.

The FTDI drivers are pre-installed in Wheezy and load automagically when you boot up Pi, if you have the adapter cable plugged in. (Note: there's lots of verbiage on the web about installing FTDI drivers for Linux. YOU DON'T NEED ANY OF THAT. With Wheezy on Pi, IJW.) Thanks to Jeff Skinner for his really useful blogpost, where I found this step and the next. In a terminal, type:

lsusb

This will produce something like this:

Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 001 Device 005: ID 046d:c52b Logitech, Inc. Unifying Receiver

Yup, it's loaded. FTDI is Future Tech etc. Now:

Step 2.3. Find out on which port your modem is.

"In Linux, everything is a file". USB ports also behave like files.

dmesg | grep tty

will get you some text including something like:

[ 0.000000] console [tty1] enabled
[ 0.576969] dev:f1: ttyAMA0 at MMIO 0x20201000 (irq = 83) is a PL011 rev3
[ 0.886821] console [ttyAMA0] enabled
[ 7.130022] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUSB0

Aha! So our port is dev/ttyUSB0. Duly noted.
If you have a Huawei modem, you may see two ports here. More on this below.

Step 2.4. (GSM phone modem only) Install & configure Minicom

Normally, you'll want to verify that your modem responds like a modem should, without any other software being a potential source of error. You could skip this step and move on to step 3. But if that doesn't work, you'll have to come back here and verify still. So you need a terminal program, one that allows you to communicate with external serial devices (like modems), using AT-commands. Not to be confused with a Linux terminal, i.e. the Linux command prompt window. Skovgaards article recommends Microcom, for DSL. Wheezy also has a program called Microcom, but it's completely different. So I chose Minicom. Before installing, it may be a good idea to make sure your distro has all the latest upgrades and updates:

apt-get upgrade
apt-get update

Next, install Minicom:
sudo apt-get install minicom

Next, configure it:
minicom --setup

This will present a configuration menu. Select Serial port setup and change serial device to /dev/ttyUSB0 (or whatever you found in step 2.3). Also change the Bps/Par/Bits setting to what you found in step 2.1. Exit out of this menu and exit again to go to Minicom's terminal window. Type AT. It should respond with OK. Just for fun, type AT&F. Again, you should get OK back. So your modem works!

Step 3.1: Install Gnoki


sudo apt-get install gnokii

should do the trick. Now, both Skovgaard and the Gnokii documentation tell you to copy Gnokii's config file to your home directory, rename it to .gnokiirc, and, using an editor, modify its contents so that port = /dev/ttyUSB0 (for cell phone modem) or port = /dev/ttyUSB1 (for Huawei) and Model = AT. In my case, this didn't work. The solution is easy, but first things first: find the config file, and copy it home. (It's sudo find so as not to get bombarded with 'permission denied'. You're searching through the entire directory tree, ie including directories not for normal user's eyes.)

pi@raspberrypi ~ $ sudo find / -name 'gnokii' -type d
/etc/xdg/gnokii
pi@raspberrypi ~ $ ls /etc/xdg/gnokii
config
pi@raspberrypi ~ $ cp /etc/xdg/gnokii/config .
pi@raspberrypi ~ $ ls
config Desktop python_games release
pi@raspberrypi ~ S mv config .gnokiirc

However, after I put in my edits as outlined, and started gnokii, I got these error messages:
Couldn't read /home/pi/.config/gnokii/config config file
Couldn't open /home/pi/.cache/gnokii/gnokii-errors log file

The natural reflex is then to Google those exact messages, but in this case, that didn't help. All you need to do is create those directories, and put your config file in /home/pi/.config/gnokii/, named config and not .gnokiirc.

Using Gnokii

  • gnokii --identify will respond with an IMEI and brand name, among others.
  • gnokii --monitor will start monitor mode. Call your modem (using a phone, yes) and you should see something happening.
  • echo "this is a test message" | gnokii --sendsms +3161234xxxx will have Pi send a test message to your cell phone, if +31 is your country code and 61234xxxx is your cell phone number.
  • gnokii --smsreader will put gnokii in smsreader mode. An SMS sent to your modem will appear in Pi's terminal window. Gnokii will store received messages in /tmp/sms/.

Note: with a Huawei, you'll see two USB ports. In my case, both worked, except --smsreader wouldn't work on USB0, but worked well on USB1. Don't ask me why.

To do

This list keeps evolving.
  1. USB enumeration may or may not change from one reboot to another. The solution is to use a persistent device name, which is explained here on Loket Diversen.
  2. Find a temperature sensor that Pi can handle.
    Update 11-12-'13 I've been working with the DS1612 temperature sensor by Maxim. I'm building a software library for it.
    Update 14-7-2014 The library for the temperature sensor is finished until further notice, you can find it in my GitHub repository.
  3. Find (or make) an SMS Daemon, something that handles SMS-messages more or less automagically.
    Update 20-08-'14 There is gnokii-smsd and there is Gammu. Both seem to be intended for sending and receiving large amounts of SMS messages, which is not desired per se for a domotica application.
    Gnokii-smsd is not a proper daemon because it won't run in the background by itself. I'm sorry to say documentation is incomplete, not very descriptive and not maintained since 2011.
    Gammu looks like a very complete package. The last stable release was from may 2013, so that's promising. Understanding all the necessary ins and outs is not trivial, though.
    Daemons aren't trivial either. Since I like to understand what I'm doing, rather than blindly following a set of instructions, I've been reading up on daemon theory. Here is a summary of this study.
  4. Some additional hardware that Pi can control, on reception of the proper SMS message.
    Update 11-12-'13 Progress has been made in this area. Report coming soon.
  5. Have Pi poll some web site on a regular basis for some interesting numbers and send me that number via SMS.

All this is still a long way from sending a text message to turn the heater on. But it's a start.

Update 23 May 2013 This is by far my most popular blog post ever. Thanks, guys!

17 opmerkingen:

rafatoba zei

Thanks for posting how to put it to work! Have just bought the Raspberry pi to make it -hope it comes fast! -
I have just one question regarding your solution: i have seen that there is another project, gammu , that looks like a fork from gnokii ... Did you knew about it? If so, why have you chosen gnokii over .gammu ?
Again, thanks for posting it. Can't wait to get all the material :-)

Rolf Blijleven zei

@rafatoba You're welcome, glad to help :-)
I didn't choose Gammu because I did not know about it at the time. Reading this, Gammu is probably a better choice if you want to expand gammu/gnokii functionality, i.e. adapt the source code to your needs, rather than build a script-like daemon on top of Gnokii/Gammu without changing the code itself.

But, I'm not at that stage yet (and haven't chosen yet, either). I'm still working (on and off) on something that is to make good use of the gateway. The deamon comes later.

Kris De Rocker zei

Beste,
Daar je Nederlander bent (+31 als landcode ;-)) vermoed ik dat je Nederlands spreekt...wat communicatie net iets makkelijker maakt (niet dat Engels een probleem is, maar de moedertaal gebruiken is toch makkelijker, niet?).

Ik heb thuis mijn eerste Raspberry PI liggen en wou deze omtoveren tot SMS gateway (gebruik hiervoor een Huawei K3565 stick). Ik heb héél jouw tutorial gelezen, maar ik zit vast bij een bepaald punt (using Gnokii).

Volgensde fout krijg ik op mijn rasp pi:
pi@raspberrypi ~ $ gnokii --identify
GNOKII Version 0.6.30
Couldn't read /home/pi/.config/gnokii/config config file.
Cannot open logfile /home/pi/.cache/gnokii/gnokii-errors
WARNING: cannot open logfile, logs will be directed to stderr
Gnokii serial_open: open: No such file or directory
Couldn't open FBUS device: No such file or directory
Gnokii serial_open: open: No such file or directory
Couldn't open FBUS device: No such file or directory
Gnokii serial_open: open: No such file or directory
Couldn't open FBUS device: No such file or directory
Telephone interface init failed: Command failed.
Quitting.
Command failed.

Kan je mij hiermee verder helpen? Ben helaas geen linuxguru :)

Je kan mij bereiken op kris (a) derocker (.) name

rafatoba zei

I hope that it is not a problem to read English ;-)

I don't speak Dutch, but I do German, and from what I have undstood, you have a permissions problem there.
I am using gammu and not gnokii , but I am pretty sure from that it has to do with your current user permissions. I would try 2 things:

1.- run those commands as superuser . For example:
sudo gnokii --identify
2.-if it does not work, give permisions to everyone on the files that it is complaining:

sudo chmod 777 /home/pi/.config/gnokii/config
sudo chmod 777 / home/pi/.cache/gnokii/gnokii-errors

Please note that if you are in a shared computer,it is quite unsafe.will be ok if you are the only user.
If the problem is that those files do not exist, then maybe something went wrong on the installation

Rolf Blijleven zei

@Kris: De eerste twee fouten die je krijgt zijn dezelfde als die ik kreeg. Hoe heb je dit gedaan: "create those directories, and put your config file in /home/pi/.config/gnokii/, named config and not .gnokiirc"?

Als je in je home directory het commando ls -a geeft, moet je daar .config en .cache zien. Zoniet, dan moet je dus die directories en subdirectories aanmaken en .gnokiirc naar de ene kopiëren en omnoemen naar config. Zoiets:

mkdir .config
mkdir .cache
cd .cache
mkdir gnokii
cd ..
cd .config
mkdir gnokii
cd gnokii
mv ..\..\.gnokiirc .\config

Ik ben ook helemaal geen Linux-goeroe, maar ik heb wel ontdekt dat je moet snappen wat je doet. Zomaar overtikken werkt niet. Jouw situatie kan anders zijn dan de mijne. Als je niet snapt wat iets doet, vraag Google.

Kris De Rocker zei

Well...it seems that i had to change the chmod value of these files indeed. Now, i can send SMS'es!

Only problem : i can't receive them :(

$ gnokii --smsreader
GNOKII Version 0.6.30
Entered sms reader mode...

This is the only message i get when i enter the smsreader mode. When i send a sms from my cell phone to the number of the sms gateway, nothing appears...

PS : /tmp/sms doesn't excist. Do i have to create it my own and will messages be stored in that file, or does gnokki creates this file/directory itselfs?

best regards,
Kris

Rolf Blijleven zei

@Kris I had a similar symptom. See 2.3: dmesg | grep tty may give you dev/USB0 and dev/USB1. Did you try them both? As I said (in section 'using Gnokii') "Note: with a Huawei, you'll see two USB ports. In my case, both worked, except --smsreader wouldn't work on USB0, but worked well on USB1. Don't ask me why."

Kris De Rocker zei

I've read that but with my model (vodafone K3565 rev2) you'll even see pptyUSB0, pptyUSB1 and pptyUSB2! I've tried them all, but no sms...

I'll try further this evening.

Best regards,
Kris

Sriram Rajamanuri zei

great post. thank you. i was thinking of expanding on your project by making the raspberry pi a 'GSM gateway'. Very informative again, thank you.

Rolf Blijleven zei

@Sriram Thank you, glad you like it. Please feel free to share your plans and ideas.
The project opens up a world of possibilities and I find it sometimes hard to choose. So any specific need or wish may well help.

gateway customer care number zei

Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep
Blogging!

Brandon Hudson zei

Your post really cool and interesting. Thanks very much.

Lenovo - 14" ThinkPad Edge Notebook - 4 GB Memory - 320 GB Hard Drive - Matte Black

Lenovo - ThinkPad Tablet PC - Black

rafatoba zei
Deze reactie is verwijderd door de auteur.
rafatoba zei

Hi Rolf! I have made two posts for installing Gammu daemon on the Raspberry Pi (of course, with credit to you ;-) ). Please check them out:

Connnect GSM modem to Linux computer
Install Gammu Daemon on Linx

When I say Linux machine, I mean Raspberry Pi :-D

Thanks for your post, it has been really useful ;-)

Rolf Blijleven zei

@rafatoba Thanks for your post and for linking! I wrote a little update too.

Anoniem zei

Would it be possible to connect multiple phones for increased sms traffic?

Rolf Blijleven zei

Gammu supports multiple phones. USB-phones would be easiest, I guess.

Mogelijk gemaakt door Blogger.