Help with creating a new node with c++

Hi everyone,
Still being quite new ( with node-red, node.js and javascript in general) so understanding code online is not easy; I was therefore hoping to find some help over her.

I wrote some code in c++, that I managed to have it called through node.js

All good so far, but I am not really sure how to proceed next..

I do have my binding.gyp file ( where all my functions and header files are called), main.js ( and all the ones created when building my program ), ending up with a main.js file that looks like this :

const calculate = require('./build/Release/sensor')
console.time('c++')
console.log(sensor.calc());
console.timeEnd('c++');

with my C++ program returning a floating number( I d like to update it to return an array since my sensor returns both temperature and humidity );

My question now is, how could I take these 4 lines of code and embed them in node-red?

Thanks in advance

Is it that you are more familiar with C++ so avoiding learning how to do this stuff natively in node-red?

TBH, unless you are doing some heavy computation or something not possible in nodejs then I would suggest you are missing out on the flexibility / dynamic programmable runtime aspect of node-red.

Well, there is a way of requiring a a non node-red js module (see here) then you can use a function node to do the require and call your code.

However, there are other options. for example...

  • if you built MQTT into your C++ application, you can simply use MQTT for the transfer of data. And if you send your data out in JSON format, it is a really simple task of accessing your property values without resorting to string splitting etc.
  • (or) you could write a custom contrib node-red node so all users could benefit from your works & install it from the palette

As this is a JS environment, you would be far better off returning a JSON string & using the JSON node to convert that back into an object with nice property names were you could simple access msg.payload.temperature and msg.payload.humidity


EDIT:

My apologies, I see you are actually trying to create a node. (should read full message before posting).

So to answer your question "with my C++ program returning a floating number( I d like to update it to return an array since my sensor returns both temperature and humidity );" - JSON is the way to go. Then in your custom node, simple do something like msg.payload = JSON.parse(sensor.calc()) to convert the JSON sent from your C++ to a native object.

EDIT2:

is there an absolute need to use C++ - is it not possible to use nodejs under the hood?

hi steve, thnx for the reply,
Yes, C++ is by far the language I'm the most familiar with, especially for the I2C readings in Linux part ( want to run my code on a BBB ), and although I'm trying to learn how to do it natively, I d feel bad having to start all over again, and will therefore try to do that as a next step once I have everything working :slight_smile:

Once everything is working, I'll gladly share my nodes, together with my findings because I don't think(at least I hope) that I 'm the only one struggling with it.

Perhaps, for starters, you could try out your bindings using the function node + loading-additional-modules that i linked to earlier

Once you have things working, you can develop a custom node (see here for info)

One thing I find helps with development of contrib nodes is to take a look at existing nodes & view its source code (lots of contrib nodes in github)

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