try nodejs instead
This commit is contained in:
parent
2acb7dcd82
commit
e79c419e40
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
node_modules
|
15
index.html
Normal file
15
index.html
Normal file
|
@ -0,0 +1,15 @@
|
|||
<html>
|
||||
<head>
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
var socket = io();
|
||||
console.log("loaded socketio");
|
||||
socket.on('connect', function(){
|
||||
socket.send('hi');
|
||||
socket.on('data', function(data){
|
||||
console.log(data);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
</html>
|
11
package.json
Normal file
11
package.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "Open_Access_Control_API",
|
||||
"version": "0.0.1",
|
||||
"main": "serial.js",
|
||||
"dependencies": {
|
||||
"serialport": "*",
|
||||
"express": "*",
|
||||
"http": "*",
|
||||
"socket.io": "*"
|
||||
}
|
||||
}
|
35
serial.js
Normal file
35
serial.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
const SerialPort = require('serialport');
|
||||
const Readline = require('@serialport/parser-readline');
|
||||
|
||||
var express = require('express'),
|
||||
app = express(),
|
||||
http = require('http'),
|
||||
server = http.createServer(app),
|
||||
io = require('socket.io').listen(server),
|
||||
scores = {};
|
||||
|
||||
// listen for new web clients:
|
||||
server.listen(8080);
|
||||
|
||||
// open the serial port. Change the name to the name of your port, just like in Processing and Arduino:
|
||||
const port = new SerialPort("/dev/ttyUSB0", { baudRate: 9600 });
|
||||
|
||||
const parser = new Readline({ delimiter: '\r' });
|
||||
port.pipe(parser)
|
||||
|
||||
// respond to web GET requests with the index.html page:
|
||||
app.get('/', function (req, res) {
|
||||
res.sendFile(__dirname + '/index.html');
|
||||
});
|
||||
|
||||
// listen for new serial data:
|
||||
parser.on('data', line => console.log(`> ${line}`))
|
||||
|
||||
// listen for new socket.io connections:
|
||||
io.on('connection', function (socket) {
|
||||
parser.on('data', line => socket.emit('data', line))
|
||||
socket.on('message', function (data) {
|
||||
console.log(data)
|
||||
port.write(data+'\r')
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user