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.
Wiring
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.
Setup
Install Gammu and python bindings;
pi@raspberrypi ~ $ sudo apt-get install gammu-smsd python-gammu
Edit the config file;
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;
Send a test SMS to a mobile number. The mobile number below is an example, you will need to update this;
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.
import RPi.GPIO as GPIO from time import sleep import gammu RED_LED = 21 GREEN_LED = 20 BLUE_LED = 16 #The phone number where you want the SMS to be received phoneNumber = '+614123456789' sendSMS = False #setup GPIO pins GPIO.setmode(GPIO.BCM) 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 sm.ReadConfig(Filename="/etc/gammu-smsdrc") # Connect to phone sm.Init() try: while True: if(GPIO.input(RED_LED)): print "RED" messageText = 'Red button' sendSMS = True elif(GPIO.input(GREEN_LED)): print "GREEN" messageText = 'Green button' sendSMS = True elif(GPIO.input(BLUE_LED)): print "BLUE" messageText = 'Blue button' sendSMS = True if(sendSMS): #Update the message content message = { 'Text': messageText, 'SMSC': {'Location': 1}, 'Number': phoneNumber, } # Actually send the message sm.SendSMS(message) sendSMS = False sleep (2) #Allow time for the SMS to be sent sleep (0.1) finally: GPIO.cleanup()