Last active
June 4, 2019 17:35
-
-
Save unixweb/d079fa0799b5817b7ee2f4745415dc32 to your computer and use it in GitHub Desktop.
GSM Raspberry Pi SMS Butler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Author
unixweb
commented
Mar 8, 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?
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