-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChewyMon.py
executable file
·63 lines (53 loc) · 1.68 KB
/
ChewyMon.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
#!/usr/bin/env python
import time
import sys
import RPi.GPIO as io
import MySQLdb as sql
import logging as log
# Logging
log.basicConfig(filename='chewymon.log', level=log.DEBUG, format='%(asctime)s.%(msecs)d - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S')
# setup input
io.setmode(io.BCM)
wheel_pin = 23
io.setup(wheel_pin, io.IN, pull_up_down=io.PUD_UP) # activate input with PullUp
# MySQL DB connection
DbUser = ""
DbPass = ""
DbName = ""
try:
db = sql.connect("localhost", DbUser, DbPass, DbName)
curs = db.cursor()
log.info("Connected to database %s", DbName)
except:
log.error("Can't connect to database %s...exiting", DbName)
sys.exit(0)
# Globals
wheel_circ = 33.772121026090275 #inches
lasttime = time.time()
# Return MPH given period of single wheel turn
def getSpeed(period):
global wheel_circ
speed = (wheel_circ*.0568181818)/period
return speed
# Return RPM given period of single wheel turn
def getRPM(period):
return round(60.0/period)
log.info('Waiting for rotations...')
try:
while True:
io.wait_for_edge(wheel_pin, io.RISING)
runtime = time.time()
log.debug('Rising edge detected - %s' % runtime)
period = (runtime - lasttime)
if (period > 4):
period = 0 # zero out speed and rpm between runs
log.info('Period greater than 4 seconds, zeroing')
sql = "INSERT INTO rundata(runtime,speed,rpm) VALUES(%s, %s, %s)" % (runtime, getSpeed(period), getRPM(period))
log.debug(sql)
curs.execute(sql)
lasttime = runtime
db.commit()
io.wait_for_edge(wheel_pin, io.FALLING)
log.debug('Falling edge detected - %s' % time.time())
except KeyboardInterrupt:
io.cleanup()