diff --git a/deploy/yaris-obd.service b/deploy/yaris-obd.service index d608685..7cbf669 100644 --- a/deploy/yaris-obd.service +++ b/deploy/yaris-obd.service @@ -12,6 +12,11 @@ WorkingDirectory=/home/yaris/app/yaris-obd # the log file promptly instead of sitting in Python's stdout buffer indefinitely # when it's redirected to a file instead of a TTY. Environment=PYTHONUNBUFFERED=1 +# Once OBD is confirmed working on this car, pin the protocol to skip the +# 3-5s auto-detect probe on every ignition-on: e.g. OBD_PROTOCOL=6 for +# ISO 15765-4 CAN 11-bit 500kbps (correct for 2013 Toyota Yaris). +# Leave unset during initial setup so auto-detect can find the right value. +#Environment=OBD_PROTOCOL=6 ExecStart=/home/yaris/app/yaris-obd/venv/bin/python app.py Restart=on-failure RestartSec=5 diff --git a/modules/app_obd.py b/modules/app_obd.py index c564b42..805c57e 100644 --- a/modules/app_obd.py +++ b/modules/app_obd.py @@ -52,10 +52,13 @@ connection = None def _decode_odometer(messages): - data = messages[0].data[2:] - if len(data) != 4: - raise ValueError(f"expected 4 odometer bytes, received {len(data)}") - return int.from_bytes(bytes(data), byteorder="big") / 10 + try: + data = messages[0].data[2:] + if len(data) != 4: + return None + return int.from_bytes(bytes(data), byteorder="big") / 10 + except Exception: + return None ODOMETER_COMMAND = obd.OBDCommand( @@ -225,6 +228,21 @@ def connect_obd(): _emit_disconnected() return None + # Reset stale sensor values from the previous ignition cycle so the + # UI doesn't briefly display outdated readings before the first polls. + global rpm, coolant_temp, fuel_level, throttle_position, mil_on, dtc_codes, odometer + global mil_updated_at, dtc_updated_at, speed_updated_at + rpm = None + coolant_temp = None + fuel_level = None + throttle_position = None + mil_on = None + dtc_codes = [] + odometer = None + mil_updated_at = None + dtc_updated_at = None + speed_updated_at = None + _log_connection_diagnostics(candidate) app_events.socketio.emit("obd_status", obd_status[1]) _start_polling_threads()