From de9d8c1db5615aa24ae7d2ba575d31d952dfd3d9 Mon Sep 17 00:00:00 2001 From: Rashid Mahmood Date: Tue, 3 Nov 2026 13:16:57 +0500 Subject: [PATCH 1/2] Add docs example: log RPM and speed to CSV This document provides an example of logging RPM and speed data to a CSV file using the OBD library. It includes a Python code snippet and important notes regarding usage and vehicle compatibility. --- docs/csv-logging.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 docs/csv-logging.md diff --git a/docs/csv-logging.md b/docs/csv-logging.md new file mode 100644 index 00000000..51d02dfa --- /dev/null +++ b/docs/csv-logging.md @@ -0,0 +1,32 @@ +# Example: log RPM and speed to CSV + +This example queries engine RPM and vehicle speed once per second and appends each reading to a CSV file for later review in a spreadsheet. It uses only the public API from the README: `obd.OBD()`, `obd.commands.RPM`, `obd.commands.SPEED`, `connection.query()`, and the Pint unit values on `response.value`. + +```python +import csv +import time +import obd + +connection = obd.OBD() + +if not connection.is_connected(): + raise SystemExit("Could not connect to the vehicle. Check the adapter and try again.") + +with open("drive_log.csv", "w", newline="") as csvfile: + writer = csv.writer(csvfile) + writer.writerow(["timestamp", "rpm", "speed_kph"]) + for _ in range(60): + rpm = connection.query(obd.commands.RPM) + speed = connection.query(obd.commands.SPEED) + if rpm.is_null() or speed.is_null(): + time.sleep(1) + continue + writer.writerow([time.strftime("%Y-%m-%d %H:%M:%S"), round(rpm.value.magnitude), round(speed.value.to("kph").magnitude, 1)]) + time.sleep(1) +``` + +## Notes + +- Support varies by car. Before relying on a command, check it first with `connection.has_command(cmd)`. +- `response.value` carries Pint units, so `.to("kph")` or `.to("mph")` converts cleanly. +- Run this only while stationary or with a passenger operating the laptop, never while driving. From cc1fc6c69dbea4da026fd38990cc13a5b2b76397 Mon Sep 17 00:00:00 2001 From: Rashid Mahmood Date: Tue, 3 Nov 2026 13:17:30 +0500 Subject: [PATCH 2/2] Add CSV logging example to docs nav --- mkdocs.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index ad9086bb..7576b3cc 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -2,16 +2,16 @@ site_name: python-OBD repo_url: https://github.com/brendan-w/python-OBD repo_name: GitHub extra_javascript: -- assets/extra.js + - assets/extra.js nav: -- 'Getting Started': 'index.md' -- 'OBD Connections': 'Connections.md' -- 'Command Lookup': 'Command Lookup.md' -- 'Command Tables' : 'Command Tables.md' -- 'Responses': 'Responses.md' -- 'Async Connections': 'Async Connections.md' -- 'Custom Commands': 'Custom Commands.md' -- 'Debug': 'Debug.md' -- 'Troubleshooting': 'Troubleshooting.md' - + - 'Getting Started': 'index.md' + - 'OBD Connections': 'Connections.md' + - 'Command Lookup': 'Command Lookup.md' + - 'Command Tables': 'Command Tables.md' + - 'Responses': 'Responses.md' + - 'Async Connections': 'Async Connections.md' + - 'Custom Commands': 'Custom Commands.md' + - 'Debug': 'Debug.md' + - 'Troubleshooting': 'Troubleshooting.md' + - 'Example: log RPM and speed to CSV': 'csv-logging.md' theme: readthedocs