Anyone got mqtt-gpio-monitor working with a PiFace?

I’ve installed this on an old Pi today but I cannot get the relays to fire using MQTT. If I push one of the PiFace buttons the script publishes the correct message but using /mqtt-gpio-monitor/out/1 or /mqtt-gpio-monitor/in/1 with a payload or either 0 or 1 while updating the broker, does not adjust the input or output states (according to the log).

Anyone using that script on a Pi with a PiFace?

I’ve got it working. I’ll post configs in a little bit.

That would be awesome! I’d given up hope :yum:

I use the Rapberry Pi with Piface to open my garage doors. I use one relay on the Piface for each garage door. I have magnetic door sensor to monitor the status of the garage door (open or closed) connected to the input pins 0 and 1. I have Home Assistant and mosquitto MQTT broker installed on another server.

First lets concentrate on the Home Assistant/mosquitto server.

I installed mosquitto mqtt broker on my Home Assistant server.
Here is how to install on Debian Jesse:

sudo apt-get update
sudo apt-get install mosquitto

I installed paho-mqtt on my raspberry pi and my home assistant server:
pip install paho-mqtt --upgrade (python 2)
or pip3 install paho-mqtt --upgrade (python3)

Here is the mosquitto config on my Home Assistant server:
pid_file /var/run/mosquitto.pid
user mosquitto
port 1883
listener 9001
protocol websockets
persistence true
persistence_file mosquitto.db
persistence_location /var/lib/mosquitto/
log_type error
connection_messages true
log_timestamp true
allow_anonymous false
password_file /etc/mosquitto/pwfile

Here is how to set the mosquitto pwdfile:
sudo mosquitto_passwd /etc/mosquitto/passwd MQTT_USER

Substitute the user name you want use for MQTT_USER and the enter the password you want to use when prompted. Make sure you change your config files for mosquitto, home assistant mqtt yaml file, and the raspberry pi mqtt-pi-monitor.ini to use your selected username and password.

Here is the mqtt.yaml file for Home Assistant:
broker: 127.0.0.1
port: 1883
client_id: home-assistant-1
username: MQTT_USER
password: MQTT_PASS
keepalive: 60

I use the following python script to send the message to the MQTT broker to open the garage door:
#!/usr/bin/python

import time
from time import sleep
import random
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

broker = ‘MQTT_HOST_IP_ADDR’
state_topic = ‘/mqtt-pi-monitor/in/1’

Send messages in a loop

client = mqtt.Client(“ha-client”)
client.username_pw_set(“MQTT_USER”,“MQTT_PASS”)
client.connect(broker)
#client.loop_start()

client.publish(state_topic, 1)
sleep(1)
client.publish(state_topic, 0)

I call the garage door python script from Home Assistant with the following command_line switch.yaml file:

  • platform: command_line
    switches:
    left_garage_door:
    command_on: ‘/usr/bin/python /home/hass/.homeassistant/shell/external/open_left_garage_door.py’
    #command_off: ‘/usr/bin/python /home/hass/.homeassistant/shell/external/open_left_garage_door.py’
    friendly_name: Left Garage Door

Now for the Raspberry Pi with Piface.
Make sure you have the Piface digital libraries installed:
sudo apt-get install python3-pifacedigital-emulator for python3 or
sudo apt-get install python-pifacedigital-emulator for python2

Here is the config file for the mqtt-pi-monitor script (mqtt-pi-monitor.ini):
[global]
MODULE = pfio
DEBUG = True

MQTT_HOST = MQTT_HOST_IP_ADDR - This is the ip address of your mosquitto MQTT broker
MQTT_PORT = 1883
MQTT_USERNAME = MQTT_USER
MQTT_PASSWORD = MQTT_PASS
MQTT_CLIENT_ID = mqtt-pi-monitor
MQTT_QOS = 2
MQTT_RETAIN = False
MQTT_CLEAN_SESSION = True

MQTT_TOPIC = /mqtt-pi-monitor
MQTT_LWT = /clients/mqtt-pi-monitor

MONITOR_PINS = 0, 1
MONITOR_POLL = 0.1
MONITOR_REFRESH = /mqtt-pi-monitor/refresh

Hopefully that is enough to get you going.

1 Like

Once I get time I’ll write up a full tutorial and post the link to it here.

I know it has been a while, but did you ever end up writing the full tutorial? Trying to get piface to talk to HA with no luck.