pygsm using pthon-gammu
This commit is contained in:
parent
5d39722dbe
commit
0042a6158a
56
pygsm/call.py
Executable file
56
pygsm/call.py
Executable file
@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
import serial
|
||||
import time
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description="Utility for dealing with calls over GSM via serial connection")
|
||||
|
||||
subparsers = parser.add_subparsers(help='make or receive calls', dest='command')
|
||||
|
||||
|
||||
### Sender subparser
|
||||
|
||||
parser_make = subparsers.add_parser('make', help='make phone call')
|
||||
parser_make.add_argument('-i', '--interface', help='serial interface', metavar='TTY', required=True)
|
||||
parser_make.add_argument('-r', '--recipient', help='phone number to call', metavar='NUM', required=True)
|
||||
|
||||
### Reader subparser
|
||||
|
||||
parser_receive = subparsers.add_parser('receive', help='answer incoming phone calls')
|
||||
parser_receive.add_argument('-i', '--interface', help='serial interface', metavar='TTY', required=True)
|
||||
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
def makeCall(phone, recipient):
|
||||
try:
|
||||
phone.write(b'ATD' + recipient.encode() + b';\r')
|
||||
fin = input('Type "end" to hang up\n')
|
||||
while fin not in ['end', '"end"']:
|
||||
fin = input('Type "end" to hang up\n')
|
||||
phone.write(b'ATH\r')
|
||||
finally:
|
||||
phone.close()
|
||||
|
||||
def answerCall(phone):
|
||||
try:
|
||||
phone.write(b'ATA\r')
|
||||
fin = input('Type "end" to hang up\n')
|
||||
while fin not in ['end', '"end"']:
|
||||
fin = input('Type "end" to hang up\n')
|
||||
phone.write(b'ATH\r')
|
||||
finally:
|
||||
phone.close()
|
||||
|
||||
def main():
|
||||
phone = serial.Serial(args.interface, 115200, timeout=5)
|
||||
if sys.argv[1] == 'make':
|
||||
makeCall(phone, args.recipient)
|
||||
elif sys.argv[1] == 'receive':
|
||||
answerCall(phone)
|
||||
else:
|
||||
parser.print_help()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
19
pygsm/enableCall.py
Executable file
19
pygsm/enableCall.py
Executable file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
import serial
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='Enable receiving phone calls')
|
||||
parser.add_argument('-i', '--interface', help='serial interface', metavar='TTY', required=True)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
phone = serial.Serial(args.interface, 115200, timeout=5)
|
||||
try:
|
||||
phone.write(b'AT+CLIP=1\r')
|
||||
finally:
|
||||
phone.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
74
pygsm/text.py
Executable file
74
pygsm/text.py
Executable file
@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
import time
|
||||
import serial
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(description="Utility for dealing with texts over GSM via serial connection")
|
||||
|
||||
subparsers = parser.add_subparsers(help='send or read messages', dest='command')
|
||||
|
||||
|
||||
### Sender subparser
|
||||
|
||||
parser_send = subparsers.add_parser('send', help='send text messages')
|
||||
parser_send.add_argument('-i', '--interface', help='serial interface', metavar='TTY', required=True)
|
||||
parser_send.add_argument('-r', '--recipient', help='phone number to send to', metavar='NUM', required=True)
|
||||
parser_send.add_argument('-m', '--message', help='message contents', metavar='MES')
|
||||
|
||||
### Reader subparser
|
||||
|
||||
parser_read = subparsers.add_parser('read', help='read text messages')
|
||||
parser_read.add_argument('-i', '--interface', help='serial interface', metavar='TTY', required=True)
|
||||
|
||||
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
def sendText(phone, recipient, message):
|
||||
try:
|
||||
time.sleep(0.5)
|
||||
phone.write(b'ATZ\r')
|
||||
time.sleep(0.5)
|
||||
phone.write(b'AT+CMGF=1\r')
|
||||
time.sleep(0.5)
|
||||
phone.write(b'AT+CMGS="' + recipient.encode() + b'"\r')
|
||||
time.sleep(0.5)
|
||||
phone.write(message.encode() + b"\r")
|
||||
time.sleep(0.5)
|
||||
phone.write(bytes([26]))
|
||||
time.sleep(0.5)
|
||||
finally:
|
||||
phone.close()
|
||||
|
||||
def readMessage(phone):
|
||||
try:
|
||||
time.sleep(0.5)
|
||||
phone.write(b'AT+CMGL="ALL"\r')
|
||||
read = phone.readlines()
|
||||
badChars = ['OK', 'AT']
|
||||
for msg in read:
|
||||
if not any(x in str(msg) for x in badChars):
|
||||
print(str(msg.decode("utf-8")))
|
||||
time.sleep(0.5)
|
||||
finally:
|
||||
phone.close()
|
||||
|
||||
|
||||
def main():
|
||||
phone = serial.Serial(args.interface, 115200, timeout=5)
|
||||
if sys.argv[1] == 'send':
|
||||
sendText(phone, args.recipient, args.message)
|
||||
elif sys.argv[1] == 'read':
|
||||
readMessage(phone)
|
||||
else:
|
||||
parser.print_help()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user