-
Notifications
You must be signed in to change notification settings - Fork 1
/
sms.py
148 lines (123 loc) · 4.45 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from time import sleep
import serial
from curses import ascii
'''Run this file in your shell, the aim for this script is to try and allow you to
send, read and delete messages from python, after playing around with this for a while i have
realised that the most importain thing is good signal strength at the modem. For a full list of
fuctions that a GSM modem is capable of google Haynes AT+ commands
I have put the sleep function into many of the functions found within this script as it give the modem time
to receive all the messages from the Mobile Network Operators servers'''
##set serial
ser = serial.Serial()
##Set port connection to USB port GSM modem
ser.port = '/dev/ttyACM0'
## set older phones to a baudrate of 9600 and new phones and 3G modems to 115200
##ser.baudrate = 9600
ser.baudrate = 115200
ser.timeout = 1
ser.open()
ser.write('AT+CMGF=1\r\n')
##following line of code sets the prefered message storage area to modem memory
ser.write('AT+CPMS="ME","SM","ME"\r\n')
## Important understand the difference between PDU and text mode, in PDU istructions are sent to the port as numbers eg: 0,1,2,3,4 and in TEXT mode as text eg: "ALL", "REC READ" etc
## following line sets port into text mode, all instructions have to be sent to port as text not number
##Important positive responses from the modem are always returned as OK
##you may want to set a sleep timer between sending texts of a few seconds to help the system process
def sendsms(number,text):
ser.write('AT+CMGF=1\r\n')
sleep(2)
ser.write('AT+CMGS="%s"\r\n' % number)
sleep(2)
ser.write('%s' % text)
sleep(2)
ser.write(ascii.ctrl('z'))
print "Text: %s \nhas been sent to: %s" %(text,number)
def read_all_sms():
ser.write('AT+CMGF=1\r\n')
sleep(5)
ser.write('AT+CMGL="ALL"\r\n')
sleep(15)
a = ser.readlines()
z=[]
y=[]
for x in a:
if x.startswith('+CMGL:'):
r=a.index(x)
t=r+1
z.append(r)
z.append(t)
for x in z:
y.append(a[x])
## following line changes modem back to PDU mode
ser.write('AT+CMGF=0\r\n')
return y
def read_unread_sms():
ser.write('AT+CMGF=1\r\n')
sleep(5)
ser.write('AT+CMGL="REC UNREAD"\r\n')
sleep(15)
a = ser.readlines()
z=[]
y=[]
for x in a:
if x.startswith('+CMGL:'):
r=a.index(x)
t=r+1
z.append(r)
z.append(t)
for x in z:
y.append(a[x])
##Following line changed modem back to PDU mode
ser.write('AT+CMGF=0\r\n')
return y
def read_read_sms():
##returns all unread sms's on your sim card
ser.write('AT+CMGS=1\r\n')
ser.read(100)
ser.write('AT+CMGL="REC READ"\r\n')
ser.read(1)
a = ser.readlines()
for x in a:
print x
def delete_all_sms():
##this changes modem back into PDU mode and deletes all texts then changes modem back into text mode
ser.write('AT+CMGF=0\r\n')
sleep(5)
ser.write('AT+CMGD=0,4\r\n')
sleep(5)
ser.write('AT+CMGF=1\r\n')
def delete_read_sms():
##this changes modem back into PDU mode and deletes read texts then changes modem back into text mode
ser.write('AT+CMGF=0\r\n')
sleep(5)
ser.write('AT+CMGD=0,1\r\n')
sleep(5)
ser.write('AT+CMGF=1\r\n')
##this is an attempt to run ussd commands from the gsm modem
def check_ussd_support():
##if return from this is "OK" this phone line supports USSD, find out the network operators codes
ser.write('AT+CMGF=0\r\n')
ser.write('AT+CUSD=?\r\n')
ser.write('AT+CMGF=1\r\n')
##This function is an attempt to get your sim airtime balance using USSD mode
def get_balance():
##first set the modem to PDU mode, then pass the USSD command(CUSD)=1, USSD code eg:*141# (check your mobile operators USSD numbers)
## Error may read +CUSD: 0,"The service you requested is currently not available.",15
## default value for <dcs> is 15 NOT 1
ser.write('AT+CMGF=0\r\n')
ser.write('AT+CUSD=1,*141#,15\r\n')
ser.read(1)
a = ser.readlines()
print a
ser.write('AT+CMGF=1\r\n')
def ussd_sms_check():
##first set the modem to PDU mode, then pass the USSD command(CUSD)=1, USSD code eg:*141# (check your mobile operators USSD numbers)
ser.write('AT+CMGF=0\r\n')
ser.write('AT+CUSD=1,*141*1#,15\r\n')
ser.read(100)
a = ser.readlines()
print a
ser.write('AT+CMGF=1\r\n')
# My code
sendsms('8442462790', 'Mensaje SMS')
print read_all_sms()