rjani1
rjani1

Reputation: 17

Updating serial port data on webpage with Node JS

I am reading data from the serial port using Node JS (which I am quite new to at the moment). Although I can see the data through an stdout stream via console.log and get a static value on the webpage I cannot get the value to update on a continuous basis. My current code looks like this:

var http = require('http');
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');

const port = new SerialPort('/dev/ttyACM0', { baudRate: 9600 });
const parser = port.pipe(new Readline({ delimiter: '\r\n' }));


http.createServer(function (request, response) {
    parser.on('data', (data) => {
        console.log(data);
        response.writeHead(200, {"Content-Type":"text/html"});
        response.write(data);
    });
}).listen(8888);

Is there a way I can get the serial data to update in realtime on the webpage as opposed to having to refresh the page?

Node JS version: v12.18.2

Upvotes: 0

Views: 566

Answers (1)

Kyle DePace
Kyle DePace

Reputation: 156

Depending on how often you want the page to update on the webpage, you could serve the most recent reading via an express api, or using something like socket io. Using express is a simpler but less real time. Socket io will be able to update in real time. https://socket.io/

Upvotes: 1

Related Questions