mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
parent
9f290f651e
commit
ecb545cf7e
@ -2,7 +2,7 @@ node-red-node-pi-mcp3008
|
||||
========================
|
||||
|
||||
A <a href="http://nodered.org" target="_new">Node-RED</a> node to read from
|
||||
the MCP3008 Analogue to Digital Converter,
|
||||
the MCP3xxx Series of Analogue to Digital Converters,
|
||||
such as the <a href="http://rasp.io/analogzero" target="_new">Rasp.io analogzero</a>, though it will work with breadboard versions also.
|
||||
|
||||
It will appear in the menu as ` A/D Converter `.
|
||||
@ -28,9 +28,9 @@ Run the following command in your Node-RED user directory - typically `~/.node-r
|
||||
|
||||
**Note**: It will appear in the menu as ` A/D Converter `.
|
||||
|
||||
### Usage
|
||||
### Usage
|
||||
|
||||
Reads from an MCP3008 Analogue to Digital (ADC) chip on the Pi SPI connection.
|
||||
Reads from an MCP3xxx Analogue to Digital (ADC) chip on the Pi SPI connection.
|
||||
|
||||
You can either set a channel in the edit dialogue, or you can set the `msg.payload` to
|
||||
select the channel dynamically. If so then the payload must be a value from 0 to 7.
|
||||
|
@ -1,16 +1,16 @@
|
||||
{
|
||||
"name" : "node-red-node-pi-mcp3008",
|
||||
"version" : "0.0.8",
|
||||
"version" : "0.1.0",
|
||||
"description" : "A Node-RED node to read from the MCP3008 Analogue to Digital Converter",
|
||||
"dependencies" : {
|
||||
"mcp-spi-adc": "^0.3.1"
|
||||
"mcp-spi-adc": "^1.0.0"
|
||||
},
|
||||
"repository" : {
|
||||
"type":"git",
|
||||
"url":"https://github.com/node-red/node-red-nodes/tree/master/hardware"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"keywords": [ "node-red", "pi", "adc", "mcp3008", "a/d converter" ],
|
||||
"keywords": [ "node-red", "pi", "adc", "mcp", "a/d converter" ],
|
||||
"node-red" : {
|
||||
"nodes" : {
|
||||
"pimcp3008": "pimcp3008.js"
|
||||
|
@ -1,7 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<script type="text/x-red" data-template-name="pimcp3008">
|
||||
<div class="form-row">
|
||||
<label for="node-input-mode"><i class="fa fa-map-pin"></i> Input pin</label>
|
||||
<label for="node-input-dev"><i class="fa fa-server"></i> Device</label>
|
||||
<select type="text" id="node-input-dev" style="width:150px;">
|
||||
<option value="3008">mcp3008</option>
|
||||
<option value="3002">mcp3002</option>
|
||||
<option value="3004">mcp3004</option>
|
||||
<option value="3202">mcp3202</option>
|
||||
<option value="3204">mcp3204</option>
|
||||
<option value="3208">mcp3208</option>
|
||||
<option value="3304">mcp3304</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-pin"><i class="fa fa-map-pin"></i> Input pin</label>
|
||||
<select type="text" id="node-input-pin" style="width:150px;">
|
||||
<option value=0>A0</option>
|
||||
<option value=1>A1</option>
|
||||
@ -15,7 +27,7 @@
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-mode"><i class="fa fa-toggle-on"></i> Device ID</label>
|
||||
<label for="node-input-dnum"><i class="fa fa-toggle-on"></i> Device ID</label>
|
||||
<select type="text" id="node-input-dnum" style="width:150px;">
|
||||
<option value=0>CE0</option>
|
||||
<option value=1>CE1</option>
|
||||
@ -28,7 +40,7 @@
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="pimcp3008">
|
||||
<p>Reads from an MCP3008 Analogue to Digital Converter (ADC) chip on the Pi SPI CE0 or CE1 connection,
|
||||
<p>Reads from an MCP3xxx Series Analogue to Digital Converter (ADC) chip on the Pi SPI CE0 or CE1 connection,
|
||||
such as the rasp.io/analogzero.</p>
|
||||
<p>You can either set a channel in the edit dialogue, or you can set the <code>msg.payload</code>
|
||||
to select the channel dynamically. If so then the expected payload must be a value from 0 to 7.</p>
|
||||
@ -47,6 +59,7 @@
|
||||
color: "#c6dbef",
|
||||
defaults: {
|
||||
name: {value:""},
|
||||
dev: {value:"3008"},
|
||||
pin: {value:0, required:true},
|
||||
dnum: {value:0}
|
||||
},
|
||||
@ -54,7 +67,7 @@
|
||||
outputs: 1,
|
||||
icon: "rpi.png",
|
||||
label: function() {
|
||||
return this.name || "ADC "+((this.pin==="M")?"":this.pin);
|
||||
return this.name || "mcp"+this.dev+" "+((this.pin==="M")?"":this.pin);
|
||||
},
|
||||
labelStyle: function() {
|
||||
return this.name?"node_label_italic":"";
|
||||
|
@ -12,52 +12,59 @@ module.exports = function(RED) {
|
||||
}
|
||||
|
||||
var mcpadc = require('mcp-spi-adc');
|
||||
var mcp3008 = [];
|
||||
var mcp3xxx = [];
|
||||
|
||||
function PiMcpNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.pin = n.pin || 0;
|
||||
this.interval = n.interval || 1000;
|
||||
this.dnum = parseInt(n.dnum || 0);
|
||||
this.dev = n.dev || "3008";
|
||||
var node = this;
|
||||
var cb = function (err) { if (err) { node.error("Error: "+err); } };
|
||||
var opt = { deviceNumber:node.dnum };
|
||||
var chans = parseInt(this.dev.substr(3));
|
||||
|
||||
try {
|
||||
fs.statSync("/dev/spidev0."+node.dnum);
|
||||
if (mcp3008.length === 0) {
|
||||
for (var i=0; i<8; i++) {
|
||||
mcp3008.push(mcpadc.openMcp3008(i, { deviceNumber:node.dnum }, function (err) {
|
||||
if (err) { node.error("Error: "+err); }
|
||||
}));
|
||||
if (mcp3xxx.length === 0) {
|
||||
for (var i=0; i<chans; i++) {
|
||||
if (node.dev === "3002") { mcp3xxx.push(mcpadc.openmcp3002(i, opt, cb)); }
|
||||
if (node.dev === "3004") { mcp3xxx.push(mcpadc.openmcp3004(i, opt, cb)); }
|
||||
if (node.dev === "3008") { mcp3xxx.push(mcpadc.openmcp3008(i, opt, cb)); }
|
||||
if (node.dev === "3202") { mcp3xxx.push(mcpadc.openmcp3202(i, opt, cb)); }
|
||||
if (node.dev === "3204") { mcp3xxx.push(mcpadc.openmcp3204(i, opt, cb)); }
|
||||
if (node.dev === "3208") { mcp3xxx.push(mcpadc.openmcp3208(i, opt, cb)); }
|
||||
if (node.dev === "3304") { mcp3xxx.push(mcpadc.openmcp3304(i, opt, cb)); }
|
||||
}
|
||||
}
|
||||
node.on("input", function(msg) {
|
||||
var pin = null;
|
||||
if (node.pin === "M") {
|
||||
var pay = parseInt(msg.payload.toString());
|
||||
if ((pay >= 0) && (pay <= 7)) { pin = pay; }
|
||||
else { node.warn("Payload needs to select channel 0 to 7"); }
|
||||
if ((pay >= 0) && (pay < chans)) { pin = pay; }
|
||||
else { node.warn("Payload needs to select channel 0 to "+(chans-1)); }
|
||||
}
|
||||
else { pin = parseInt(node.pin); }
|
||||
if (pin !== null) {
|
||||
mcp3008[pin].read(function (err, reading) {
|
||||
mcp3xxx[pin].read(function (err, reading) {
|
||||
if (err) { node.warn("Read error: "+err); }
|
||||
else { node.send({payload:reading.rawValue, topic:"adc/"+pin}); }
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch(err) {
|
||||
node.error("Error : Can't find SPI device - is SPI enabled in raspi-config ?");
|
||||
}
|
||||
|
||||
node.on("close", function(done) {
|
||||
if (mcp3008.length !== 0) {
|
||||
if (mcp3xxx.length !== 0) {
|
||||
var j=0;
|
||||
for (var i=0; i<8; i++) {
|
||||
mcp3008[i].close(function() { j += 1; if (j === 8) {done()} });
|
||||
mcp3xxx[i].close(function() { j += 1; if (j === 8) {done()} });
|
||||
}
|
||||
mcp3008 = [];
|
||||
mcp3xxx = [];
|
||||
}
|
||||
else { done(); }
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user