Webux Lab - Blog
Webux Lab Logo

Webux Lab

By Studio Webux

Search

By Tommy Gingras

Last update 2021-09-28

CNCRaspberry PI

Raspberry PI with CNCjs to control a CNC

The goal of this project is to prepare a raspberry pi 3b to be able to connect a CNC and control it using the Web UI with my iPhone / Laptop.

And optionally, you can also connect the raspberry pi camera to the setup.

Materials

  • Raspberry PI
  • SD Card
  • (Optional) Camera
  • Raspberry PI OS Lite up-to-date

Software

  • NodeJS > 10
  • NPM
  • SSH
  • cncjs

Steps

OS installation and basic configuration

The installation is not covered here, please follow the official documentation

sudo raspi-config

I've enabled the camera and configured the WiFi

Also you might want to update the pi user password.

Install NodeJS and npm

For this I'll use the NodeJS version available in the apt repo.

sudo apt update && sudo apt upgrade -y
sudo apt install -y nodejs npm

Install CNCjs globally

npm i -g cncjs --unsafe-perm

Create a service to start cncjs at boot

This service will start the small web server at boot time.

sudo nano /lib/systemd/system/cncjs.service

Add the following:

[Unit]
Description=CNCJS Service
After=network.target

[Service]
Type=simple
User=pi
ExecStart=/bin/bash /home/pi/scripts/cnc.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Create this script : /home/pi/scripts/cnc.sh

Add the following

#!/bin/bash

cncjs --allow-remote-access

To enable the service:

sudo chmod 644 /lib/systemd/system/cncjs.service
sudo systemctl daemon-reload
sudo systemctl enable cncjs.service

You can either sudo reboot or sudo systemctl start cncjs to test the service.

To retrieve the logs or the service status, sudo systemctl status cncjs

Access

You can access the WebUI using the http://<ip>:8000 from any devices.


(Optional) Raspberry PI Camera

I've followed the steps from cncjs documentation,

Install prerequisites

sudo apt-get install -y cmake libjpeg8-dev gcc g++ libv4l-0

Download mjpg streamer

wget https://github.com/jacksonliam/mjpg-streamer/archive/refs/tags/v1.0.0.tar.gz
tar xvzf v1.0.0.tar.gz
cd mjpg-streamer-1.0.0/mjpg-streamer-experimental
make
sudo make install

Include the command to our start at boot script

update the following script : /home/pi/scripts/cnc.sh

Add the following

#!/bin/bash

pushd /home/pi/mjpg-streamer-1.0.0/mjpg-streamer-experimental
./mjpg_streamer -o "output_http.so -w ./www" -i "input_raspicam.so" &
popd

cncjs --allow-remote-access

Extra

  • cncjs recommends to create a user, you can do that using the webui.
  • I've tested the setup with a ShapeOko 2, it works great !
    • The connection uses the baud rate 9600 and the $$ command is well supported (it uses GRBL).

First time using new device

When trying to connect with cncjs to the webcam you must enter the feed URL

You can access this page to retrive the URL : http://<ip>:8080/videolan.html


Voila !