-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·112 lines (95 loc) · 2.46 KB
/
server.js
File metadata and controls
executable file
·112 lines (95 loc) · 2.46 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
// initialize everything, web server, socket.io, filesystem, johnny-five
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs = require('fs');
var five = require("johnny-five");
var path = require("path");
var board, servo, led, sensor;
board = new five.Board();
// on board ready
board.on("ready", function() {
// init a led on pin 13, strobe every 1000ms
led = new five.Led(13).strobe(1000);
// setup a stanard servo, center at start
servo = new five.Servo({
pin:6,
range: [0,180],
type: "standard",
center:true
});
// poll this sensor every second
sensor = new five.Sensor({
pin: "A0",
freq: 58
});
});
// make web server listen on port 80
app.listen(3000);
// handle web server
function handler (request, response) {
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image/jpg';
break;
case '.wav':
contentType = 'audio/wav';
break;
}
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT'){
fs.readFile('./404.html', function(error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
}
else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
response.end();
}
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
};
// on a socket connection
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
// if board is ready
if(board.isReady){
// read in sensor data, pass to browser
sensor.on("data",function(){
socket.emit('sensor', { raw: this.raw });
});
}
// if servo message received
socket.on('servo', function (data) {
console.log(data);
if(board.isReady){ servo.to(data.pos); }
});
// if led message received
socket.on('led', function (data) {
console.log(data);
if(board.isReady){ led.strobe(data.delay); }
});
});