-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
72 lines (53 loc) · 2.25 KB
/
index.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
/*
thermostat
a node.js app to communicate between a serial thermostat device
and a web client version of the same.
requires:
* node.js (http://nodejs.org/)
* express.js (http://expressjs.com/)
* socket.io (http://socket.io/#how-to-use)
* serialport.js (https://github.com/voodootikigod/node-serialport)
based on the core examples for socket.io and serialport.js
created 21 Jan 2014
by Tom Igoe
*/
var serialport = require("serialport"), // include the serialport library
SerialPort = serialport.SerialPort, // make a local instance of serial
express = require('express'), // make an instance of express
open = require('open'), // used to open the browser
url = 'http://localhost:8080'; // URL to open in the browser
var app = express(), // start Express framework
server = require('http').createServer(app); // start an HTTP server
// set up static folders for client-side JavaScript and CSS:
app.use('/js', express.static(__dirname + '/js'));
app.use('/css', express.static(__dirname + '/css'));
// third word of the command line is serial port name:
var portName = process.argv[2];
// print out the port you're listening on:
console.log("opening serial port: " + portName);
// listen for incoming requests on the server:
server.listen(8080);
console.log("Listening for new clients on port 8080");
// open the app in a browser:
open(url);
// open the serial port:
var myPort = new SerialPort(portName, {
// look for return and newline at the end of each data packet:
parser: serialport.parsers.readline("\r\n")
});
// respond to web GET requests with the index.html page:
app.get('/', function (request, response) {
response.sendfile(__dirname + '/index.html');
});
// take anything that begins with /output:
app.get('/output/*', function (request, response) {
// the route is the first parameter of the URL request:
var blindCommand = request.params[0];
console.log("received "+ blindCommand);
// send it out the serial port:
myPort.write(blindCommand);
// send an HTTP header to the client:
response.writeHead(200, {'Content-Type': 'text/html'});
// send the data and close the connection:
response.end(blindCommand);
});