-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (70 loc) · 1.85 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
var Transform = require('stream').Transform
var ina219 = require('ina219')
ina219.init()
const coeff = -0.020351086
function I2cProvider () {
const pushIt = this.push.bind(this)
ina219.calibrate32V1A(function () {
setInterval(measure.bind(this, pushIt), 5000)
})
Transform.call(this, {
objectMode: true
})
}
require('util').inherits(I2cProvider, Transform)
var Ads1x15 = require('node-ads1x15')
var ads1x15 = new Ads1x15(1, 0x48)
function measure (callback) {
var amps
ina219
.getShuntVoltage_raw()
.then(function (shuntVoltage) {
amps =
(shuntVoltage & 0x8000 ? shuntVoltage - 0x10000 : shuntVoltage) * coeff
return ads1x15.readADCSingleEnded(0, 4096, 250)
})
.then(function (measuredVolts) {
callback({
updates: [
{
source: {},
values: [
// {
// path: 'electrical.batteries.house.voltage',
// value: (measuredVolts / 1000) * 4.11135334
// },
{
path: 'electrical.batteries.house.current',
value: amps
}
]
}
]
})
})
.then(() => {
return new Promise((resolve, reject) => {
bmp180.read(data => {
callback({
updates: [
{
values: [
{
path: 'environment.outside.pressure',
value: data.pressure
}
]
}
]
})
})
resolve()
})
})
}
const i2cbmp180 = require('node-red-contrib-bmp180/resources/i2cbmp180')
const bmp180 = new i2cbmp180({})
I2cProvider.prototype._transform = function (chunk, encoding, done) {
console.error("Not a real transformer, this shouldn't be happening")
}
module.exports = I2cProvider