-
Notifications
You must be signed in to change notification settings - Fork 2
/
sms.py
72 lines (51 loc) · 1.57 KB
/
sms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import serial
import time
import sys
ser = serial.Serial('COM5', 115200, timeout=12) #initialize modem com port
def SendCommand(command, getline=True):
#print(command)
ser.write(command.encode())
time.sleep(2)
response = ''
if getline:
response=ReadLine()
return(response)
def ReadLine():
response = ser.readline()
print (response)
return response
def ReceiveSms():
SendCommand('ATZ\r')
SendCommand('AT+CMGF=1\r')
ser.flushInput()
ser.flushOutput()
SendCommand('AT\r')
command = 'AT+CMGL="REC UNREAD"\r' #gets sms that has not been read
#print (SendCommand(command, getline=True))
response = ser.readall() #read response from serial
response=str(response)
#print(response)
if "REC UNREAD" in response:
numberIndex=response.find('+255')
smsIndex=response.find('"\\r\\n')+5
smsLastIndex=response.find('\\r\\n\\r\\nOK\\r\\n')
phone=response[numberIndex:numberIndex+13]
sms=response[smsIndex:smsLastIndex]
print("Phone:"+phone)
print("sms :" +sms)
return(phone,sms)
def SendSms(message,to):
SendCommand('ATZ\r')
SendCommand('AT+CMGF=1\r')
to='AT+CMGS='+'"'+to+'"'
SendCommand('ATE0\r')
SendCommand('AT\r')
SendCommand('AT+CMGD="ALL"\r')
SendCommand('AT+CMGF=1\r')
SendCommand(to + "\r")
SendCommand(message + "\r")
SendCommand(chr(26))
print ("disconnecting")
ser.flush()
ser.close()
print(ReceiveSms())