{"id":15172866,"url":"https://github.com/jonnoftw/rpi-can-logger","last_synced_at":"2025-10-26T04:30:45.674Z","repository":{"id":86717090,"uuid":"95529193","full_name":"JonnoFTW/rpi-can-logger","owner":"JonnoFTW","description":"Project to log CAN bus data from a PiCAN2 and a GPS module","archived":false,"fork":false,"pushed_at":"2021-03-14T01:05:28.000Z","size":236,"stargazers_count":132,"open_issues_count":2,"forks_count":20,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-01-31T14:21:47.567Z","etag":null,"topics":["can-bus","car-hacking","fms","gps","pican2","python3","raspberry-pi-3","raspberry-pi-gpio","rpi","rpi-zero-w","sd-card","tesla"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JonnoFTW.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-06-27T07:17:12.000Z","updated_at":"2025-01-29T12:04:38.000Z","dependencies_parsed_at":"2023-07-12T10:45:53.415Z","dependency_job_id":null,"html_url":"https://github.com/JonnoFTW/rpi-can-logger","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonnoFTW%2Frpi-can-logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonnoFTW%2Frpi-can-logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonnoFTW%2Frpi-can-logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonnoFTW%2Frpi-can-logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JonnoFTW","download_url":"https://codeload.github.com/JonnoFTW/rpi-can-logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238258972,"owners_count":19442509,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["can-bus","car-hacking","fms","gps","pican2","python3","raspberry-pi-3","raspberry-pi-gpio","rpi","rpi-zero-w","sd-card","tesla"],"created_at":"2024-09-27T10:21:31.432Z","updated_at":"2025-10-26T04:30:40.281Z","avatar_url":"https://github.com/JonnoFTW.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Raspberry PI CAN Bus Logger\n\nThis project provides code for logging CAN Bus data with a Raspberry Pi. It additionally also logs GPS data. All of this data is stored on an SD card and can then be easily uploaded to a server for easy viewing.\n\n## Features\n\n* Logs and interprets CAN bus data from:\n  * OBD2\n  * Tesla vehicles\n  * Bus and Truck with FMS\n  * Outlander PHEV\n* Logs GPS\n* Can operate in querying and sniffing mode\n* Stores data on SD card. Can be configured to automatically upload via web API when connected to WiFi or 4G internet.\n* Can be powered entirely from power provided by the OBD port in your vehicle!  You can also wire it into your fuse box or cigarette lighter to prevent it being powered permanently and draining your battery.\n* Accompanying [Bluetooth App](https://github.com/JonnoFTW/OBD-Datalogger) to:\n  * Visualise your data in realtime \n  * Fetch and upload stored data  \n* [Web based data visualiser](https://github.com/JonnoFTW/webcan)\n\n## Parts\n\nThe following parts are used:\n\n* Raspberry Pi 3 Model B or Raspberry Pi Zero W\n* [PiCAN CAN-Bus board](http://skpang.co.uk/catalog/pican2-duo-canbus-board-for-raspberry-pi-23-p-1480.html) or equivalent PiCAN product with 1 or 2 CAN buses. Any CAN receiver compatible with [python-can](https://python-can.readthedocs.io/en/latest/index.html) should work though.\n* [GPS Receiver](https://www.dfrobot.com/product-1103.html)\n* [DC-DC Converter](https://www.digikey.com.au/products/en?keywords=1597-1243-ND)\n\n\nIf you want WiFi to work with the PiCAN2 shield attached, you'll need to unsolder the GPIO pins and drop them to the bottom and reattach the shield.\n\n\nIf you are using the DC-DC converter, you will need to plug its outputs into the GPIO pins of the Raspberry Pi at pins 2 (5v power) and 6 (ground):\n\n![Rpi Pins](https://www.element14.com/community/servlet/JiveServlet/previewBody/73950-102-11-339300/pi3_gpio.png)\n\nTo make your own OBD connector, you will need:\n\n* [OBD Male connector](http://au.rs-online.com/web/p/automotive-connectors/8010991/)\n* [Crimps](http://au.rs-online.com/web/p/automotive-connector-accessories/8010884/)\n\nYou'll only need to connect pins:\n\n* 4 Chassis Ground\n* 6 CAN High\n* 14 CAN Low\n* 16 12V Power\n\n![OBD Pins](http://www.mbcluster.com/Old_Website/Media_Diagnostics/ODBII%20Master%20Pinout.jpg)\n\n\n### Tesla\n\nPlease follow steps 1 and 2 from this instructable to access the Tesla CAN bus http://www.instructables.com/id/Exploring-the-Tesla-Model-S-CAN-Bus/: \n \nYou will need the following parts:\n \n * [Tesla port](http://au.rs-online.com/web/p/pcb-connector-housings/7201162/)\n * [Crimps](http://au.rs-online.com/web/p/pcb-connector-contacts/7196555/)\n \n## Full Setup\n\n1. Download the latest raspbian  lite image from here: https://raspberrypi.org/downloads/raspbian\n2. Insert your SD card into your computer\n3. Use your preferred method to put the rasbpian image onto your machine. On linux:\n````bash\nwget https://downloads.raspberrypi.org/raspbian_lite_latest\ntar -xvf  raspbian_lite_latest\n# the if argument might be different\ndd if=2017-09-07-raspbian_stretch-lite.img of=/dev/sdb bs=4M conv=fsync status=progress\n````\n4. Unmount your SD card, and plug it into your raspberry pi\n5. Run the following commands after logging in (default username is `pi`, password is `raspberry`) and configuring \nwifi by putting your settings in `/etc/wpa_supplicant/wpa_supplicant.conf` (you will need to restart the wifi to have\n the settings take effect by \nrunning `sudo service networking restart`):\n````bash\nsudo apt update\nsudo apt install git make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils bluez python-bluez pi-bluetooth python3-yaml python-yaml\ncurl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash\nenv PYTHON_CONFIGURE_OPTS=\"--enable-shared\" pyenv install 3.6.2\ngit clone https://github.com/JonnoFTW/rpi-can-logger.git\n````\n### Install Rpi-Logger\n\n1. Determine the configuration file you want to use or roll your own.\n2. To install the dependencies and system services, run:\n```bash\npip3 install -r requirements.txt\nsudo python3 setup.py config_file.yaml\n```\n3. Enable UART on your RPI (for the GPS) and CAN (skip the second `dtoverlay` line if your CAN shield only has 1 input)\n for the CAN shield by adding these lines to `/boot/config.txt`:\n```\nenable_uart=1\ndtparam=spi=on\ndtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25\ndtoverlay=mcp2515-can1,oscillator=16000000,interrupt=24\ndtoverlay=spi-bcm2835\n```\n4. In order to stop the RPI from asking your serial ports `/ttyS0` to log on, change `/boot/cmdline.txt` and remove:\n```\nconsole=serial0,baudrate=115200\n```\n5. Add these lines to your `/etc/network/interfaces` file (set it to 250000 if you are using FMS and skip `can1` if \nyou only have 1 CAN port):\n```\nauto can0\niface can0 inet manual\n    pre-up /sbin/ip link set can0 type can bitrate 500000 triple-sampling on restart-ms 100\n    up /sbin/ifconfig can0 up\n    down /sbin/ifconfig can0 down\n\nauto can1\niface can1 inet manual\n    pre-up /sbin/ip link set can1 type can bitrate 500000 triple-sampling on restart-ms 100\n    up /sbin/ifconfig can1 up\n    down /sbin/ifconfig can1 down\n\n```\n\n6. The logging and file upload service will now run on startup. By default it will use: [example_fms_logging.yaml](example_fms_logging.yaml).\n7. To setup uploading of files, you will need to create a `mongo_conf.yaml` file in the project directory:\n```yaml\nlog_dir: ~/log/can-log/\nkeys:\n - vid_key_1\n - vid_key_2\napi_url: http://url.to/api/ # the api on the end is important\n```\n  \n## Configuration\nRPI-CAN-Logger is highly configurable and supports nearly all standard OBD-2 PIDs and the currently understood frames from Tesla as described in [this document](https://skie.net/uploads/TeslaCAN/Tesla%20Model%20S%20CAN%20Deciphering%20-%20v0.1%20-%20by%20wk057.pdf).\n### Configuring CAN Logging\n\nWe currently support 4 forms of logging:\n\n* [Sniffing OBD](example_obd_sniffing_conf.yaml)\n* [Querying OBD](example_obd_querying_conf.yaml)\n* [Sniffing Tesla](example_tesla_conf.yaml)\n* [Sniffing FMS](example_fms_logging.yaml)\n\nHere we will examine the various configuration options:\n\n```yaml\ninterface: socketcan # can bus driver\nchannel: can1 # which can bus to use\nlog-messages: /home/pi/log/can-log/messages/ # location of debug messages\nlog-folder: /home/pi/log/can-log/ # location of log files\nlog-size: 32 # maximum size in MB before log file is rotated \nlog-pids: # the pids we want to log, refer to rpi_can_logger/logger/obd_pids.py,  tesla_pids.py fms_pids.py outlander_pid\n  - PID_ENGINE_LOAD\n  - PID_THROTTLE\n  - PID_INTAKE_MAP\n  - PID_RPM\n  - PID_SPEED\n  - PID_MAF_FLOW\nlog-trigger: PID_MAF_FLOW # when this PID is seen, return the buffer in current state (only works in sniffing mode)\ndisable-gps: false # enable/disable GPS logging\ngps-port: /dev/ttyS0 # serial port for GPS device\ntesla: false # indicates whether or not we are logging a tesla vehicle\nsniffing: false # indicates that we are sniffing\nlog-bluetooth: true # whether or not we log to bluetooth\nbluetooth-pass: super_secret_password # the password required to stream the bluetooth data\nlog-level: warning # log level (warning or debug)\nfms: false # are we logging FMS? (Bus and Truck only)\nverbose: true # give verbose message output on stdout\n```\n\n### Configuring Data Upload\n\nIn the root directory of this project create a file called: `mongo_conf.yaml`, it should look like this:\n\n```yaml\nlog_dir: /home/pi/log/can-log/\npid-file: ~/log/can-log.pid\napi_url: 'https://url.to.server.com/api/'\nkeys:\n    - vid_key_1\n    - vid_key_2\n```\nThe keys are the API keys for each vehicle that this logger will log for.\n\n## Cloning SD Cards\n\nBecause we're deploying to a lot of these devices, you'll need to make an image after setting everything up on your SD\ncard. Once you're done, plug your SD card into another computer and run:\n\n`dd of=logger.img if=/dev/sdb bs=4M conv=fsync status=progress`\n\nOnce that's finished you'll have a file called `logger.img` on your machine, insert a new card and run:\n\n`dd if=logger.img of=/dev/sdb bs=4M conv=fsync status=progress`\n\nThis should clone the SD card assuming they're exactly the same. If the cards are different sizes:\n \n##### Larger Card\n* Run `raspi-config` and resize the partition OR\n* Remount the SD card and use your favourite partitioning tool to expand the 2nd partition\n\n##### Smaller Card\nAssuming the amount of data used on the image is less than the target SD card size, then you will need to shrink the \ndata partition before you make the clone SD card. You can do this on Linux with the following (from this [tutorial](https://softwarebakery.com/shrinking-images-on-linux)):\n\n```bash\nsudo modprobe loop\nsudo losetup -f\nsudo losetup /dev/loop0 logger.img\nsudo partprobe /dev/loop0\ngksu gparted /dev/loop0\n```\nUse gparted to resize the 2nd partition so that it fits within the size of your target SD card. Then hit apply. Now we \nwill truncate the image.\n\n```bash\nsudo losetup -d /dev/loop0\nfdisk -l logger.img\n```\nYou should get something like:\n\n```\n$ fdisk -l logger.img \n\nDisk fmslogger.img: 15.9 GB, 15931539456 bytes\n255 heads, 63 sectors/track, 1936 cylinders, total 31116288 sectors\nUnits = sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\nDisk identifier: 0x96bbdd32\n\n        Device Boot      Start         End      Blocks   Id  System\nlogger.img1            8192       93813       42811    c  W95 FAT32 (LBA)\nlogger.img2           94208    31116287    15511040   83  Linux\n```\nRecord the sector size (on the 2nd line `\"Units = ...\"`, 512 bytes here) and end for the 2nd partition (31116287 here),\nnow you can run:\n\n```bash\ntruncate --size=$[(31116287+1)*512] logger.img\n```\nYou can now write the shrunken image.\n\n#### Configuring Clones\n\nAfter you've done all that set a new hostname (with no hyphens after `rpi-logger-`) for your device by running:\n\n```bash\nsudo python3 ./systemd/pariable.py rpi-logger-12345\nsudo reboot\n```\n\nWhere `12345` is the vehicle identifier. In order to connect via the bluetooth app, the device hostname must start with `rpi-logger-`\n \nYou'll also probably need to pair the bluetooth with your phone, run:\n\n```bash\nsudo bluetoothctl\ndiscoverable on\npairable on\n```\nInitiate pairing on your phone. Then run:\n```bash\ndiscoverable off\npairable off\nquit\n```\nReboot your device and everything should be good to go.\n\n## Testing\n\nThere's a bunch of different tests provided the `tests` folder:\n\n* [`can_spam.py`](tests/can_spam.py) will transmit CAN frames with the last two of eight bytes as increasing integers from 0 to 0xff \n* [`gps_test.py`](tests/gps_test.py) will dump output of the GPS\n* [`gps_sniff_test.py`](tests/gps_sniff_test.py) will dump raw CAN messages to display\n* [`gpio_led_test.py`](tests/gpio_led_test.py) will test the LEDs\n* [`can_dump.py`](tests/can_dump.py) will dump the CAN data to a CSV file\n* [`query_single_pid.py`](tests/query_single_pid.py) will query every OBD PID and check for a response\n* [`phev_query`](tests/phev_query.py) will query data from the battery control unit on a Mistubishi PHEV Outlander","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonnoftw%2Frpi-can-logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonnoftw%2Frpi-can-logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonnoftw%2Frpi-can-logger/lists"}