How to setup a raspberry pi 3 with CAN Interface

How to setup a raspberry pi 3 with CAN Interface?
Hardware:

  • Raspberry pi 3 b+
  • PiCAN 2 Duo – CAN-Bus Board

Software:

  • balenaOS 2.31.5+rev5

My fist step to setup this projct:
Add the overlays by: to config.txt
4 lines to the end of config.txt file:
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=mcp2515-can1,oscillator=16000000,interrupt=24
dtoverlay=spi-bcm2835-overlay

But no can driver is visable. What ist going wrong?

Hi @tomtom68 welcome to the forums!

Could you share the Dockerfile that you’re using to set up your container? I would check to make sure you’re using a stretch-based image (if you’re using Debian).

Additionally could you enable support access for your device and share the URL here, we can then take a look to see if we can figure out what’s happening. There are some ideas around debugging CAN interfaces in this thread.

The basis setup based on: https://github.com/balena-io-playground/balena-rpi-gpio-sample-with-python

FROM resin/%%RESIN_MACHINE_NAME%%-python
ENV INITSYSTEM on
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
COPY . /usr/src/app
WORKDIR /usr/src/app
CMD [“python”, “gpio_example.py”]

requirements.txt:
RPi.Gpio
python-can

And i get the following error:
[Info] Starting build for helloworld, user xxx
[Info] Dashboard link: https://dashboard.balena-cloud.com/apps/1454763/devices
[Info] Building on arm03
[Info] Pulling previous images for caching purposes…
[Success] Successfully pulled cache images
[main] Step 1/7 : FROM resin/raspberrypi3-64-python
[main] repository resin/raspberrypi3-64-python not found: does not exist or no pull access
[Error] Some services failed to build:
[Error] Service: main
[Error] Error: repository resin/raspberrypi3-64-python not found: does not exist or no pull access
[Error] Not deploying release.

The repository: resin/raspberry3-65-python is not found!

Hey @tomtom68,

the basis setup you used is out of date since we dropped support for old resin base images a while ago, the resin/raspberrypi3-64-python image is not found because that device was added after the support ended.

Please switch to the new balenalib base images by changing to FROM balenalib/%%BALENA_MACHINE_NAME%%-python. You can drop this line ENV INITSYSTEM on and add ENV UDEV on since udev is off by default.

With the new balenalib base image:

FROM balenalib/%%BALENA_MACHINE_NAME%%-python
ENV INITSYSTEM on
UDEV on
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
COPY . /usr/src/app
WORKDIR /usr/src/app
CMD [“python”, “gpio_example.py”]

i get the following errors:


[main] Step 4/7 : RUN pip install -r /requirements.txt
[main] —> Running in 431f691ef15d
[main] DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won’t be maintained after that date. A future version of pip will drop support for Python 2.7.
[main]
[main] Collecting RPi.Gpio (from -r /requirements.txt (line 1))
[main] Downloading https://files.pythonhosted.org/packages/af/2f/407b6e4cc8a0bdf434825a160bba1807991886b63cce16a5f1a6e1f24cdf/RPi.GPIO-0.6.5.tar.gz
[main] Collecting python-can (from -r /requirements.txt (line 2))
[main] Downloading https://files.pythonhosted.org/packages/7e/b2/84b5926a19a7a45602c14671defc8cbe0dbc55a8df122a12ff31e83632d0/python-can-3.2.0.tar.gz (174kB)
[main] Collecting wrapt~=1.10 (from python-can->-r /requirements.txt (line 2))
[main] Downloading https://files.pythonhosted.org/packages/67/b2/0f71ca90b0ade7fad27e3d20327c996c6252a2ffe88f50a95bba7434eda9/wrapt-1.11.1.tar.gz
[main] Collecting aenum (from python-can->-r /requirements.txt (line 2))
[main] Downloading https://files.pythonhosted.org/packages/44/c4/7cda7e7e71e18ad999cb9d23f9969f818af1ed6be71d18db7963680b8320/aenum-2.1.2-py2-none-any.whl
[main] Collecting typing (from python-can->-r /requirements.txt (line 2))
[main] Downloading https://files.pythonhosted.org/packages/cc/3e/29f92b7aeda5b078c86d14f550bf85cff809042e3429ace7af6193c3bc9f/typing-3.6.6-py2-none-any.whl
[main] Building wheels for collected packages: RPi.Gpio, python-can, wrapt
[main] Building wheel for RPi.Gpio (setup.py): started
[main] Building wheel for RPi.Gpio (setup.py): finished with status ‘error’
[main] ERROR: Complete output from command /usr/local/bin/python -u -c ‘import setuptools, tokenize;file=’"’"’/tmp/pip-install-6186Ci/RPi.Gpio/setup.py’"’"’;f=getattr(tokenize, ‘"’"‘open’"’"’, open)(file);code=f.read().replace(’"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, file, ‘"’"‘exec’"’"’))’ bdist_wheel -d /tmp/pip-wheel-XNhnzK --python-tag cp27:
[main]
[main] ERROR: running bdist_wheel
[main] running build
[main] running build_py
[main] creating build
[main] creating build/lib.linux-aarch64-2.7
[main] creating build/lib.linux-aarch64-2.7/RPi
[main] copying RPi/init.py -> build/lib.linux-aarch64-2.7/RPi
[main] creating build/lib.linux-aarch64-2.7/RPi/GPIO
[main] copying RPi/GPIO/init.py -> build/lib.linux-aarch64-2.7/RPi/GPIO
[main] running build_ext
[main] building ‘RPi._GPIO’ extension
[main] creating build/temp.linux-aarch64-2.7
[main] creating build/temp.linux-aarch64-2.7/source
[main] gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python2.7 -c source/py_gpio.c -o build/temp.linux-aarch64-2.7/source/py_gpio.o
[main] unable to execute ‘gcc’: No such file or directory
[main] error: command ‘gcc’ failed with exit status 1
[main] ----------------------------------------
[main]
[main] ERROR: Failed building wheel for RPi.Gpio

@tomtom68, we have some breaking changes in the balenalib base images (you can find more details in the blog post here https://www.balena.io/blog/new-year-new-balena-base-images/). So the default image now only contains essential packages for a project to run, not all the tools or common libraries.

Looks like you should install build-essential package here, please add RUN install_packages build-essential to your Dockerfile before RUN pip install... command.

The basic setting (raspi, balena gpio’s) works very well.

But the CAN interface driver is not detected.

I modified the config.txt and add these 3 lines to the end of file:

dtparam=spi=on
dtoverlay=mcp2515-can1-overlay,oscillator=16000000,interrupt=24
dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay

After reboot the spi controller (can0 and can1) is not detected.

Hey @tomtom68,

Could you share your Dockerfile on a gist so I can try building it on my end?

Did you make the configuration change on the dashboard or on the config.txt itself?
If so, you should make the changes on the dashboard. Go to the device page and on Device Configuration, add the custom variables there.

So RESIN_HOST_CONFIG_dtparam = on (it should be already ON)

And for the other configs, you need to add the RESIN_HOST_CONFIG_ or BALENA_HOST_CONFIG_ prefix.

Let me know how it goes!
Cheers