Scott Penrose

Linux Serial via Websockets

Scott is an expert software developer with over 30 years experience, specialising in education, automation and remote data.

Difference between revision 4 and current revision

No diff available.

The intel Edison doesn't actual have 'screen' or other serial monitoring on board. But even if it did, this is a useful tool for monitoring serial real time.

HTML / CSS / Javascript

#!HTML
<!doctype html>
<html>
  <head>
    <title>Serial Port</title>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('serialio', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('serialio', function(msg){
        $('#messages').append($('<li>').text(msg.data));
      });
    </script>
  </body>
</html>

Node JS Serial

#!JavaScript
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mraa = require('mraa');
var SerialPort = require("serialport").SerialPort;

u = new mraa.Uart(0); 
var serialPath = u.getDevicePath(); 
var serialPort = new SerialPort(serialPath, {baudrate: 9600});

function convertArrayBufferToString(buf){
	var bufView = new Uint8Array(buf);
	var encodedString = String.fromCharCode.apply(null, bufView);
	return decodeURIComponent(encodedString);
}

io.sockets.on('connection', function (socket) { 
	serialPort.on("data", function(data) { 
		var str = convertArrayBufferToString(data);
		socket.emit('serialio', { data: str });
	});

	socket.on('serialio', function(data) {
		serialPort.write(data);
		serialPort.write("\r\n");
	});
});

  • Edison
  • RaspberryPi
  • Serial