Basic Usage

This page explains the main components and core concepts of the library.

AT Commands

AT commands are special commands you send directly to the OBDII adapter. They let you configure it or retrieve information for/from the adapter.

main.py
1from obdii import at_commands
2
3# AT Command example, get the version ID of the adapter
4at_commands.VERSION_ID

Commands

Commands are predefined instructions to request data from the vehicle, like engine speed, coolant temperature, DTC, etc. There are three equivalent ways of using commands.

main.py
 1from obdii import commands
 2
 3# 1. Using a command directly
 4commands.ENGINE_SPEED
 5
 6# 2. Using the command name
 7commands["ENGINE_SPEED"]
 8
 9# 3. Using the Mode and PID
10commands[0x01][0x0C]
11
12# These three lines are equivalent
13# and all return the same command object:
14# <Command Mode.REQUEST 0C ENGINE_SPEED>

Scan for devices

Find your OBDII devices before you start. It could be connected via serial or WiFi. The obdii.utils.scan module makes this easy by scanning for available devices.

Note

Not sure which function to use ? Check Determining Your Port.

main.py
1from obdii.utils.scan import scan_ports, scan_wifi
2
3# Scan for devices connected via serial ports
4ports = scan_ports()
5print("Available OBDII devices:", ports)
6
7# Scan for devices connected via WiFi
8wifi_devices = scan_wifi()
9print("Available OBDII WiFi devices:", wifi_devices)

Query data

To read real-time data, you need an obdii.Connection to the device. Once connected, you can send commands and get responses from your car.

main.py
 1from obdii import Connection, at_commands, commands
 2from obdii.utils.scan import scan_ports
 3
 4# Find first available OBDII device connected via serial
 5ports = scan_ports(return_first=True)
 6if not ports:
 7    raise ValueError("No OBDII devices found.")
 8
 9# Connect to the device and query engine speed
10with Connection(ports[0]) as conn:
11    version = conn.query(at_commands.VERSION_ID)
12    print(f"Version: {version.value}")
13
14    response = conn.query(commands.ENGINE_SPEED)
15    print(f"Engine Speed: {response.value} {response.unit}")