brendan-w / python-OBD

OBD-II serial module for reading engine data
GNU General Public License v2.0
1.02k stars 360 forks source link

Get voltage from adapter while connected to vehicle #233

Open rack201 opened 2 years ago

rack201 commented 2 years ago

With no supported PID for battery voltage on my car, I'm pulling it from my adapter via ATRV in my main OBD loop. Not a programmer and never used python, so wonder if there is a more elegant way to do this. Tried to build a custom command but as it's not an OBD response I couldn't make it work.

def obdStuff():
    tic=time.time()
    c = obd.OBD("/dev/rfcomm0", baudrate=115200)
    time.sleep(1)
    ser = Serial("/dev/rfcomm0", 115200, timeout=0.5)
    while True:
        toc=time.time()
        try:
            if toc-tic > 5:
                ser.write(b'ATRV\r')
                data_in = ser.read(ser.in_waiting)
                if str(data_in, 'UTF8') !="":
                    #print("V is:", str(data_in, 'UTF8'))
                    client.publish("Voltage", str(data_in, 'UTF8'))
                    tic = time.time()
                    ...
incense commented 2 years ago

The following might be what you have been looking for: cmd = obd.commands.ELM_VOLTAGE (found in commands.py). Haven't tested it though.