:satellite: Welcome I2C Relay to MQTT! :electric_plug:
This repo can be used to control a Dockerpi relay hat for the Raspberry Pi over MQTT, but it might work for other I2C-type relays.
To get started, even before this repo, check this nice tutorial page.
Basic implementation:
Main functions
- Fill in the
config.yaml
with your settings
- The script listens on a predefined MQTT topic (
command_topic
) and
forwards the requests to the relays on I2C protocol.
- At the same time, if a relay state is changed, it updates the
state_topic
Secondary functions
- Configure it as a service for autostart :gear:
- Enable HomeAssistant discovery :house_with_garden:
Functionalities of the script :toolbox:
- Change the state of the relays based on MQTT messages
- Configurable settings for MQTT and I2C addresses
- HomeAssistant discovery
- Online state monitoring on MQTT -> could be enhanced
- Configurable service template for autostart on boot -> could be enhanced
- Last will messages on MQTT -> tbd
Get started :rocket:
Are you interested? Cool, let's go:
- Clone the repo ->
git clone https://github.com/Andoramb/i2c_2_mqtt.git
- Install prerequisites with
pip install -r requirements.txt
- Fill in
config.yaml
, according to your needs (explanation in the file itself)
- Run
python3 mqtt2i2c.py config.yaml
and check the output.
- Upon succsessful connect, it should output
Connected with result code 0
:thumbsup:
- The state topic should be
online
:white_check_mark:
- Check MQTT so the states are reported:
- Possible values for toggling to ON state: "on", "1", "true", "ON", "TRUE", "True"
- Possible values for toggling to OFF state: "off", "0", "false", "OFF", "FALSE", "False"
- However, the app will report
"on"
or "off"
string only :bulb:
- Optional: HomeAsisstant should register the relay based on
device_name
from config.yaml :house_with_garden:
- Optional: copy the service template to
/etc/systemd/system
for autostart
- Replace
username
and /path/to/i2c_2_mqtt/
with your data :gear:
sudo systemctl daemon-reload
sudo systemctl enable i2c_2_mqtt.service
- :thumbsup:
Future improvements :thought_balloon:
Not sure yet, seems to be working :man_shrugging: