Running Flows outside Development Environment

Hi. I am brand new to NR and running on a Raspberry Pi. All of the tutorials I have read or watched show running flows within the development environment, usually by sending output to a Debug node. Surly the flows can be run outside this development environment but I don't see how. For example, I have a flow that turns an led on a gpio pin on and off. How can I run this either from a web browser or command line? Thanks for helping.

You can trigger a flow in Node-RED by using any input node. So it is possible to run your flow in a number of ways other than manually clicking on an inject node. It can be an HTTP request that arrives in your Node-RED instance, at a regular interval, at specific time of the day, after some TCP or UDP packet arrives, when an MQTT message arrives from the broker (to a topic that your input node subscribed of course), etc...

When you are editing your flows, that is editing them in the Node-RED runtime. When you close your browser tab, the flows continue running in the runtime - there is no separate environment.

As @Andrei has described, there's a broad range of Input nodes to pick from to trigger your flows.

Thanks for the quick response Andrei. I’m afraid I’m still confused about the run time environment. Are all the flows that have been deployed to the server always waiting for a trigger? Do I just build a web page that for example has buttons and send a message to a url? What would be the url for a particular flow? How do I name the flows in Node-RED? Sorry, I know these are novice questions but I want to make sure I will be able to do what I need before spending a lot of time developing flows. Maybe you can point me to a few examples where flows are used outside the development tool.

Thanks,

Ken Kayser

Tobaccoville, NC

kxkayser@gmail.com

Hi Ken, let me use the flow you mentioned in the OP to guide this reply. So, you have built a flow that turn on / off a led on your Rasberry PI. Now say you want to trigger this flow from the internet. There are several ways to do so but the easiest one is probably adding an HTTP in node to your flow (which demands adding also an HTTP out).

This is how your flow will look like:

When you enter in any browser the URL for your flow the HTTP request will arrive to the configured endpoint and will trigger the flow.

In this example, I configured the endpoint http:127.0.0.1/myPI. You would use here the public address of your PI.

r-02

This is what you will see in the debug panel, confirming the flow was triggered:
r-03

Flow:

[{"id":"43f4472f.375598","type":"tab","label":"t1","disabled":false,"info":""},{"id":"a61e3b1b.e26698","type":"function","z":"43f4472f.375598","name":"Toggle LED","func":"msg.payload = \"Toggled LED status\";\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":140,"wires":[["2fae1ed.e9f34e2","3054f2f7.8b547e"]]},{"id":"bf425e17.d1b37","type":"http in","z":"43f4472f.375598","name":"/mypi","url":"/mypi","method":"get","upload":false,"swaggerDoc":"","x":250,"y":140,"wires":[["a61e3b1b.e26698"]]},{"id":"2fae1ed.e9f34e2","type":"debug","z":"43f4472f.375598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":140,"wires":[]},{"id":"3054f2f7.8b547e","type":"http response","z":"43f4472f.375598","name":"","statusCode":"","headers":{},"x":620,"y":80,"wires":[]}]

You can use many other ways to trigger your flow. It is important to note that if you connect your PI to the internet it is advisable to take precautiions to make this safer. There are plenty of posts on hardening your PI for the internet.

Hi Andrei. You provided an excellent explanation and example. Thanks you very much. So in your model flow, I take it there are 2 http requests: one input and one output. How do I associate my flow with the URL for this particular flow?

Best regards,

Ken Kayser

Tobaccoville, NC

kxkayser@gmail.com

You have to configure the http in node with the URL field. This will create the so called endpoint that will expose the service to the clients of your server (your PI running Node-RED is a full fledged server).

r-04

You can create as many endpoints you wish for your use case. For instance, I changed the flow to add an endpoint to provide the clients with my computername using another endpoint (/mycomp).

Flow:

[{"id":"8f8f3bab.d42ad8","type":"tab","label":"http - toggle LED","disabled":false,"info":""},{"id":"e88d3ba2.7601f8","type":"function","z":"8f8f3bab.d42ad8","name":"Toggle LED","func":"msg.payload = \"Toggled LED status\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":180,"wires":[["ff98df07.ef1df","1d7bf31e.07b00d"]]},{"id":"f4967954.77d768","type":"http in","z":"8f8f3bab.d42ad8","name":"/mypi","url":"/mypi","method":"get","upload":false,"swaggerDoc":"","x":190,"y":180,"wires":[["e88d3ba2.7601f8"]]},{"id":"88f9c32a.3c261","type":"debug","z":"8f8f3bab.d42ad8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":570,"y":240,"wires":[]},{"id":"ff98df07.ef1df","type":"http response","z":"8f8f3bab.d42ad8","name":"","statusCode":"","headers":{},"x":570,"y":180,"wires":[]},{"id":"a8c4353.8c875c8","type":"http in","z":"8f8f3bab.d42ad8","name":"/mycomp","url":"/mycomp","method":"get","upload":false,"swaggerDoc":"","x":200,"y":240,"wires":[["598d78de.47d738"]]},{"id":"598d78de.47d738","type":"change","z":"8f8f3bab.d42ad8","name":"Env computername","rules":[{"t":"set","p":"payload","pt":"msg","to":"COMPUTERNAME","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":240,"wires":[["ff98df07.ef1df","88f9c32a.3c261"]]},{"id":"1d7bf31e.07b00d","type":"debug","z":"8f8f3bab.d42ad8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":570,"y":140,"wires":[]}]

Your illustration has really cleared things up for me. Thanks again. I wasn’t thinking in an iot mode, more in a ui mode. Speaking of which, if you use the Dashboard nodes, where is the display viewed?

Regards,

Ken Kayser

The dashboard is another web application, so can be viewed on any browser on the same network as the Node-RED device.

Hi Andrei,

You were so helpful in the past that I thought would try and impose on you again. I have tried the Forum but haven’t had any luck. I’ve been doing all sorts of things in NR and really love it.

I’m trying to run a Python program on the same Raspberry Pi as NR is running on.  I can successfully do this with a simple test program using the Pythonshell node.  However, when I try to run a program using some site libraries, it can’t find the imported modules.  In particular I am trying to use some of the Adafruit CircuitPython modules.  My program runs fine from the command line but when launching from node-red, I get errors such as “in <module>   import board ImportError: module named board”  where “board” is a module from CircuitPython.  I have the normal shebang statement in my program (#!/usr/bin/env python3) and as I say, the program runs normally from the command line.  Any ideas?

Thanks.

Ken Kayser

Tobaccoville, NC

kxkayser@gmail.com

Hello Ken,

I have absolutely no expeience on running python. I leave the question open as surely there are many in this forum that have the knowledge to advise you.

I see that you are using node node-red-contrib-pythonshell. Did you try some alternative like the exec node (possibly on spawn mode) or node-red-node-daemon ?

Hi Andrew. Thanks for the reply. Yes, I was able to successfully launch my program with the exec node. Just wondering why the Pythonshell node didn’t work. NR is incredibly powerful.

Regards,

Ken Kayser

If the python shell didn’t work it’s worth opening an issue on the nodes github page so either the author can explain what you need to do or can hopefully fix the issue.

1 Like

Good idea, I’ll do it

Ken Kayser

I believe it is because the command it uses to run python does not work on your system. What command do you use to run python via an exec node?

Another approach would be to disaggregate your Node-RED and Python processes. You could easily do that by wrapping your python code in a small server and making it listen either using an http API or via MQTT (or indeed both).

Here is a similar example of disaggregation:

https://ianhinsdale.com/post/communicating-between-nodejs-and-python/

Weirdly, despite similar naming, node-red-contrib-pythonshell does not seem to use python-shell which perhaps would have been more sensible.