-
Notifications
You must be signed in to change notification settings - Fork 3
/
main2019.py
executable file
·313 lines (268 loc) · 6.8 KB
/
main2019.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#
# main picoGPS code for glacsweb.org BergProbe 2019 version
# check hour, do a job, set next alarm
# gps loop reads until it gets lots of fixes, saves one to data.txt
# sat loop sends batch of fixes from file
# K.Martinez and J.Curry, University of Southampton, 2017/8/9
import math
import os
from time import sleep
from ds3231 import DS3231
from gps import *
from sat import *
from common import *
import stm
# What hours to do gps (must incl transmit)
schedule = [0,3,6,9,12,13,15,18,21]
#schedule = [0,3,6,9,12,13,14,15,16,18,21]
# when to send data
transmit = [13]
# max time of gps loops in ms
positiontimeout = 200*1000
# use fix type of this quality (dGPS FIX is 4)
FIXQUALITY = '4'
# minimum Iridium strength to use
MINIRIDIUM = 2
#Get the current time
extrtc = DS3231()
#Wakeup code run on startup is at bottom of file.
#get next alarm time
def getnextalarm(hh):
nexttime = None
if(hh in schedule):
position = schedule.index(hh)
nextpos = position+1
if(nextpos>(len(schedule)-1)):
nextpos = 0
nexttime = schedule[nextpos]
else:
for i in schedule:
if( i > hh):
nexttime = i
break
if(nexttime==None):
nexttime = 0
return nexttime
def sendtosat():
#Pull data from file and send it.
data = []
payload = ""
i = 0
#Get x readings from file.
with open('data.txt','r') as file:
for line in file:
# need to swap \n for ; for iridium
linenoCR = line.rstrip('\n')
linenoCR = linenoCR + ';'
payload = payload + linenoCR
#only sending a few readings 300/50=6 max
i = i + 1
if(i >= 5):
break
d(payload)
#We've finished all the readings
if(len(payload)<10):
#Empty payload
return False
# send fix via sat
d('Sending fixes via Iridium')
if sendmsg(payload) == True:
d('Done')
#Remove lines from file that we sent.
file= open('data.txt','r')
count = i
# read the lines we sent
while( count > 0):
file.readline()
count -= 1
restoffile = file.read()
file.close()
file = open('data.txt','w')
file.write(restoffile)
file.close()
return True
else:
d('SatSend failed')
return False
# power up Iridium, wait for connection, send data file messages
def satloop():
d('SAT on')
satpower.value(1)
#wait for sat to boot
waitforsat()
d('Getting Iridium signal')
strength = satsignal()
if (strength != None) and (int(strength) >= MINIRIDIUM) :
d('OK Sat strength')
else:
d('Sat strength failed')
satpower.value(0)
return
while(sendtosat()==True):
d("Satellite data send complete.")
#turn SAT off
satpower.value(0)
#we're done.
return
def gpsloop(waiter=False):
global finalgps
fixcount = 0
CheckedRTC = False
gpsYY = ''
gpsMM = ''
gpsDD = ''
gpshh = ''
gpsmm = ''
nmeafix = ''
#turn GPS on
gpspower.value(1)
t = 0
gps10 = []
d('waiting for warmup period')
tempC = temperature()
if(not waiter):
sleep(11)
d('starting gps loop')
start = pyb.millis()
while t < positiontimeout:
thetype = ''
nmea = gpsuart.readline()
if (nmea == None) or (len(nmea) < 32):
continue
try:
thetype, data = processGPS(nmea)
except:
d('processGPS ERROR - continuing.')
if(thetype=='t'):
#We got a timestamp
d('got timestamp')
(gpsYY, gpsMM, gpsDD, gpshh, gpsmm, gpsss ) = data
if CheckedRTC != True:
CheckedRTC = True
# get current datetime from rtc
(YY, MM, DD, hh, mm, ss, wday, n1) = extrtc.get_time()
# if gpstime is > 10s different from extrtc - set extrtc
if(abs(int(gpsYY)-YY)>0 or abs(int(gpsMM)-MM)>0 or abs(int(gpsDD)-DD)>0 or abs(int(gpshh)-hh)>0 or abs(int(gpsmm)-mm)>0 or abs(int(gpsss)-ss)>10):
#update external RTC. We're >10s out
d('Setting ext RTC')
extrtc.set_time(int(gpsYY), int(gpsMM), int(gpsDD), 0, int(gpshh), int(gpsmm), int(gpsss))
elif(thetype=='p'):
#got fix, must be 4
(lat,lon,alt,qual,hdop,sats,nmeafix) = data
print('Quality: ', qual)
if(qual == FIXQUALITY):
d('got fix')
fixcount += 1
else:
#some kind of problem
d('No data received - Timeout?')
pass
# after 15 fixes store and exit, assume we got a datetime
print('fixcount is ', fixcount)
if(fixcount >= 15):
# store it in file
with open('data.txt','a') as file:
tostore= gpsYY[2:] + gpsMM + gpsDD + gpshh + gpsmm + "," + nmeafix + "," + tempC + "\n"
print(tostore)
file.write(tostore)
break
# how long have we been looping? for positiontimeout
t = pyb.millis()-start
# turn GPS off
d('GPS off')
gpspower.value(0)
# print gps stream for tests
def printgps():
while True:
s = gpsuart.readline()
if( s != None) :
print(s)
# debug - print ext rtc date
def date():
extrtc = DS3231()
print(extrtc.get_time())
def standby():
pyb.standby()
# vital dumper in case of stuck readings on Pico
def dumpfile():
# really need to sleep(10) so logging can be started
print('Start your logger - 10s to go')
sleep(10)
try:
fd = open('data.txt','r')
b = " "
while b != "" :
b = fd.readline()
print(b)
fd.close()
except :
d('no data file')
def temperature():
a = pyb.ADC(pyb.Pin.board.A5)
g = pyb.Pin('A7', pyb.Pin.OUT_PP)
g.high()
sleep(0.02)
sum = 0.0
for count in range(1,10):
w = a.read() * 3300 / 4096.0
tc = ((10.888 - math.sqrt(118.548544 + 0.01388 * (1777.3 - w)))/-0.00694) + 30
sum = sum + tc
g.low()
tc2 = sum/10.0
# if its broken clamp to flag broken
if tc2 > 40:
tc2 = 40
elif tc2 < -40:
tc2 = -40
# return C X 10 to save decimal places
temp = round(tc2,1)
d(str(temp) + 'C')
return(str(int(temp * 10)))
# useful with cold hands
def saton():
satpower.value(1)
def satoff():
satpower.value(0)
def gpson():
gpspower.value(1)
def gpsoff():
gpspower.value(0)
def ls():
print(os.listdir() )
def rm(path):
os.remove(path)
def diskfree():
filesys = os.statvfs("/flash")
return(filesys[0] * filesys[3])
#Main run method. Run on startup.
#First check the time.
(YY, MM, DD, hh, mm, ss, wday, n1) = extrtc.get_time()
if YY == '1900' :
print('You need to set extrtc')
#Setup wake interrupt
pin = pyb.Pin.board.A0
pin.init(mode = pyb.Pin.IN, pull = pyb.Pin.PULL_DOWN)
# In this mode pin has pulldown enabled
stm.mem32[stm.PWR + stm.PWR_CR] |= 4 # set CWUF to clear WUF in PWR_CSR
stm.mem32[stm.PWR + stm.PWR_CSR] |= 0x100 # Enable wakeup
#Set next alarm time
extrtc.clearalarm()
nextwake = getnextalarm(hh)
extrtc.setalarm(nextwake)
print('\nWAKEUP', str(hh)+":"+str(mm), '=> **', nextwake, "hrs **. ALARM SET\n")
if(hh in schedule and mm<10 and hh in transmit):
#Time to send readings
satloop()
pyb.standby()
elif (hh in schedule and mm<10 and hh not in transmit):
#GPS reading time
if diskfree() > 1000 :
gpsloop()
else :
d("disk too full")
pyb.standby()
else:
#We've been woken up externally
print('Press CTRL-C Now to prevent going back to sleep!')
sleep(20)
pyb.standby()