-
Notifications
You must be signed in to change notification settings - Fork 2
/
hx711.js
67 lines (60 loc) · 2.09 KB
/
hx711.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) {
var m = require('mraa');
var fs = require('fs');
var hx711 = require('jsupm_hx711');
function upm_hx711(n) {
//init
RED.nodes.createNode(this,n);
RED.log.info("Node created");
//properties
this.name = n.name;
this.scale = parseFloat(n.scale);
this.dataPin = parseInt(n.dataPin);
this.clockPin = parseInt(n.clockPin);
this.platform = parseInt(n.platform);
this.interval = n.interval;
RED.log.info("Conifg parsed " + this.clockPin + " " + this.dataPin);
if(this.platform === 512) {
var file;
try {
file = fs.readFileSync('/tmp/imraa.lock', 'utf8');
var arr = JSON.parse(file).Platform;
for (var i = 0; i < arr.length; i++) {
if(arr[i].hasOwnProperty('uart')) {
//explicitly add the FIRMATA subplatform for MRAA
m.addSubplatform(m.GENERIC_FIRMATA, arr[i].uart);
}
}
} catch (e) {
if (e.code === 'ENOENT') {
//if we cannot find lock file we assume ttyACM0 and try
m.addSubplatform(m.GENERIC_FIRMATA, "/dev/ttyACM0");
}
}
}
RED.log.info("Got device");
var dataPinAndPlatform = this.dataPin + this.platform;
var clockPinAndPlatform = this.clockPin + this.platform;
RED.log.info(dataPinAndPlatform + " " + clockPinAndPlatform);
this.sensor = new hx711.HX711(dataPinAndPlatform, clockPinAndPlatform);
RED.log.info("Created scale");
this.sensor.setScale(this.scale);
this.sensor.tare();
RED.log.info("Configured and zeroed scale");
this.status({});
var node = this;
var msg = { topic: node.name + '/A' + node.dataPin + '/A' + node.clockPin };
//poll reading at interval
RED.log.info("Set to send message every " + this.interval);
this.timer = setInterval(function() {
msg.payload = node.sensor.readUnits(10);
node.send(msg);
}, node.interval);
//clear interval on exit
this.on('close', function() {
clearInterval(node.timer);
});
}
RED.log.info("Registering node");
RED.nodes.registerType('upm-hx711', upm_hx711);
};