Friday 19 March 2021

Python obd doesn't connect raspberry pi to car

I have a Raspberry Pi 3 and I'm trying to hook it up to my car's OBD. I've got a Veepeak Mini Bluetooth OBD2 Scanner and I've tested it works perfectly fine with Torque on Android.

I've setup bluetooth on the Raspberry Pi and have paired the device, but when trying to run the following script, it works about 1 in 10 to 20 tries with the following logs:

import obd

obd.logger.setLevel(obd.logging.DEBUG)
connection = obd.OBD() # auto-connects to USB or RF port

cmd = obd.commands.RPM # select an OBD command (sensor)

response = connection.query(cmd) # send the command, and parse the response

print(response.value) # returns unit-bearing values thanks to Pint

connection.close()

When it fails (vast majority of time) it displays this:

$> python obdtest.py 
[obd.obd] ======================= python-OBD (v0.7.1) =======================
[obd.obd] Using scan_serial to select port
[obd.obd] Available ports: ['/dev/rfcomm47']
[obd.obd] Attempting to use port: /dev/rfcomm47
[obd.elm327] Initializing ELM327: PORT=/dev/rfcomm47 BAUD=auto PROTOCOL=auto
[obd.elm327] Response from baud 38400: '\x7f\x7f\r?\r\r>'
[obd.elm327] Choosing baud 38400
[obd.elm327] write: 'ATZ\r'
[obd.elm327] wait: 1 seconds
[obd.elm327] read: b'ATZ\r\r\rELM327 v1.5\r\r>'
[obd.elm327] write: 'ATE0\r'
[obd.elm327] read: b'ATE0\rOK\r\r>'
[obd.elm327] write: 'ATH1\r'
[obd.elm327] read: b'OK\r\r>'
[obd.elm327] write: 'ATL0\r'
[obd.elm327] read: b'OK\r\r>'
[obd.elm327] write: 'AT RV\r'
[obd.elm327] read: b'15.0V\r\r>'
[obd.elm327] write: 'ATSP0\r'
[obd.elm327] read: b'OK\r'
[obd.elm327] write: '0100\r'
[obd.elm327] read: b'>'
[obd.elm327] write: 'ATDPN\r'
[obd.elm327] read: b'SEARCHING...\r86 F1 10 41 00 BF BF F9 90 CF \r\r>'
[obd.elm327] Failed to retrieve current protocol
[obd.elm327] Adapter connected, but the ignition is off
[obd.obd] Cannot load commands: No connection to car
[obd.obd] ===================================================================
[obd.obd] '010C: Engine RPM' is not supported
None
[obd.obd] Closing connection
[obd.elm327] closing port
[obd.elm327] write: 'ATZ\r'

When it succeeds (1 in 10 or more tries) it displays this:

$> python obdtest.py 
[obd.obd] ======================= python-OBD (v0.7.1) =======================
[obd.obd] Using scan_serial to select port
[obd.obd] Available ports: ['/dev/rfcomm47']
[obd.obd] Attempting to use port: /dev/rfcomm47
[obd.elm327] Initializing ELM327: PORT=/dev/rfcomm47 BAUD=auto PROTOCOL=auto
[obd.elm327] Response from baud 38400: '\x7f\x7f\r?\r\r>'
[obd.elm327] Choosing baud 38400
[obd.elm327] write: 'ATZ\r'
[obd.elm327] wait: 1 seconds
[obd.elm327] read: b'ATZ\r\r\rELM327 v1.5\r\r>'
[obd.elm327] write: 'ATE0\r'
[obd.elm327] read: b'ATE0\rOK\r\r>'
[obd.elm327] write: 'ATH1\r'
[obd.elm327] read: b'OK\r\r>'
[obd.elm327] write: 'ATL0\r'
[obd.elm327] read: b'OK\r\r'
[obd.elm327] write: 'AT RV\r'
[obd.elm327] read: b'14.7V\r\r>'
[obd.elm327] write: 'ATSP0\r'
[obd.elm327] read: b'OK\r'
[obd.elm327] write: '0100\r'
[obd.elm327] read: b'SEARCHING...\r86 F1 10 41 00 BF BF F9 90 CF \r\r>'
[obd.elm327] write: 'ATDPN\r'
[obd.elm327] read: b'A5\r\r>'
[obd.protocols.protocol] map ECU 16 --> ENGINE
[obd.elm327] Connected Successfully: PORT=/dev/rfcomm47 BAUD=38400 PROTOCOL=5
[obd.obd] querying for supported commands
[obd.obd] Sending command: 0100: Supported PIDs [01-20]
[obd.elm327] write: '0100\r'
[obd.elm327] read: b'86 F1 10 41 00 BF BF F9 90 CF \r\r>'
[obd.obd] finished querying with 51 commands supported
[obd.obd] ===================================================================
[obd.obd] Sending command: 010C: Engine RPM
[obd.elm327] write: '010C\r'
[obd.elm327] read: b'84 F1 10 41 0C 08 3C 16 \r\r>'
527.0 revolutions_per_minute
[obd.obd] Closing connection
[obd.elm327] closing port
[obd.elm327] write: 'ATZ\r'

Note that 84 F1 10 41 0C 08 3C 16 is not the OBD scanner's MAC address. Also rfcomm47 is set manually to a random value, just happened to pick 47.

What's consistent about the success logs is the number of the protocol (5). I've seen somewhere else a suggestion to just keep trying to connect, but when I'm adding something like this

while len(connection.supported_commands) < 100:
    connection = obd.Async("/dev/rfcomm47", protocol = "5", baudrate = "38400", fast = False, timeout = 30)

I'm getting those logs every time it is called from the second time on:

$> python sandboxobd.py 
<<startup logs>>
[obd.elm327] write: 'ATDPN\r'
[obd.elm327] read: b'SEARCHING...\r86 F1 10 41 00 BF BF F9 90 CF \r\r>'
[obd.elm327] Failed to retrieve current protocol
[obd.elm327] Adapter connected, but the ignition is off
[obd.obd] Cannot load commands: No connection to car
[obd.obd] ===================================================================
[obd.obd] ======================= python-OBD (v0.7.1) =======================
<<startup logs>>
[obd.elm327] read: b'>'
[obd.elm327] write: 'ATDPN\r'
[obd.elm327] read: b'SEARCHING...\rUNABLE TO CONNECT\r\r>'
[obd.elm327] Failed to retrieve current protocol
[obd.elm327] Adapter connected, but the ignition is off
[obd.obd] Cannot load commands: No connection to car
[obd.obd] ===================================================================

With the second part repeating indefinitely. Connecting directly to protocol 5 using connection = obd.OBD(protocol="5") gives me this:

[obd.elm327] Connected Successfully: PORT=/dev/rfcomm47 BAUD=38400 PROTOCOL=5
[obd.obd] querying for supported commands
[obd.obd] Sending command: 0100: Supported PIDs [01-20]
[obd.elm327] write: '0100\r'
[obd.elm327] read: b'BUS INIT: STOPPED\r\r>'
[obd.OBDCommand] 0100: Supported PIDs [01-20] did not receive any acceptable messages
[obd.obd] No valid data for PID listing command: 0100: Supported PIDs [01-20]
[obd.obd] finished querying with 7 commands supported
[obd.obd] ===================================================================
[obd.obd] '010C: Engine RPM' is not supported
None
[obd.obd] Closing connection
[obd.elm327] closing port
[obd.elm327] write: 'ATZ\r'

Did anybody encounter this issue before? I can't think of anything to else to try and see if it works. Any help would be greately appreciated.



from Python obd doesn't connect raspberry pi to car

No comments:

Post a Comment