> For the complete documentation index, see [llms.txt](https://docs.macchina.cc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.macchina.cc/projects/p1-projects/p1-can-vehicle-data.md).

# P1 CAN Vehicle Data

Use P1 to read vehicle data and export it to a CSV file.

#### Items Used

* 1x [P1](https://www.macchina.cc/catalog/p1-boards/p1-under-dash)
* 1x Car with OBD-II port

#### Download P1 Vehicle Data Python Program

After connecting to P1 via SSH, run the command below to download the python program and save it to \~/scripts. (If using putty to SSH, just right click to paste!)

```
wget -P ~/scripts https://gist.githubusercontent.com/kenny-macchina/cf47daf663f9091460fc3454f5e38de9/raw/676f2a36f300fa8e5c9c43c05a7bc18af2479b5c/p1_data.py
```

![Downloading the python program](https://1875989339-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lhg5aaQ-eiSLOcDoH85%2F-MB_JtbW9niXojluyjR8%2F-MBdsXg6Y2fBvKroH5dG%2Fwget.png?alt=media\&token=b825165d-5031-42c4-8816-42115c88ca58)

#### Install Pip

Run the commands below to install the "Pip" package manager and reboot.

```
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
sudo reboot now
```

![Installing Pip, curl and reboot not shown.](https://1875989339-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lhg5aaQ-eiSLOcDoH85%2F-MB_JtbW9niXojluyjR8%2F-MBeBM6Egbq-GPjrOMCY%2Fpip2.png?alt=media\&token=04ebc17e-6dfc-4d8d-91e2-d1fec87ee404)

#### Install Python-CAN Library

Run the commands below to install the python-can library and reboot.

```
pip install python-can
sudo reboot now
```

![Installing python-can, reboot not shown.](https://1875989339-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lhg5aaQ-eiSLOcDoH85%2F-MB_JtbW9niXojluyjR8%2F-MBeBClG2gqUeVufii_3%2Fpython-can.png?alt=media\&token=11814472-76da-4e49-a137-da0ea7217448)

#### Collecting Data

First, start your car, then plug P1 into the OBD-II port and run the data collection program using the command below. (Make sure you're in the home directory beforehand by running the command "cd".) P1 will prompt for a password as the program sets up the CAN0 bus. After entering the password data collection will begin until the program is halted by pressing CTRL-C.

```
python scripts/p1_data.py
```

![Terminal while data collection is running](https://1875989339-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lhg5aaQ-eiSLOcDoH85%2F-MBeCkNhd7Ui79InOeIT%2F-MBeFexvWZz3AKtqF_30%2Fcollect.png?alt=media\&token=fbf37520-4d40-467c-a936-e307e3edac8c)

#### Retrieving Data

To download the CSV file from P1, the command below can be used to host an HTTP server on the port specified, this can then be visited in a web browser (from a computer on the same LAN) and the file(s) downloaded.

```
python3 -m http.server 1337
```

![Hosting the HTTP server](https://1875989339-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lhg5aaQ-eiSLOcDoH85%2F-MBeCkNhd7Ui79InOeIT%2F-MBeFvlVRh_WMpX2d0vJ%2Fhttp.png?alt=media\&token=7eaa3d0b-10c4-4af7-b8ae-fa741837a4ea)

![Retrieving the file from a browser](https://1875989339-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lhg5aaQ-eiSLOcDoH85%2F-MBeCkNhd7Ui79InOeIT%2F-MBeG7bEBQ8mB-Rmc2_8%2Fhttp2.png?alt=media\&token=6b4d4dc0-0008-4473-bef2-9df15aa8013e)

#### Example Results

![Vehicle and engine speed for a short drive around the block](https://1875989339-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lhg5aaQ-eiSLOcDoH85%2F-MBeKAfOJuljNSPNK4rt%2F-MBeNJTbNzjnbtIppcuA%2Fgraph.png?alt=media\&token=2bb0b7cf-0a74-419c-a8b5-781404c0738a)

![Coolant temperature from the same test drive](https://1875989339-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lhg5aaQ-eiSLOcDoH85%2F-MBeKAfOJuljNSPNK4rt%2F-MBeNMyQnUQ6wm6aYnjJ%2Fgraph2.png?alt=media\&token=f37d7cc3-f841-40ca-9cfe-a9311f50421c)
