I know there is possible to use other node.js modules however and after one year playing with node red is for me still not clear how to use this.
Will be great if there is some more easy way to implement this integration or at least some tutorial that makes easier for people like me to understand and make use of it.
In my opinion this will speed up much more the use of node red for many more ppl.
Can you expand a bit more what you mean here. I’m not clear on what you’re suggesting. You mention ‘this integration’ - what integration are you referring to?
Is a global idea not speaking about a specific one.
Just as example time ago I was trying to understand how to use node.js modules already created to use some I2C devices or to use a Ultrasonic Sensor HC-SR04, today I have seen a partner checking to use one developed to use a gopro, I think there will be plenty of examples, I also know this is possible to be handled but for me is not clear at all how to manage this and at the end simply I scrapped the idea.
I guess that probably having some easy way to manage this or maybe some clear tutorial will bring light on this topic can push a lot of people to get interested into testing or using node-red.
Is just a personal feedback, maybe helps, maybe is a bit silly … I don’t know.
Yes, exactly, for me this is not clear and having this on my hands I know I can make a huge increase on the capabilities, is clear that maybe not all will work but at least to know how to try it.
The main route to using any node module in Node-RED is for someone to wrap the module in a node-red node. That requires some time and effort to do right.
You do it by loading the module in your settings.js file, and adding it to the functionGlobalContext setting. The Function node can then access the module via global context. The docs have an example of doing that with the built-in os module.
One other “generic” way to use an npm module is through the node-red-contrib-npm node – you configure it to pull in a specific library, then call write a bit of code to call its functions.
Here is an example of how I am using the suncalc module to get today’s sunrise/sunset timestamps. Select Module Style: "Custom" and enter this code into the Module Invocation editor:
var dts = +msg.timestamp || Date.now();
var lat = +msg.latitude || 35.75;
var lon = +msg.longitude || -80.80;
var dtm = new Date(dts);
// NPM module exposed as variable, npm_module
var times = npm_module.getTimes(dtm, lat, lon);
var topic = msg.topic || "*";
if (topic === "*") {
return times;
}
else if (times.hasOwnProperty(topic)) {
return times[topic];
}
else {
return {};
}
It’s not a super efficient way to use this module, since it has to be loaded from the internet every time node-red starts, but it was pretty simple to set up. YMMV - Just be aware that not every module will work with this method.
//-----------------------------------------------------------------------------------------------------------
const { Controller } = require("ethernet-ip"); // This line is not necessary because Node-Red can now load external libraries from the node configuration.
//-----------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------
const PLC = new Controller(); // This line of code is showing error in Node-Red
//-----------------------------------------------------------------------------------------------------------
// Controller.connect(IP_ADDR[, SLOT])
// NOTE: SLOT = 0 (default) - 0 if CompactLogix
PLC.connect("192.168.1.1", 0).then(() => {
console.log(PLC.properties);
});