Enable spi and gpio in docker?

I have Home Assistant running in Docker on a PI4.
My addons are all installed through the hass.io dashboard int otheir own little containers.

What is the easiest way to communicate with an ADC hat that uses SPI and requires access to GPIO?

Should I use python code in AppDaemon container or can a flow be created in Node-Red container?
What GPIO library should I use? It doesn’t seem that any of them work with appDaemon in a container.

I’ve tried so many ways to enable gpio in AppDaemon; Rpi.GPIO, wiringPi, and now pigpio.
Latest attempt is with pigpio. I have AppDaemon operating with protection mode off. I have remote gpio enabled in raspi-config.
I know its running, pigs t in ssh returns a number.
error lately has been ‘Can’t connect to pigpio at localhost(8888)’.

I’ve installed the node-red node-red-node-pi-gpio and can actually get data into an mqtt sensor in home assistant, but no clue if I should be pursuing this idea to engage the SPI bus with it.

Any hints are appreciated.!

Hi - did you ever get this sorted out?
I’m thinking about the same setup but am worried about GPIO access from HassIO running in a container.

sort of.
GPIO access with Hassio in a container is easy. Trying to manipulate the SPI bus in a container, well, that’s the trick.

I should point out that this post related to the WaveShare board noted here: https://www.waveshare.com/wiki/High-Precision_AD/DA_Board. Their ‘demo’ software is great if you don’t need it to work with anything else.

So I started out with this code: https://github.com/ul-gh/PiPyADC and began learning python.
I could not get wiringPi to work with docker.
I was able to modify his code to work with PigPIO. Where it stands now is I have a command line sensor access my modified python script. It’s actually pretty easy; the sensor returns whatever the python prints, so I have it print all 8 values as json.
One of my todo’s is to figure out how to use github so that someone else can build on this and maybe fix it… see below

sensor:
  - platform: command_line
    name: raw adc inputs
    scan_interval: 60
    command: 'python3 /config/adc/sensor_read.py'

so it returns:
{ “ad0”: 2.2249677449426346, “ad1”: 2.883271918686857, “ad2”: 0.8064151771563502, “ad3”: 0.8307732141939657, “ad4”: 0.833444694691264, “ad5”: 0.7961363549395031, “ad6”: 0.8000291347538393, “ad7”: 0.7868058427340797, “chip_ID”: 3 }

I then publish it into this sensor:

  - platform: mqtt
    state_topic: adc/inputs
    name: "adc_inputs"
    json_attributes_topic: "adc/inputs"
    unit_of_measurement: 'V'
    value_template: "Analog/Digital Converter inputs"

which then allows me to address the values with things like ‘{{ state_attr(“sensor.adc_inputs”, “ad0”) | round(1) }}’

Now here’s the problem:

  1. the board goes offline a couple of times a week. Employing what I call ‘voodoo’, I am able to restore function by ssh’ing in and running a bash script with “for ((i=0; i<32; i++)); do pigs spic $i; done” in it. It closes all open pig handles (of which there are none open anyways, as it turns out. I only suspected this, and I have no freakin clue why this works in restoring board function.)
    Due to permissions, I have yet to successfully integrate the bash script into a command line automation. Still working on it.
    Incidentally, that’s the reason for “chip_ID”. It returns ‘0’ when the board stb.

Currently:
I’m waiting on a pi ADC-PI board from AbelElectronics in the UK right now. instead of using the SPI bus, it uses I2C and appears to have software available that might even work with it.
I’ll report back in a few more weeks…

What commands did you use in your Docker.template file to start the pigpio daemon?

I have tried putting
CMD ["sudo pigpiod"]
before the
CMD ["python","-u","src/test_main.py"]
that starts my main loop.

I also create the pigpio instance via the following

pi_gpio = pigpio.pi('soft', 8888)

However, I keep getting the following message.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)

Did you start the pigpio daemon? E.g. sudo pigpiod

Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888

Did you specify the correct Pi host/port in the
pigpio.pi() function? E.g. pigpio.pi('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

I have tried to start the daemon in its own container, via the following

And the following environment variables are set.

export PIGPIO_ADDR=soft
export PIGPIO_PORT=8888

But still getting the same message about not being able to connect to pigpio at localhost(8888)

I couldn’t figure out how to start it in its own container, let alone tell the HA container to allow it.
I ssh’d to it and installed it as root user. Then Node Red was able to ‘see’ it. I would suggest installing node red first, if you haven’t already. Some of this stuff is already there; I didn’t need to (re)install it.

The ADC-PI board from AbelElectronics turned out to be pure magic. It worked right out of the box, their example code needed minimal modification for my purposes, voltage divider needs only 1 resistor (there’s one on the board).