In general, such a problem, the code from the bottom works, but after 1-2 minutes it stops working without displaying any errors. If you comment out the line:
io.emit('update', {'temperature': temp}); That script works forever.
var socket = require('socket.io'); var express = require('express'); var app = express(); var io = socket.listen(app.listen(8080)); var SerialPort = require("serialport").SerialPort var serialPort = new SerialPort("COM3", { baudrate: 9600, dataBits: 8, parity: 'none', stopBits: 1, flowControl: false }); app.get('/', function(req, res) { res.sendFile(__dirname + '/index.html'); }); io.sockets.on('connection', function(client) { var remoteAddress = client.request.connection.remoteAddress; console.log('New client ' + remoteAddress); }); serialPort.on("open", function () { console.log('Serial port is open'); }); serialPort.on('data', function(data) { temp = data.toString(); try { io.emit('update', {'temperature': temp}); } catch(err) { console.log(err); } process.stdout.write(temp); }); serialPort.on('close', function() { console.log('Serial port is closed'); }); serialPort.on('error', function(error) { console.log('Something wrong... ' + error); }); What is the reason I do not understand.