2 * Copyright 2013,2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 module.exports = function(RED) {
19 var util = require("util");
20 var ArduinoFirmata = require('arduino-firmata');
21 var fs = require('fs');
22 var plat = require('os').platform();
23 var portlist = ArduinoFirmata.list(function (err, ports) {
27 // The Board Definition - this opens (and closes) the connection
28 function ArduinoNode(n) {
29 RED.nodes.createNode(this,n);
30 this.device = n.device || null;
31 this.repeat = n.repeat||25;
32 //node.log("opening connection "+this.device);
34 node.board = new ArduinoFirmata();
35 if (portlist.indexOf(node.device) === -1) {
36 node.warn("Device "+node.device+" not found");
39 node.board.connect(node.device);
42 node.board.on('boardReady', function(){
43 node.log("version "+node.board.boardVersion);
46 node.on('close', function() {
49 node.board.close(function() {
50 node.log("port closed");
56 RED.nodes.registerType("arduino-board",ArduinoNode);
60 function DuinoNodeIn(n) {
61 RED.nodes.createNode(this,n);
62 this.buttonState = -1;
65 this.arduino = n.arduino;
66 this.serverConfig = RED.nodes.getNode(this.arduino);
67 if (typeof this.serverConfig === "object") {
68 this.board = this.serverConfig.board;
69 //this.repeat = this.serverConfig.repeat;
71 node.status({fill:"red",shape:"ring",text:"connecting"});
73 node.board.on('connect', function() {
74 node.status({fill:"green",shape:"dot",text:"connected"});
75 //console.log("i",node.state,node.pin);
76 if (node.state == "ANALOG") {
77 node.board.on('analogChange', function(e) {
78 if (e.pin == node.pin) {
79 var msg = {payload:e.value, topic:"A"+e.pin};
86 node.board.pinMode(node.pin, ArduinoFirmata.INPUT);
87 node.board.on('digitalChange', function(e) {
88 if (e.pin == node.pin) {
89 var msg = {payload:e.value, topic:e.pin};
97 util.log("[Firmata-arduino] port not configured");
100 RED.nodes.registerType("arduino in",DuinoNodeIn);
104 function DuinoNodeOut(n) {
105 RED.nodes.createNode(this,n);
106 this.buttonState = -1;
108 this.state = n.state;
109 this.arduino = n.arduino;
110 this.serverConfig = RED.nodes.getNode(this.arduino);
111 if (typeof this.serverConfig === "object") {
112 this.board = this.serverConfig.board;
114 node.status({fill:"red",shape:"ring",text:"connecting"});
116 node.board.on('connect', function() {
117 node.status({fill:"green",shape:"dot",text:"connected"});
118 //console.log("o",node.state,node.pin);
119 node.board.pinMode(node.pin, node.state);
120 node.on("input", function(msg) {
121 if (node.state == "OUTPUT") {
122 if ((msg.payload == true)||(msg.payload == 1)||(msg.payload.toString().toLowerCase() == "on")) {
123 node.board.digitalWrite(node.pin, true);
125 if ((msg.payload == false)||(msg.payload == 0)||(msg.payload.toString().toLowerCase() == "off")) {
126 node.board.digitalWrite(node.pin, false);
129 if (node.state == "PWM") {
130 msg.payload = msg.payload * 1;
131 if ((msg.payload >= 0) && (msg.payload <= 255)) {
132 //console.log(msg.payload, node.pin);
133 node.board.servoWrite(node.pin, msg.payload);
136 if (node.state == "SERVO") {
137 msg.payload = msg.payload * 1;
138 if ((msg.payload >= 0) && (msg.payload <= 180)) {
139 //console.log(msg.payload, node.pin);
140 node.board.servoWrite(node.pin, msg.payload);
147 util.log("[Firmata-arduino] port not configured");
150 RED.nodes.registerType("arduino out",DuinoNodeOut);
152 RED.httpAdmin.get("/arduinoports",function(req,res) {
153 ArduinoFirmata.list(function (err, ports) {
154 //console.log(JSON.stringify(ports));
155 res.writeHead(200, {'Content-Type': 'text/plain'});
156 res.write(JSON.stringify(ports));