-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsim800_send_sms.py
More file actions
80 lines (70 loc) · 2.16 KB
/
sim800_send_sms.py
File metadata and controls
80 lines (70 loc) · 2.16 KB
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
#!/usr/bin/python
from time import sleep, time
import serial
SERIAL_PORT = "/dev/serial0"
DEBUG = True
def debug(verb):
if DEBUG:
print(verb)
sim800 = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=1)
def sim800_sms(s):
number = input("Enter number: ")
message = input("Enter message: ")
## set text mode
command = 'AT+CMGF=1\r'
debug("set text mode, sending command: %s" % command)
s.write(command.encode())
timeout = time() + 10
timedout = False
while True:
response = s.readline()
debug(response)
if response.decode().strip() == "OK":
break
elif time() > timeout:
timedout = True
debug("text mode timeout")
break
sleep(0.2)
if timedout:
return
## set to number
command = 'AT+CMGS="%s"\r' % number
debug("set to number, sending command: %s" % command)
s.write(command.encode())
timeout = time() + 10
while True:
response = s.readline()
debug(response)
if response.decode().strip() == ">":
## send message
command = message + chr(26) + '\r'
debug("sending message, sending command: %s" % command)
s.write(command.encode())
timeout = time() + 10
while True:
response = s.readline()
debug(response)
if response.decode().strip() == "OK":
debug("Message sent")
break
elif time() > timeout:
timedout = True
debug("message timeout")
break
sleep(0.2)
if timedout:
return
break
elif response.decode().strip() == "ERROR":
debug("There was an error! Please check the number and try again")
s.write(chr(27).encode())
break
elif time() > timeout:
debug("set number timeout")
break
sleep(0.2)
try:
sim800_sms(sim800)
except Exception as e:
print(e)