Skip to content

Instantly share code, notes, and snippets.

@unixweb
Last active June 4, 2019 17:35
Show Gist options
  • Save unixweb/d079fa0799b5817b7ee2f4745415dc32 to your computer and use it in GitHub Desktop.
Save unixweb/d079fa0799b5817b7ee2f4745415dc32 to your computer and use it in GitHub Desktop.
GSM Raspberry Pi SMS Butler
# SIMBUTLER.py
# pip install pyserial
import RPi.GPIO as GPIO
import serial
import time, sys
import datetime
P_BUTTON = 24 # Button, adapt to your wiring
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(P_BUTTON, GPIO.IN, GPIO.PUD_UP)
SERIAL_PORT = "/dev/ttyAMA0" # Raspberry Pi 2 / Zero W
#SERIAL_PORT = "/dev/ttyS0" # Raspberry Pi 3
ser = serial.Serial(SERIAL_PORT, baudrate = 115200, timeout = 5)
setup()
ser.write("AT+CMGF=1\r") # set to text mode
time.sleep(3)
ser.write('AT+CMGDA="DEL ALL"\r') # delete all SMS
time.sleep(3)
reply = ser.read(ser.inWaiting()) # Clean buf
print "Listening for incomming SMS..."
while True:
reply = ser.read(ser.inWaiting())
if reply != "":
ser.write("AT+CMGR=1\r")
time.sleep(3)
reply = ser.read(ser.inWaiting())
print "SMS received. Content:"
print reply
if "getStatus" in reply:
t = str(datetime.datetime.now())
if GPIO.input(P_BUTTON) == GPIO.HIGH:
state = "Button released"
else:
state = "Button pressed"
ser.write('AT+CMGS="+49XXXXXXXXX"\r')
time.sleep(3)
msg = "Sending status at " + t + ":--" + state
print "Sending SMS with status info:" + msg
ser.write(msg + chr(26))
time.sleep(3)
ser.write('AT+CMGDA="DEL ALL"\r') # delete all
time.sleep(3)
ser.read(ser.inWaiting()) # Clear buf
time.sleep(5)
@unixweb
Copy link
Author

unixweb commented May 6, 2018

I have at the moment no idea how you can turn on the PWRKEY automatically
You can modify the script and try. The sender telephone number you can read from "ser.write("AT+CMGR=1\r")" but is dangerous and everybody can control your Raspberry Pi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment