Calling C code from within node-red

Hello everybody,

I'm new to javascript. I am interested in running C code inside of node-red. I have read it is possible to run C code in Javascript code using C++ addons, is this possible to do in Node-RED? if not, is there a way to do so?
Thanks for the help

C is not C++, however C & C++ code both need to be compiled to be able to run.
Can you elaborate why you would want this ? Is it just because you are new to javascript ?

I am using C code of an SDK to a device I am using which is Analog Discovery 2 to read measurements from it and then transferring them into Node-RED and do some signal analysis and signal depicting using charts. I am using to transfer the samples Mosquitto MQTT service but I am interested in running the C code directly in Node-RED function to be able to acess the samples without the need to use an MQTT service and thereby reducing the computational load on my PC. I hope I am able to do this inside of Node-RED.

It is possible to run C/C++ by wrapping it in a node.js module. But that is well outside the scope of this forum. However, you can look up how to do it. Not simple though probably.

As for MQTT, assuming that you use Mosquitto as your MQTT broker, its performance overhead on your PC will be minimal. Generally a lot less than running a Microsoft Office app such as Outlook for example :grinning:

1 Like

There is also Aedes, an MQTT broker that runs within Node-red. It's limited compared to mosquitto but it works, and you can be sure it's running when Node-red is.

1 Like

It also uses a LOT more resources than Mosquitto.


Wow, didn't know that. Good thing I switched to Mosquitto! :mosquito:

1 Like

Alternatively, you may look into "exec" node. The "exec" node is pretty efficient.
You can call an C executable with stdin parameters.
The node returns with C stdout parameters.

1 Like

Thank you for this tip, this could save a lot of time.

I tried it, it worked for the first couple of times but then I get code "command failed, code: 3221226356"
I am able to run other programs, but the one I'm interested in I get this error, anyone knows what does this mean?

A debug node on the second output from the exec node (stderr) might give a clue what's going wrong.

1 Like

I have the node on the second output, but I don't get any output on it....means I can't see what's the issue, only the reason node is saying code number

Did you set the debug node to display the 'complete msg object' ?

1 Like

It worked out, I didn't know that the exec node waits until the program is completely finished to show the message payload on the debug node, I had a long for loop which was taking so much time that I thought it is not working, now I reduced the number of cycles and it worked fine, thank you guys.

One question regarding this matter, now I get 1 complete message which has a lot of samples (corresponding to the number of times the program has looped) , now I am interested in showing the results on a chart, but I have only one big message with all these individual samples, is there a way to plot this on a chart or maybe convert this big string arrab into individual values to be able plot it on a chart? thanks as always!

You may want to try the node in spawn mode - that may send an output per line/loop (but may depend on if your app does flush the print buffer every time or not - (If you also have source for that you can force it to flush on each loop)..

Failing that it depends what the string looks like - if just values then the split node set with whatever character separates the values may work. But if you post a sample I'm sure someone can help.

@Hishamjz That's great. You may parse the big message for chart display per @dceejay . This is the beauty of the dashboard that can show charts quickly. There are many posts about how to parse a big message in this forum.

You can use WaveForms program

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.