python-OBD icon indicating copy to clipboard operation
python-OBD copied to clipboard

OBD speed not updated after restarting car ecu

Open tharunjoy opened this issue 7 years ago • 2 comments

Hi, I recently used python-OBD to monitor real time car speed using Raspberry pi2. I was able to successfully connect to obd and watch the car speed real-time. But if I switch the car ignition OFF, I am getting None value in speed and after while switching ON the ignition the speed value is not updated. Please help me to resolve and thanks in advance

tharunjoy avatar Jul 22 '17 14:07 tharunjoy

I'm having the same problem. I'm using the Async

connection.watch(obd.commands.RPM, callback=update_rpm, force=True) # keep track of the RPM connection.start()

If I start the connection before the vehicle is running, I won't get any data. If I stop the engine and start again, I won't get any data.

I only get data when I start watching and the vehicle is already running.

bcavileer avatar Nov 19 '17 15:11 bcavileer

The only way that I found to solve this problem is starting a new connection for every null RPM value. I don't know if this is the better solution, but works.

exemple:

import sys
import obd
import time

stop = False
    
def new_rpm(r):
    global stop
    if not r.is_null():
        value = r.value.magnitude
        stop = False
    else:
        value = 0
        stop = True
    print (value)
    sys.stdout.flush()
    time.sleep(0.3)
    
def new_speed(r):
    if not r.is_null():
        value = r.value.magnitude
    else:
        value = 0
    print (value)
    sys.stdout.flush()
    time.sleep(0.3)

def connect():
    print("connecting")
    global connection
    connection = obd.Async("/dev/ttyUSB0", fast=False)
    connection.watch(obd.commands.RPM, callback=new_rpm, force=True)
    connection.watch(obd.commands.SPEED, callback=new_speed, force=True)
    connection.start()

connect()

while True:
    if stop == False:
        time.sleep(1)
    else:
        connection.stop()
        connection.unwatch_all()
        stop = False
        connect()

andrepbap avatar Apr 03 '18 13:04 andrepbap