-
Notifications
You must be signed in to change notification settings - Fork 2
/
BikeConnector.py
executable file
·174 lines (163 loc) · 5.96 KB
/
BikeConnector.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
#!/usr/bin/python
from Adafruit_CharLCDPlate.Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from BUGswarm import apikey
from BUGswarm import resource
from BUGswarm import participation
from pynmea import nmea
import logging
from datetime import datetime
from commands import getoutput
import time
import serial
import json
import socket
class BikeConnector:
def __init__(self, resource_name, device="/dev/ttyUSB0", lcd=Adafruit_CharLCDPlate()):
logging.basicConfig(level=logging.INFO)
self.api = apikey.apikey("demo", "buglabs55", port=8080)
self.res = resource.getResourceByName(self.api, resource_name)
self.swarms = self.res.getSwarms()
self.airquality = 1024
self.sats = 0
self.tx = 0
self.rx = 0
self.locationupdates = 0
self.airupdates = 0
self.lcd = lcd
self.lcd.clear()
self.lastCapabilities = time.time()
self.lastLCDUpdate = time.time()
self.ser = serial.Serial(device, 9600, timeout=1)
self.warmedup = False
self.gpslock = False
getoutput('mkdir /home/pi/bikelogs/')
self.f = open(time.strftime("/home/pi/bikelogs/bikelog_%m%d%y_%H%M%S.csv", "w"))
self.f.write('time(min),latitude,longitude,airquality\n')
self.f.flush()
self.starttime = time.now()
def runLoop(self):
self.running = True
reconnect = True
try:
self.pt = participation.participationThread(self.api, self.res, self.swarms,
onPresence=self.presence, onMessage=self.message, onError=self.error)
self.running = self.pt.connected
while(self.running):
line = self.ser.readline()
if len(line) > 5:
self.parseSerial(line)
if time.time() > self.lastCapabilities + 5.0:
self.sendCapabilities()
self.lastCapabilities = time.time()
if time.time() > self.lastLCDUpdate + 1.0 and self.running:
self.lcd.clear()
self.lcd.message("sats:"+str(self.sats)+" Air:"+str(self.airquality)+"\n")
if not self.warmedup:
self.lcd.message("Sensor WarmingUp\n")
elif not self.gpslock:
self.lcd.message("No GPS Fix\n")
else:
self.lcd.message("tx:"+str(self.tx)+" rx:"+str(self.rx)+"\n")
self.lastLCDUpdate = time.time()
if self.lcd.buttonPressed(self.lcd.SELECT):
reconnect = False
self.running = False
except Exception as e:
reconnect = True
print e
finally:
print "Loop quit"
return reconnect
def sendCapabilities(self):
msg = {"capabilities": {
"feeds": ["Location", "AirQuality"]
}}
self.produce(msg)
def NMEAPostoDec(self, param):
if len(param) < 2:
return None
period = param.find('.')
pos = float(param[period-2:])/60.0
pos = pos + int(param[:period-2])
return pos
def parseSerial(self, msg):
payload = {}
if msg.startswith("$GPGGA"):
pos = nmea.GPGGA()
pos.parse(msg)
lat = self.NMEAPostoDec(pos.latitude)
lon = self.NMEAPostoDec(pos.longitude)
#western hemisphere HACK
if lon != None:
lon = lon * -1
payload = { "name": "Location",
"feed": {
"latitude": lat,
"longitude": lon,
"valid": (int(pos.gps_qual) > 0),
"satellites": int(pos.num_sats)
}}
self.sats = int(pos.num_sats)
self.gpslock = (int(pos.gps_qual) > 0)
self.locationupdates = self.locationupdates + 1
if self.locationupdates%6==0:
self.produce(payload)
self.f.write(str((time.time()-self.starttime)/60.0)+','+
str(lat)+','+str(lon)+','+str(self.airquality)+'\n')
self.f.flush()
if msg.startswith("$GPOSD"):
msg = msg[:msg.find('*')]
vals = msg.split(',')
payload = { "name": "AirQuality",
"feed": {
"value": int(vals[1])
}}
if int(vals[1]) > self.airquality:
self.warmedup = True
self.airquality = int(vals[1])
self.airupdates = self.airupdates + 1
if self.airupdates%6==2:
self.produce(payload)
def produce(self, obj):
msg = json.dumps(obj)
#print msg
try:
self.pt.produce(msg)
except socket.timeout:
self.stop()
self.lcd.clear()
self.lcd.message("ERROR DISCONNECT\n")
print "Socket timeout, lets reconnect"
self.tx = self.tx + 1
def presence(self, obj):
print "presence from "+obj['from']['resource']
def message(self, obj):
try:
resid = obj["from"]["resource"]
if resid == self.res.id:
return
except Exception as e:
print e
return
self.rx = self.rx + 1
print "message "+str(obj['payload'])
def error(self, obj):
print "error "+str(obj['errors'])
def stop(self):
self.running = False
if self.pt:
self.pt.stop()
if __name__ == '__main__':
lcd=Adafruit_CharLCDPlate()
bike = BikeConnector("Bike01", lcd=lcd)
try:
while(True):
bike.runLoop()
lcd.message("Please wait\n")
time.sleep(1.0)
lcd.clear()
lcd.message("Reconnecting\n")
except KeyboardInterrupt:
print "Quit the loop"
bike.stop()
print "Should be quit..."