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!