Skip to content

Commit 0a700ff

Browse files
committed
Added HTML and Node.js files
1 parent e3b0f12 commit 0a700ff

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

app.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var http = require('http');
2+
var fs = require('fs');
3+
var index = fs.readFileSync( 'index.html');
4+
5+
var SerialPort = require('serialport');
6+
const parsers = SerialPort.parsers;
7+
8+
const parser = new parsers.Readline({
9+
delimiter: '\r\n'
10+
});
11+
12+
var port = new SerialPort('/dev/tty.usbmodem1421',{
13+
baudRate: 9600,
14+
dataBits: 8,
15+
parity: 'none',
16+
stopBits: 1,
17+
flowControl: false
18+
});
19+
20+
port.pipe(parser);
21+
22+
var app = http.createServer(function(req, res) {
23+
res.writeHead(200, {'Content-Type': 'text/html'});
24+
res.end(index);
25+
});
26+
27+
var io = require('socket.io').listen(app);
28+
29+
io.on('connection', function(socket) {
30+
31+
console.log('Node is listening to port');
32+
33+
});
34+
35+
parser.on('data', function(data) {
36+
37+
console.log('Received data from port: ' + data);
38+
39+
io.emit('data', data);
40+
41+
});
42+
43+
app.listen(3000);

index.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
5+
<script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js'></script>
6+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
7+
8+
<script>
9+
10+
$(document).ready(function(){
11+
12+
var socket = io();
13+
14+
socket.on('data', function(data) {
15+
16+
console.log(data);
17+
18+
$('#sample').css('opacity',data/100);
19+
20+
});
21+
22+
});
23+
24+
</script>
25+
26+
<style>
27+
28+
#sample {
29+
background-color: red;
30+
width: 300px;
31+
height: 300px;
32+
}
33+
34+
</style>
35+
36+
</head>
37+
<body>
38+
39+
<div id="sample"></div>
40+
41+
</body>
42+
</html>

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "basic-socket",
3+
"version": "0.0.1",
4+
"description": "Sample communication from an HTML document to a NodeJS using socket.io.",
5+
"dependencies": {
6+
"socket.io": "^2.0.4",
7+
"serialport": "^6.0.5"
8+
}
9+
}

0 commit comments

Comments
 (0)