def run(self): """ Main class function :return: """ while True: if self.connection: if not self.connection.status() == OBDStatus.CAR_CONNECTED: self.connection = OBD(self.port, self.baud, self.protocol) elif self.connection.status() == OBDStatus.CAR_CONNECTED: self.get_all_pids() else: self.init_bt() self.connection = OBD(self.port, self.baud, self.protocol)
def speedometer(): speedData = OBD().query(commands.SPEED).value.to("mph") speedText = str(speedData) + " mph" speed.config(text=speedText, bg='black', fg='#F4511E', font=("Helvetica", 72, "bold")) speed.place(anchor='center', relx=0.5, rely=0.5) root.after(1000, speedometer) # Update speedometer every second
# Weirdly the OBD port doesn't differentiate forward movement from backward movement class OBDReading(): def __init__(self, connection): self.throttle = connection.query(commands.THROTTLE_ACTUATOR).value.magnitude self.rpm = connection.query(commands.RPM).value.magnitude self.engineLoad = connection.query(commands.ENGINE_LOAD).value.magnitude self.speed = connection.query(commands.SPEED).value.magnitude if self.speed == 0 or self.speed is None: self.moving = False else: print('moving') self.moving = True self.csvstring = "{},{},{},{},".format(self.throttle,self.rpm,self.engineLoad,self.speed) conn = OBD(portstr='/dev/ttyUSB0',baudrate=38400,fast=False) with open('/home/stephen/loggeddata.csv', 'w', newline='') as csvfile: if conn.status() == OBDStatus.CAR_CONNECTED: print(conn.status()) try: while True: reading = [] print('reading reset') reading.append(OBDReading(conn)) if not reading[0].moving: reading.append(OBDReading(conn)) if reading[1].moving: print('{},{}'.format(reading[0].moving, reading[0].speed)) print('{},{}'.format(reading[1].moving, reading[1].speed)) print('beginlogging') for _ in range(0,10):
# Blink display led 5 times to show device has started # led will blink at 2500ms intervals while data is uploaded # to firebase led = LED(17) led.off() for i in range(0, 4): led.on() sleep(.5) led.off() sleep(.5) # Connect to Car ECU ecuAttempt = 1 try: f.write('Attempting Connection to OBD \n') connection = OBD() f.write(str(connection) + '\n') except: sleep(.25) f.write("Connection to ECU #:{} unsuccsesfull, retrying...\n".format( str(ecuAttempt))) ecuAttempt += 1 break # Fetch the service account key for firebase from JSON file contents & initilize cred = credentials.Certificate('OBDII.json') initialize_app( cred, {'databaseURL': 'https://obdiidata.firebaseio.com/'}) ref = db.reference('/')
from obd import OBD # Create the OBD object - we will leave the port blank so the app will run a port scan and let us know which port # the OBD connector will be plugged into. This will be different on each device so this may help debug some initial # issues when running on the pi for the first time conn = OBD(portstr=None, baudrate=None, protocol=None, fast=True)