No problem about not knowing the size of the display.
I changed the topics (finally did find them) to are more understandable:
PIFACE_CMDS
and PIFACE_BUTTON
I use upper case only to keep things constant.
This is the script now:
# coding: utf-8
import pifacecad
import time
import paho.mqtt.client as mqtt
def update_pin_text(event):
event.chip.lcd.set_cursor(13, 0)
event.chip.lcd.write(str(event.pin_num))
send_mqtt_message("PIFACE_BUTTON", str(event.pin_num))
def send_mqtt_message(topic, msg):
for i in range(5):
try:
result, mid = client.publish(topic, msg, 0)
#print ('result', result)
if result == 0:
break
except:
print ("retrying...")
def on_connect(client, obj, flags, rc):
client.subscribe("PIFACE_CMDS", 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(mosq, obj, msg):
global abort
try:
event = msg.payload.decode("utf-8").split('.')
#print (event)
if event[0] == 'abort':
listener.deactivate()
abort = True
except:
pass
abort = False
client = mqtt.Mosquitto()
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.connect("192.168.0.99", 1883, 60)
client.loop_start()
cad = pifacecad.PiFaceCAD()
cad.lcd.write("Welcome to your piFaceCAD! You pressed: ")
listener = pifacecad.SwitchEventListener(chip=cad)
for i in range(8):
listener.register(i, pifacecad.IODIR_FALLING_EDGE, update_pin_text)
listener.activate()
while not abort:
time.sleep(0.5)
client.loop_stop()
client.disconnect()
del client
exit(0)
This is the flow I have - basic one:
(Nodes needed: node-red-contrib-simple-gate
)
More for basic control of what is happening.
[{"id":"20f34bd1.6a0464","type":"inject","z":"cc585130.29b7e8","name":"","topic":"","payload":"This is a test","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":410,"y":880,"wires":[["c8333c.caa94cc8"]]},{"id":"c8333c.caa94cc8","type":"change","z":"cc585130.29b7e8","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"0","tot":"num"},{"t":"set","p":"position","pt":"msg","to":"1,1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":880,"wires":[["118d9066.a076c"]]},{"id":"118d9066.a076c","type":"function","z":"cc585130.29b7e8","name":"Build MQTT message","func":"msg.payload = {\n payload:msg.payload,\n delay:msg.delay,\n position:msg.position\n}\nreturn msg;","outputs":1,"noerr":0,"x":880,"y":950,"wires":[["99948203.32add","82472447.89f28"]]},{"id":"99948203.32add","type":"mqtt out","z":"cc585130.29b7e8","name":"","topic":"PFC_LCD","qos":"","retain":"","broker":"c97b6780.72589","x":1110,"y":950,"wires":[]},{"id":"6c85fb2b.30082c","type":"mqtt in","z":"cc585130.29b7e8","name":"","topic":"PFC_LCD","qos":"2","datatype":"auto","broker":"c97b6780.72589","x":140,"y":470,"wires":[["5d881b63.b2030c","5cd50f9e.41f8a8"]]},{"id":"5cd50f9e.41f8a8","type":"json","z":"cc585130.29b7e8","name":"","property":"payload","action":"","pretty":false,"x":290,"y":470,"wires":[["9dbf0935.d41188","31053769.d90578"]]},{"id":"31053769.d90578","type":"function","z":"cc585130.29b7e8","name":"Build message","func":"let pos = msg.payload.position;\nlet pld = msg.payload.payload;\nlet temp = pos + ':\"' + pld + '\"';\nmsg.delay = msg.payload.delay;\nmsg.payload = temp;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":470,"wires":[["f65ec48a.bae2c","9456039d.96cef8","e47a1494.e123b"]]},{"id":"9456039d.96cef8","type":"function","z":"cc585130.29b7e8","name":"Make seconds.","func":"//msg.delay = parseInt(msg.payload.delay);\n\nnode.warn(msg.delay);\n\nif (msg.delay > 0)\n{\n msg.delay = msg.delay * 1000;\n node.warn(msg.delay);\n return msg;\n}\n\n// payload.delay","outputs":1,"noerr":0,"x":650,"y":490,"wires":[["c3d5d766.2c932","6b9a665c.541f18"]]},{"id":"c3d5d766.2c932","type":"delay","z":"cc585130.29b7e8","name":"","pauseType":"delayv","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":630,"y":530,"wires":[["559af61b.1ef19"]]},{"id":"559af61b.1ef19","type":"change","z":"cc585130.29b7e8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":530,"wires":[["e47a1494.e123b"]]},{"id":"e47a1494.e123b","type":"switch","z":"cc585130.29b7e8","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"OFF","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1060,"y":470,"wires":[["c73cf27c.fecfc"],["ed66b5c2.8a7a18"]]},{"id":"c73cf27c.fecfc","type":"gate","z":"cc585130.29b7e8","name":"","controlTopic":"HALT","defaultState":"open","openCmd":"GO","closeCmd":"STOP","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":1240,"y":470,"wires":[["3d749079.fb982","c4db9353.f211e8"]]},{"id":"ed66b5c2.8a7a18","type":"exec","z":"cc585130.29b7e8","command":"/home/pi/PiFace/LCD_off.sh","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Turn off light & Wipe Display","x":1300,"y":530,"wires":[[],[],[]]},{"id":"3d749079.fb982","type":"pfc_lcd","z":"cc585130.29b7e8","name":"Display","x":1420,"y":470,"wires":[]},{"id":"c97b6780.72589","type":"mqtt-broker","z":"","name":"MQTT Host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"20","cleansession":true,"birthTopic":"SOM","birthQos":"2","birthPayload":"'Awaiting PiFace'","closeTopic":"","closePayload":"","willTopic":"EOM","willQos":"0","willPayload":"'PiFace telemetry failure'"}]
This is the flow to read the buttons:
Node needed (node-red-contrib-repeat
)
[{"id":"15bee644.50e7c2","type":"inject","z":"710ca09a.8302a8","name":"Start script","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"1","x":140,"y":120,"wires":[["b6f18359.433dc8"]]},{"id":"b6f18359.433dc8","type":"exec","z":"710ca09a.8302a8","command":"export DISPLAY=:0 && lxterminal -e python /home/pi/piFaceCAD.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":580,"y":120,"wires":[[],[],[]]},{"id":"73cbc1ca.86f548","type":"mqtt in","z":"710ca09a.8302a8","name":"","topic":"PIFACE_BUTTON","qos":"0","datatype":"auto","broker":"c97b6780.72589","x":160,"y":380,"wires":[["46460da3.617224","c59b6c53.4d347"]]},{"id":"c59b6c53.4d347","type":"repeat","z":"710ca09a.8302a8","name":"","repetitions":"1","elseOutput":false,"outputs":1,"x":370,"y":380,"wires":[["e50784b9.6ffb4","9ac54bf.4dbceb8"]]},{"id":"9ac54bf.4dbceb8","type":"debug","z":"710ca09a.8302a8","name":"After Repeat","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":550,"y":340,"wires":[]},{"id":"c97b6780.72589","type":"mqtt-broker","z":"","name":"MQTT Host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"20","cleansession":true,"birthTopic":"SOM","birthQos":"2","birthPayload":"'Awaiting PiFace'","closeTopic":"","closePayload":"","willTopic":"EOM","willQos":"0","willPayload":"'PiFace telemetry failure'"}]
So I just then put a switch
node and split/route the message as per what it is.
Just want to check how I get rid of the extra console which is opened.
I thought it was in the exec
node but when I deleted the first part before the &&
it didn't work. Well, it possibly will now I said it didn't.
And not display the message when the script starts. But I don't think that is too difficult.