• Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    Python
  • Created over 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Project to log CAN bus data from a PiCAN2 and a GPS module

Raspberry PI CAN Bus Logger

This 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.

Features

  • Logs and interprets CAN bus data from:
    • OBD2
    • Tesla vehicles
    • Bus and Truck with FMS
    • Outlander PHEV
  • Logs GPS
  • Can operate in querying and sniffing mode
  • Stores data on SD card. Can be configured to automatically upload via web API when connected to WiFi or 4G internet.
  • 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.
  • Accompanying Bluetooth App to:
    • Visualise your data in realtime
    • Fetch and upload stored data
  • Web based data visualiser

Parts

The following parts are used:

If 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.

If 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):

Rpi Pins

To make your own OBD connector, you will need:

You'll only need to connect pins:

  • 4 Chassis Ground
  • 6 CAN High
  • 14 CAN Low
  • 16 12V Power

OBD Pins

Tesla

Please 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/:

You will need the following parts:

Full Setup

  1. Download the latest raspbian lite image from here: https://raspberrypi.org/downloads/raspbian
  2. Insert your SD card into your computer
  3. Use your preferred method to put the rasbpian image onto your machine. On linux:
wget https://downloads.raspberrypi.org/raspbian_lite_latest
tar -xvf  raspbian_lite_latest
# the if argument might be different
dd if=2017-09-07-raspbian_stretch-lite.img of=/dev/sdb bs=4M conv=fsync status=progress
  1. Unmount your SD card, and plug it into your raspberry pi
  2. Run the following commands after logging in (default username is pi, password is raspberry) and configuring wifi by putting your settings in /etc/wpa_supplicant/wpa_supplicant.conf (you will need to restart the wifi to have the settings take effect by running sudo service networking restart):
sudo apt update
sudo 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
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.6.2
git clone https://github.com/JonnoFTW/rpi-can-logger.git

Install Rpi-Logger

  1. Determine the configuration file you want to use or roll your own.
  2. To install the dependencies and system services, run:
pip3 install -r requirements.txt
sudo python3 setup.py config_file.yaml
  1. Enable UART on your RPI (for the GPS) and CAN (skip the second dtoverlay line if your CAN shield only has 1 input) for the CAN shield by adding these lines to /boot/config.txt:
enable_uart=1
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=mcp2515-can1,oscillator=16000000,interrupt=24
dtoverlay=spi-bcm2835
  1. In order to stop the RPI from asking your serial ports /ttyS0 to log on, change /boot/cmdline.txt and remove:
console=serial0,baudrate=115200
  1. Add these lines to your /etc/network/interfaces file (set it to 250000 if you are using FMS and skip can1 if you only have 1 CAN port):
auto can0
iface can0 inet manual
    pre-up /sbin/ip link set can0 type can bitrate 500000 triple-sampling on restart-ms 100
    up /sbin/ifconfig can0 up
    down /sbin/ifconfig can0 down

auto can1
iface can1 inet manual
    pre-up /sbin/ip link set can1 type can bitrate 500000 triple-sampling on restart-ms 100
    up /sbin/ifconfig can1 up
    down /sbin/ifconfig can1 down

  1. The logging and file upload service will now run on startup. By default it will use: example_fms_logging.yaml.
  2. To setup uploading of files, you will need to create a mongo_conf.yaml file in the project directory:
log_dir: ~/log/can-log/
keys:
 - vid_key_1
 - vid_key_2
api_url: http://url.to/api/ # the api on the end is important

Configuration

RPI-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.

Configuring CAN Logging

We currently support 4 forms of logging:

Here we will examine the various configuration options:

interface: socketcan # can bus driver
channel: can1 # which can bus to use
log-messages: /home/pi/log/can-log/messages/ # location of debug messages
log-folder: /home/pi/log/can-log/ # location of log files
log-size: 32 # maximum size in MB before log file is rotated 
log-pids: # the pids we want to log, refer to rpi_can_logger/logger/obd_pids.py,  tesla_pids.py fms_pids.py outlander_pid
  - PID_ENGINE_LOAD
  - PID_THROTTLE
  - PID_INTAKE_MAP
  - PID_RPM
  - PID_SPEED
  - PID_MAF_FLOW
log-trigger: PID_MAF_FLOW # when this PID is seen, return the buffer in current state (only works in sniffing mode)
disable-gps: false # enable/disable GPS logging
gps-port: /dev/ttyS0 # serial port for GPS device
tesla: false # indicates whether or not we are logging a tesla vehicle
sniffing: false # indicates that we are sniffing
log-bluetooth: true # whether or not we log to bluetooth
bluetooth-pass: super_secret_password # the password required to stream the bluetooth data
log-level: warning # log level (warning or debug)
fms: false # are we logging FMS? (Bus and Truck only)
verbose: true # give verbose message output on stdout

Configuring Data Upload

In the root directory of this project create a file called: mongo_conf.yaml, it should look like this:

log_dir: /home/pi/log/can-log/
pid-file: ~/log/can-log.pid
api_url: 'https://url.to.server.com/api/'
keys:
    - vid_key_1
    - vid_key_2

The keys are the API keys for each vehicle that this logger will log for.

Cloning SD Cards

Because we're deploying to a lot of these devices, you'll need to make an image after setting everything up on your SD card. Once you're done, plug your SD card into another computer and run:

dd of=logger.img if=/dev/sdb bs=4M conv=fsync status=progress

Once that's finished you'll have a file called logger.img on your machine, insert a new card and run:

dd if=logger.img of=/dev/sdb bs=4M conv=fsync status=progress

This should clone the SD card assuming they're exactly the same. If the cards are different sizes:

Larger Card
  • Run raspi-config and resize the partition OR
  • Remount the SD card and use your favourite partitioning tool to expand the 2nd partition
Smaller Card

Assuming the amount of data used on the image is less than the target SD card size, then you will need to shrink the data partition before you make the clone SD card. You can do this on Linux with the following (from this tutorial):

sudo modprobe loop
sudo losetup -f
sudo losetup /dev/loop0 logger.img
sudo partprobe /dev/loop0
gksu gparted /dev/loop0

Use gparted to resize the 2nd partition so that it fits within the size of your target SD card. Then hit apply. Now we will truncate the image.

sudo losetup -d /dev/loop0
fdisk -l logger.img

You should get something like:

$ fdisk -l logger.img 

Disk fmslogger.img: 15.9 GB, 15931539456 bytes
255 heads, 63 sectors/track, 1936 cylinders, total 31116288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x96bbdd32

        Device Boot      Start         End      Blocks   Id  System
logger.img1            8192       93813       42811    c  W95 FAT32 (LBA)
logger.img2           94208    31116287    15511040   83  Linux

Record the sector size (on the 2nd line "Units = ...", 512 bytes here) and end for the 2nd partition (31116287 here), now you can run:

truncate --size=$[(31116287+1)*512] logger.img

You can now write the shrunken image.

Configuring Clones

After you've done all that set a new hostname (with no hyphens after rpi-logger-) for your device by running:

sudo python3 ./systemd/pariable.py rpi-logger-12345
sudo reboot

Where 12345 is the vehicle identifier. In order to connect via the bluetooth app, the device hostname must start with rpi-logger-

You'll also probably need to pair the bluetooth with your phone, run:

sudo bluetoothctl
discoverable on
pairable on

Initiate pairing on your phone. Then run:

discoverable off
pairable off
quit

Reboot your device and everything should be good to go.

Testing

There's a bunch of different tests provided the tests folder:

More Repositories

1

img_term

Display image, video or USB camera in your ANSI terminal!
Python
162
star
2

traffic-prediction

Predict traffic flow with LSTM. For experimental purposes only, unsupported!
Python
53
star
3

mongo-schema-export

Simple tool to clone metadata for mongodb collections
Python
31
star
4

Material-Design-Copier

A python script to copy content from a google material design folder into your android project
Python
24
star
5

BibTeX-Label-Creator

Plugin for Endnote to generate bibtex labels
C++
23
star
6

keras_find_lr_on_plateau

Keras Callback to Automatically Adjust the learning rate when it stops improving
Python
15
star
7

htm-models-adelaide

Using NuPIC as part of my PhD work.
Python
12
star
8

markov-img-gen

Experiment in generating images and audio using markov chains
Python
11
star
9

OBD-Datalogger

Android app to log data from a Freematics OBD-II v3
Java
7
star
10

htm-cl

Simple HTM implementation that uses OpenCL
Python
7
star
11

5-of-Diamonds

Server Browser for Ace of Spades
Python
7
star
12

py-obd-ii-emulator

Python Library to Control the Freematics OBD-II Emulator
Python
7
star
13

PyScrabble

Command line Scrabble written in python, a gui one day... maybe.
Python
6
star
14

webcan

View data from RPi-can-logger
Python
6
star
15

TwatBot2

TwatBot reborn
Python
6
star
16

pyramid_mongodb2

Simple library to integrate mongodb into your pyramid application
Mako
5
star
17

SMS-Morse

Morse code application for android that reads your message in morse code
Java
4
star
18

TwatBot

THIS SOFTWARE IS DEPRECATED A bot to update twitter from IRC using user quotes
Python
4
star
19

TonsleyLEDManager

Application to manage the sign at Tonsley
Python
3
star
20

jMarkovChain

Markov chains in java
Java
3
star
21

OpenCL-Support-Plugin

An IntelliJ plugin to provide support for OpenCL
Java
3
star
22

ProjectEulerJava

Solutions to some project euler questions in java
Haskell
2
star
23

IndoorTracking

Track your location inside IST
Java
2
star
24

tf2trade

A Trading site where you decide your buy sell prices
PHP
2
star
25

JLife

Game of life in java with a GUI
Java
2
star
26

Fetbot_discord

Stupid bot for a stupid channel
Python
2
star
27

codehandin

A new activity type for Moodle that allows students to submit and have their code tested through moodle
PHP
2
star
28

ScrabbleHelper

An Android application that tells you if a word is valid in Scrabble (or any other game that uses the official scrabble dictionary)
Java
2
star
29

Bear-Boards

A bulletin board system written in Python using web.py
1
star
30

Pypoker

Poker in python
1
star
31

nn-go

Neural network in go
Go
1
star
32

denstream-test

Test script for denstream clustering algorithm
Python
1
star
33

Android-Oscilloscope-View

Demonstration of a basic oscilloscope view for android.
Java
1
star
34

jonnoftw.github.com

My Personal Blog
Ruby
1
star
35

xgb-playground

Playing around with xgboost and hyperopt
Python
1
star
36

nn-cl

(Deep) neural network library written in pyopencl and opencl
Python
1
star
37

incident_detection_eval

Detect road crashes by the anomalies in flow data
Python
1
star
38

CoralSimulation

CoralSimulation for internship
Java
1
star
39

django-sql-log-middleware

Django Middleware to log SQL queries to jsonl file
Python
1
star
40

PySeparation

6 degrees of separation from IMDB
Python
1
star