forked from CoretechR/ZeroBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
107 lines (89 loc) · 2.99 KB
/
app.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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var exec = require('child_process').exec, child;
var port = process.env.PORT || 3000;
var ads1x15 = require('node-ads1x15');
var adc = new ads1x15(1); // set to 0 for ads1015
var Gpio = require('pigpio').Gpio,
A1 = new Gpio(13, {mode: Gpio.OUTPUT}),
A2 = new Gpio(26, {mode: Gpio.OUTPUT}),
B1 = new Gpio(6, {mode: Gpio.OUTPUT}),
B2 = new Gpio(5, {mode: Gpio.OUTPUT});
LED = new Gpio(21, {mode: Gpio.OUTPUT});
app.get('/', function(req, res){
res.sendfile('Touch.html');
console.log('HTML sent to client');
});
child = exec("sudo bash start_stream.sh", function(error, stdout, stderr){});
//Whenever someone connects this gets executed
io.on('connection', function(socket){
console.log('A user connected');
socket.on('pos', function (msx, msy) {
//console.log('X:' + msx + ' Y: ' + msy);
//io.emit('posBack', msx, msy);
msx = Math.min(Math.max(parseInt(msx), -255), 255);
msy = Math.min(Math.max(parseInt(msy), -255), 255);
if(msx > 0){
A1.pwmWrite(msx);
A2.pwmWrite(0);
} else {
A1.pwmWrite(0);
A2.pwmWrite(Math.abs(msx));
}
if(msy > 0){
B1.pwmWrite(msy);
B2.pwmWrite(0);
} else {
B1.pwmWrite(0);
B2.pwmWrite(Math.abs(msy));
}
});
socket.on('light', function(toggle) {
LED.digitalWrite(toggle);
});
socket.on('cam', function(toggle) {
var numPics = 0;
console.log('Taking a picture..');
//Count jpg files in directory to prevent overwriting
child = exec("find -type f -name '*.jpg' | wc -l", function(error, stdout, stderr){
numPics = parseInt(stdout)+1;
// Turn off streamer, take photo, restart streamer
var command = 'sudo killall mjpg_streamer ; raspistill -o cam' + numPics + '.jpg -n && sudo bash start_stream.sh';
//console.log("command: ", command);
child = exec(command, function(error, stdout, stderr){
io.emit('cam', 1);
});
});
});
socket.on('power', function(toggle) {
child = exec("sudo poweroff");
});
//Whenever someone disconnects this piece of code is executed
socket.on('disconnect', function () {
console.log('A user disconnected');
});
setInterval(function(){ // send temperature every 5 sec
child = exec("cat /sys/class/thermal/thermal_zone0/temp", function(error, stdout, stderr){
if(error !== null){
console.log('exec error: ' + error);
} else {
var temp = parseFloat(stdout)/1000;
io.emit('temp', temp);
console.log('temp', temp);
}
});
if(!adc.busy){
adc.readADCSingleEnded(0, '4096', '250', function(err, data){ //channel, gain, samples
if(!err){
voltage = 2*parseFloat(data)/1000;
console.log("ADC: ", voltage);
io.emit('volt', voltage);
}
});
}
}, 5000);
});
http.listen(port, function(){
console.log('listening on *:' + port);
});