Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-red-contrib-i2c

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-red-contrib-i2c - npm Package Compare versions

Comparing version 0.1.0 to 0.5.2

245

i2c/26-i2c.js
module.exports = function(RED) {
"use strict";
var I2C = require("i2c");
var I2C = require("i2c-bus");
// The Server Definition - this opens (and closes) the connection
function I2CServerNode(n) {
// The Output Node
function I2CScanNode(n) {
RED.nodes.createNode(this, n);
this.device = n.device || "/dev/I2C-1";
this.address = n.address || 0x18;
this.port = null;
this.on("close", function() {
if (this.port != null) {
// this.port.disconnect();
}
var node = this;
node.port = I2C.openSync( 1 );
node.on("input", function(msg) {
node.port.scan(function(err, res) {
// result contains a buffer of bytes
if (err) {
node.error(errI);
} else {
node.send([{
payload: res
}, null]);
res.forEach(function(entry) {
node.send([null, {
payload: entry,
address: entry
}]);
});
}
});
});
node.on("close", function() {
node.port.closeSync();
});
}
RED.nodes.registerType("i2c-device", I2CServerNode);
RED.nodes.registerType("i2c scan", I2CScanNode);
// The Input Node
function I2CInNode(n) {
RED.nodes.createNode(this, n);
this.i2cdevice = n.i2cdevice;
this.serverConfig = RED.nodes.getNode(this.i2cdevice);
this.address = n.address;

@@ -29,14 +45,25 @@ this.command = n.command;

var node = this;
if (node.serverConfig.port === null) {
node.log("CONNECT: " + node.serverConfig.device);
node.serverConfig.port = new I2C(parseInt(this.serverConfig.address), {
device: node.serverConfig.device
});
}
node.port = node.serverConfig.port;
node.port = I2C.openSync( 1 );
node.on("input", function(msg) {
var address = node.address || msg.address || this.serverConfig.address;
var command = node.command || msg.command;
node.port.setAddress(parseInt(address));
node.port.readBytes(parseInt(command), node.count, function(err, res) {
var address = node.address || msg.address ;
var command = node.command || msg.command ;
address = parseInt(address);
command = parseInt(command);
var buffcount = parseInt(node.count);
if (isNaN(address)) {
this.status({fill:"red",shape:"ring",text:"Address ("+address+") value is missing or incorrect"});
return;
} else if (isNaN(command) ) {
this.status({fill:"red",shape:"ring",text:"Command value is missing or incorrect"});
return;
} else if ((!buffcount) || isNaN(buffcount) ) {
this.status({fill:"red",shape:"ring",text:"Read bytes value is missing or incorrect"});
return;
} else {
this.status({});
}
var buffer = new Buffer(buffcount);
node.port.readI2cBlock(address, command, buffcount, buffer, function(err, size, res) {
if (err) {

@@ -46,3 +73,3 @@ node.error(err);

var payload;
if (Buffer.isBuffer(res) && node.count == 1) {
if (node.count == 1) {
payload = res[0];

@@ -52,11 +79,9 @@ } else {

}
// msg.address = address;
// msg.command = command;
// msg.payload = payload;
node.send({
address: address,
command: command,
payload: payload
});
msg = Object.assign({}, msg);
// node.log('log returned data'+ JSON.stringify([size, res.length, res, res.toString("utf-8")]));
msg.address = address;
msg.command = command;
msg.payload = payload;
msg.size = size;
node.send(msg);
}

@@ -67,3 +92,3 @@ });

node.on("close", function() {
// node.port.free();
node.port.closeSync();
});

@@ -77,4 +102,2 @@ }

RED.nodes.createNode(this, n);
this.i2cdevice = n.i2cdevice;
this.serverConfig = RED.nodes.getNode(this.i2cdevice);
this.address = parseInt(n.address);

@@ -84,88 +107,78 @@ this.command = parseInt(n.command);

this.payload = n.payload;
this.payloadType = n.payloadType;
var node = this;
if (node.serverConfig.port === null) {
node.log("CONNECT: " + node.serverConfig.device);
node.serverConfig.port = new I2C(parseInt(this.serverConfig.address), {
device: node.serverConfig.device
});
}
node.port = node.serverConfig.port;
node.port = I2C.openSync( 1 );
node.on("input", function(msg) {
msg.address = node.address || msg.address || this.serverConfig.address;
msg.command = node.command || msg.command;
node.port.setAddress(msg.address);
var payload = node.payload || msg.payload;
if (payload == null || node.count == 0) {
node.port.writeByte(parseInt(node.command), function(err) {
if (err) node.error(err);
});
} else if (!isNaN(payload)) {
var data = payload;
payload = Buffer.allocUnsafe(node.count);
payload.writeIntLE(data, 0, node.count, true);
var myPayload;
var address = node.address;
if (isNaN(address)) address = msg.address;
var command = node.command;
if (isNaN(command)) command = msg.command;
address = parseInt(address);
command = parseInt(command);
var buffcount = parseInt(node.count);
if (isNaN(address)) {
this.status({fill:"red",shape:"ring",text:"Address ("+address+") value is missing or incorrect"});
return;
} else if (isNaN(command) ) {
this.status({fill:"red",shape:"ring",text:"Command ("+command+") value is missing or incorrect"});
return;
} else if (isNaN(buffcount) ) {
this.status({fill:"red",shape:"ring",text:"Send bytes value is missing or incorrect"});
return;
} else {
this.status({});
}
try {
if (this.payloadType == null) {
myPayload = this.payload;
} else if (this.payloadType == 'none') {
myPayload = null;
} else {
myPayload = RED.util.evaluateNodeProperty(this.payload, this.payloadType, this,msg);
}
} else if (String.isString(payload) || Array.isArray(payload)) {
payload = Buffer.from(payload);
}
if (payload.count > 32) {
node.error("To many elements in array to write to I2C");
} else {
node.port.writeBytes(parseInt(node.command), payload, function(err) {
if (err) node.error(err);
});
}
if (myPayload == null || node.count == 0) {
node.port.sendByte(address, command, function(err) {
if (err) { node.error(err, msg);
} else {
node.send(msg);
};
});
} else if (!isNaN(myPayload)) {
var data = myPayload;
myPayload = Buffer.allocUnsafe(node.count);
myPayload.writeIntLE(data, 0, node.count, true);
} else if (typeof myPayload === "string" || Array.isArray(myPayload)) {
myPayload = Buffer.from(myPayload);
}
if (myPayload.length > 32) {
node.error("Too many bytes to write to I2C");
} else {
// node.log('log write data'+ JSON.stringify([address, command, myPayload.length, myPayload, myPayload.toString("utf-8")]));
node.port.writeI2cBlock(address, command, myPayload.length, myPayload, function(err) {
if (err) {
node.error(err, msg);
} else {
node.send(msg);
};
});
}
} catch(err) {
this.error(err,msg);
}
});
node.on("close", function() {
// node.port.free();
node.port.closeSync();
});
}
RED.nodes.registerType("i2c out", I2COutNode);
// The Output Node
function I2CScanNode(n) {
RED.nodes.createNode(this, n);
this.i2cdevice = n.i2cdevice;
this.serverConfig = RED.nodes.getNode(this.i2cdevice);
var node = this;
if (node.serverConfig.port === null) {
node.log("CONNECT: " + node.serverConfig.device);
node.serverConfig.port = new I2C(parseInt(this.serverConfig.address), {
device: node.serverConfig.device
});
}
node.port = node.serverConfig.port;
node.on("input", function(msg) {
node.port.scan(function(err, res) {
// result contains a buffer of bytes
if (err) {
node.error(errI);
} else {
node.send([{
payload: res
}, null]);
res.forEach(function(entry) {
node.send([null, {
payload: entry,
address: entry
}]);
});
}
});
});
node.on("close", function() {
// node.port.free();
});
}
RED.nodes.registerType("i2c scan", I2CScanNode);
}
}
{
"name": "node-red-contrib-i2c",
"version": "0.1.0",
"description": "A Node-RED node to talk to the Raspiberry PI's I2C port",
"_from": "node-red-contrib-i2c@0.1.0",
"_id": "node-red-contrib-i2c@0.1.0",
"_inBundle": false,
"_integrity": "sha1-+7Pu3cdlcOQ+h3xrAlkQXBw0Wk8=",
"_location": "/node-red-contrib-i2c",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "node-red-contrib-i2c@0.1.0",
"name": "node-red-contrib-i2c",
"escapedName": "node-red-contrib-i2c",
"rawSpec": "0.1.0",
"saveSpec": null,
"fetchSpec": "0.1.0"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/node-red-contrib-i2c/-/node-red-contrib-i2c-0.1.0.tgz",
"_shasum": "fbb3eeddc76570e43e877c6b0259105c1c345a4f",
"_spec": "node-red-contrib-i2c@0.1.0",
"_where": "/home/pi/.node-red",
"author": {
"name": "Niels v.d. Spek",
"email": "nielsnl68@gmail.com",
"url": "https://github.com/nielsnl68/node-contrib-i2c"
},
"bundleDependencies": false,
"dependencies": {
"i2c": "^0.2.3"
"i2c-bus": "^1.2.2"
},
"main": "26-i2c.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "Apache-2.0",
"deprecated": false,
"description": "A Node-RED node to talk to the Raspiberry PI's I2C port",
"homepage": "https://github.com/nielsnl68/node-red-contrib-i2c#readme",
"keywords": [

@@ -18,6 +43,5 @@ "node-red",

],
"repository": {
"type": "git",
"url": "https://github.com/nielsnl68/node-contrib-i2c"
},
"license": "Apache-2.0",
"main": "26-i2c.js",
"name": "node-red-contrib-i2c",
"node-red": {

@@ -28,11 +52,10 @@ "nodes": {

},
"author": {
"name": "Niels v.d. Spek",
"email": "nielsnl68@gmail.com",
"url": "https://github.com/nielsnl68/node-contrib-i2c"
"repository": {
"type": "git",
"url": "git+https://github.com/nielsnl68/node-red-contrib-i2c.git"
},
"readmeFilename": "README.md",
"bugs": {
"url": ""
}
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "0.5.2"
}
# node-red-contrib-i2c
This set of node-red nodes communicate with the Raspberry Pi I2C driver and uses the node-I2C package.
Run the following command in the root directory of your Node-RED install, usually
this is ~/.node-red .
this is ~/.node-red.
npm install —unsafe-perm node-red-contrib-i2c
As of version 0.5.0 the nodes are using the i2c-bus library to communicate with your devices. It looks more stable and up-to-date. Also the need to use a config-node is removed. The config node was needed to store the common i2c object. With the new i2c-bus library is that not needed anymore.
**Warning**: After upgrading to this version you will get an error of an unknown node in the config side-bar, you can safely remove this one.
Downside my nodes work only for newer version where the i2c driver is on /dev/i2c-1
npm install --unsafe-perm node-red-contrib-i2c
Usage

@@ -27,7 +33,11 @@ -----

This node will send a given String/array/buffer to a given device. The address and command can both be set in the dialog screen or dynamicly with <b>msg.address</b> and <b>msg.command</b>.
The payload can be staticly or dynamicly (using msg.payload) set. This payload can be a Buffer, Array, String or Integer. When you use integers then the number of bytes to send is importend and can be set between 0 and 6 bytes.
The payload can be staticly or dynamicly (using msg.payload) set. This payload can be a Buffer, Array, String or Integer. When you use integers then the number of bytes to send is importend and can be set between 0 and 31 bytes.
NEW(0.5.0): you can daisychain this node, the input msg is send unchanged to the next node.
#### Inportend Note
This set of nodes is using the work of kelly's I2C package to work. And i like to thank hem for the work he did on that package.
Version v0.5.0 is now using the I2C-bus package from fivdi. It looks more robust and better for asyncrone processes like node-red. I would like the maker as much thanks for his work as Kelly in the past version. You can vind his work on github: https://github.com/fivdi/i2c-bus
old version did use the work of kelly's I2C package to work. And i like to thank hem for the work he did on that package.
For more info check out his github account at: https://github.com/kelly/node-i2c

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc