-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed.py
64 lines (49 loc) · 1.51 KB
/
speed.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
import RPi.GPIO as GPIO
import time
import pyrebase
import datetime
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
config = {
"apiKey": "*******************************",
"authDomain": "************************************",
"databaseURL": "************************************",
"storageBucket": "************************************",
"serviceAccount": "************************************"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
GPIO.setup(14, GPIO.IN, pull_up_down = GPIO.PUD_UP)
circum = 66 # cirumference of the wheel in inches
start = time.time()
sensorStatus = False
mph = 0
dist = 0
odo = int(db.child("odometer").get().val())
def main():
try:
global start, sensorStatus, mph, dist, odo
while True:
if (time.time() - start) > 1:
mph = 0
db.update({'speed': mph})
db.update({'odometer': odo})
db.update({'trip_distance': round((dist / 63360), 2)})
db.update({'last_update': str(datetime.datetime.utcnow())})
f = open("speed.txt", "w")
f.write(str(mph))
f.close()
except KeyboardInterrupt:
GPIO.cleanup()
def speedy(number):
global start, mph, dist, odo
end = time.time()
dur = end - start
start = end
dist += 66
odo += 66
mph = round((circum * 3600) / (dur * 63360), 2)
print(mph)
GPIO.add_event_detect(14, GPIO.RISING, callback=speedy)
if __name__=="__main__":
main()