Search code examples
pythonpyserialobd-ii

OBD2 Python Getting Speed


I'm having a little trouble with getting the correct speed out of my Python OBD2 reading program. It stays at 13 even if I'm not moving. I have based my code off of pi2go on git hub. Though no matter what the speed_float value is always 13 once converted from hex to a float.

def speed(self, oldValues):
    """ Gets the speed of the vehicle """
    if self.serialIO is None:
        return "Serial IO not setup."
    self.serialWrite("0D")
    speed_list = self.serialRead()
    if speed_list == -1 or speed_list == 0:
        print("There is an issue with reading the speed of the vehicle.")
        return 0
    else:
        speed_hex = speed_list[0]
        speed_float = float(int("0x" + speed_hex, 0))
        print("Speed float = " + str(speed_float))
        if speedFormat == "mph":
            # display speed in miles per hour
            #speed_float = speed_float * 0.621371
            speed_float = speed_float * 1.609 - 20.917 #made it go to zero by subtracting 20.917

            print("mph = " + str(speed_float))
        elif speedFormat == "kph":
            # display speed in kilometers per hour
            print("kph = " + str(speed_float))
            return speed_float
        else:
            # error
            print("Configuration is wrong. Please check config.py for speedFormat")
    return speed_float

After I made the mph value zero it stays at zero. It never changes. The equation above that makes it something like 8.0. My issue is how do I get the actual speed.


Solution

  • After wasting time at work on researching this issue I was having, I found a Java open source project on git hub doing the same thing. Instead of using the first location of the list it used the second. Instead of

    speed_hex = speed_list[0]
    

    I changed it to

    speed_hex = speed_list[1]
    

    It now works correctly.