-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
148 lines (123 loc) · 3.79 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2022 Jean-David Caprace <[email protected]>
*
* Add the MIT license
*/
const ina219 = require('ina219-async');
module.exports = function (app) {
let timer = null
let plugin = {}
plugin.id = 'signalk-raspberry-pi-ina219'
plugin.name = 'Raspberry-Pi ina219'
plugin.description = 'ina219 i2c current/voltage/power sensor on Raspberry-Pi'
plugin.schema = {
type: 'object',
properties: {
rate: {
title: "Sample Rate (in seconds)",
type: 'number',
default: 5
},
pathvoltage: {
type: 'string',
title: 'SignalK Path of voltage',
description: 'This is used to build the path in Signal K for the voltage sensor data',
default: 'electrical.batteries.battery01.voltage' //Units: V (Volt)
//https://signalk.org/specification/1.5.0/doc/vesselsBranch.html
},
reportcurrent: {
type: 'boolean',
title: 'Also send the current data to Signalk',
default: true
},
pathcurrent: {
type: 'string',
title: 'SignalK Path of current',
description: 'This is used to build the path in Signal K for the current sensor data',
default: 'electrical.batteries.battery01.current' //Units: A (Ampere)
//https://signalk.org/specification/1.5.0/doc/vesselsBranch.html
},
i2c_bus: {
type: 'integer',
title: 'I2C bus number',
default: 1,
},
i2c_address: {
type: 'string',
title: 'I2C address',
default: '0x40',
},
}
}
plugin.start = function (options) {
function createDeltaMessage (voltage, current) {
var values = [
{
'path': options.pathvoltage,
'value': voltage
}
];
// Report current if desired
if (options.reportcurrent == true) {
values.push(
{
'path': options.pathcurrent,
'value': current
});
}
return {
'context': 'vessels.' + app.selfId,
'updates': [
{
'source': {
'label': plugin.id
},
'timestamp': (new Date()).toISOString(),
'values': values
}
]
}
}
// The ina219 constructor options are optional.
//const inaoptions = {
// bus : options.i2c_bus || 1, // defaults to 1
// address : options.i2c_address || '0x40', // defaults to 0x40
// };
// Read ina219 sensor data
async function readina219() {
const sensor = await ina219(Number(options.i2c_address), options.i2c_bus);
await sensor.calibrate32V2A();
const busvoltage = await sensor.getBusVoltage_V();
console.log("Bus voltage (V): " + busvoltage);
const shuntvoltage = await sensor.getShuntVoltage_mV();
console.log("Shunt voltage (mV): " + shuntvoltage);
const shuntcurrent = await sensor.getCurrent_mA();
console.log("Shunt Current (mA): " + shuntcurrent);
//console.log(`data = ${JSON.stringify(data, null, 2)}`);
//console.log(data)
// Change units to be compatible with SignalK
shuntcurrentA = shuntcurrent / 1000;
console.log("Load Current (A): " + shuntcurrentA);
loadvoltageV = busvoltage + (shuntvoltage / 1000);
console.log("Load voltage (V): " + loadvoltageV);
// create message
var delta = createDeltaMessage(loadvoltageV, shuntcurrentA)
// send data
app.handleMessage(plugin.id, delta)
//close sensor
//await sensor.close()
.catch((err) => {
console.log(`ina219 read error: ${err}`);
});
}
//readina219();
timer = setInterval(readina219, options.rate * 1000);
}
plugin.stop = function () {
if(timer){
clearInterval(timer);
timeout = null;
}
}
return plugin
}