Broken pipe error when running MQTT publisher with touch phat script


#1

WIth my touch phat, I am using it to create a menu on my Raspberry Pi Zero W (stretch OS). To do this (and ensure the button responsiveness), I decided to implement Paho MQTT functions in my touch phat python script which I took from pimoroni example script. So for instance, when I press the ‘A’ button, the script will publish a MQTT message to another simultaneously running script (MQTT client), informing that the button has been pressed, and the MQTT client script will do the final actions (e.g. play an audio file). The problem arise when my script has been inactive for about 5 minutes. When I touch the button again, terminal prints an error stating broken pipe ([Errno 32] Broken pipe) for the touchphat script with MQTT publisher functions. Subsequent attempts to press the touchphat buttons does not result in the printing of the same error (broken pipe) on terminal, but the MQTT client fails to recieve any further messages. I suspect that the signal.pause() at the MQTT publisher script could be causing the connection between the MQTT publisher and MQTT client communication to be permanently cut off from each other after a short period of inactivity. Any thoughts on this? And is this signal.pause() code at the bottom of the example script absolutely necessary for the touchphat to function? How can I resolve this issue? Alternatively, how can I re-establish connection in my script automatically without me needing to relaunch these python scripts.

The code for my MQTT publisher script is:

#!/usr/bin/env python

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

import signal
import time
import os

import touchphat

mqttc = mqtt.Client("client1", clean_session=False)
mqttc.username_pw_set("user", "mypassword")
mqttc.connect("m14.cloudmqtt.com", 21543, 50)


@touchphat.on_touch(['A'])
def handle_touch(event):
	mqttc.publish("touchphat", payload="A", qos=0)
	print("Button A pressed")


    
@touchphat.on_touch(['B'])
def handle_touch(event):
	mqttc.publish("touchphat", payload="B", qos=0)
	print("Button B pressed")


@touchphat.on_touch(['Back'])
def handle_touch(event):
	mqttc.publish("touchphat", payload="Back", qos=0)
	print("Button back pressed")
	
@touchphat.on_touch(['Enter'])
def handle_touch(event):
	mqttc.publish("touchphat", payload="Enter", qos=0)
	print("Button Enter pressed")
	
signal.pause() # I think this might be causing the broken pipe problem but I am not sure.

The MQTT client script is:

#!/usr/bin/env python

import signal
import time
from subprocess import call# added for aplay

import touchphat

import paho.mqtt.client as mqtt


def on_connect(client, userdata, flags, rc):
	print("Connected with result code "+str(rc))
	client.subscribe("touchphat")

def on_message(client, userdata, msg):
	print(msg.topic+" "+str(msg.payload))

	if msg.topic == "touchphat":
		if msg.payload == "A":
			print("button A is pressed")
			call(["aplay", "/home/pi/projectfolder/music.wav"])

	if msg.topic == "touchphat":
		if msg.payload == "B":
			print("button B is pressed")
			
	if msg.topic == "touchphat":
		if msg.payload == "Back":
			print("button Back is pressed")
			
	if msg.topic == "touchphat":
		if msg.payload == "Enter":
			print("button Enter is pressed")
		
# Create an MQTT client and attach our routines to it.
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set("user", "mypassword") 
client.connect("m14.cloudmqtt.com", 21543, 50) 

client.loop_forever()