-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht-logger.js
117 lines (98 loc) · 2.37 KB
/
dht-logger.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
// define serialport
var SERIALPORT = require('serialport');
var SerialPort = SERIALPORT.SerialPort;
var serialport = new SerialPort('/dev/ttyACM0', {
parser: SERIALPORT.parsers.readline('\n')
});
// data collector
var COLLECT_INTERVAL = 300;
var data_buffer = (function() {
var temperature = [], humidity = [];
return {
set_temperature: function(t) {
temperature.push(t);
},
set_humidity: function(h) {
humidity.push(h);
},
get_temperature: function() {
if(temperature.length === 0) {
return 0;
} else {
var total = 0, i = 0, max = temperature.length;
for(; i < max; i++) {
total += parseInt(temperature[i], 10);
}
temperature = [];
return total / max;
}
},
get_humidity: function() {
if(humidity.length === 0) {
return 0;
} else {
var total = 0, i = 0, max = humidity.length;
for(; i < max; i++) {
total += parseInt(humidity[i], 10);
}
humidity = [];
return total / max;
}
}
}
})();
// mysql
var mysql = require('mysql');
var make_insert_query = function(data) {
var fields = [], values = [];
for(var key in data) {
fields.push(key);
values.push(data[key]);
}
return 'INSERT INTO dht (' + fields.join(',') + ') VALUES (' + values.join(',') + ')';
};
// moment
var moment = require('moment');
serialport.open(function(error) {
if(error) {
console.log('failed to open: ' + error);
} else {
console.log('serial port opened.');
serialport.on('data', function(data) {
var splited_data = data.split('|');
data_buffer.set_temperature(splited_data[0]);
data_buffer.set_humidity(splited_data[1]);
});
}
});
setInterval(function() {
var db_connection = mysql.createConnection({
host: 'localhost',
user: 'dht',
password: 'dht',
database: 'dht_log'
});
db_connection.connect(function(error) {
if(error) {
console.log('error db connecting');
console.error(error);
return;
}
db_connection.query(make_insert_query({
temperature: data_buffer.get_temperature(),
humidity: data_buffer.get_humidity(),
year: moment().year(),
month: moment().month() + 1,
date_of_month: moment().date(),
day_of_week: moment().day(),
hour: moment().hour(),
created_at: moment().format('X')
}), function(error) {
if(error) {
console.log('error db insert');
console.error(error);
}
db_connection.end();
});
});
}, COLLECT_INTERVAL * 1000);