-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (93 loc) · 3 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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var ModbusRTU = require("modbus-serial");
// firebase stuff
/*
var admin = require('firebase-admin');
var serviceAccount = require('./SolarSensor-06d5c234b657.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://solar-sensor.firebaseio.com'
});
var db = admin.firestore()
// Add a new document in collection "cities"
db.collection("datapoint").doc().set({
batteryVoltage: "28",
time: new Date(),
})
.then(function() {
console.log("Firebase document successfully written!");
})
.catch(function(error) {
console.error("Error writing document: ", error);
});
*/
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
var connectionExists = false;
io.on('connect', function(socket){
connectionExists = true;
console.log('client connected');
setTimeout( function() {
solarStatus();
}, 1000);
socket.on('disconnect', function(){
connectionExists = false;
console.log('client disconnected');
});
});
// create an empty modbus client
var client = new ModbusRTU();
var i=0;
// open connection to a serial port
client.connectRTUBuffered("/dev/tty.usbserial-DN008CH0", {baudRate: 9600, dataBits: 8, stopBits: 2, flowControl: false, parity: 'none'});
client.setID(1);
var details = [];
function solarStatus() {
// read the values of 3 registers starting at address 11
// on device number 1.
//console.log('solarstatus');
if (connectionExists) {
client.readHoldingRegisters(0x0010, 48)
.then(function(data) {
console.log(data);
if (data) {
details = {
arrayCurrent: float16_to_float(data.data[1]).toFixed(2),
arrayVoltage: float16_to_float(data.data[3]).toFixed(2),
batteryVoltage: float16_to_float(data.data[2]).toFixed(2),
chargeCurrent: float16_to_float(data.data[0]).toFixed(2),
targetVoltage: float16_to_float(data.data[47]).toFixed(2),};
// console.log(data);
//};
io.sockets.emit('message', details );
/* db.collection("datapoint").doc().set({
arrayCurrent: details.arrayCurrent,
batteryVoltage: details.batteryVoltage,
time: new Date(),
})*/
setTimeout(solarStatus, 1000);
} else { console.log('no data'); }
})
.catch(function(error) {
console.log('Error getting data from Modbus');
})
}
}
http.listen(9000, function(){
console.log('listening on *:9000');
});
// function to input a float16 and return a float
function float16_to_float(h) {
var s = (h & 0x8000) >> 15;
var e = (h & 0x7C00) >> 10;
var f = h & 0x03FF;
if(e == 0) {
return (s?-1:1) * Math.pow(2,-14) * (f/Math.pow(2, 10));
} else if (e == 0x1F) {
return f?NaN:((s?-1:1)*Infinity);
}
return (s?-1:1) * Math.pow(2, e-15) * (1+(f/Math.pow(2, 10)));
}