# 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:** 7\
**Page:** 2

<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:** [18 January 2020 12:16 UTC](https://discourse.nodered.org/t/piface-cad-a-different-request/20295/22 "2020-01-18T12:16:51Z")

</div>

Actually, could be it would work,,,,at least the library import works, so worth trying:

1. Install node-red-contrib-python-function (it seems to be newer than the python3 you mentioned)

2. Import and deploy the flow below (NR must in this case run in the same Pi as the piFaceCAD is mounted)

If it works, you should be able to see messages written on the lcd and in the debug window

The simple flow:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/66145fa74080ce8d2499ff4d9c98b06f9629426a.png)

```auto
[{"id":"13305b2.f5872a5","type":"python-function","z":"ae89c6f4.025a58","name":"","func":"import pifacecad\n\ndef update_pin_text(event):\n event.chip.lcd.set_cursor(13, 0)\n event.chip.lcd.write(str(event.pin_num))\n node.send(\"You pressed button:\"+str(event.pin_num))\n\ncad = pifacecad.PiFaceCAD()\ncad.lcd.write(\"Just a test\")\nlistener = pifacecad.SwitchEventListener(chip=cad)\nfor i in range(8):\n listener.register(i, pifacecad.IODIR_FALLING_EDGE, update_pin_text)\nlistener.activate()\n\nif msg['payload'] == 'abort':\n node.warn('aborting listener')\n listener.deactivate()","outputs":1,"x":740,"y":480,"wires":[["b2d81da9.0f60a"]]},{"id":"28c4c4cf.eb5c6c","type":"inject","z":"ae89c6f4.025a58","name":"","topic":"","payload":"abort","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":580,"y":480,"wires":[["13305b2.f5872a5"]]},{"id":"b2d81da9.0f60a","type":"debug","z":"ae89c6f4.025a58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":920,"y":480,"wires":[]}]

```

The python code in the python function node:

```auto
import pifacecad

def update_pin_text(event):
    event.chip.lcd.set_cursor(13, 0)
    event.chip.lcd.write(str(event.pin_num))
    node.send("You pressed button:"+str(event.pin_num))

cad = pifacecad.PiFaceCAD()
cad.lcd.write("Just a test")
listener = pifacecad.SwitchEventListener(chip=cad)
for i in range(8):
    listener.register(i, pifacecad.IODIR_FALLING_EDGE, update_pin_text)
listener.activate()

if msg['payload'] == 'abort':
    node.warn('aborting listener')
    listener.deactivate()

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [18 January 2020 20:45 UTC](https://discourse.nodered.org/t/piface-cad-a-different-request/20295/23 "2020-01-18T20:45:49Z")

</div>

Thanks very much for that.

That post of mine was more rhetorical to you. I was just _thinking aloud_ of the possibility.  
But I have imported it and it does seem to work.

I'll now sit down and teak bits of the code.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [18 January 2020 21:18 UTC](https://discourse.nodered.org/t/piface-cad-a-different-request/20295/24 "2020-01-18T21:18:16Z")

</div>

I'll say I know **notihing** about python.

When I read the code I try to simply break it down into blocks which I try to not look into/at.

(This is again me just thinking aloud, so please don't take it as I am unhappy with what you did)

The original bit of code which was an external file:  
That was a lot bigger.

The new code is (therefore) a lot smaller than the original.  
It doesn't have MQTT. No problem.  
It only seems to work once.  
The other code kept running, and although the node says `running` I'm not sure it is.

I implement MQTT, I put in the line:  
`import paho.mqtt.client as mqtt`  
and then added:  
` send_mqtt_message("PIFACE_BUTTON", str(event.pin_num))`  
in the bit which shows what button is pressed. But noting is sent.  
(Probably because I haven't really connected to MQTT.)

Which I guess are there lines:

```auto
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...')

```

I'll keep plodding away at it.

But - going way off topic:  
Are you good at understanding how node-code works (I guess that means JS) also?  
Someone else wrote some code for the LCD which works, but it is missing what I think is a really basic feature which is being _faked_ in how you send it messages.

I won't go on at this stage. I think I have enough to deal with just now getting the MQTT working in the new code you posted.

---

<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:** [19 January 2020 05:14 UTC](https://discourse.nodered.org/t/piface-cad-a-different-request/20295/25 "2020-01-19T05:14:51Z")

</div>

Andrew, don't waste time on the mqtt stuff in the code while using the python function node. It is not needed in that case, we are in NR, we will just send out what we need from the node and then to a mqtt out node. One reason this code is shorter than the external script

Another reason, I just wanted to get a confirmation that you could communicate with the piface from the python function node. Just to clarify, you wrote it worked once. Did you see the text "Just a test" on the lcd? Did any button work but just once? If so there is hope, then it is worth adding more code to make it work as we want

I think the text "Running" is just indicating that the python code syntax is correctly written

Regarding coding nodes, no, that is not on my agenda. I made some changes to some nodes but in general, I have no plans. If a node author lost interest or appetite there must be a reason behind. Limited time, technical problems, lost faith

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [19 January 2020 06:33 UTC](https://discourse.nodered.org/t/piface-cad-a-different-request/20295/26 "2020-01-19T06:33:49Z")

</div>

> [@krambriw](#):
>
> Andrew, don't waste time on the mqtt stuff in the code while using the python function node. It is not needed in that case, we are in NR, we will just send out what we need from the node and then to a mqtt out node. One reason this code is shorter than the external script

Of course! Silly me.

I have got the original code working great. It accepts multiple button presses.

On the last part about node coding.  
3 hours of head bashing I found and fixed the problem.  
An erroneous `=` in the code made something not work as expected.

---

<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:** [19 January 2020 06:54 UTC](https://discourse.nodered.org/t/piface-cad-a-different-request/20295/27 "2020-01-19T06:54:27Z")

</div>

A bit unclear to me what you use, the original python script I wrote, the 3 hours head bashing node (JS) fix. Or what?

You never confirmed if you saw that message on the lcd ("Just a test") when you tried the python code in the python node ???

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [19 January 2020 07:02 UTC](https://discourse.nodered.org/t/piface-cad-a-different-request/20295/28 "2020-01-19T07:02:28Z")

</div>

Sorry.

Your code works.

The original works fine. The newer bit works also, I was just messing around trying to make it a complete replacement for the _older_ external file. (Keeping it all in NR)

**one** of my other (many) problems is the node which does the PiFaceCAD LCD.  
It works. It requires a _prefix_ of `1,1:` (or something like that) to position where on the display the message is printed.

Alas _out of the box_ it **only** works with `1,1:`  
Putting anything else, like `2,2:` it is printed at `1,1:`  
I mentioned this to the author but it isn't on their priority list.  
Which is strange because if you don't put the `1,1:` the node throws an error.  
My suggestion was if there isn't a valid _prefix_ it defaults to `1,1:` Again: Little interest.

So, I was messing around with that - which is why I am needing/wanting to get the buttons to work.  
Button presses put messages at different places on the screen.

I decided today to put my effort to trying to work out why this other node isn't/wasn't working out of the box as claimed.

3 hours. Painful hours. I found the problem and fixed it. Now I can send messages to be printed anywhere on the screen with a working prefix.

[Previous page](https://discourse.nodered.org/t/piface-cad-a-different-request/20295.md?page=1)
