-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsim800_call.py
More file actions
75 lines (63 loc) · 1.91 KB
/
sim800_call.py
File metadata and controls
75 lines (63 loc) · 1.91 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
#!/usr/bin/python
from time import sleep, time
import serial
SERIAL_PORT = "/dev/serial0"
DEBUG = False
def debug(verb):
if DEBUG:
print(verb)
sim800 = serial.Serial(SERIAL_PORT, baudrate=9600, timeout=1)
def sim800_call(s):
number = input("Enter number: ")
# dial number
command = 'ATD%s;\r' % number
debug("establishing call, sending command: %s" % command)
s.write(command.encode())
timeout = time() + 10
while True:
response = s.readline()
debug(response)
if response.decode().strip() == "OK":
debug("Establishing call...")
input("Press any key to hang up...")
sim800_hangup(s)
break
elif response.decode().strip() == "NO DIALTONE":
debug("No dial tone...")
break
elif response.decode().strip() == "BUSY":
debug("Line busy...")
break
elif response.decode().strip() == "NO CARRIER":
debug("No signal...")
break
elif response.decode().strip() == "NO ANSWER":
debug("No answer...")
break
elif response.decode().strip() == "ERROR":
debug("Error, check number...")
break
elif time() > timeout:
debug("call timeout")
break
sleep(0.2)
def sim800_hangup(s):
command = 'ATH\r'
debug("disconnecting call, sending command: %s" % command)
s.write(command.encode())
timeout = time() + 10
while True:
response = s.readline()
debug(response)
if response.decode().strip() == "OK":
debug("Hang up successful")
break
elif time() > timeout:
debug("hangup timeout")
break
sleep(0.2)
try:
while True:
sim800_call(sim800)
except Exception as e:
print(e)