Building a canbus app part1

This is the text supplement to this tutorial video. Here will be all the used commands and code snippets

Code can be found on this git repo

Required items


In this tutorial we will be building a basic app the shows rev and speed data using some gauges, hosted on a webpage and getting the required info over canbus. We will be using NodeJs for the backend, where it will be talking to the canbus and serving up the required html and allowing realtime communication with the can data.

Installing Samba

Due to one of our main NodeJs modules being a native module, this code can only be tested on the Pi itself. This means it can be a bit tricky to develop the code using an IDE. To get around this we will be installing samba onto the Pi, and then we can modify the required code on our mac/windows pc.

The first step is installing samba, to do this we run the following command

sudo apt install samba samba-common-bin

You may get a prompt asking whether you want to enable a service, if you do click yes. Next we need to create our shared folder, I have used the one below, however feel free to change it, just take not the below commands will also change.

mkdir /home/pi/shared

Once create we need to set up a share. To do this run

sudo nano /etc/samba/smb.conf

Inside this file, add the below text at the very end of the text file

Comment = Pi shared folder
Path = /share
Browseable = yes
Writeable = Yes
only guest = no
create mask = 0777
directory mask = 0777
Public = yes
Guest ok = yes

Exit using Ctrl+X and then when prompted to save press Y and then enter. Samba should now be all set up. To test open the file explorer on your windows machine, in the address bar type \\{your Pi Ip here}\ and it should show you the shared folders. Right click on the PiShare one, and then click mount network drive. The folder will now appear as a usual drive on your PC.

Setting up the virtual canbus

Setting up a virtual canbus is easy, and it saves needing to create a test can network with extra Pi’s etc. First make sure you have can-utils installed

sudo apt-get -y install can-utils libsocketcan2 libsocketcan-dev

Then we set up the virtual can, to do this we run three commands

sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0

Using NodeJs/NPM

To check if node is installed type node -v if you dont get a version back, you will need to install it. If its lower than v10 I would suggest upgrading. To install/upgrade run the following commands

curl -sL | sudo -E bash -
sudo apt-get install -y nodejs

Now navigate to the shared folder we created

cd /home/pi/share

And create a new folder for our project

mkdir Can-App
cd Can-App

Once in that folder, we need to initalise the folder as a NodeJs project. To do this we run

npm init

NPM will then ask a series of question, for this guide just press enter for them all, or if you want they can be filled out. Once complete there will be a new file within that folder called package.json. NodeJs works on a package system, where you can install packages that other people have create. Their packages may contain other peoples packages and so on and so on. When a package is installed it will be listed in that file as a dependancy. So lets install our first package, this is called socketcan, and it allows nodejs to interact with the canbus through the kernel, and in turn gets the information we need into the app. We can install this using

npm install socketcan

Once installed we are ready to begin to program the app. Open the project directory in the IDE of your choice on the windows/mac machine.

Writing the Simulator

The first part we will create is a simulator that sends out the can info for both the speed and the rpm. This text guide willl list the basics, the video goes into more detail on what each section is going. Create a new file in the directory call car.js. Inside this file, first we need to import the module we just installed so the first line should contain

var can = require("socketcan");

Then we create a raw channel

var channel = can.createRawChannel('vcan0', true);

Create a message object, and also set default values for speed and rpm, up is used to create a rev limiter effect. As shown in the video.

var msg = {

    'id': 500,

    data: [0,0,0,0,0,0,0,0]

var speed = 0
var revs = 0
var up = true

Finally we create the interval function that changes the values every 100ms and then sends them out over can bus.

setInterval(() => {

    var out = {}

    var buff = Buffer.alloc(8)

    if(speed <155) {

        speed = speed + 1

        revs = revs + 240

    } else {

        if(up) {

            revs = revs + 100

            up = false

        } else {

            revs = revs - 100

            up = true




    if(revs > 7000) {

        revs = 1000


    buff.writeUIntBE(revs, 0, 4)

    buff.writeUIntBE(speed, 4, 2)

    console.log(buff) = = buff



}, 100)


The simulator code in now created, on the Pi, in a terminal in the project directory run node car.js

In a seperate terminal, run candump vcan0 and you should see the simulated canbus messages being sent over the can network.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

ModernDayMods is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to, buying the linked products gives me a small percentage :) .