ProwtProject
ProwtProject

Reputation: 11

Connecting ELM327 OBD to Raspberry Pi

I'm trying to connect elm327 Bluetooth with my raspberry pi and use the obd lib in python code to get car speed.

I've bind the device as below: sudo rfcomm bind /dev/rfcomm0 xx:xx:xx:xx:xx:xx

Result of that when writing sudo rfcomm: rfcomm0: xx:xx:xx:xx:xx:xx channel 1 clean

Now my python script:

import obd
import time

def connect_to_obd():
    connection = None
    while connection is None or not connection.is_connected():
        try:
            connection = obd.OBD("/dev/rfcomm0")
        except :
            print("Failed to connect to OBD-II adapter. Retrying in 5 seconds...")
            time.sleep(5)
    return connection

def get_vehicle_speed(connection):
    if connection.is_connected():
        cmd = obd.commands.SPEED  # select an OBD command (sensor)
        response = connection.query(cmd)  # send the command, and parse the response
        if response.value:
            return response.value.to("km/h").magnitude
    return 0

connection = connect_to_obd()
vehicle_speed = get_vehicle_speed(connection)
print(f"Vehicle Speed: {vehicle_speed} km/h")

Error MSG when ruining my code:

serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

I did make sure that no other process is using the /dev/rfcomm0 port

Please any solution?

After connecting the ELM327 using Bluetooth and downloading all required lbs I expected my code run without errors and shows the car speed, which is did not.

Upvotes: 0

Views: 320

Answers (0)

Related Questions