CNCJS is kinda amazing and on a Raspberry Pi it gives you an easy way to get modern controls onto your CNC machine. But the pi is still SDCard and Linux based which means it doesn’t care much for being panic powered off and eventually, you’ll find yourself with an unbootable CNC controller.
This is a quick guide on how to install CNCJS on rasbian, make it read only so you can power it on and off all you want and how to update it afterwards.
First, start with a fresh Raspbian install (I’m not gonna duplicate those instructions here) and get SSH access into your Pi. I use Raspbian light, no sense in having a desktop UI you won’t be using. After the standard apt-get update and upgrade, we need to get CNCJS installed.
Installing CNCJS
The CNCJS folks have a great guide available on how to install on a pi, but to make it copy and paste
sudo apt-get update &&
sudo apt-get upgrade -y &&
sudo apt-get dist-upgrade -y &&
sudo apt-get install -y build-essential git &&
sudo apt-get install htop iotop nmon lsof screen npm -y &&
sudo npm install npm@latest -g &&
sudo npm install -g cncjs@latest --unsafe-perm &&
sudo npm install -g pm2 &&
sudo pm2 startup &&
sudo env PATH=$PATH:/usr/bin /usr/local/bin/pm2 startup systemd -u pi --hp /home/pi &&
pm2 start $(which cncjs) -- --port 8000 -m /tinyweb:/home/pi/tinyweb &&
pm2 save &&
sudo reboot
We have to reboot here because the kernel was likely upgraded and the following iptables command will fail without a reboot. Once you reboot, run the following.
sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000 &&
sudo apt-get install iptables-persistent -y
#Select Yes for both questions about saving iptables rules
That gets CNCJS installed and running under a supervisor so it starts at boot and setups a forward rule so you can connect on port 80 and be sent to port 8000. You should test that by browsing to your pi over port 80 http://raspi.local or whatever you named your machine.
Make your pi read only
To make your pi read only we used to have to jump through a whole bunch of hoops, but it’s pretty simple now. Just run
sudo raspi-config
Go to Performance Options
Select Overlay File System
Say “Yes” to Would you like Overlay filesystem enabled
Select “Yes” to Would you like to protect the boot partition
Select finish and reboot! When your machine comes online, you will now be in a read only file system. you can create all the files you want, and as soon as you reboot they will be gone. This also means no more proper shutdowns, just pull the power cord and walk away.
Updating your pi
You’ll eventually want to make changes, apply updates etc and you can’t do that while the file system is read only. The easiest way to make changes is to ssh into your pi and run
sudo mount -o remount,rw /boot
Which remounts the /boot partition as read write. Now you can run raspi-config again and disable the overlay filesystem.
sudo raspi-config
Select Performance Options, then select Overlay Filesystem
When you are prompted select No to Would you like the overlay file system to be enabled
You’ll probably get an error message about the boot filesystem being read-only. Ignore that, it doesn’t seem to be accurate.
Reboot.
When your system comes up you can patch and update just like normal. Don’t forget to rerun making the pi read only once you are done.
And that’s it, you now have a pi that should never need the SDCard replaced due to wear, all the benefits of CNCJS and a way to keep things current when you need to patch.