Skip to content

Commit f48d6da

Browse files
committed
Added HTML and Node.js files
1 parent 9a41b39 commit f48d6da

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

app.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
socket.on('lights',function(data){
32+
33+
console.log( data );
34+
35+
port.write( data.status );
36+
37+
});
38+
39+
});
40+
41+
app.listen(3000);

index.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
$('#lightOn').click(function(){
15+
16+
socket.emit('lights', { "status":"1" });
17+
18+
});
19+
20+
$('#lightOff').click(function(){
21+
22+
socket.emit('lights', { "status":"0" });
23+
24+
});
25+
26+
});
27+
28+
</script>
29+
</head>
30+
<body>
31+
32+
<button id="lightOn">Turn Light On</button>
33+
<button id="lightOff">Turn Light Off</button>
34+
35+
</body>
36+
</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)