Reputation: 453
I bought a touch phat from pimoroni which I am using on my Raspberry Pi Zero W (stretch OS). Touch phat is basically a capacitive button add on shield for the raspberry device.(https://shop.pimoroni.com/products/touch-phat). I decided to implement Paho MQTT functions in my touch phat python script which I took from pimoroni example script (https://github.com/pimoroni/touch-phat/blob/master/examples/buttons.py). 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? 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()
Upvotes: 0
Views: 2535
Reputation: 83
I see your port is 21543 which may indicate the ssl port on cloudmqtt. Check your account and if it is, either use the non ssl port or call tls_set_context() on your client before connecting.
Upvotes: 1
Reputation: 59751
You need to let the MQTT client in the publisher handle network events, to do this you need to start the network loop. The easiest way to do this should be to replace the signal.pause()
with client.loop_forever()
Upvotes: 0