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

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?

Thank you for the files!
Unfortunately I’m stuck with the implementation.
Can you tell me what is needed in the configuration.yaml as well?

I tried it simply with:

dobiss:

But that was not enough as I get an error after restart:
Setup failed for custom integration dobiss: No setup or config entry setup function defined.

When I only enter the sensor.yaml code, like you mentioned, nothing happens

I had the same thought as you and I think it was also discussed earlier in this topic (here and the post below)

The main advantage of the CAN approach is (similar to KNX) that the sensors, buttons, lights and plugs will work independently of Home Assistant as the configuration is on each ESPHome device (on_frame, canbus_send, etc → see here)

I tried this on the workbench with two ESP32, one with a button (canbus.send) and one only listening to CAN (on_frame), both equipped with cheap SN65HVD230 CAN Boards and wired together. It works perfectly. I even implemented the ESPHome <–> Home Assistant interface with the onframe of the second ESP which also works flawlessly:

on_frame:
    - can_id: 30
      then:
        - if:
            condition:
              lambda: 'return x[0] == 1 && x[1] == 1 && x[2] == 1;'
            then:
            - homeassistant.service:
                service: input_boolean.toggle
                data:
                  entity_id: input_boolean.can_test1

This leads to my main concern and to the main question of how we want to use Home Assistant in our homes → Only as a second screen or as the real automation platform.

With this setup the devices will act as programmed (updatable via OTA (wifi) for example from within HA with the great ESPHome integration) and the ESPHome can update the status to Home Assistant via the interface.
What I think is the main disadvantage is the following:
If the Wifi drops out → No updates will be sent to HA and therefore all automations that will send back commands to the ESPHome will also not be applied via CAN
You basically have made the effort to implement CAN but still have the interface to HA via a wireless connection (even if you use an ESP with LAN, the switch/router can still drop out)

This is why I was so interested in the CAN-to-USB devices.
Basically all that Home Assistant needs is power, all sensors are updated directly via CAN and all automation commands are sent directly via CAN.
Of course I still think that the CAN nodes (light switches, plugs, lights) could still be equipped with ESPHome devices (with CAN boards and relays, etc.) that are programmable via OTA over wifi (as this is not real-time dependant)

Hey @flix-one , you need to put the full config like:

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

I added this code to sensors.yaml like you described:

#CAN BUS via dobiss custom_component      
- platform: dobiss
  url_states: "http://localhost:8123/api/states/"
  sensor_name: "sensor.dobiss"
  bearer_token: !secret bearer

added some random password EDIT: long-time token to secrets.yaml for bearer
bearer: *********

If I don’t add anything to configuration.yaml (of course sensor: !include sensors.yaml is included)
then I can restart HA and it loggs the warning

Logger: custom_components.dobiss.sensor
Source: custom_components/dobiss/sensor.py:40
Integration: dobiss ([documentation](https://github.com/pergolafabio), [issues](https://github.com/pergolafabio))
First occurred: 13:59:39 (1 occurrences)
Last logged: 13:59:39

1 : http://localhost:8123/api/states/ sensor.dobiss ***********

But there is no sensor.dobiss in Entities or States under Developer Tools

That was why I was wondering if there is something else I need to add

What is interesting is, that it seems that the general setup of the CAN connection is working somehow.

Edit: Ok the problem below was caused by me not using a long term token for authentication.
So if I add additional _LOGGER.warning in sensor.py (e.g. _LOGGER.warning(message) in line 53), I can see the messages incoming, but there is no sensor.dobiss available where it is shown?

When I press on my testbutton of one ESP and a CAN message is sent, then I get a warning in the HA logs:

Login attempt or request with invalid authentication from localhost (::1). Requested URL: '/api/states/sensor.dobiss'. (python-requests/2.28.1)

ow, sorry , for got something, you need to create the template sensor first manually, the component just updates it… add below as a template sensor

sensor: 
  - name: dobiss
    state: "dobiss"  

But remember; the code is customized for my project, uncomment line 61 to see the data coming from your canbus; and then change the code like you want it to be shown in the sensor…