You Can Bus Build This Vehicle Monitor System Too!

About the project

This project by Hayden Roche use Notecard and the Adafruit M4 CAN Express to monitor CAN bus for messages with particular IDs and upload those messages to the cloud.

Project info

Difficulty: Moderate

Platforms: AdafruitMicrosoftBlues Wireless

Estimated time: 2 hours

License: Apache License 2.0 (Apache-2.0)

Items used in this project

Hardware components

2 male-to-male jumper wires 2 male-to-male jumper wires x 1
Blues Notecard (Cellular) Blues Notecard (Cellular) x 1
Blues Notecarrier F Blues Notecarrier F x 1
Adafruit Feather M4 CAN Express with ATSAME51 Adafruit Feather M4 CAN Express with ATSAME51 x 1
USB to CAN Converter Cable USB to CAN Converter Cable x 1
USB Cable, USB Type C Plug USB Cable, USB Type C Plug x 1

Software apps and online services

Solder Flux, Soldering Solder Flux, Soldering
Solder Wire, Lead Free Solder Wire, Lead Free
Microsoft VS Code Microsoft VS Code You'll also need the PlatformIO extension.

Hand tools and fabrication machines

Soldering iron (generic) Soldering iron (generic) x 1

Story

Note: This project and the documentation were made by Hayden Roche and uploaded by dcschelt on his behalf.


Modern vehicles use tons of computers for a variety of tasks. These computers, known as ECUs, typically communicate with one another via a CAN bus. In order to understand the behavior of these devices, debug problems, and collect data, one needs a way of getting the CAN messages from the vehicle.

This app provides a Notecard-based system for snooping a CAN bus and forwarding relevant messages to Notehub. CAN messages contain an ID. The user of this solution can configure a list of IDs of interest, and every message that has one of those IDs will be uploaded to Notehub.

Project Breakdown

  • Hardware Setup
  • Notehub Setup
  • Firmware Setup
  • Testing

Hardware Setup

MCU

The Adafruit Feather M4 CAN Express (the "MCU") will receive CAN traffic from your development PC, filter that traffic based on the IDs in the messages, and pass the filtered messages on to the Notecard for transmission to Notehub. The MCU comes with male headers and a CAN transceiver screw terminal block (the "terminal block") that need to be soldered onto the MCU board in order for it to plug into the Notecarrier F.

Adafruit has a handy guide for soldering headers.

Adafruit has a handy guide for soldering headers.

Adafruit has a handy guide for soldering headers.

Adafruit has a handy guide for soldering headers. Once you've soldered on the headers and the terminal block, your MCU should look like this:

Now, plug the headers of the MCU into the female headers on the Notecarrier F. Install the Notecard into the Notecarrier and the antenna onto the Notecard as described in the Notecard Quickstart guide. Finally, connect the MCU to your development PC with a USB C cable.

Note: This project lists a USB C cable in the bill of materials (BOM); however, if your computer does not have a USB C port, you'll need to add a USB C to USB A adapter to the BOM.

USB to CAN Converter

The USB to CAN converter (the "converter") allows us to send CAN messages from our development PC to the MCU via a CAN bus. A CAN bus requires two signal lines: CAN high (CANH) and CAN low (CANL). The converter comes in two parts. First, there's the converter itself, which connects to your PC via its USB cable and has a male RS232 connector on the other end:

The converter's male RS232 connector plugs into the second part, a small PCB with a female RS232 connector and a screw terminal block that exposes the CANH and CANL lines:

Step 1: Connect to Your Computer

Plug the part with the USB connector into your development PC.

Step 2: Connect the RS232 Converter

Connect the converter's RS232 connector to the PCB's RS232 port:

Step 3: Wire In Jumper Wires

This terminal block has ports labeled CANH and CANL. Using a Philips head screwdriver, loosen the screws above these two connectors, insert the end of a male-to-male jumper wire into each, and tighten the screws, securing the wires.

Similarly, take the free ends of each jumper wire and make the following two connections:

  • Plug the CANL wire into the port labeled L on the MCU's terminal block.
  • Plug the CANH wire into the port labeled H.

Notehub Setup

Next you'll need to use your notehub.io account (it's free) and create a new project.

Firmware Setup

To build and upload the firmware onto the MCU, you'll need VS Code with the PlatformIO extension.

1. Download and install Visual Studio Code.

2. Install the PlatformIO IDE extension via the Extensions menu of Visual Studio Code.

3. Click the PlatformIO icon on the left side of VS Code, then click Pick a folder, and select the the firmware directory, 35-CAN-vehicle-monitor/firmware.

4. In the file explorer, open main.cpp and uncomment this line:

// #define PRODUCT_UID "com.my-company.my-name:my-project"

Replace com.my-company.my-name:my-project with the ProductUID of the Notehub project you created in Notehub Setup.

5. Click the PlatformIO icon again, and under the Project Tasks menu, click Build to build the firmware image.

6. Under the Project Tasks menu, click Upload to upload the firmware image to the MCU. Note that this step can fail if 1) you have a serial connection open to the MCU or 2) the USB C cable is for charging only.

From here, you can view logs from the firmware over serial with a terminal emulator (e.g. minicom). On Linux, the serial device will be something like /dev/ttyACM0. Use a baud rate of 9600 and 8-N-1 for the serial parameters. Remember that you'll have to close this serial connection if you want to modify and upload new firmware.

Testing

These testing instructions assume the developer is using a Linux or Linux-like operating system.

By default, the firmware does not forward any CAN messages to Notehub. First, we need to tell it which CAN IDs we care about. Then, whenever a message with one of those IDs shows up on the bus, it'll be captured and forwarded to Notehub. To propagate the IDs of interest to the MCU, we'll use the environment variable ids.

On your Notehub project page, click Devices, and then double click on the device entry for your Notecard. Click the Environment tab, and under Device environment variables, enter a new key ids and a value with IDs of interest in hexadecimal, delimited with semicolons (e.g. abc;123;de7). Click Save. In your terminal emulator hooked up to the serial log, you should see that the environment variable update was received:

Environment variable changed detected.
{"req":"env.get","names":["ids"]}
{"body":{"_tri_mins":"1440","ids":"abc;123;de7"}}
New ID list: 0xabc, 0x123, 0xde7

Now, we need to set up the CAN interface on our development PC so that we can send messages on the bus. Fortunately, CAN interfaces are supported on Linux using familiar network interface commands and sockets. To set up the CAN interface, run the setup_can_sender.sh script with ./setup_can_sender.sh. This script requires sudo, so it will prompt you for your password. If everything worked, you should see this message:

CAN sender interface ready.

Next, we'll use a Python script, send_can_packet.py, to send messages over the CAN bus. You'll need to install the python-can module for this script to work (e.g. pip install python-can). Here's the help message for the script:

usage: send_can_packet.py [-h] [--dev DEV] --id ID --data DATA

Send a single CAN packet on a CAN bus.

options:
-h, --help show this help message and exit
--dev DEV The interface name of the USB to CAN converter (default: can0).
--id ID The ID to put in the packet, specified in hex. Example: ab12.
--data DATA The data to put in the packet. Specified in hex, with colons separating
bytes, up to a max of 8 bytes. Example: 01:02:03:0a:ff:dd:ee:56.

Assuming you set the ids environment variable to abc;123;de7 as used in the example above, we can send a message with ID abc like this:

python send_can_packet.py --id abc --data 01:02:03

In the serial log, we should see that the message was received, that it matched our filter, and that it was sent to Notehub:

Received extended packet with id 0xabc.
ID matches filter list, keeping packet.
3 bytes in packet. Reading...
{"req":"note.add","file":"data.qo","body":{"id":2748,"data":[1,2,3],"length":3},"sync":true}
{"template":true}

Note that the id in the note.add request is logged in decimal, not hex (0xabc is 2748 in decimal).

Then, if we check back on the Events tab of our project on Notehub, we should see an event with a JSON body like this:

{
"data": [
1,
2,
3,
0,
0,
0,
0,
0
],
"id": 2748,
"length": 3
}

The data array has been padded with zeros to 8 elements. The firmware is using a Note Template to send this data. Setting aside the nitty gritty details, suffice to say that an array in a Note Template will always be padded to the max length. Since the max data length in a CAN message is 8 bytes, that explains the padding. To extract the appropriate "valid" bytes from the data array, you can use the length field.

Next, try sending a message with an ID we don't care about:

python send_can_packet.py --id 111 --data 01:02:03

In the serial log, you should see:

Received extended packet with id 0x111.
ID not in filter list, dropping packet.

Blues Community

We’d love to hear about you and your project on the Blues Community Forum!

Additional Resources

Code

CAN Vehicle Monitor

Credits

Leave your feedback...