147 lines
4.0 KiB
JavaScript
147 lines
4.0 KiB
JavaScript
/* const express = require('express');
|
|
const { createServer } = require('node:http');
|
|
// socket
|
|
const { Server } = require('socket.io');
|
|
// usb serial (arduino)
|
|
var { SerialPort } = require("serialport");
|
|
const { ReadlineParser } = require('@serialport/parser-readline')
|
|
|
|
const app = express();
|
|
const server = createServer(app);
|
|
const io = new Server(server);
|
|
|
|
const port = 3000
|
|
|
|
// https://serialport.io/docs/guide-usage
|
|
|
|
var arduinoCOMPort = "/dev/ttyUSB0";
|
|
var arduinoSerialPort = new SerialPort({
|
|
path: arduinoCOMPort,
|
|
baudRate: 9600
|
|
});
|
|
|
|
arduinoSerialPort.on('open',function() {
|
|
console.log('Serial Port ' + arduinoCOMPort + ' is opened.');
|
|
});
|
|
|
|
|
|
// // Read data that is available but keep the stream in "paused mode"
|
|
// arduinoSerialPort.on('readable', function () {
|
|
// console.log('Data:', arduinoSerialPort.read())
|
|
// })
|
|
|
|
// // Switches the port into "flowing mode"
|
|
// arduinoSerialPort.on('data', function (data) {
|
|
// console.log('Data:', data)
|
|
// })
|
|
|
|
// const serialParser = arduinoSerialPort.pipe(new Readline({ delimiter: '\n' }));
|
|
|
|
// serialParser.on('data', data =>{
|
|
// console.log('got word from arduino:', data);
|
|
// });
|
|
|
|
app.set('view engine', 'pug')
|
|
app.use(express.static('assets'))
|
|
|
|
app.get('/', (req, res) => {
|
|
// res.send('Hello World!')
|
|
// res.render('index', { title: 'Hey', message: 'Hello there PUG!' })
|
|
res.sendFile('assets/main.html', { root : __dirname});
|
|
})
|
|
|
|
io.on('connection', (socket) => {
|
|
console.log('a user connected');
|
|
// let speed = 0.1;
|
|
// setInterval(() => {
|
|
// speed+=0.1;
|
|
// socket.emit('speed', speed);
|
|
// }, 2000);
|
|
|
|
// Listening to arduino through serial port
|
|
const parser = arduinoSerialPort.pipe(new ReadlineParser({ delimiter: '\n' }))
|
|
parser.on('data', data => {
|
|
console.log('got word from arduino:', data);
|
|
socket.emit('position', data);
|
|
})
|
|
|
|
});
|
|
|
|
server.listen(port, () => {
|
|
console.log(`Example app listening on port ${port}`)
|
|
}) */
|
|
|
|
const express = require('express');
|
|
const http = require('http');
|
|
const socketIo = require('socket.io');
|
|
const player = require('play-sound')({ player: 'mpg123' }); // forcer mpg123
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const io = socketIo(server);
|
|
|
|
const SerialPort = require('serialport');
|
|
const ReadlineParser = require('@serialport/parser-readline');
|
|
|
|
const portName = "/dev/ttyUSB0"; // adapte selon ta config
|
|
const arduinoSerialPort = new SerialPort(portName, { baudRate: 115200 });
|
|
|
|
const parser = arduinoSerialPort.pipe(new ReadlineParser({ delimiter: '\n' }));
|
|
|
|
parser.on('data', (data) => {
|
|
console.log('Données reçues Arduino:', data.trim());
|
|
io.emit('position', data.trim()); // envoi à tous les clients
|
|
});
|
|
|
|
io.on('connection', (socket) => {
|
|
console.log('Client connecté');
|
|
// Ici tu peux gérer d'autres events socket si besoin
|
|
});
|
|
|
|
app.use(express.static('assets')); // dossier public pour fichiers front
|
|
|
|
let currentAudioProcess = null;
|
|
|
|
io.on('connection', (socket) => {
|
|
console.log('Client connecté');
|
|
|
|
socket.on('play-sound', (filename) => {
|
|
console.log('Lecture demandée:', filename);
|
|
|
|
// Stopper si un son est déjà joué
|
|
if (currentAudioProcess) {
|
|
console.log('Arrêt du son précédent');
|
|
currentAudioProcess.kill();
|
|
currentAudioProcess = null;
|
|
}
|
|
|
|
// Lancer la lecture avec mpg123
|
|
const path = require('path');
|
|
const filePath = path.join(__dirname, 'node-server', 'sounds', filename);
|
|
console.log('Chemin complet du fichier:', filePath);
|
|
|
|
currentAudioProcess = player.play(filePath, (err) => {
|
|
if (err && !err.killed) {
|
|
console.error('Erreur lecture audio:', err);
|
|
}
|
|
currentAudioProcess = null;
|
|
});
|
|
});
|
|
|
|
socket.on('stop-sound', () => {
|
|
if (currentAudioProcess) {
|
|
currentAudioProcess.kill();
|
|
currentAudioProcess = null;
|
|
console.log('Lecture arrêtée');
|
|
}
|
|
});
|
|
|
|
socket.on('disconnect', () => {
|
|
console.log('Client déconnecté');
|
|
});
|
|
});
|
|
|
|
server.listen(3000, () => {
|
|
console.log('Serveur démarré sur http://localhost:3000');
|
|
});
|