dear all
i'm pondering what the right way is to create a node that listens (ideally async) for output from a python script that never ends (it is monitoring GPIO interrupts)? the code, as it stands, is below. This is the first time trying to build a node for node-red and am doing so as I cannot find a pre-rolled node for EC11 rotary encoders.
separately I am getting an error when I try to drop the node on a flow. the error is below. Any help on this would be great. It looks like an html issue but the html is just the below; so not much scope for errors.
There is also a Daemon node, very similar to the exec node, which you could use to run and get print() output from your python script.
You could use the built in exec mode itself but you would have to run the script yourself, perhaps as a systemd service.
The Daemon node is designed to manage permanently running services from Node-red.
With either of these options you have to call python -u so that it's output is unbuffered, ie immediately visible to Node-red
@Colin's suggestion of mqtt is probably more elegant but takes a bit of python coding to set up.
thank you both.
The daemon node seems to be close to what I am looking for, if not exact.
as to how quickly it needs to react: the point is to have a knob controlling speed of rotation for a pump motor. the movement clock/anticlock-wise should result in a smooth and responsive change in speed. I'd suspect that any lag may reduce the user experience but I have not yet tested it.
and I think I must pause in further testing as my python skills are not up to scratch. What is working perfectly in python (i.e. from the command line) is not working from within the daemon node.
the error I get, for info, is
daemon:Rotary Encoder] error: Error: spawn python3 -u ./rotaryencoder/rotaryEncoder.py ENOENT
gpio: GPIO Sysfs Interface for Userspace is deprecated (https://www.kernel.org/doc/Documentation/gpio/sysfs.txt).
Function is now useless and empty.
wiringPiISR: unable to open /sys/class/gpio/gpio20/value: No such file or directory
as I said, it's working fine from the command line so I'm not sure what's up. and in the python script I'm using RPi.gpio which should still be ok on a RPi 4.
ENOENT means that it cannot find the file. The reason is probably that ./ is not what you think it is from inside node-red. Always specify the full path to files for such cases.
the same invocation works ok on the command line. python and python3 are both in /usr/bin and there is no doubt that the script is where indicated, with -rwxr-xr-x perms
I still think the best way would be to amend the python script to publish to MQTT (which I believe is easy, I am sure there are examples of how to do it available) and pick that up in node red. MQTT is very efficient and will handle hundreds of messages/second if necessary. Use the Mosquitto broker.
in the node-red dashboard I used the pythonshell node (node-red-contrib. the flow looks like this. The switch is to differentiate between the btn and the encoder inputs. the function is to cast to an int as the python script currently is treated by the node as a string and a line break is appended.
this behaviour can be altered in the src file at line 85.