7 Replies Latest reply on Dec 21, 2016 2:20 PM by Intel Corporation

    Issue about console.log in Node.JS and memory of Intel Edison

    Danil.Borchevkin

      Hello!

       

      Now in prototype we use Intel Edison as gateway for our system (from UART 921600 to TCP socket). And now we have some strange issue about Edison's working. The Edison works well durin 1 day, after that - it's down, and have no ability connect over Wi-Fi, even Intel XDK. Helps ony firmware new Yocto image, I realized what I give now incomplete information but I have some thougths about what situation.

       

      In own code there are a lot of console.log outputs (for each UART packet on speed 1 Mbps). Is it may be a reason for stucking of the Edison?

       

      By the way - where is exist output of console.log() - in RAM or ROM?

       

      There is my code (some console.log was commented for tests)

       

      /*jslint node:true, vars:true, bitwise:true, unparam:true */
      /*jshint unused:true */
      
      /*
      
      app_EdisonSocket is purposed to for enabling transparent bridge for UART1 over WiFi using TCP.
      
      On clients part (balalaika.local:3000) there is some features:
      - set UART speed
      - enable/disable logging
      - visualise connected clients
      
      */
      
      /* Definitions */
      
      var UART_SPEED = 921600;
      var HTTP_PORT = 3000;
      var TCP_PORT = 56789;
      
      var timerId = setInterval(function() {
        console.log("This is log console. We are testing overflow of the disk. 123453345767597807iutkmhbWETY  3Q5UYHE4T5UYHQ4EA35T6YUHQ4RA65TUYH4AQR65JUHR4AJURA5YJARTJUY");
      }, 100);
      
      /* Imports */
      var mraa = require('mraa'); //require mraa
      console.log('MRAA Version: ' + mraa.getVersion()); 
      
      //Init UART1
      var u = new mraa.Uart(0);
      var serialPath = u.getDevicePath();
      var SerialPort = require("serialport");
      var serialPort = new SerialPort(
          serialPath, 
          {
              baudrate: UART_SPEED
          }
      );
      
      //TCP Server
      var net = require('net');
      var tcpServer = net.createServer();
      var tcpClients = [];
      
      
      tcpServer.listen(TCP_PORT, function() {
          console.log("TCP Server start listening on %j", tcpServer.address());
      });
      
      tcpServer.on("connection", handleConnection);
      
      function handleConnection(conn) {
          var remoteAddress = conn.remoteAddress + ":" + conn.remotePort;
          console.log("New client connection from %s", remoteAddress);
          io.emit('log message', {value: "Connection open", userId: remoteAddress});
          
          tcpClients.push(conn);
          
          conn.on("data", onConnData);
          conn.once("close", onConnClose);
          conn.on("error", onConnError);
          
          function onConnData(message) {
              var sendBuf = new Buffer(message);
              serialPort.write(sendBuf);
              //console.log('Connection data from ', remoteAddress, ": ", sendBuf);
              io.emit('log message', {value: sendBuf.toString('hex'), userId: remoteAddress});
          }
      
          function onConnClose() {
              console.log('Connection from %s closed', remoteAddress);
              io.emit('log message', {value: "Connection closed", userId: remoteAddress});
              tcpClients.splice(tcpClients.indexOf(conn), 1);
          }
      
          function onConnError(err) {
              console.log('Connection %s error: %s', remoteAddress, err.message);
              io.emit('log message', {value: "Connection error: " + err.message, userId: remoteAddress});
          }
      }
      
      function broadcast(message) {
          io.emit('log message', {value: message.toString('hex'), userId: "UART1"});
          tcpClients.forEach(function (tcpClient) {
              tcpClient.write(message);
          });
      }
      
      //HTTP server init
      var express = require('express');
      var app = express();
      var path = require('path');
      var http = require('http').Server(app);
      var io = require('socket.io')(http);
      
      // HTTP Port start listening
      http.listen(HTTP_PORT, function() {
          var address = http.address();
          var port = address.port;
          console.log("HTTP listen on port " + port);
          //LCD_updateHttp(port);
      });
      
      
      var connectedUsersArray = [];
      var userId;
      
      app.get('/', function(req, res) {
          //Join all arguments together and normalize the resulting path.
          res.sendFile(path.join(__dirname + '/client', 'index.html'));
      });
      
      //Allow use of files in client folder
      app.use(express.static(__dirname + '/client'));
      app.use('/client', express.static(__dirname + '/client'));
      
      //Socket.io Event handlers
      io.on('connection', function(socket) {
          
          console.log("\n Add new User: u"+connectedUsersArray.length);
          if(connectedUsersArray.length > 0) {
              var element = connectedUsersArray[connectedUsersArray.length-1];
              userId = 'u' + (parseInt(element.replace("u", ""))+1);
          }
          else {
              userId = "u0";
          }
          console.log('a user connected: '+userId);
          io.emit('user connect', userId);
          connectedUsersArray.push(userId);
          console.log('Number of Users Connected ' + connectedUsersArray.length);
          console.log('User(s) Connected: ' + connectedUsersArray);
          io.emit('connected users', connectedUsersArray);
          
          socket.on('user disconnect', function(msg) {
              console.log('remove: ' + msg);
              connectedUsersArray.splice(connectedUsersArray.lastIndexOf(msg), 1);
              io.emit('user disconnect', msg);
          });
          
          socket.on('log message', function(msg) {
              io.emit('log message', msg);
              //console.log('message: ' + msg.value);
          });
          
          socket.on("change uart speed", function(newSpeed) {
              console.log("Invoke speed change to " + newSpeed);
              serialPort.update(
                  {
                      baudrate: newSpeed
                  }
              );
              io.emit('log message', {value: "UART Speed changed to " + newSpeed, userId: "UART1"});
          });
      });
      
      serialPort.on("open", function() {
          console.log("UART opened and connected to " +serialPath);
      
          //If we get data on UART then resend it to all connected users
          serialPort.on("data", function(data) {
              var rxBuf = new Buffer(data);
              //console.log("Data received from UART: ", rxBuf);
              broadcast(rxBuf);
          });
      });