-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (65 loc) · 1.54 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
//$ npm install socket.io
var http = require('http')
, io = require('socket.io').listen(app)
, fs = require('fs')
, gpio = require('rpi-gpio')
, express = require('express');
var app = express();
var port = 8080;
app.use(express.static(__dirname + '/public'));
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
function turnOn() {
gpio.write(7, true, function(err) {
if (err) throw err;
});
console.log("now its on");
}
function turnOff() {
gpio.write(7, false, function(err) {
if (err) throw err;
});
console.log("now its off");
}
function turnOn11() {
gpio.write(11, true, function(err) {
if (err) throw err;
});
console.log("now its on");
}
function turnOff11() {
gpio.write(11, false, function(err) {
if (err) throw err;
});
console.log("now its off");
}
io.sockets.on('connection', function (socket) {
socket.on('socket1Toggle', function (data) {
console.log(data.status);
if(data.status=='OFF'){
gpio.setup(7, gpio.DIR_OUT, turnOn);
}else{
gpio.setup(7, gpio.DIR_OUT, turnOff);
}
});
socket.on('socket2Toggle', function (data) {
console.log(data.status);
if(data.status=='OFF'){
gpio.setup(11, gpio.DIR_OUT, turnOn11);
}else{
gpio.setup(11, gpio.DIR_OUT, turnOff11);
}
});
});
http.createServer(app).listen(port);
console.log("Server is running on port " + port);
~