-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
126 lines (115 loc) · 3.55 KB
/
app.js
File metadata and controls
126 lines (115 loc) · 3.55 KB
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
const five = require('johnny-five');
const Raspi = require("raspi-io");
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const cmd = require('node-cmd');
function all_ledon() {
cmd.run('sudo i2cset -y 1 0x70 0x00 0xff');
};
function all_ledoff() {
cmd.run('sudo i2cset -y 1 0x70 0x00 0x00');
};
function ledon() {
cmd.run('sudo i2cset -y 1 0x70 0x00 0x5a');
};
function led_maxgain() {
cmd.run('sudo i2cset -y 1 0x70 0x09 0x0f');
};
function ledbright() {
cmd.run('sudo i2cset -y 1 0x70 0x02 0x32');
cmd.run('sudo i2cset -y 1 0x70 0x04 0x32');
cmd.run('sudo i2cset -y 1 0x70 0x05 0x32');
cmd.run('sudo i2cset -y 1 0x70 0x07 0x32');
};
var board = new five.Board({
io: new Raspi()
});
app.use(express.static(__dirname + '/site'));
app.get('/', function (req, res, next) {
res.sendFile(__dirname + '/site/index.html')
});
var time = new Date();
var connection = null;
var sampleCount = [1, 1];
var sampleRate = [3, 10];
var accel = [0, 0, 0, 0];
var gyro = [0, 0, 0];
var temp = 0;
var press = 0;
var clients = 0;
board.on("ready", function () {
console.log("Device Ready..");
/*var imu = new five.IMU({
controller: "MPU6050",
freq: 100,
address: 0x68
});
var multi = new five.Multi({
controller: "BMP180",
freq: 700,
adress: '0x70GPIO1'
});
/*from data to change??
imu.on("data", function () {
sampleCount[0]++;
accel[0] += this.accelerometer.x;
accel[1] += this.accelerometer.y;
accel[2] += this.accelerometer.z;
accel[3] += this.accelerometer.acceleration - 1;
gyro[0] += this.accelerometer.pitch;
gyro[1] += this.accelerometer.roll;
gyro[2] += this.accelerometer.inclination;
if (sampleCount[0] % sampleRate[0] == 0) {
accel[0] = accel[0] / (sampleRate[0] - 1);
accel[1] = accel[1] / (sampleRate[0] - 1);
accel[2] = accel[2] / (sampleRate[0] - 1);
accel[3] = accel[3] / (sampleRate[0] - 1);
io.emit('accel', accel);
gyro[0] = gyro[0] / (sampleRate[0] - 1);
gyro[1] = gyro[1] / (sampleRate[0] - 1);
gyro[2] = gyro[2] / (sampleRate[0] - 1);
io.emit('angle', gyro);
accel = [0, 0, 0, 0];
gyro = [0, 0, 0];
sampleCount[0] = 1;
}
});
multi.on("data", function () {
sampleCount[1]++;
temp += this.thermometer.celsius;
press += this.barometer.pressure;
if (sampleCount[1] % sampleRate[1] == 0) {
temp = temp / (sampleRate[1] - 1);
io.emit('temp', temp);
press = press / (sampleRate[1] - 1);
io.emit('pressure', press);
press = 0;
temp = 0;
sampleCount[1] = 1;
}
});*/
io.on('connection', function (socket) {
//console.log('a user connected');
clients++;
io.emit('clients', clients);
socket.on('disconnect', function () {
//console.log('user disconnected');
clients--;
io.emit('clients', clients);
});
socket.on('light', function (value) {
if (value) {
all_ledon();
} else {
all_ledoff();
}
});
socket.on('picture', function (value) {
d = Math.floor(Date.now() / 100)
cmd.run('raspistill -o /home/pi/camera/' + d);
});
});
});
server.listen(3000);