-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_gps.py
executable file
·112 lines (92 loc) · 2.73 KB
/
start_gps.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
#!/usr/bin/env python
import gps
import sys
import time
import socket
import netifaces as ni
host = "192.168.1.132"
port = 4000
#log outputs
def writeToLog(the_string):
f = open("/home/pi/robo-ops/pi-clients/server_listener.log", "a")
f.write(the_string + "\n")
f.close()
def connect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
#find my ip
ni.ifaddresses('wlan0')
ip = ni.ifaddresses('wlan0')[2][0]['addr']
send(ip + "~", s)
return s
def send(a_str, s):
s.sendall(a_str)
if (len(sys.argv) < 2):
print(sys.argv[0])
print("gps: server_ip")
writeToLog("gps: server_ip")
sys.exit(1)
host = str(sys.argv[1])
print("gps: connecting to server with ip: " + str(host) + " and port: " + str(port))
writeToLog("gps: connecting to server with ip: " + str(host) + " and port: " + str(port))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
isConnected = 0
session = None
gps_connected = 0
while True:
#try to connect socket
while isConnected == 0:
try:
s = connect()
isConnected = 1
print("gps: connected to server")
writeToLog("gps: connected to server")
except socket.error:
time.sleep(1)
s.close()
isConnected = 0
break
#try to connect gps
while gps_connected == 0:
# try:
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
gps_connected = 1
#except:
# session = None
# gps_connected = 0
# print("gps: gps disconnected")
# writeToLog("gps: gps disconnected")
while True:
#try to send data
try:
report = session.next()
if report['class'] == 'TPV':
lat = ''
lon = ''
if hasattr(report, 'lat'):
lat = report.lat
print(report)
if hasattr(report, 'lat'):
lon = report.lon
send(("lat:" + str(lat) + ",lon:" + str(lon) + "|"), s)
print("lat:" + str(lat) + ",lon:" + str(lon) + "|")
except socket.error:
s.close()
isConnected = 0
break
except StopIteration:
session = None
gps_connected = 0
print "gps: gps disconnected"
writeToLog("gps: gps disconnected")
break
except:
session = None
gps_connected = 0
s.close()
isConnected = 0
print("gps: something went wrong")
writeToLog("gps: something went wrong")
break
s.close()