mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
parent
0cb8e2594d
commit
41acbf5af7
@ -10,26 +10,35 @@ module.exports = function(RED) {
|
|||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.device = n.device || null;
|
this.device = n.device || null;
|
||||||
var node = this;
|
var node = this;
|
||||||
|
var running = false;
|
||||||
|
|
||||||
|
var startup = function() {
|
||||||
node.board = new Board(node.device, function(e) {
|
node.board = new Board(node.device, function(e) {
|
||||||
//console.log("ERR",e);
|
|
||||||
if ((e !== undefined) && (e.toString().indexOf("cannot open") !== -1) ) {
|
if ((e !== undefined) && (e.toString().indexOf("cannot open") !== -1) ) {
|
||||||
node.error(RED._("arduino.errors.portnotfound",{device:node.device}));
|
node.error(RED._("arduino.errors.portnotfound",{device:node.device}));
|
||||||
}
|
}
|
||||||
else if (e === undefined) {
|
else if (e === undefined) {
|
||||||
node.board.on('ready', function() {
|
running = true;
|
||||||
|
node.board.once('ready', function() {
|
||||||
node.log(RED._("arduino.status.connected",{device:node.board.sp.path}));
|
node.log(RED._("arduino.status.connected",{device:node.board.sp.path}));
|
||||||
if (RED.settings.verbose) {
|
if (RED.settings.verbose) {
|
||||||
node.log(RED._("arduino.status.version",{version:node.board.firmware.name+"-"+node.board.version.major+"."+node.board.version.minor}));
|
node.log(RED._("arduino.status.version",{version:node.board.firmware.name+"-"+node.board.version.major+"."+node.board.version.minor}));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
node.board.once('close', function() {
|
||||||
node.board.on('close', function() {
|
|
||||||
node.error(RED._("arduino.status.portclosed"));
|
node.error(RED._("arduino.status.portclosed"));
|
||||||
});
|
});
|
||||||
|
node.board.once('disconnect', function() {
|
||||||
|
if (running) { setTimeout(function() { running = false; startup(); }, 5000); }
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setTimeout(function() { if (!running) { startup(); } }, 5000);
|
||||||
|
};
|
||||||
|
startup();
|
||||||
|
|
||||||
node.on('close', function(done) {
|
node.on('close', function(done) {
|
||||||
|
running = false;
|
||||||
if (node.board) {
|
if (node.board) {
|
||||||
try {
|
try {
|
||||||
node.board.transport.close(function() {
|
node.board.transport.close(function() {
|
||||||
@ -53,12 +62,15 @@ module.exports = function(RED) {
|
|||||||
this.state = n.state;
|
this.state = n.state;
|
||||||
this.arduino = n.arduino;
|
this.arduino = n.arduino;
|
||||||
this.serverConfig = RED.nodes.getNode(this.arduino);
|
this.serverConfig = RED.nodes.getNode(this.arduino);
|
||||||
|
this.running = false;
|
||||||
if (typeof this.serverConfig === "object") {
|
if (typeof this.serverConfig === "object") {
|
||||||
|
var startup = function() {
|
||||||
this.board = this.serverConfig.board;
|
this.board = this.serverConfig.board;
|
||||||
var node = this;
|
var node = this;
|
||||||
node.oldval = "";
|
node.oldval = "";
|
||||||
node.status({fill:"red",shape:"ring",text:"node-red:common.status.connecting"});
|
node.status({fill:"grey",shape:"ring",text:"node-red:common.status.connecting"});
|
||||||
var doit = function() {
|
var doit = function() {
|
||||||
|
node.running = true;
|
||||||
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
||||||
if (node.state === "ANALOG") {
|
if (node.state === "ANALOG") {
|
||||||
node.board.pinMode(node.pin, 0x02);
|
node.board.pinMode(node.pin, 0x02);
|
||||||
@ -95,17 +107,23 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// node.board.on('close', function() {
|
node.board.once('disconnect', function() {
|
||||||
// node.board.removeAllListeners();
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"});
|
||||||
// node.status({fill:"grey",shape:"ring",text:"node-red:common.status.not-connected"});
|
if (node.running) { setTimeout(function() { node.running = false; startup(); }, 5500); }
|
||||||
// });
|
});
|
||||||
}
|
}
|
||||||
if (node.board.isReady) { doit(); }
|
if (node.board.isReady) { doit(); }
|
||||||
else { node.board.on("ready", function() { doit(); }); }
|
else { node.board.once("ready", function() { doit(); }); }
|
||||||
|
setTimeout(function() { if (!node.running) { startup(); } }, 4500);
|
||||||
|
}
|
||||||
|
startup();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.warn(RED._("arduino.errors.portnotconf"));
|
this.warn(RED._("arduino.errors.portnotconf"));
|
||||||
}
|
}
|
||||||
|
node.on('close', function() {
|
||||||
|
node.running = false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("arduino in",DuinoNodeIn);
|
RED.nodes.registerType("arduino in",DuinoNodeIn);
|
||||||
|
|
||||||
@ -118,13 +136,17 @@ module.exports = function(RED) {
|
|||||||
this.state = n.state;
|
this.state = n.state;
|
||||||
this.arduino = n.arduino;
|
this.arduino = n.arduino;
|
||||||
this.serverConfig = RED.nodes.getNode(this.arduino);
|
this.serverConfig = RED.nodes.getNode(this.arduino);
|
||||||
if (typeof this.serverConfig === "object") {
|
this.running = false;
|
||||||
this.board = this.serverConfig.board;
|
|
||||||
var node = this;
|
var node = this;
|
||||||
node.status({fill:"red",shape:"ring",text:"node-red:common.status.connecting"});
|
if (typeof node.serverConfig === "object") {
|
||||||
|
var startup = function() {
|
||||||
|
node.board = node.serverConfig.board;
|
||||||
|
node.status({fill:"grey",shape:"ring",text:"node-red:common.status.connecting"});
|
||||||
var doit = function() {
|
var doit = function() {
|
||||||
|
node.running = true;
|
||||||
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
||||||
node.on("input", function(msg) {
|
node.on("input", function(msg) {
|
||||||
|
if (node.board.isReady) {
|
||||||
if (node.state === "OUTPUT") {
|
if (node.state === "OUTPUT") {
|
||||||
node.board.pinMode(node.pin, 0x01);
|
node.board.pinMode(node.pin, 0x01);
|
||||||
if ((msg.payload === true)||(msg.payload.toString() == "1")||(msg.payload.toString().toLowerCase() == "on")) {
|
if ((msg.payload === true)||(msg.payload.toString() == "1")||(msg.payload.toString().toLowerCase() == "on")) {
|
||||||
@ -154,17 +176,25 @@ module.exports = function(RED) {
|
|||||||
if (node.state === "STRING") {
|
if (node.state === "STRING") {
|
||||||
node.board.sendString(msg.payload.toString());
|
node.board.sendString(msg.payload.toString());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
node.board.once('disconnect', function() {
|
||||||
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"});
|
||||||
|
if (node.running) { setTimeout(function() { node.running = false; startup(); }, 5500); }
|
||||||
});
|
});
|
||||||
// node.board.on('close', function() {
|
|
||||||
// node.status({fill:"grey",shape:"ring",text:"node-red:common.status.not-connected"});
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
if (node.board.isReady) { doit(); }
|
if (node.board.isReady) { doit(); }
|
||||||
else { node.board.on("ready", function() { doit(); }); }
|
else { node.board.once("ready", function() { doit(); }); }
|
||||||
|
setTimeout(function() { if (!node.running) { startup(); } }, 4500);
|
||||||
|
}
|
||||||
|
startup();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.warn(RED._("arduino.errors.portnotconf"));
|
node.warn(RED._("arduino.errors.portnotconf"));
|
||||||
}
|
}
|
||||||
|
node.on('close', function() {
|
||||||
|
node.running = false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("arduino out",DuinoNodeOut);
|
RED.nodes.registerType("arduino out",DuinoNodeOut);
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name" : "node-red-node-arduino",
|
"name" : "node-red-node-arduino",
|
||||||
"version" : "0.1.0",
|
"version" : "0.2.0",
|
||||||
"description" : "A Node-RED node to talk to an Arduino running firmata",
|
"description" : "A Node-RED node to talk to an Arduino running firmata",
|
||||||
"dependencies" : {
|
"dependencies" : {
|
||||||
"firmata" : "^2.0.0"
|
"firmata" : "^2.0.0"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user