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.
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.
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:
Plug the part with the USB connector into your development PC.
Connect the converter's RS232 connector to the PCB's RS232 port:
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.
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.
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.
Click the PlatformIO icon again, and under the Project Tasks menu, click Build to build the firmware image.
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.
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 variableids.
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.
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.
-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:
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...
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:
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: