Support for CAN Bus (Peak, Ixxat, canable etc)

Just read about the ”WiCAN” crowdfunding campaign from MeatPi Electronics on Crowd Supply for an an open source ESP32-C3 based CAN adapters with either USB or OBD-II/OBD2 in (addition to Wi-Fi and BLE support) which made me want to request for an integration based on python-can or similar?

https://www.crowdsupply.com/meatpi-electronics/wican/updates/our-campaign-is-now-live-on-crowd-supply

https://github.com/meatpiHQ/wican-fw

WiCAN currently supports three protocols; ReadDash, slcand, and saavycan GVRET Binary Protocol available for third-party integrations;

I started investigating into the topic of CAN-bus as part of my home automation and think the idea is still great.
I bought a Canable USB-to-CAN from Aliexpress and flashed the original firmware from canable.io → Candlelight, so gs-usb onto it.
When connecting it to a spare raspberry pi with raspbian, I see the interface with ip link list, but in Home Assistant (running with Home-Assistant OS on another raspberry pi), I can’t see the interface.
I thought after your PR, that all the modprobes are activated or do I miss something here?

What is needed to get the canable running on Home Assistant OS?
Do I have to activate some additional drivers? If yes, where to put the configs?

I hope this topic is still being worked on, as I think the option to have Home Assistant with CAN bus is really interesting, especially as an alternative to KNX.
As far as I’ve done my research, there is no real “wired” alternative to KNX available, which is a possibility for further USP.

Hi, no the PR was only to include the driver support for different type of can adapters
You still need to modprobe it… :slight_smile:
I do it in a python script that i load with a custom component, you can do it for example in an automation on startup HA

i use this in my .py file:

subprocess.call(['modprobe', 'can'])
subprocess.call(['modprobe', 'can_raw'])
subprocess.call(['modprobe', 'slcan'])

os.system("apk add /config/python_scripts/canutils.apk")
os.system("slcand -o -c -s4 /dev/ttyACM0 can0")
os.system("ifconfig can0 txqueuelen 1000")
os.system("ifconfig can0 up")

Btw, i use SLCAN firmware on it, i had better experience with it
if you use candlelight firmware, then i dont think you need to use the slcand/ifconfig commands, but i believe you still need to modprobe it

1 Like

you also see i do the canutils.apk manually, i do for tools like candump, cansend etc…
seems canutils is not part of the edge alpine main repo, only from the community

grab it from here:
https://pkgs.alpinelinux.org/packages?name=can-utils&branch=edge&repo=community&arch=&maintainer=

and

https://dl-cdn.alpinelinux.org/alpine/latest-stable/community/

Thank you for your very fast feedback!

I’ve now added the python_scripts functionality and added an “modprobe_canbus.py” script inside /config/python_scripts/modprobe_canbus.py with the content as described.
This script is started on HA startup with an Automation.

subprocess.call(['modprobe', 'can'])
subprocess.call(['modprobe', 'can_raw'])
subprocess.call(['modprobe', 'can_dev'])
subprocess.call(['modprobe', 'gs_usb'])
subprocess.call(['modprobe', 'slcan'])

os.system("apk add /config/python_scripts/canutils.apk")
#os.system("slcand -o -c -s4 /dev/ttyACM0 can0")
#os.system("ifconfig can0 txqueuelen 1000")
#os.system("ifconfig can0 up")

How can I check if it is working now?
When I open up the Terminal via the Terminal addon (supervisor addon “Terminal & ssh”) there is still no can interface showing up when I enter “ip link list”.
Is it a problem that I’m using Home Assistant OS?

I found something on another discussion where you’ve been involved

Has it something to do with me “looking” inside the wrong system with the terminal addon?
When I enter “slcand” or “candump”, there is no package even after restarting HA with the Python script.

Sorry for the inconvenience, I think I haven’t fully understood the Home Assistant structure yet…

Can you check with ssh addon with lsmod if the module is actually loaded? It should be listed there… Or try to run the python script first from ssh addon… Normally a candump inside ssh addon after installing canutils should give you indeed the dump visible

lsmod shows
gs_usb → used by: 0
can_dev → used by: 1 gs_usb

But this was actually already showing before I did the python script, so I’m not sure if the script did anything at all.

It doesn’t show slcan, can_raw or can

How do I start the python script from the ssh addon?
I can cd to the /config/python_scripts folder and there is the modprobe_canbus.py but when I try to start it with
python3 modprobe_canbus.py
It only errors with
bash: python3: command not found

The same as with “candump” “slcand” etc.

Hmm, if it shows that already then you don’t need the script indeed… From the ssh addon, just do : “APK add can-utils”

Then you can use candump

You have candlelight firmware, I use slcan firmware, then the commands are needed, but not with candlelight according to canable website

OK that was successful, I can now do candump!
Thank you.

Unfortunately I still only see Ethernet and local loopback (127.0.0.1) when entering ip link list or ifconfig but no can0.

I just don’t understand it.
Maybe I have to try it with a different firmware. The AliExpress Canable is labelled Makerbase MKS Canable Pro V1.0

What ensured me that it is working, was that it shows up on a standard raspbian under ifconfig as can0

Hmm , need to test tomorrow if I can see can0 device

Maybe try testing slcan?

I have the official canable adapter, and also a second one MKS like you, but not tested yet, it’s a spare one

1 Like

Ahh, but you also need to those commands with your firmware:

ip link set can0 up type can bitrate 500000

With the full
ip link set can0 up type can bitrate 500000
it errors with “either dev is duplicate or type is garbage”

but with
ip link set can0 up
it tries to do something but shows the error “ioctl 0x8913 failed: No such device”

I’ve also tried adding the command to the python script with:
os.system("ip link set can0 up")
but no difference

Ok, try slcan … :slight_smile:
Was a long time for me, when testing candlelight, I had issues with it too…

So flash slcan, and just do the commands in ssh manually, make sure turnoff protection mode on ssh addon, because otherwise you can’t do modprobe…

Keep me posted , interested if that mks works

Ok I installed slcan and it shows up under dmesg as ttyACM0 device!

I was able to do
slcand -o -c -s4 /dev/ttyACM0 can0
ifconfig can0 txqueuelen 1000
ifconfig can0 up

Now it shows can0 under “ip link list” and “ifconfig”! Success!
Thank you already so much!!

What would be the next steps to receive and process CAN messages within Home Assistant?

Set up a custom component like you did here?
Support for CAN Bus (Peak, Ixxat, canable etc) - #76 by pergola.fabio

But when I try
slcand -o -c -s4 /dev/ttyACM0 can0
it says “ioctl TIOCSETD: Operation not permitted”

I suppose this is due to protection mode being activated.
Unfortunately I didn’t find out how to switch it off.
I found something about the API roles and that only “admin” can switch off protection mode.
But the ssh addon has
hassio_role: manager
according to
addons/config.yaml at 5a8e13e29aa1f6e7e6167233a6f7bc8dcdc82e87 · home-assistant/addons · GitHub

How do I turn off protection mode?

Nevermind, I found the community addon “SSH & Web Terminal” and was able to turn off protection mode

Aha, good to hear, so when you know do a candump, you see data passing?

Well, what do you want Todo, if you install that can-utils package with that script, you can use shell commands to send messages…

I only read stuff, that was the most important for me, I indeed started with that addon I created, but I now created a compoent, that reads stuff and updates a template sensor

Yes I can see messages on candump! Very impressive (at least for me… :wink: )

Today I built up an ESP32 with a SN65HVD230 CAN Board (only 3.3 V but should be enough for some first tests) and put a button to it (GPIO to GND).
With ESPHome on the ESP32, I can do canbus.send with can_id and up to 8 Bytes data.
When I connect it to the canable on Home Assistant via CAN, I can do candump on HA and see the messages perfectly.

Unfortunately the procedure

apk add can-utils
slcand -o -c -s4 /dev/ttyACM0 can0
ifconfig can0 txqueuelen 1000
ifconfig can0 up 

has to be repeated when restarting home assistant…
To clarify, when I do the Restart within Home Assistant, it is still there, but when I do reboot, it looses the configuration and can-utils.
It seems that the script is not being run correctly?

I’ve installed PyScript hoping it would change something but I couldn’t get it to run with my HA startup automation as it’s not showing up in the “Call a Service” list…

Do I have to add something else to my .py script?

subprocess.call(['modprobe', 'can'])
subprocess.call(['modprobe', 'can_raw'])
subprocess.call(['modprobe', 'can_dev'])
subprocess.call(['modprobe', 'gs_usb'])
subprocess.call(['modprobe', 'slcan'])

os.system("apk add can-utils")
os.system("slcand -o -c -s4 /dev/ttyACM0 can0")
os.system("ifconfig can0 txqueuelen 1000")
os.system("ifconfig can0 up")

Do I have to “import os” or “import sys” or add anything else?
If this step can be done by your custom_component, I’m very interested.

Apart from that, I would like to have the possibility to analyse the can messages and update sensors, just like you did.
In addition, it would be great to have the possibility to send can messages via an input_button or input_boolean that is switcheable
→ Updated by received CAN messages and sending CAN messages when being switched within HA (manually or via automations)

The great goal I’m thinking about is installing ESP32 with CAN transceivers (or similar solutions) in my walls and connecting them all by CAN-bus, when I do my house renovation.
Then I can configure them, so that they work without HA. With ESPHome it’s also possible to update them OTA and adjust the configuration. But I’m not dependant on WiFi for the general data transmission, but this is done instead via safe and robust CAN bus.

Sorry for the long text…

Goodnto hear, yeah on reboot the commands are gone indeed, that’s why you need the script and yes, it’s incomplete, you need Todo some imports… I will post my custom component tomorrow with full script , so you have an example to start

1 Like

Add this to your sensor.yaml

  - platform: dobiss
    url_states: "http://localhost:8123/api/states/"
    sensor_name: "sensor.dobiss"
    bearer_token: !secret LONGBEARERTOKEN

Above updates my sensor.dobiss with values coming from component, below, just add the dobiss folder to your custom components, folder, its customized for me, but setyp how you want it…

In my case it creates a sensor like this below, all my lighs are on relays, the value 00 is off 01 is on, thats how i can read/write the state of my lights

To send to can, you can jsut use shell command like cansend …

Hi Everyone, I’m new here only reading for now. I also would like to have everything connected via wires. After reading I tend to believe the CAN Bus is the way to go. Initially was looking into improving i2c communication over long distance, but now decided that no need invent wheel and use CAN Bus.
Just now I got an idea why not to use ESP32 with CAN Bus. ESPHome has support for CAN Bus with ESP32 and I only need wires to report the signals from devices. I’m thinking it is ok to do upgrades OTA. As generally updates not often happens. The important part is reporting back via CAN Bus is needed as wireless not reliable and can be jammed using jammers or some de-auth devices or whatever. To me ESP32 with OTA updates and reporting back via CAN Bus seems a winner. It is cheap and at the same time seems very reliable. Anyone sees problems with my way of thinking? Anyone tried same approach?