Repository navigation
Expand file tree
/
Copy pathgsmtrack.py
More file actions
executable file
·130 lines (108 loc) · 4.1 KB
/
Copy pathgsmtrack.py
File metadata and controls
executable file
·130 lines (108 loc) · 4.1 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
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
import time
import math
import sys
from pygsm import GsmModem
def CalculateDistance(Latitude1, Longitude1, Latitude2, Longitude2):
Latitude1 = Latitude1 * math.pi / 180
Longitude1 = Longitude1 * math.pi / 180
Latitude2 = Latitude2 * math.pi / 180
Longitude2 = Longitude2 * math.pi / 180
return 6371000 * math.acos(math.sin(Latitude2) * math.sin(Latitude1) + math.cos(Latitude2) * math.cos(Latitude1) * math.cos(Longitude2-Longitude1))
if len(sys.argv) < 3:
print ""
print "Usage: python gsmtrack.py <payload_ID> <phone_number> [gateway_number]"
print ""
print "payload_ID is the payload ID to include in text messages."
print "phone_number is the number of a mobile phone to send texts to."
print "gateway_number (optional) is the number of a gateway to send texts to."
print ""
print "Texts to phone_number will contain the GPS position including a Google map link"
print "Texts to gateway_number are intended to be processed by a script (e.g. uploaded to the HAB map)"
print ""
print "Example: python gsmtrack.py 07000987621 07000987622"
print ""
quit()
PayloadID = sys.argv[1]
MobileNumber = sys.argv[2]
print "Texts will be sent to mobile phone " + MobileNumber
if len(sys.argv) > 3:
GatewayNumber = sys.argv[3]
print "Texts will be sent to gateway number " + GatewayNumber
else:
GatewayNumber = None
SendTimeout = 5 # Send position every x minutes regardless of movement
HorizontalDelta = 50 # Send position if it moves horizontally by at keast this many metres
VerticalDelta = 50 # Send position if it moves vertically by at least this many metres
MaxGSMAltitude = 2000 # Don't try to send above this altitude
PreviousSeconds = 0
PreviousAltitude = 0
PreviousLatitude = 0
PreviousLongitude = 0
gsm = GsmModem(port="/dev/ttyAMA0")
gsm.boot()
print "GPS/GSM UKHAS Tracker"
print "====================="
print
print "Modem details ..."
reply = gsm.hardware()
print "Manufacturer = " + reply['manufacturer']
print "Model = " + reply['model']
print
# Try and get phone number
reply = gsm.command('AT+CNUM')
if len(reply) > 1:
list = reply[0].split(",")
phone = list[1].strip('\"')
print "Phone number = " + phone
print
# Switch GPS on
print "Switching GPS on ..."
reply = gsm.command('AT+CGNSPWR=1')
print reply
while True:
# Get position
reply = gsm.command('AT+CGNSINF')
list = reply[0].split(",")
if len(list[2]) > 14:
UTC = list[2][8:10]+':'+list[2][10:12]+':'+list[2][12:14]
Latitude = list[3]
Longitude = list[4]
Altitude = list[5]
print 'Position: ' + UTC + ', ' + Latitude + ', ' + Longitude + ', ' + Altitude
Seconds = int(UTC[0:2]) * 3600 + int(UTC[3:5]) * 60 + int(UTC[6:8])
if Altitude <> '':
Latitude = float(Latitude)
Longitude = float(Longitude)
Altitude = float(Altitude)
if Seconds < PreviousSeconds:
PreviousSeconds = PreviousSeconds - 86400
# Send now ?
if Altitude <= MaxGSMAltitude:
# Low enough
Send = False
if Seconds > (PreviousSeconds + SendTimeout * 60):
Send = True
print("Send because of timeout")
Distance = abs(CalculateDistance(Latitude, Longitude, PreviousLatitude, PreviousLongitude))
if Distance >= HorizontalDelta:
Send = True
print("Send because of horizontal movement")
if abs(Altitude - PreviousAltitude) >= VerticalDelta:
Send = True
print("Send because of vertical movement")
if Send:
PreviousSeconds = Seconds
PreviousAltitude = Altitude
PreviousLatitude = Latitude
PreviousLongitude = Longitude
# Text to my mobile
Message = PayloadID + ' position: ' + UTC + ', ' + str(Latitude) + ', ' + str(Longitude) + ', ' + str(int(Altitude)) + ' http://maps.google.com/?q=' + str(Latitude) + ',' + str(Longitude)
print "Sending to mobile " + MobileNumber + ": " + Message
gsm.send_sms(MobileNumber, Message)
# Text to my gateway
if GatewayNumber:
print ("Sending text to gateway")
Message = 'HAB:' + PayloadID + ',1,' + UTC + ',' + str(Latitude) + ',' + str(Longitude) + ',' + str(int(Altitude))
print "Sending to gateway " + GatewayNumber + ": " + Message
gsm.send_sms(GatewayNumber, Message)
time.sleep(1)