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.

    2 answers 2

    The problem may also be in the data curves. Comes some line breaks, converted to NULL. When you try to write it to the socket, there is a quiet departure.

      I understand that clients connect to the script, io.broadcast.emit('update', {'temperature': temp}) tells them the temperature. So when you send another script crashes with some kind of error. Try changing to:

       try { io.broadcast.emit('update', {'temperature': temp}); } catch(err) { console.log(err); } 
      • Now I will try - Mark
      • Not. There is still no error. Script silently stops working http://prntscr.com/8zzhec - Mark
      • io.broadcast.emit and where did you get the io.broadcast.emit code io.broadcast.emit ? I do not see such an example in the documentation. The documentation just goes: io.emit('an event sent to all connected clients'); - lampa
      • Fixed io.emit ('update', {'temperature': temp}); no difference ( - Mark
      • @Mark and try to add a catch for closing the stream with an Arduino serialPort.on("close", function () { console.log('Serial port is close'); }); - lampa