Home Assistant autoconfiguring MQTT-powered GPIO control for a Raspberry Pi.
This is designed to allow you to automagically configure some switches to show up in Home Assitant which control GPIOs on your Raspberry Pi. This was made because I couldn't get the Home Assistant remote GPIO functionality to work when running HA in docker.
It also avoids having to allow remote access to the pigpio daemon, which is kinda bad.
The following configuration:
[Devices]
powerpi_1 = 13
powerpi_1_default = 1
powerpi_2 = 6
powerpi_2_default = 1
powerpi_3 = 5
powerpi_4 = 12
Gives:
The configuration file needs to be in one of the following locations:
/etc/mqttgpio.conf
./mqttgpio.conf
(local to where you're running it)/opt/mqttgpio/mqttgpio.conf
See mqttgpio.conf.example
for an example of how to configure the application.
The following python libraries:
- paho.mqtt.client
- schedule
- gpiozero
You'll need to be running pigpio, which is fairly easy to install and start:
sudo apt install pigpio pigpio-tools pigpiod; sudo systemctl enable pigpiod; sudo systemctl start pigpiod
You might need to change the configuration in raspi-config
depending on what you've been playing with.
sudo git clone https://github.com/yaleman/mqttgpio /opt/mqttgpio
cd /opt/mqttgpio
sudo ln -s /opt/mqttgpio/mqttgpio.service /etc/systemd/system/mqttgpio.service
sudo pip3 install -r /opt/mqttgpio/requirements.txt
sudo systemctl enable mqttgpio
- Make sure you configure your
mqttgpio.conf
file in one of the above locations. sudo systemctl start mqttgpio
This'll run as root, if you want to change the user it runs as, undo the link, copy mqttgpio.service to /etc/systemd/system/
and add a User=
line to the Service section. Documentation for the service file format is here on freedesktop.org.
In the case that I update my code, or you do:
cd /opt/mqttgpio
git pull
sudo systemctl daemon-reload
(if the.service
file has changed, or you've edited it)sudo systemctl restart mqttgpio
I'm seeing "Unable to connect to MQTT Broker localhost:1883" but I thought I configured it properly
Double check the configuration file, you might have mistyped something, or you might have multiple config files. You'll probably see a line like "Configuration file had a misconfigured 'logging' setting (something) - setting to DEBUG" if it doesn't match the options available.
- Add a
_friendly
config option to push a friendly name to Home Assistant - Maybe make it so it reads back the GPIO state periodically, and if it's changed (ie, something else changes it) then udpate the state