-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (63 loc) · 1.93 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
const serialPort = require('serialport');
const portNumber = "COM10";// situasional, lihat port di device manager
const baudPort = new serialPort(portNumber ,{baudRate:57600});
console.log("Port Number : "+portNumber);
// parser agar tidak buffer
const parsers = serialPort.parsers;
const parser = new parsers.Readline({
delimiter: '\r\n'
})
baudPort.pipe(parser);
// akses data yang masuk node js
baudPort.on('open', ()=>{
console.log('Connected at : '+portNumber);
let timeOut = 3000; // 3 detik
setTimeout(()=>{
// kirim command 1 ke arduino
baudPort.write('1',(err)=>{
if (err){
console.log(err);
}else {
console.log('Node connected');
}
});
},timeOut);
});
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io').listen(server);
const path = require('path');
app.use(express.static(path.join(__dirname,'www')));
const portListen = 3123;
server.listen(portListen);
//buat socket event
let clientCount = 0;
io.on('connection',(socket)=>{
clientCount++;
console.log('New client connected '+clientCount);
parser.on('data',(data)=>{
let hasilParsing = parsingRawData(data);
let parsePort = portNumber;
socket.emit('socketData',{dataHasil : hasilParsing, dataPort : parsePort});
socket.emit('dataCoordinate', {dataHasil : hasilParsing});
socket.emit('data3d', {dataHasil : hasilParsing});
console.log(data);
});
})
/**
*
* @param {String} data
* @returns
* [<latitude>, <longitude>, <humidity>, <temperature>, <acc_x>, <acc_x>, <acc_x>, <gyro_x>, <gyro_y>, <gyro_z>]
* "Parsing untuk intern payload"
*/
function parsingRawData (data) {
const regex = /([0-9|\.|-]*)/g;
let hasilParsing = [];
data.match(regex).forEach(element => {
if (element != '')
hasilParsing.push(element);
});
return hasilParsing;
}