master
louisa 2 years ago
parent 0cb83df278
commit b7ffcf255f

@ -3,18 +3,14 @@ var fs = require('fs');
var express = require('express'); var express = require('express');
var app = express(); var app = express();
var path = require('path'); var path = require('path');
// var index = fs.readFileSync('index.html');
var server = http.createServer(app); var server = http.createServer(app);
var port = 8000; var port = 8000;
const { SerialPort } = require('serialport') const { SerialPort } = require('serialport');
const { ReadlineParser } = require('@serialport/parser-readline') const { ReadlineParser } = require('@serialport/parser-readline');
const sport = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) const sport = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 });
const five = require('johnny-five'); const parser = sport.pipe(new ReadlineParser({ delimiter: '\r\n' }));
const board = new five.Board(); parser.on('data', console.log);
const parser = sport.pipe(new ReadlineParser({ delimiter: '\r\n' }))
parser.on('data', console.log)
server.listen(port, () => { server.listen(port, () => {
console.log("Server is listening at port %d", port); console.log("Server is listening at port %d", port);
@ -22,38 +18,10 @@ server.listen(port, () => {
app.use(express.static(path.join(__dirname, "public"))); app.use(express.static(path.join(__dirname, "public")));
board.on('ready', function() {
const led = new five.Led(13); // Assuming LED connected to GPIO 13 (D13)
// Emit 'blink' event to clients when the LED turns on
led.on('on', function() {
io.emit('blink');
});
// Emit 'stop' event to clients when the LED turns off
led.on('off', function() {
io.emit('stop');
});
// Function to blink the LED
function blinkLed() {
led.blink(500); // Blink every 500 milliseconds
}
// Start blinking the LED
blinkLed();
});
// var server = http.createServer(function(req, res){
// res.writeHead(200, {'Content-Type':'text/html'});
// res.end(index);
// });
var io = require('socket.io')(server); var io = require('socket.io')(server);
io.on('connection', function(socket){ io.on('connection', function(socket) {
console.log('A new client connected');
parser.on('data', function(data) { parser.on('data', function(data) {
const msg = data.split(' '); const msg = data.split(' ');
@ -61,12 +29,19 @@ io.on('connection', function(socket){
io.emit('node-data', data); io.emit('node-data', data);
}); });
console.log('Node.js is listening!!'); socket.on('hello', (arg, callback) => {
socket.on("hello", (arg, callback) => { console.log("Received 'hello' event from client");
console.log("hellohelo"); // "world"
io.emit('blink'); io.emit('blink');
});
socket.on('disconnect', () => {
console.log('A client disconnected');
});
socket.on('error', (error) => {
console.log('Socket error:', error);
}); });
});
socket.on('chat message', (msg) => { socket.on('chat message', (msg) => {
console.log('[user]['+ socket.id + '][' + msg + ']'); console.log('[user]['+ socket.id + '][' + msg + ']');

Loading…
Cancel
Save