Using MQTT with Raspberry Pi GPIO

Hi, I’ve been trying to integrate MQTT with HA for a project, and I’m using Relay Module for my home devices.

The MQTT has been working on my Pi/HA when I tested, but I’m still being rather confused about the configuration.yaml code for GPIO and Relay Module, any help?

I’ve been trying to use this link: https://github.com/flyte/mqtt-io, but that still didn’t work for me. Here’s my configuration for MQTT:

mqtt:
  broker: (my IP address)
  port: 1883
  client_id: home-assistant-1
  username: (my username)
  password: (my password)

switch:
  - platform: mqtt
    name: “Switch”
    state_topic: “ha/switch”
    command_topic: “ha/switch/set”
    payload_on: “ON”
    payload_off: “OFF”
    state_on: “ON”
    state_off: “OFF”
    optimistic: false
    qos: 0
    retain: true

gpio_modules:
  - name: rpi
    module: raspberrypi

digital_outputs:
  - name: ‘Indoor light’
    module: rpi
    pin: 20
    on_payload: “ON”
    off_payload: “OFF”

  - name: ‘Outdoor light’
    module: rpi
    pin: 21
    on_payload: “ON”
    off_payload: “OFF”

But when checking configuration, it’s said that gpio_modules and digital_outputs are not found.

1 Like

I think you are mixing config here. The gpio_modules entry should be in your configuration file for mqtt-io (the config file you start it with: python3 -m mqtt_io config.yml), not in Home Assistant’s configuration.yaml.

That configuration does not go in Home Assistant. It goes in a file called config.yml on the pi flyte’s application is running on.

Oh ok, thank you for the information, first step to get out of my stuck state. Though I want to ask again that I’m still quite a beginner, where to edit the config.yml file in mqtt-io? I executed the command in RPi terminal and it ended with

FileNotFoundError: [Errno 2] No such file or directory: 'config.yml'

I created this directory and put it in there: /home/pi/Documents/mqtt-gpio-bridge

However I used these older installation instructions:

For this part:

Create yourself a config file, following instructions and examples above, and save it somewhere, such as /home/pi/pi-mqtt-gpio.yml.

Do you have to create it in home/pi directory or anywhere? As you said you create it in /home/pi/Documents/mqtt-gpio-bridge instead? (the ‘mqtt-gpio-bridge’ is a folder right?). And the yml file was created with good-ol sudo nano config.yml?

And about the supervisor config file, as it’s an older instruction, it has to be changed into like this?

[program:config]
command = python3 -m mqtt_io config.yml
directory = /home/pi
redirect_stderr = true
stdout_logfile = /var/log/config.log

No that should exist.

Yes

No I created it with VSCode and copied it to the pi with winscp. But nano will do too.

No idea.

If it works properly, will this be the proper output shown in terminal?

Figured it out on how to use it. But is it necessary for me to keep the:

switch:
  - platform: mqtt
    name: (insert name)
    state_topic: (insert topic)
    command_topic: (insert name)
    payload_on: “ON”
    payload_off: “OFF”
    state_on: “ON”
    state_off: “OFF”
    optimistic: false
    qos: 0
    retain: true

Part in the configuration.yaml? But are there any way for the Lovelace to reflect the MQTT device status? The switch entity in the Lovelace bounces back to off when I turned it on.

Also the flyte/mqtt-io can’t use the PIR and Flame Module Sensors? Are there any way to use them or I have to use substitutes?

Then either your state topic or state_on/off is incorrect.

Can you also answer my other question here?

Also the flyte/mqtt-io can’t use the PIR and Flame Module Sensors? Are there any way to use them or I have to use substitutes?

I’d like to ask, if my state topic or on/off is incorrect, then if the GPIO configuration is this (a bit of a change):

mqtt:
  host: (my IP address)
  topic_prefix: ha

gpio_modules:
  - name: rpi
    module: raspberrypi

digital_outputs:
  - name: indoorlight
    module: rpi
    pin: 20
    on_payload: “ON”
    off_payload: “OFF”

And if the subscribed topic written in the terminal (like the screenshot above) is:

ha/output/indoorlight/set
ha/output/indoorlight/set_on_ms
ha/output/indoorlight/set_off_ms

Then what should I fill for the state_topic and command_topic in the MQTT switch configuration? Currently I filled it with:

    state_topic: ha/output/indoorlight
    command_topic: ha/output/indoorlight/set

Probably something to do with those fancy quotes (should be "" not “”).

I forgot to specify that the quote part is already the "" part.

And there wasn’t a problem of turning on/off the GPIO pins, sending the payload ON/ OFF to the MQTT topics completely work. The problem’s only the display (switch configuration) at the Lovelace.

OK finally got it for the Lovelace part, I had to use the entities provided by the server which appears after connecting to MQTT in the Terminal. But it seems that I don’t need the HA configuration.yaml for this.

Can users create automations using Pi + MQTT + GPIO?

Hi I’ve been trying to setup a basic relay control with MQTT-io but I keep getting errors around no access to DEV/mem.

The config I’ve been using is below (basic example from the docs) and I’m using pin 11 (GPIO17), I’ve tried every combination I can think of but I keep getting

RuntimeError: No access to /dev/mem. Try running as root!

I am running hassio on pi4 and looked into installing the addon via terminal as sudo but I can’t find installation instructions for installing with hassio, as well normal linux commands (like sudo or apt) dont seem to work. Does anyone have any experience with this? can i change permissions on hassio somehow?

I can control the gpio’s with the raspberry pi gpio integration so pin control is definitely doable.

gpio_modules:

  • name: gate

    module: raspberrypi

digital_outputs:

  • name: socket1

    module: gate

    pin: 11 #17 and GPIO_17 also attempted

    on_payload: “ON”

    off_payload: “OFF”