# PiFace CAD - a different request

**URL:** <https://discourse.nodered.org/t/piface-cad-a-different-request/20295>\
**Category:** General\
**Created:** [14 January 2020 04:53 UTC](https://discourse.nodered.org/t/piface-cad-a-different-request/20295 "2020-01-14T04:53:51Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [16 January 2020 09:23 UTC](https://discourse.nodered.org/t/piface-cad-a-different-request/20295/13 "2020-01-16T09:23:38Z")

</div>

@Trying_to_learn

Dear Andrew, no problems at all, sorry and sad for those wild fires, worst ever I think? Terrible, really.

I took some time to write a small python script that you can test when time and if you like. It is rather straight forward, it connects to a mqtt broker (I assumed in the same Pi, otherwise you have to edit the ip). It loads the pifacecad python library, writes a welcome message to the lcd. When you press a button you should see it's number on the lcd and receive a mqtt message in Node-RED

To make it happen:

1. install the pifacecad python library, see here [http://piface.github.io/pifacecad/installation.html](http://piface.github.io/pifacecad/installation.html)

```auto
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install python{,3}-pifacecad

```

1. Install paho mqtt client (if you do not have it already)

```auto
sudo pip install paho-mqtt

```

1. Create a new file piFaceCAD.py in /home/pi/ with the following content (and change ip if necessary)

```auto
# 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("pifacecad", "You pressed 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("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("127.0.0.1", 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)

```

To try it out, you can import/use the following flow. To start or abort the script, just click on the buttons. To make it "visible" the script is started on the display nbr 0. I hope it works (not able to test with the piface), if so, you should see the messages in the NR debug window

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a2b35028578507dc075689de5d0e2bfb308f972.png)

```auto
[{"id":"d4ed32d4.b091a","type":"exec","z":"ae89c6f4.025a58","command":"export DISPLAY=:0 && lxterminal -e python /home/pi/piFaceCAD.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":700,"y":100,"wires":[[],[],[]]},{"id":"e5597030.9c372","type":"inject","z":"ae89c6f4.025a58","name":"Start script","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":100,"wires":[["d4ed32d4.b091a"]]},{"id":"9f5a12c4.c3359","type":"inject","z":"ae89c6f4.025a58","name":"Abort script","topic":"","payload":"abort","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":180,"wires":[["2b2c9050.0d932"]]},{"id":"2b2c9050.0d932","type":"mqtt out","z":"ae89c6f4.025a58","name":"","topic":"cmds","qos":"","retain":"","broker":"75eba16c.094f9","x":500,"y":180,"wires":[]},{"id":"d1ff0302.d0d1a","type":"mqtt in","z":"ae89c6f4.025a58","name":"","topic":"pifacecad","qos":"0","datatype":"auto","broker":"75eba16c.094f9","x":250,"y":260,"wires":[["e16edbaf.068c18"]]},{"id":"e16edbaf.068c18","type":"debug","z":"ae89c6f4.025a58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":520,"y":260,"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":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/399216e50f21bedbedb1bf15ce5fb7c8b307a434.jpeg)

---

_[View the full topic](https://discourse.nodered.org/t/piface-cad-a-different-request/20295)._
