-
Notifications
You must be signed in to change notification settings - Fork 1
/
max31865.js
67 lines (58 loc) · 1.92 KB
/
max31865.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module.exports = function(RED)
{
"use strict";
var fs = require('fs');
// Unlikely if not on a Pi
try
{
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
if (cpuinfo.indexOf(": BCM") === -1)
{
throw "Info : "+RED._("rpi-gpio.errors.ignorenode");
}
}
catch(err)
{
throw "Info : "+RED._("rpi-gpio.errors.ignorenode");
}
// Initialize MAX31865 dependency
const MAX31865 = require('max31865');
// Node-RED main function
function PiMax31865Node(n)
{
RED.nodes.createNode(this, n);
// Initialize input variables
var node = this;
this.bus = parseInt(n.bus || 0); // bus - first digit in /dev/spidev0.0
this.device = parseInt(n.dev || 0); // device - second digit in /dev/spidev0.0
this.rtdNominal = parseInt(n.rtd || 1000); // nominal resistance of sensor
this.refResistor = parseInt(n.ref || 4300); // reference resistance on board
this.sensorWires = parseInt(n.wir || 4); // wires used for sensor (2, 3 or 4)
this.name = n.name || "max31865-"+this.bus+"/"+this.device;
// Configure sensor
const sensor = new MAX31865(
this.bus,
this.device,
{
rtdNominal: this.rtdNominal,
refResistor: this.refResistor,
wires: this.sensorWires,
}
);
// Initialize sensor
sensor.init()
.then(() =>
{
node.on("input", function(msg) {
// Get temperature from sensor
sensor.getTemperature()
.then((temperature) =>
{
// Send payload
node.send({payload:temperature.toFixed(2), topic:this.name});
});
});
});
}
RED.nodes.registerType("pimax31865",PiMax31865Node);
}