I'm familiar with C programming, and some experience with Python.
But new to Node-Red, can Python and C code run in Node-Red?
Have moved the thread to General - as I don't believe this relates to developing Nodes for Node RED.
Not directly - Node RED is Javascript full stop.
But that is not to say you can't use the exec
Node - to execute an executable written in such languages.
such as
node-red-contrib-py3run (node) - Node-RED (nodered.org)
Yes. There are nodes that will run Python scripts or you can use an exec node to run it externally.
For C, it is more complex. Node.js, which is what Node-RED is built on, allows C (or at least C++) runtime integration in modules. A number of existing Node-RED nodes use Node.js modules that include this feature including the serial nodes and SQLite nodes. Of course, a C native app could also be run via exec.
For custom written Nodes, indeed, But for the End User, I don't think its possible to just start using a .Node binary - unless I'm missing some magic
EDIT
That will be an idea for a new Node however, to consume native modules without depending on the scaffolding
But the question wasn't necessarily restricted to only using existing nodes.
If the compiled C/C++ binary is already wrapped into a Node.js packaged module then it might be quite simple to use in Node-RED even without a custom node. It might even be relatively simply to create such a wrapper package - I've never actually done it - but it probably relies on having access to the C/C++ source code and it being able to compile via standard compilers and make tools.
I think that is what I'm trying to articulate - I'm great at the English language you know
python, C++, a c library can't be run inside a flow natively, i.e inside the function
node for example, it will require either.
- A Module written exclusively to load the binary (at least I cant find anything for Node RED to lazy load Native Bindings)
- Using the exec node
But then, using a module that loads a Native binding exposed in the function
node is one way, which I think is what you touched on.
It is simple to run an external executable with either the exec or daemon nodes. It doesn't matter if it's compiled C, python or other language.
It is also, just about, possible to run a python script which is stored within Node-red, ie no external script file.
[{"id":"3cce5247d5657414","type":"group","z":"db04fd79f3a93d19","name":"Executing a python script ","style":{"label":true},"nodes":["23a656330a015255","0231db705087ba50","d943c3197090c7a9","b3e6f7810bb068bb","1f69acd98bdddd94","33b0bca1f9569ef6"],"x":14,"y":559,"w":872,"h":122},{"id":"23a656330a015255","type":"inject","z":"db04fd79f3a93d19","g":"3cce5247d5657414","name":"You can pass parameters in msg.payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4.2","payloadType":"num","x":220,"y":600,"wires":[["1f69acd98bdddd94"]]},{"id":"0231db705087ba50","type":"template","z":"db04fd79f3a93d19","g":"3cce5247d5657414","name":"Python script","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"python -c ' # -c argument - accept command inline. Must enclose whole script in single quotes.\nimport sys # Process argument list\nnum1 = float(sys.argv[1])\nstr = sys.argv[2] # Note python/bash style comments\n\nprint (str)\nif(num1 <= 10):\n print (num1, \"is less than 10\")\nelse :\n print (num1, \"is greater than 10\")\n\n' {{payload}} {{flow.printme}} # Closing single quote followed by list of arguments\n","output":"str","x":350,"y":640,"wires":[["d943c3197090c7a9","b3e6f7810bb068bb"]]},{"id":"d943c3197090c7a9","type":"exec","z":"db04fd79f3a93d19","g":"3cce5247d5657414","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":650,"y":620,"wires":[["33b0bca1f9569ef6"],["33b0bca1f9569ef6"],[]]},{"id":"b3e6f7810bb068bb","type":"debug","z":"db04fd79f3a93d19","g":"3cce5247d5657414","name":"Full Script","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":500,"y":600,"wires":[]},{"id":"1f69acd98bdddd94","type":"change","z":"db04fd79f3a93d19","g":"3cce5247d5657414","name":"or in context variables","rules":[{"t":"set","p":"printme","pt":"flow","to":"This\\ script\\ defined\\ in\\ Node-red.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":640,"wires":[["0231db705087ba50"]]},{"id":"33b0bca1f9569ef6","type":"debug","z":"db04fd79f3a93d19","g":"3cce5247d5657414","name":"output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":620,"wires":[]}]
There are unfortunately limitations that I so far can't resolve.
- Spaces in data passed to the script have to be escaped.
- Since the script itself has to be enclosed in single quotes, you can't use them in the script.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.