Skip to content

procedure 6

Now it’s time to think about a way to transfer our gcode from our website which it made by HTML and JavaScrip to our Arduino microcontroller. we thought about many solution, we need a way that provide us the ability to communicate between our javaScript and our Arduino microcontroller. we could do it by using node.js.

What is Node.js?

Node.js is actully a single-threaded, open-source, cross-platform runtime environment for building fast and scalable server-side and networking applications.

and thats what we exactly need! node.js can extablish a server and provide a feature to communicate through ports or ‘COM#’

Node.js has a library could ‘SerialPort’ and can be used to communicate with any device by local ports.

 var http = require('http');
 var fs = require('fs');

 var index = fs.readFileSync('index.html');

 var SerialPort = require('serialport');

 const parsers = SerialPort.parsers;
 const parser = new parsers.Readline({
    delimiter: '\r\n'
 });

 var port = new SerialPort('COM10', {
    baudRate: 115200,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
    flowControl: false
 });

 port.pipe(parser);

 var app = http.createServer(function (req, res) {

    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end(index);

 });

 const io = require('socket.io')(app, {
    cors: {
        origin: "http://localhost:3000",
        methods: ["GET", "POST"],
        transports: ['websocket', 'polling'],
        credentials: true
    },
    allowEIO3: true
 });

 io.on('connection', function (socket) {

    socket.on('lights', function (data) {

        port.write(data.status);
        console.log(data);

    });

 });

 app.listen(3000);

first we made a test to blink a microcontroller base on a value sent from our website to it.

Testing connection

Now after we opened the connection. we could send our gcode to microcontroller and move the product by grbl.

how to deal with grbl?

this video exxplain how to deal with grbl and make the needed setup to your CNC:


Last update: September 30, 2023