DesignSpark Electrical Logolinkedin
Menu Search
Ask a Question

Safer laser cutting with the Raspberry Pi Camera Module v2

title

Remote monitoring with Rpi Cam Web Interface, MQTT and Node-RED

When version 2 of the Raspberry Pi camera was launched there was much celebration of the higher resolution and improved image quality, and with good reason. The new unit provides superior performance whilst remaining compatible with all Pi models with a camera connector. More information can be found over at the official Raspberry Pi website.

This new camera module is perfect for building an intelligent webcam that monitors use of the laser cutter in our workshop, allowing all members of staff to see what is happening within the machine. In this post we will build the camera into an acrylic housing and configure the software for video streaming and motion detection.

First steps

title

To determine the best location for our webcam, we first needed to connect the camera module to a suitable Raspberry Pi. The latest Raspbian image (at time of writing, this was Jessie 2016-05-27) was written to an SD card and configured as per instructions here.

The following commands were executed, ensuring the software was up-to-date:

$ sudo apt-get update

$ sudo apt-get upgrade

Next, camera module support was enabled, and the filesystem expanded to fit the entire SD card, using raspi-config:

$ sudo raspi-config

The Pi could now be configured to connect to our network using the wireless interface by editing the file wpa_supplicant.conf (we used the nano editor for simplicity):

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

and adding the following lines to the bottom of the file:

network={

ssid=”yourNetworkSSID”

psk=”yourPassword”

}

The file was then saved and closed. Next, a static IP address was configured, as this Pi will run headless and be controlled over the network. This is a different, somewhat simpler process in Jessie compared to older versions of Raspbian. Only one file requires editing, dhcpcd.conf:

$ sudo nano /etc/dhcpcd.conf

The following lines were added to the bottom of the file:

interface wlan0

static ip_address=10.0.10.21 /24

static routers=10.0.10.1

static domain_name_servers=10.0.10.1

Note that you will have to change the IP address information as required for your local network.

The file was then saved and the system rebooted.

title

Following a system reboot, we logged back in to the Pi, using the command ifconfig to check the IP address.

A simple test could then be conducted using the command raspistill, to check if the camera is functioning correctly.

Building the webcam

title

Rpi Cam Web Interface is a well-developed piece of software that simplifies the use of the Pi camera, with control over a browser-based interface. It is available in a GitHub repo, and can be installed on the Pi according to the instructions here. Once installed, the interface can be accessed over the network. To test, simply open a browser window on a computer connected to the same network as the Pi and type the IP address into the address bar.

You should be presented with the web interface and a video stream from the camera.

title

With the camera working and streaming video over the network, we could focus our attention on where to put it. Our requirements were as follows: the camera must cover the entirety of the cutting bed, be simple to maintain and ideally housed within the laser cutter itself.

Since we have used the Pi camera in several projects before, and the new camera module has the same mounting holes as the previous model, we already had a suitable design for a simple laser cut enclosure. Combined with a USB power bank, this provided a convenient wire-free way of testing out the best location for the camera.

Unfortunately, due to the relatively compact nature of the laser cutter enclosure, we could not find a position that gave full coverage of the cutting bed. We either needed to mount the camera outsideof the laser cutter, or try something else. A solution was found in putting an additional wide-angle lens in front of the Pi camera module. These lenses are commonly sold as accessories for mobile phone cameras and are readily available online.

Whilst we were able to get a good view of the cutting bed, the enclosure we had made up was still too bulky to fit inside the machine without fouling on the moving gantry. Therefore we decided to mount only the camera and lens inside the cutting area, and put the Pi next to the rest of the electronics. A 50cm ribbon cable, instead of the 15cm cable provided with the camera module, made this much easier.

A new design for a camera and lens holder was drawn up and laser cut, the files for which are available here. The Pi was mounted to an acrylic plate. Both of these were held in place using strong self-adhesive neodynium magnets, a simple, repositionable solution with no modification to the laser cutter case needed.

Motion detection to GPIO control

title

Scratching the surface of the RPi Cam Web Interface reveals a whole host of advanced camera settings and functions, with one particularly cool feature being motion detection. There are plenty of variables to be adjusted, covered in the documentation, but getting started could not be easier, with a big 'motion detection start' button displayed as part of the main interface.

Video recording, photo capture and much more can easily be configured based upon motion detection. Also present is the option of executing commands when motion starts and stops, a great way to interface with a wider system, in our case using MQTT to publish messages to our broker.

MQTT or 'Message Queueing Telemetry Transport' is a Machine-to-Machine (M2M) or Internet of Things (IoT) connectivity protocol that was designed to be extremely lightweight and useful when battery power and network bandwidth is at a premium and connection quality can be unreliable. It was invented in 1999 by Dr Andy Stanford-Clark and Arlen Nipper and is now an Oasis Standard. More information can be found on the MQTT website.

title

It is assumed that you have a local MQTT broker, for example Mosquitto, working alongside an instance of Node-RED, on your network. Referral to older posts on both MQTT and Node-RED may help with this.

The following steps also require installation of Mosquitto client tools on your webcam Pi and that you can publish messages to the broker, over the network.

For example, your publish command may look like this:

$ mosquitto_pub -d -t 'laser/motion' -m “1” -h '10.0.10.21'

This will publish a message with the payload '1' to the topic 'laser/motion' to a broker running on IP address 10.0.10.21. Moving from the command line to the 'Edit motion settings' menu on the web interface, we can copy this command into the 'on_event_start' field, so that it is executed each time motion is detected. A second command with a payload of '0' can then be copied into the 'on_event_end' field.

title

Next, a simple flow can be created within Node-RED, whereby the message payload is passed to a GPIO-out node. This payload of '1' will set the GPIO pin to high, whereas a payload of '0' will set it to low.

This GPIO pin could be connected to many different things to indicate the state of the laser cutter, for simplicity, we chose an LED beacon, switched through a transistor.

Reliable Pi

title

If a Raspberry Pi is turned off without being shut down the SD card can become corrupt, resulting in unreliable operation or inability to boot. Since our Pi is mounted in and takes power from the laser cutter, it is highly likely that it will be powered off without the correct shutdown procedure, so a solution is required.

There are several ways of protecting against SD card corruption, one of which is mounting the file system as read-only. Doing so can stop programs from working, requiring some investigation and changes to get them running again. The goal is to only allow programs to write to RAM-based filesystems, rather than on the physical SD card, so that no card corruption is possible.

There are many guides online on how to make the Pi's file system mount as read only, we found this one to be particularly useful, as it includes a method of using aliases to quickly switch between a read-only (RO) and read-write (RW) situation.

title

Once the filesystem had been changed to RO, the webcam interface no longer worked. Following some investigation, we first of all found that start.sh, the script that initialises the camera interface, tries to write some log files upon starting. To change the location of these log files, first edit /etc/rc.local:

$ sudo nano /etc/rc.local

Add two lines, one to create a new folder in the/tmpfs/partition (in RAM) called logs:

mkdir -p /dev/shm/logs

The nextline gives user 'www-data' (used by Rpi-Web-Cam-Interface) read/write access:

chown www-data:www-data /dev/shm/logs

title

Next, edit config.php to change the location of logs written by the camera software:

$ sudo nano /var/www/html/config.php

Two lines, defining where the debug file and the schedule log are stored need to be changed to the /dev/shm/logs directory (shown in the screenshot above).

With the above changes made, start.sh will execute without error, but the web interface will still not load. Suspecting problems with Apache, we tried to start it manually:

$ sudo apachectl start

This gave errors pertaining to the inability to create log files. So, a similar modification as above was made, changing Apache's log file location to /dev/shm/logs.

$ sudo nano /etc/apache2/envvars

Change this line:

export APACHE_LOG_DIR=/var/log/apache2$SUFFIX

to:

export APACHE_LOG_DIR=/dev/shm/logs

Save the changes and exit the editor. Apache can now be started with the following command:

$ sudo apachectl start

This should result in a working web interface, indicating that the changesare successful. To double-check, restart the Pi. After reboot, the web interface should start as expected, with a live video stream from the camera as before, and the motion detection working.

All of this is now taking place on a Pi with a read-only file system, with only the /tmpfs/ partition in RAM being written to.

In summary

title

The updated camera module for the Raspberry Pi has all of the convenience and practicality of the original camera module, but with a great performance boost. Coupled with fantastic Rpi-Cam-Web-Interface, building your own customisable webcam has never been simpler.

As far as the Pi and camera in our laser cutter goes, we could easily add more features, for example logging when the machine is in use, how many run time hours there have been, and so forth. Other peripherals or sensors could also be connected to the Pi, for example a temperature or smoke sensor would both be useful additions.

Important note: a camera is not a suitable replacement for observing a laser cutter in person. These machines should be attended at all times, you are responsible for your own actions.

maker, hacker, doer

18 Jul 2016, 9:37

Comments

August 3, 2016 08:25

Rob, that's interesting - can you post more information about the IR sensors you used? Sounds like a great solution!

0 Votes

August 1, 2016 10:43

Nice article.
We added two "IR fire sensors" to our laser and plugged the output on the reset input of the controller.
Once flames are detected the reset will move the laser-head away from the fire zone and sound an alarm.

0 Votes