我正在尝试从多个传感器获取数据,并使用socket.io在图表上显示它们,但我不知道该如何在Node.js express中进行扩展。
我用一个传感器做到了,但是我想在我添加新传感器时复制它
app.set('socket.io',io);
var io = require('socket.io')(server);
const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort('COM4'); //Connect serial port to port COM3. Because my Arduino Board is connected on port COM3. See yours on Arduino IDE -> Tools -> Port
const parser = port.pipe(new Readline({delimiter: ',\r\n'})); //Read the line only when new line comes.
parser.on('data', (temp) => { //Read data
console.log(temp);
var today = new Date();
io.sockets.emit('temp', {date: today.getDate()+"-"+today.getMonth()+"-"+today.getFullYear(), time: (today.getHours())+":"+(today.getMinutes()+":"+ today.getMilliseconds()), temp: temp.split(",")}); //emit the datd i.e. {date, time, temp} to all the connected clients
var sensor = new SensorPost({status: temp});
sensor.save();
});
io.on('connection', (socket) => {
console.log("Someone connected.");
});
例如 我输入传感器的名称和数据库的端口,然后函数读取该数据并创建新的套接字。