Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (44 loc) · 2.58 KB
/
Copy pathserver.js
File metadata and controls
52 lines (44 loc) · 2.58 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
var five = require("johnny-five"); // Load the node library that lets us talk JS to the Arduino
var board = new five.Board(); // Connect to the Arduino using that library
board.on("ready", function() { // Once the computer is connected to the Arduino
// Save convenient references to the LED pin and an analog pin
var LEDpin = new five.Pin(13);
var analogPin = new five.Pin('A0');
var express = require('express'); // Load the library we'll use to set up a basic webserver
var app = express(); // And start up that server
app.get('/', function(req, res) { // what happens when we go to `/`
res.send("Hello from `server.js`!"); // Just send some text
});
app.get('/hello', function(req, res) { // what ha1ppens when we go to `/hello`
res.sendFile('hello.html', { root: '.' }); // Send back the file `hello.html` located in the current directory (`root`)
});
app.get('/:pin/state', function(req, res) { // what happens when someone goes to `/#/state`, where # is any number
console.log("Someone asked for the state of pin", req.params.pin + "…");
var pins = {
'analog': analogPin,
'led': LEDpin
};
if (pins.hasOwnProperty(req.params.pin)) { // If our pins dictionary knows about the pin name requested
pins[req.params.pin].query(function(state) { // Look up the pin object associated with the pin name and query it
res.send(state); // sending back whatever the state we get is
});
}
else {
var errorMessage = "Sorry, you asked for the state of pin `" + req.params.pin + '`, ' + "but I haven't been told about that pin yet.";
res.send(errorMessage);
}
});
app.get('/led/off', function(req, res) { // what happens when someone goes to `/led/off`
console.log("Someone told me to turn the led off…");
LEDpin.low(); // Set the pin referred to by the `LEDpin` variable 'low' (off)
res.send("Now the LED for pin 13 should be off."); // And tell the user that it should be off in the webpage
});
app.get('/led/on', function(req, res) { // what happens when someone goes to `/led/off`
console.log("Someone told me to turn the led on…");
LEDpin.high(); // Set the pin referred to by the `LEDpin` variable 'high' (on)
res.send("Now the LED for pin 13 should be on.") // And tell the user that it should be off in the webpage
});
app.listen(3000, function() { // Actually turn the server on
console.log("Server's up at http://localhost:3000!");
});
});