Skip to content

Instantly share code, notes, and snippets.

@unixweb
Last active June 4, 2019 17:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 Mar 8, 2018

gsm-gps-hat-raspberry-pi

@grenej
Copy link

grenej commented Mar 18, 2018

Hello. Very useful script. Do you have an idea how to automatically power on HAT? there is PWRKEY that should be manually pressed.
My idea is to turn it on via Raspberry. Is it possible?

Do you know how to get sender telephone number in order to automatically reply for the sender?

@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