-
Notifications
You must be signed in to change notification settings - Fork 3
/
sat.py
executable file
·140 lines (133 loc) · 3.01 KB
/
sat.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
#!/usr/bin/env python
# use Rockblock sat coms on UART 1 (tiny pins)
# may have to disable handshake with AT&K0 ???
# have to read back anything sent as its echod
from time import sleep
import pyb
from utime import sleep_ms
from common import d
# gives 0 none to 5 strong
# after few s it replies +CSQ:2 newline then OK
# We NEED TO INHERIT THIS FROM BOOT?
satuart = pyb.UART(2,19200)
satuart.init(19200,bits=8,parity=None,timeout=60)
# sig str can take > 4s
# try three times in case its increasing
def satsignal():
maincount = 3
while maincount > 0:
d('getting signal strength')
msg = 'AT+CSQ\r'
satuart.write(msg)
# this will discard our echo, blank line etc
# now we hope for reply
# at some read we will get CSQ:3\r\n
count = 60
while count > 0:
ret = str(satuart.readline())
#d(ret)
csqpos = ret.find("CSQ:")
if (csqpos >= 0) and (len(ret) >5) :
strength = int( ret[4 + csqpos] )
if(strength>0):
return strength
else:
d('STRENGTH IS ZERO - trying again.')
break
count = count -1
sleep_ms(100)
sleep(2)
maincount = maincount - 1
return(0)
# wait until message sent return and parse code
# SUCCESS is +SBDIX: 0, 0, 0, 0, 0, 0
# partSUCCESSnolocupdate is +SBDIX: 2, 0, 0, 0, 0, 0
# FAIL like +SBDIX: 32, 1, 2, 0, 0, 0
def waitforsentOK():
count = 200
while count > 0:
ret = str(satuart.readline())
d('Count is '+ str(count))
#d(ret)
pos = ret.find("+SBDIX:")
if (pos >= 0) and (len(ret) >5) :
fields = ret.split(',')
rcode = fields[0].split(' ')[1]
d('returncode'+str(rcode))
#d(rcode)
if (rcode == '0') or (rcode =='2'):
return ( True )
count = count -1
sleep_ms(200)
return(False)
def waitforREADY():
count = 20
while count > 0 :
ret = satuart.read()
if ret == None:
count = count - 1
sleep_ms(50)
else:
ret = str(ret)
if ret.find("READY") > 0:
d('got READY')
return(True)
return(False)
def waitforOK():
count = 20
while count > 0 :
ret = satuart.read()
if ret == None:
count = count - 1
sleep_ms(50)
else:
d('got OK')
return(True)
return(False)
# wait for Sat to be responsive - allow for lock
def waitforsat():
count = 10
while count > 0:
sleep(1)
d('sending AT')
satuart.write('AT\r')
#discard our echo
satuart.readline()
#utime.sleep_ms(200)
ret = satuart.readline()
if ret != None:
d(ret)
count = 0
return(True)
else:
count = count -1
d('nothing yet')
return(False)
# flush sat buffer - use before turning off acording to 9602 guide
def satflush():
satuart.write('AT*F\r')
satuart.readline()
# send a msg - can take many seconds
def sendmsg(msg):
d('sending message')
#txt = 'AT+SBDWT=' + msg + '\r'
#satuart.write(txt)
#waitforOK()
satuart.write('AT+SBDWT\r')
waitforREADY()
txt = msg + '\r'
satuart.write(txt)
sleep(1)
# was it ok?
satuart.write('AT+SBDIX\r')
#discard our echo
satuart.readline()
# probably need sleep 1 or 2
sleep(1)
count = 200
if waitforsentOK():
d('message sent')
return(True)
else:
d('message failed')
return(False)