Search code examples
node.jssocket.ioarduinotcpsocketeconnreset

Node.js with TCP socket to arduino


I have a project were the node.js server talks with the arduino true a TCP socket. It's receiving all his data from a webpage true a socket.io and then transfers it to the TCP socket. Everything is working fine. Except when:

I download my arduino with new software, Or when I unplug my ethernet cable, When I open my serial monitor two times on the arduino. I got events.js:85 throw er; // Unhandled 'error' event Error: read ECONNRESET at exports._errnoException (util.js:746:11) at TCP.onread (net.js:550:26)

Does any know what's the problem. How can I fix it especially an auto-connect function.

var express = require('express');
var app     = express();
var hbs     = require('hbs');
var port    = 3000;
var http = require('http').Server(app);
var io = require('socket.io')(http);
net = require('net');
var tcpGuests = [];

app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use(express.bodyParser());
app.use(express.static('public'));


app.get('/', function(req, res) {
    res.render('index', {title:"HTML5-SuperTemplate"});
});

app.get('/about', function(req, res) {
    res.render('about', {title:"About Me"});
});

app.get('/instructions', function(req, res) {
    res.render('instructions', {title:"Instructions"});
});

app.get('/buttons', function(req, res) {
    res.render('buttons', {title:"Buttons"});
});

app.get('/Lists', function(req, res) {
    res.render('Lists', {title:"Lists"});
});

app.get('/menus', function(req, res) {
    res.render('menus', {title:"Menus"});
});

app.get('/tables', function(req, res) {
    res.render('tables', {title:"Tables"});
});

app.get('/tooltips', function(req, res) {
    res.render('tooltips', {title:"Tooltips"});
});

app.get('/typography', function(req, res) {
    res.render('typography', {title:"Typography"});
});

app.get('/hr', function(req, res) {
    res.render('hr', {title:"Horizontal Rules"});
});

app.get('/icons', function(req, res) {
    res.render('icons', {title:"ICONS"});
});

app.get('/code', function(req, res) {
    res.render('code', {title:"CODE & PRE"});
});

app.get('/tabs', function(req, res) {
    res.render('tabs', {title:"TABS"});
});

app.get('/breadcrumbs', function(req, res) {
    res.render('breadcrumbs', {title:"Breadcrumbs"});
});

app.get('/grids', function(req, res) {
    res.render('grids', {title:"Grid System"});
});

app.get('/images', function(req, res) {
    res.render('images', {title:"IMAGES"});
});

app.get('/slideshow', function(req, res) {
    res.render('slideshow', {title:"Slideshow"});
});

app.get('/forms', function(req, res) {
    res.render('forms', {title:"Forms"});
});

app.get('/classes', function(req, res) {
    res.render('classes', {title:"Classes"});
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
    console.log('received on io-socket:'+msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

// socket.io, I choose you
io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
    console.log('received on io-socket:'+msg);
    for (g in tcpGuests) {
        tcpGuests[g].write(msg);
    }
  });
});

//tcp socket server
var tcpServer = net.createServer(function (socket) {
  console.log('tcp server running on port 1337');
  console.log('web server running on http://localhost:3000');
});

tcpServer.on('connection',function(socket){
    socket.write('connected to the tcp server\r\n');
    console.log('num of connections on port 1337: ' + tcpServer.connections);

    tcpGuests.push(socket);


    socket.on('data',function(data){
        console.log('received on tcp socket:'+data);
        socket.write(' msg received\r\n');


        //send data to guest socket.io chat server
        for (g in io.clients) {
            var client = io.clients[g];
            client.send({message:["arduino",data.toString('ascii',0,data.length)]});
          }
    })
});
tcpServer.listen(1337);

Solution

  • You do not do error handling. The error message you receive is being thrown by node.js every time there is a connection error (unexpected cable unplug). You can catch those errors so that the application won't quit.

    Add few error handlers. tcpServer.on('error', ...), socket.on('error', ...), io.on('error', ...), etc. Maybe some other error handlers.