# How does Node-Red interact with Python?

**URL:** <https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854>\
**Category:** General\
**Created:** [29 July 2020 06:21 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854 "2020-07-29T06:21:25Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [29 July 2020 06:21 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/1 "2020-07-29T06:21:25Z")

</div>

It isn't I want to try for the title of "Most active posts". I am trying to keep things segmented.

I am faced with this scenario:

I have a PiFaceCAD hat for the RasPi. It works.  
It came with some python scripts and they work.  
(Well, that's a bit debatable. However.)

There is a _problem_ though, and thus me posting:

Say I use their script to write to the display:  
I send "Hello there". (Quotes included)  
I see `Hello there` on the screen. No big. The `"` _absorbs_ the space and make the two words one parameter.

But if I run the script again: The display blinks (LED/backlight goes off then on); the display is wiped and `Hello there` is again displayed. But in the same position as before.

If I open a CLI (terminal) and type `python` and basically set things up with `import sys` and `import pifacecad` then enter the command to write "Hello there" I get `Hello there` on the screen.  
I enter the command again and this time I see a second `Hello there` on the screen appended to the first.

The display doesn't blink. It isn't cleared!

(I think it is now customary to say.....) _Woo Hoo!_

So....... Thinking about it.  
And with help from other research.  
When the `python` code is _invoked_ (the session - if you will) it _initialises_ things. And part of that is clearing the display.

However, when I am still in `python` via the CLI, that is only done ONCE, and I don't get the _initialisation_ done every time the script is run.

This is a concern, as I want to write a flow in Node-Red which writes data to the display at co-ordinates I give in the message.

The last thing I want is every time a messages is sent, the rest of the display is wiped. (And the backlight is turned off then back on.)

So, are my theories correct in what is happening? (Kind of a bit late. I have ordered the hardware)

But if this theory is true: I could _hack_ the python stuff and negate the initialisation stuff and set it as a new _parameter_ I can send to the display.

Yes, it is murky waters into which I am sailing. But I am hoping this is a learning curve which I can over come.

I'm asking here because it is how Node-Red interacts with `python` which is of interest.

Another way is that I could set _variables_ in/via Node-Red and have a python script that merely _echoes_ those onto the display.

But that is whole other way of doing it.

I'd prefer the prior way.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [29 July 2020 06:29 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/2 "2020-07-29T06:29:55Z")

</div>

I noticed in another topic from you, where you ran a script, in that script it specifically calls python3, while you had errors stating `python2`.

When you do a `python --version` on the cli, which version do you see ?

It might not matter for your issue, this appears more to be a parsing issue (or it might even be related to the different versions and the way it handles CLI arguments).

---

<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:** [29 July 2020 06:45 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/3 "2020-07-29T06:45:33Z")

</div>

Well, the other topic is/was me trying to understand what is/was going on.

I noticed that if I used `python` the script would work. But if I entered `python3` as the language, the script would really not like me.

So that is one to me being too smart for my own good.

I dug around and it would seem the script (for that **hat** ) is python 2.7 (or 6) I can't find python 2.6 in my raspi. Only 2.7.

I have bought (but it is still on its way) another display (20 x 4) and a SPI _hat_/board that is RasPi compatible (as is the display).

I am going to make the display be controlled by the RasPi - SPI preferred, and want to understand how Node-Red interacts with Python.

Be it 2.7, 3.0, or what ever.

I won't know until I get the display and build that.

Though I guess I could download the node and look at it also.

This project is on an altogether new RasPi, with a new version of NR, latest O/S. Buster I believe.

It will be a world unto itself.

I am playing with what I have to get a feel for what is needed.  
Which is NOT the ultimate devices.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [29 July 2020 07:05 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/4 "2020-07-29T07:05:10Z")

</div>

Yes if you run python in "interactive" mode through the shell/editor/idle then you setup the initial instance and the script does its thing - until you leave the editor/shell then python is in interactive mode and is essentially doing what you tell it to do line by line.

If you want to use NR to talk to Python then you will need to either 1) not initialise the screen each time - you wll have to track down in the pyhton code how that is handled and turn that into "manual mode" or 2) Call python once and pass it a heap of parameters - assuming you know them up front - if you can not do that and you more want to update individual lines on the screen etc then you will need to pull apart the python scripts they have given you and remove the initialisation codes, put them into a seperate routine that you call when needed and then just pass screen writing info the rest of the time - this would require you to keep track of where the cursor is at all times or to provide absolute (line and position) numbers each time you want to write characters out to the screen.

Craig

---

<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:** [29 July 2020 07:09 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/5 "2020-07-29T07:09:19Z")

</div>

Yes, that is my suspicion.

Another thing which has bitten me is the 20x4 nodes.....  
There are two of them with the `SPI` chip in their names.

They both look like the nearly the same code claimed by two people.

I am also wanting to send programmable characters to the display and use them.

A whole new world of pain is awaiting me.

Oh well: What's life without a challenge?

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [29 July 2020 07:09 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/6 "2020-07-29T07:09:27Z")

</div>

Is it just one of the common 20x4LCD with a MCP23008 port expander or something fancier ?

Craig

---

<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:** [29 July 2020 07:10 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/7 "2020-07-29T07:10:49Z")

</div>

If I go the _direct_ way, that may open more possibilities to me.

I did that with the Arduino when I did the clock project.

But I am not sure N-R would handle the work load _bit bashing_ stuff out the pins every second every minute, every hour for ...... ever.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [29 July 2020 07:10 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/8 "2020-07-29T07:10:55Z")

</div>

Have a look at this for example

[https://readthedocs.org/projects/rplcd/downloads/pdf/latest/](https://readthedocs.org/projects/rplcd/downloads/pdf/latest/)

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [29 July 2020 07:12 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/9 "2020-07-29T07:12:39Z")

</div>

Yeah that makes no sense to do that.

It will be just a standard display with either an i2c interface or a SPI - plenty of libraries and ways to interact with it.

I am sure there would be some low level javascript stuff as well if you did not want to learn python

Craig

---

<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:** [29 July 2020 07:16 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/10 "2020-07-29T07:16:49Z")

</div>

Interesting looking at the PDF.

There seems to be a lot of blank pages at the start.

I'm confused on page.... (_About_)

It says:

> t supports bothGPIO (parallel) mode as well as boards with an I2C port expander (e.g. the PCF8574 or the MCP23008).

That's weird. PCF8574.... Ah.... The `SPI` board I ordered is also I2C, so that's good.  
That now gives me 3 ways to connect the display: Direct, I2C or SPI.

How I read it: The PCF8574 is SPI, but.....

Spoilt for choice! 😉

I'll keep reading the PDF.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [29 July 2020 07:18 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/11 "2020-07-29T07:18:40Z")

</div>

Yep people made daughterboards that handled the bit banging of the parallel interface on these and then front ended it with either serial, i2c or spi - the most common back in the arduino days was i2c.

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [29 July 2020 07:20 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/12 "2020-07-29T07:20:31Z")

</div>

Terry at Yourduino used to make some of the most common and easiest to use - the big difference between peoples iterations was the firmware on the expander - that decided how easy or hard they were to write to

[http://www.yourduino.com/sunshop/index.php?l=product\_list&c=41](http://www.yourduino.com/sunshop/index.php?l=product_list&c=41)

Craig

---

<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:** [29 July 2020 07:23 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/13 "2020-07-29T07:23:26Z")

</div>

Again, thanks.

(Alas a bit too late.)

I've ordered the display, and all the stuff needed. Backpack, RasPi, Heat sinks.....

The display is RGB backlit, so that is nice.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [29 July 2020 09:39 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/14 "2020-07-29T09:39:06Z")

</div>

Dig down into it and you will probably be able to find whose piggyback module they are using and then decide on an appropriate library to use for the interfacing - send us a link to have a look

Craig

---

<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:** [29 July 2020 09:41 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/15 "2020-07-29T09:41:02Z")

</div>

Yeah. Thanks.

But I think I will need to wait until I have like the hardware.

Because without that, I am _flying blind_.

---

<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:** [29 July 2020 09:57 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/16 "2020-07-29T09:57:52Z")

</div>

> [@Trying\_to\_learn](#):
>
> I'm asking here because it is how Node-Red interacts with `python` which is of interest.

So much discussion about MQTT and still no wake-up-call? You should always think MQTT before anything else 😉

You say you have python code that works correctly, you say you know how to send commands. Then go ahead and write a python script that runs there in a loop, just waiting for your commands via MQTT with the content you sent to write to display

---

<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:** [29 July 2020 10:03 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/17 "2020-07-29T10:03:31Z")

</div>

What you are saying is beyond my understanding.

No, I don't have the code. I am messing around with what others have written. It kind of works.

The _problem_ is if I do it via Node-Red, the display is _initialised_ with every message.

Yet doing it from within python (in a CLI on the machine) I can send it messages and there is no initialisation with every message.

I am wanting to learn how to get around that problem so I can use Node-red.  
Writing `python` code to act as a loop and get it to receive messages from `MQTT` is **way way way** beyond my skills.

---

<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:** [29 July 2020 10:06 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/18 "2020-07-29T10:06:18Z")

</div>

> [@Trying\_to\_learn](#):
>
> I am wanting to learn how to get around that problem so I can use Node-red.  
> Writing `python` code to act as a loop and get it to receive messages from `MQTT` is **way way way** beyond my skills.

OK, no problem, we are all here trying to learn

Give me example of a python python command that works and I will make you a simple sample python script that will demonstrate what I mean. Then you can take it from there and you will be much happier

---

<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:** [29 July 2020 10:24 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/19 "2020-07-29T10:24:48Z")

</div>

(The remote machine is booting.)

Existing problems:  
I somehow have lost the ability to move the cursor on the screen.  
So I can't send it a message prefixed with its x/y position.  
(eg:) 2,2:"Message here"  
That should be position 2 of line 2.  
It is shown at position 1 of line 1.

(Great. I can't upload .py files.)

[lcd.txt](https://discourse.nodered.org/uploads/short-url/iUIGXa6PRI3UdTxna01wtm4eGH7.txt) (14.8 KB)

That is a `.py` file. Renamed to comply.  
That is used in the scripts.

This is an example script:

[message\_display3.txt](https://discourse.nodered.org/uploads/short-url/tGkPMDguENOtHS34Tzb1UNs60ry.txt) (380 Bytes)

(Again this is `.py` file.)

That prints the message on the display. Nothing fancy.

This is the code from the node which works, but the position isn't honoured.  
It used to be, but isn't any more.

At the start: `import pifacecad`, the file used is the `lcd.txt` file, just renamed `.py`

[pfc\_lcd.txt](https://discourse.nodered.org/uploads/short-url/lhYkAqzcEPkZpgnwGx2FNQlVJme.txt) (1.2 KB)

So, I think that's where I am at.

Yes, the `pfc_lcd.txt` (`.py`) file is a real hack.

It was taken from the original one and I just tried to make it better. (Cough)  
The commands use - according to what I read on the web - are for files. Not for what they were/are being used here.

I also tried to tidy up the X/Y position part and how it is _extracted_.

I hope that is good enough.  
And sorry for the messy code. I was just trying things based on what I read.  
There is a lot of `print` commands so when I run it from the CLI, I can see what is being seen as what.

A lot of those are `#` out because I was seeing what I expected. So it wasn't needed there after.

---

<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:** [29 July 2020 10:31 UTC](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854/20 "2020-07-29T10:31:26Z")

</div>

Fine, I'll look into this, might be ready for you tomorrow morning

[Next page](https://discourse.nodered.org/t/how-does-node-red-interact-with-python/30854.md?page=2)
