How does Node-Red interact with Python?

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.

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).

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.

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

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?

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

Craig

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.

Have a look at this for example

https://readthedocs.org/projects/rplcd/downloads/pdf/latest/

Craig

1 Like

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

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! :wink:

I'll keep reading the PDF.

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

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

Craig

1 Like

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.

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

Yeah. Thanks.

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

Because without that, I am flying blind.

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

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

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.

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

(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 (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 (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 (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.

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

1 Like