-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecordData.js
42 lines (38 loc) · 1.52 KB
/
recordData.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
const SerialPort = require('serialport');
const config = require('./config').database2;
const ReadLine = require('@serialport/parser-readline');
const sql = require('mssql');
sql.connect(config).then(connection => {
SerialPort.list().then(ports => {
ports.forEach(p => {
if (p.manufacturer === 'Arduino LLC') {
const arduino = new SerialPort(p.comName, {
baudRate: 9600,
autoOpen: true
});
let parser = new ReadLine();
arduino.on('open', () => {
console.log(`Connected to Arduino on port ${p.comName}`);
arduino.pipe(parser);
});
parser.on('data', data => {
data = data.split(':');
return connection.request().input('temperatura', data[0])
.input('umidade', data[1])
.query(`INSERT INTO leitura (temperatura, umidade, momento)
VALUES (@temperatura, @umidade, CURRENT_TIMESTAMP);`);
}).then(result => {
//console.log('Success!');
//console.log(result);
}).catch(error => {
console.error('Failed!');
console.error(error);
});
}
});
}).catch(error => {
console.error(error);
});
}).catch(error => {
console.log(error);
});