Preliminary… As electronics have gotten more powerful, the need for embedded devices to communicate in a simple, robust, and scalable way has gotten more and more important. The go to network for this has become the CAN (Controller Area Network) Bus. Car manufacturers have been one of the first to adopt the CAN Bus as it is one of the most resilient communication interfaces out there for operating in such a demanding environment. Now we are starting to see CAN crop up in more and more things as the advantages of using a CAN start to win out.
We are going to walk through how to interact with this low level network traffic and bring it out to some higher level storage and visualization tools. This will all be done with free and open source software and reasonably priced hardware for a professional, configurable and widely scalable real-time monitoring and analytics system.
For this demo we are going to monitor a temperature sensor over a CAN Bus. This is certainly not the recommended or most economical way to monitor the temperature of your house. But if this were part of a vehicle system with multiple other devices interacting and sensors transmitting data, this same setup should suffice to monitor all the additional traffic that would be present.
To get started we need a sensor and there are basically two different approaches now on how to add sensing to your CAN Bus.
The most economical way to add CAN bus sensing to a project is to add it into a microcontroller that is already going to be in the system. You can get up and going to experiment with an OLIMEXINO-STM32 which already has an on-board CAN Bus transceiver to interface with the integrated CAN peripheral on an STM32. However the CAN Bus does tend to be one of the more complicated peripherals on these types of controllers so we will leave those details for another article. I would suggest using one of the integrated sensors below to get up and going quickly.
These kinds of sensors are a bit more expensive as they have the hardware and MCU built in, but they are very handy and could be perfect for many applications.
A CAN is a differential bus interface to an embedded device. It operates similar to USB in order to handle environmental noise, however arbitration on the bus is built in at the signal level. This means CAN Bus requires no other hardware to add a device on the bus, just wire it into your bus and you are ready to go!
Typical Bit Rate vs Bus Length
Here is a table of bit rates and their corresponding bus lengths. This can vary depending on the noise and traffic in the system, but can be a good guideline. Most common bus speeds tend to be 250k, 500k and 1M bit/s.
|Bit rate||Bus length|
|1 Mbit/s||25 m|
|800 kbit/s||50 m|
|500 kbit/s||100 m|
|250 kbit/s||250 m|
|125 kbit/s||500 m|
|50 kbit/s||1000 m|
|20 kbit/s||2500 m|
|10 kbit/s||5000 m|
Gateway Device Setup
Setup with Raspberry Pi and CAN board:
To get started with the Raspberry Pi as a CAN Bus Gateway you need an operating system. Raspian Lite is recommended as you shouldn’t need a terribly complex setup, however a desktop version would work just as well. There are plenty of guides on getting an SD card setup with the image for your OS.
Next you will need something to interface with the CAN Bus. One powerful and rather easy option is to use the PiCAN2 adapter board. There are even versions available that have an integrated power supply to power the device from the CAN Bus itself, as well as dual CAN adapters if that is something you might need.
Here is a document to walk you through the setup of the adapter board. Basically just press it onto the header, install the standoffs, and screw it together. MOST IMPORTANT, be sure to set the solder jumpers as needed, you will likely need to jumper them to work with CAN Cables unless you would like to use this in an OBDII setup.
Of course now we need to setup Raspbian to detect our freshly installed adapter board!
First we need to update the OS with the typical:
sudo apt-get update sudo apt-get upgrade
Next we need to add in the overlay to interface with the CAN bus via the SPI interface that is driving the CANPi2. Open up an editor to
/boot/config.txt and add the following at the end:
dtparam=spi=on dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25 dtoverlay=spi-bcm2835-overlay
That’s it! Save your changes and reboot the Raspberry Pi:
Of course we need to wire this system up. Each sensing device may be a little different, but typically you will need +12V DC, Ground, and CAN High and CAN Low. Sometimes you will have a separate CAN GND which you should use if available.
The Raspberry Pi will need a matching connections on the PiCAN board for CAN High, CAN Low and CAN Ground (use the sensor ground if no dedicated CAN Ground). The Raspberry Pi itself will just need USB power if it isn’t powered from the CAN Bus, and Ethernet unless you would like to setup a Wifi interface.
actual wired image
Testing Your CAN interface
- (Is this special or different from can-utils??)
- via can-utils
git clone https://github.com/linux-can/can-utils.git cd can-utils make sudo make install
CAN Bus Testing with python-can
This is an optional step and depends on your application. For just this single sensor and some minimal visualizations the python-can library and a simple web based plotter or a terminal interface may be a very usable option on the Raspberry Pi. But for doing some serious data crunching and visualizations, it doesn’t take too much to overwhelm the Raspberry Pi using the tools in this article. Below we will take the data processing requirements off of the Raspberry Pi and let a more powerful computer do the work.
Code snippet based on examples to get raw data
SocketCANd setup and configuration for Raspberry Pi:
SocketCAN is a standard networking stack that is used to allow access to a CAN bus. The socketcand project packages this all up and gives us an easy way to setup the interface and run it in the background as a daemon. This will allow us to get the raw data off of the CAN Bus and onto our network.
- Steps to clone, compile, make, execute
- Set to run on boot
If everything is working here, now would be a great time to make a backup image of your SD card so you can always come and reuse or restore this working image.
At this point it is possible to stop here and use something like Kayak, which is a very powerful open source CAN interface utility. Or we can continue on with an even more flexible and web friendly solution!
Setting up a simple socket interface with Python from another device
Scaling raw CAN data to J1939 or other format
- CAN Packets
Extended ID vs Standard ID
- Sensor Specific setup
- J1939 Generic setup
Sending data to a TSDB and basic queries
Now it is time to install InfluxDB on your server or PC!
Setting up Grafana and linking to InfluxDB
For an Industrial Grade CAN Bus Environmental Monitoring setup
- CAN Bus Sensor
- Integrated CAN Device: $85-$150
- Custom: $30 or less
- Raspberry Pi: $30
- SD Card: $10
- Power USB (optional): $5
- CAN Adapter Board w/Enclosure: $85
- Total : ~$155->$215
- Read-only OS for power safe operation
- Larger CAN bus for multi point sensing
- Sensor Meta Data?? Location/type/ranges
- J1939 detail abstraction with SQLite
- CAN Bus Writing and simulation
- InfluxDB Data optimization, retention
- Filter levels and sampling with Grafana