-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsm_sms.py
81 lines (76 loc) · 2.29 KB
/
gsm_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
#!/usr/bin/python
import sys
import serial
import time
from curses import ascii
import utils
import logger
import config
log = logger.get_logger(__name__)
conf = config.get_config()
# variables
settings = conf["output"]["gsm_sms"]
timeout = 30
# send a sms with the given text
def notify(text):
text = "["+conf["general"]["house_name"]+"] "+text
# truncate the text
text = (data[:150] + '..') if len(text) > 150 else text
# connect to the modem
try:
log.debug("Connecting to GSM modem on port "+settings["port"]+" with baud rate "+str(settings["baud"]))
modem = serial.Serial(settings["port"],settings["baud"], timeout=0.5)
except Exception,e:
log.error("Unable to connect to the GSM modem: "+utils.get_exception(e))
return
# for each recipient
for to in settings["to"]:
try:
i = timeout
done = False
while True:
# send the sms
if i == 30: send_sms(modem,to,text)
# read the output
output = modem.readlines()
for line in output:
line = str(line).rstrip()
if line == "": continue
log.debug("Modem output: "+line)
if "+CMGS:" in line:
log.info("Sent SMS to "+str(to)+" with text: "+text)
done = True
if "ERROR" in line:
done = True
break
if done: break
i = i - 1
if i == 0:
# timeout reached
log.error("Unable to send SMS to "+str(to)+": timeout reached")
break
except Exception,e:
log.error("Failed to send SMS to "+str(to)+": "+utils.get_exception(e))
# disconect
modem.close()
# send a sms message
def send_sms(modem,to,text):
log.debug("Sending SMS "+str(to))
time.sleep(2)
# switch to text mode
modem.write(b'AT+CMGF=1\r')
time.sleep(2)
# set the recipient number
modem.write(b'AT+CMGS="' + to.encode() + b'"\r')
time.sleep(2)
# send the message
modem.write(text.encode())
time.sleep(1)
# end the message with ctrl+z
modem.write(ascii.ctrl('z'))
# main
if __name__ == '__main__':
if len(sys.argv) == 1:
print "Usage: "+__file__+" <text>"
else:
notify(sys.argv[1])