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