So this is a kind of Python API server using MQTT as the API transport. In this very first version, the API is limited, it supports the following
-
Send some text to be written on the display
-
Send heartbeat command to the script to check it is alive and get a response back
-
Send stop command to terminate the script
I do not have a PiFaceCAD hat so I could not test the actual writing to display, let's hope I got that part right!
A typical test flow to play with. Start the script and view it's operation
PS To run it with python3, just change python to python3 in the exec node
[{"id":"e42464b7.a95968","type":"inject","z":"917016c6.7508a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Heartbeat","payloadType":"str","x":180,"y":310,"wires":[["75b2fa84.747054"]]},{"id":"a69bdba1.20c1b8","type":"debug","z":"917016c6.7508a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":360,"wires":[]},{"id":"75b2fa84.747054","type":"mqtt out","z":"917016c6.7508a8","name":"","topic":"pifacecad","qos":"","retain":"","broker":"75eba16c.094f9","x":550,"y":310,"wires":[]},{"id":"c18b4225.8289d","type":"mqtt in","z":"917016c6.7508a8","name":"","topic":"hb-response","qos":"2","datatype":"auto","broker":"75eba16c.094f9","x":190,"y":360,"wires":[["a69bdba1.20c1b8"]]},{"id":"70743de8.b96b64","type":"exec","z":"917016c6.7508a8","command":"export DISPLAY=:0 && lxterminal -e python /home/pi/pifcad.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":610,"y":110,"wires":[[],[],[]]},{"id":"88aaad6b.88c11","type":"inject","z":"917016c6.7508a8","name":"Start script","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":180,"y":110,"wires":[["70743de8.b96b64"]]},{"id":"9dbe90cb.23397","type":"inject","z":"917016c6.7508a8","name":"Abort script","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Stop","payloadType":"str","x":190,"y":180,"wires":[["faf96fdf.dfb83"]]},{"id":"faf96fdf.dfb83","type":"mqtt out","z":"917016c6.7508a8","name":"","topic":"pifacecad","qos":"","retain":"","broker":"75eba16c.094f9","x":440,"y":180,"wires":[]},{"id":"b4d37661.68e6b8","type":"mqtt out","z":"917016c6.7508a8","name":"","topic":"pifacecad/write","qos":"","retain":"","broker":"75eba16c.094f9","x":580,"y":480,"wires":[]},{"id":"22230a37.ebaf36","type":"inject","z":"917016c6.7508a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Some text","payloadType":"str","x":190,"y":480,"wires":[["b4d37661.68e6b8"]]},{"id":"bebffa.78aca008","type":"comment","z":"917016c6.7508a8","name":"Use to check if script is alive","info":"","x":240,"y":270,"wires":[]},{"id":"e08c823c.4d44c","type":"comment","z":"917016c6.7508a8","name":"Use to write some text to display","info":"","x":260,"y":440,"wires":[]},{"id":"75eba16c.094f9","type":"mqtt-broker","z":"","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"online","birthQos":"0","birthPayload":"BULB-1/LWT","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"offline","willQos":"0","willPayload":"BULB-1/LWT"}]
The Python code (copy and paste into a text file named to pifcad.py, save it in /home/pi)
import sys
import os
import time
import pifacecad
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected:", rc)
client.subscribe("pifacecad/#", 0)
def on_subscribe(client, userdata, mid, granted_qos):
print ('Subscribed:', userdata, mid, granted_qos)
print ('We are here, waiting for commands...')
def on_message(client, userdata, msg):
global abort
global cad
if msg.payload.decode("utf-8") == 'Heartbeat':
client.publish("hb-response", "Script is alive")
if msg.payload.decode("utf-8") == 'Stop':
print ("Script terminates..")
abort = True
if msg.payload.decode("utf-8") == 'Reboot':
cmnd = 'sudo reboot'
os.system(cmnd)
if (msg.topic == "pifacecad/write"):
txt = msg.payload.decode("utf-8")
print (txt)
cad.lcd.backlight_on()
cad.lcd.clear()
cad.lcd.write(txt)
# inits
cad = pifacecad.PiFaceCAD()
mqtt_broker = "127.0.0.1"
mqtt_port = 1883
abort = False
client = mqtt.Mosquitto()
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
resp = client.connect(mqtt_broker, mqtt_port, 60)
client.loop_start()
while abort == False:
time.sleep(1)
client.loop_stop()
client.disconnect()
del client
print ('Script has terminated')
exit(0)