OzzMaker
Published © GPL3+

Control the GPIO of a Raspberry Pi using SMS from a mobil...

This is an example of how to use a SMS from a mobile phone to control the GPIO on a Raspberry Pi

IntermediateProtip1 hour27
Control the GPIO of a Raspberry Pi using SMS from a mobil...

Things used in this project

Story

Read more

Code

Control GPIO pins using SMS and Python

Python
import RPi.GPIO as GPIO
import time
import sys
import re


RED_LED =  21
GREEN_LED =  20
BLUE_LED =  16

GPIO.setmode(GPIO.BCM)
filename=str(sys.argv[1])                               #Gammu will pass the filename of the new SMS as an argument
complete_filename="/var/spool/gammu/inbox/"+filename    #we create the full path to the file here

GPIO.setup(RED_LED , GPIO.OUT)
GPIO.setup(GREEN_LED , GPIO.OUT)
GPIO.setup(BLUE_LED , GPIO.OUT)

sms_file=open(complete_filename,"r")
#read the contents of the SMS file
message=sms_file.read(160) #note that a not-parted SMS can be maximum 160 characters

#search the contents and perform an action. Rather than use 'find',
# we will use regular expression (re) so we can ignore case.
#Most smartphones will have the first letter capitalised
if re.search('red', message, re.IGNORECASE):
        GPIO.output(RED_LED , GPIO.HIGH)
        time.sleep(2)
        GPIO.output(RED_LED , GPIO.LOW)
elif re.search('green', message, re.IGNORECASE):
        GPIO.output(GREEN_LED , GPIO.HIGH)
        time.sleep(2)
        GPIO.output(GREEN_LED , GPIO.LOW)
elif re.search('blue', message, re.IGNORECASE):
        GPIO.output(BLUE_LED , GPIO.HIGH)
        time.sleep(2)
        GPIO.output(BLUE_LED , GPIO.LOW)



GPIO.cleanup()

Credits

OzzMaker
6 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.