-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtomatino_Server.js
57 lines (46 loc) · 2.04 KB
/
tomatino_Server.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
/*
serialServer.js
a node.js app to read serial strings and send them to webSocket clients
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)
Sourced from: https://github.com/tigoe/NetworkExamples/blob/master/nodeSerialServer/index.js
*/
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
io = require('socket.io').listen(server); // listen for websocket requests
// 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 + '/tomatino_Listener.html');
});
// listen for new socket.io connections:
io.sockets.on('connection', function (socket) {
// if there's a socket client, listen for new serial data:
myPort.on('data', function (data) {
// for debugging, you should see this in Terminal:
console.log(data);
// send a serial event to the web client with the data:
socket.emit('serialEvent', data);
});
});