Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/csv-logging.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 11 additions & 11 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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