DEV Community

Jon Neverland
Jon Neverland

Posted on • Originally published at jonnev.se on

Raspberry Pi Zero as SMS gateway

Raspberry Pi Zero as SMS gateway

I've previously written about setting up a SMS gateway using a 3G modem. I had that plugged into my HTPC and honestly it just didn't look good next to the TV. I figured using a Raspberry Pi and just stash it away somewhere would be better. At the same time I was preparing to create a Magic Mirror of sorts (without the actual mirror) and I accidentally (?) stumbled across the Adafruit Fona 808 when doing my regular window shopping for Pi stuff.

After some reading up - there are a lot of great tutorials I'll link to at the end of the post - I felt that there should be something a bit easier and I found a couple of HATs designed for the Pi.

I ended up buying one from Sixfab but either of these two should work as well. (If you try one of those I'd love to hear about it!)

Raspberry Pi Zero as SMS gateway

Assembling

I own a soldering iron but prefer not to use it unless necessary so imagine my joy when I found the solderless GPIO header. While at it I also bought a nice little case. Assembling the header and case was pretty easy and the result great! Just follow this guide.

Raspberry Pi Zero as SMS gateway

Zero mounted in the rig

Raspberry Pi Zero as SMS gateway

Header attached

Raspberry Pi Zero as SMS gateway

Case assembled

Raspberry Pi Zero as SMS gateway

and finally GSM pHAT added

Setup

Now to getting the thing to work! There's a blog post at Sixfab, but it's aimed at setting up a GRPS connection. I was just interested in sending and receiving sms with gammu which I already had installed (with postgres as backend this time).

First you need to disable the serial console.

sudo systemctl stop serial-getty@ttyAMA0.service
sudo systemctl disable serial-getty@ttyAMA0.service
Enter fullscreen mode Exit fullscreen mode

That one worked for me. Depending on your Pi you might need to do this instead.

sudo systemctl stop serial-getty@ttyS0.service
sudo systemctl disable serial-getty@ttyS0.service
Enter fullscreen mode Exit fullscreen mode

Then remove the corresponding line in boot commandline

sudo nano /boot/cmdline.txt
# Remove "console=serial0, 115200" or "console=ttyAMA0, 115200"
Enter fullscreen mode Exit fullscreen mode

Enable UART and disable Bluetooth on serial.

sudo nano /boot/config.txt
# Add these
enable_uart=1
dtoverlay=pi3-disable-bt
Enter fullscreen mode Exit fullscreen mode

That last bit with disabling BT did the trick for me and took a reasonable amount of googling to find out. I don't use BT but if you do I recall seeing a solution somewhere...

Connecting

Reboot your Pi and to test the connection to the pHat do this

sudo nano /etc/gammurc
# Add 
[gammu]
device = /dev/serial0
connection = at115200
Enter fullscreen mode Exit fullscreen mode

And gammu should find your device

sudo gammu --identify
Enter fullscreen mode Exit fullscreen mode

You can also interact with the shield via screen

sudo screen /dev/serial0 115200
Enter fullscreen mode Exit fullscreen mode

Type AT, press enter and you should get OK back. Ctrl-A, D to exit.

If everything is ok you should now be able to use gammu-smsd to send and receive SMS. First power up the shield by pressing POWER-UP for a few seconds.

sudo nano /etc/gammu-smsdrc
Enter fullscreen mode Exit fullscreen mode

This is my entire config with postgres and all.

[gammu]
port = /dev/serial0
connection = at115200

# SMSD configuration, see gammu-smsdrc(5)
[smsd]
service = sql
driver = native_pgsql
host = localhost
user = smsd
password = SUPERSECRET
database = smsd
RunOnReceive = python /opt/gammu/receivesms.py
Enter fullscreen mode Exit fullscreen mode

Sending SMS!

sudo service gammu-smsd restart
gammu-smsd-inject TEXT 123456 -unicode -text "Oh hai"
Enter fullscreen mode Exit fullscreen mode

I've also written a small html client with an API backend for gammu that I'll (probably) publish soon.

Reading list

Top comments (0)