Using a button and the GPIO on a Raspberry Pi to send a SMS

In this guide we will show you how to send a SMS using a button connected to the GPIO pins of a Rasberry Pi Zero.


For this guide, the GSM modem we are using to send the SMS is the  BerryGPS-GSM.

On the software side, we will be using Gammu, which is specifically designed to control  phones and GSM modules.

Python will be used to monitor some buttons connected to GPIO pins and Gammu python bindings will be used to send a SMS.

Buttons are used here as an example, but you can use anything to trigger the SMS, E.g. Temperature sensor, water level sensor, light sensor, etc..

We have includes some LEDs so we can see when the buttons are pressed.


Raspberry Pi LED button

We will be using the three bottom right GPIO pins on the Raspberry Pi header. These are GPIO 16, 20 and 21.
Each is connected to a button and different color LED as shown above. The internal pull-down resisters will be used on these GPIO.
3.3v and GND are connect to the power rails on the breadboard.
The resistors used are 330 Ohm  and the GND pin (shorter pin) of the LEDs is connected to the GND power rail.


Install Gammu and python bindings;

pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install gammu-smsd python-gammu

Edit the config file;

pi@raspberrypi ~ $ sudo nano /etc/gammu-smsdrc

Find the below lines and add port and speed.
For the BerryGPS-GSM, use /dev/ttyACM1 for port and at115200 for speed

port = /dev/ttyACM1
connection = at115200

We will do a quick test. Restart the gammu service so the new config takes effect;

pi@raspberrypi ~ $ sudo /etc/init.d/gammu-smsd restart

Send a test SMS to a mobile number. The mobile number below is an example, you will need to update this;

pi@raspberrypi ~ $ echo "This is a test from a Raspberry Pi" | /usr/bin/gammu --sendsms TEXT +614123456789


Python Script

Before creating the python script, you will need to disable the gammu daemon as it will lock the port which is used to communicate to the GSM modem. This python script needs to access this port.

pi@raspberrypi ~ $ sudo systemctl disable gammu-smsd.service
import RPi.GPIO as GPIO
from  time import sleep
import gammu
RED_LED =  21
BLUE_LED =  16
#The phone number where you want the SMS to be received
phoneNumber = '+614123456789'
sendSMS = False
#setup GPIO pins
GPIO.setup(RED_LED, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(GREEN_LED, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(BLUE_LED, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# Create state machine object
sm = gammu.StateMachine()
# Read ~/.gammurc
# Connect to phone

        while True:
                        print "RED"
                        messageText = 'Red button'
                        sendSMS = True
                        print "GREEN"
                        messageText = 'Green button'
                        sendSMS = True
                        print "BLUE"
                        messageText = 'Blue button'
                        sendSMS = True
                        #Update the  message content
                        message = {
                                'Text': messageText,
                                'SMSC': {'Location': 1},
                                'Number': phoneNumber,
                        # Actually send the message
                        sendSMS = False
                        sleep (2)    #Allow time for the SMS to be sent
                sleep (0.1)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.