3D Printer Web Cam

3D printers can be very temperamental beasts, small changes in temperatures, types of plastics used, overhang angles etc, can all cause a print to fail. Ideally you would watch the print from start to end i.e. allowing you to push down peeling layers or remove waste material. However, the fumes given off when heating plastic are not that nice. Solution: Raspberry Pi web cam server. The Pi is mounted in a 3D printed box as shown in figures 1 and 2. I downloaded the original STL files from Thingiverse, a lovely site for 3D printing (Site), (Link), (Local).

Figure 1 : Raspberry Pi box

Figure 2 : 3D model

To view the status of the print i mounted two web cameras on the printer, as shown in figures 3 and 4. One camera looks at the front screen so that you can see the bed / extruder temperatures and the percentage completed readouts. The second looks into the printer so that you can check that the print is still attached to the bed and that its structurally sound i.e. it has not been turned into a giant rats nest or melted into a big pool of plastic. To capture these video streams i used mjpg-streamer, this has a good fps, a lot faster than motion which i used in the bug press demo. Details of how to install this on the Pi can be found here: (Link) (Local), (Link) (Local). Found information on how to specify multiple cameras here: (Link) (Local).

Figure 3 : Cameras

Figure 4 : 3D printer

To start these video streams at boot i created these shell scripts and crontab entry below.

File: start-camera-0.sh

#!/bin/sh

/home/pi/mjpg/mjpg-streamer/mjpg_streamer -i "/home/pi/mjpg/mjpg-streamer/input_uvc.so -d /dev/video0 -y -r 640x480 -f 20" -o "/home/pi/mjpg/mjpg-streamer/output_http.so -p 8080 -w /var/www"

File: start-camera-1.sh

#!/bin/sh /home/pi/mjpg/mjpg-streamer/mjpg_streamer -i "/home/pi/mjpg/mjpg-streamer/input_uvc.so -d /dev/video1 -y -r 640x480 -f 20" -o "/home/pi/mjpg/mjpg-streamer/output_http.so -p 8090 -w /var/www"

At reboot the two mjpg-steams are started by the crontab entry below, standard out and error are redirected into files.

#
# CRONTAB
#

PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/sbin

@reboot /home/pi/bin/start-camera-0.sh 1>>/home/pi/bin/camera-0-message.txt 2>>/home/pi/bin/camera-0-error.txt
@reboot /home/pi/bin/start-camera-1.sh 1>>/home/pi/bin/camera-1-messgae.txt 2>>/home/pi/bin/camera-1-error.txt

0,10,20,30,40,50 * * * * /home/pi/bin/send-email.sh 1>>/home/pi/bin/ipaddr-message.txt 2>>/home/pi/bin/ipaddr-error.txt

Could of finished here but decided to add a web server as a front end: Apache2 (Link) (Local). However, just one small problem , the Raspberry Pi obtains its IP address using dhcp, this is normally quite static, but can change. If the IP address was to change this would causes two problems (1) the user will have to physically log into the Pi to obtain the new IP address, (2) the IP address used in the web page will be wrong. The web page update can again be done automatically using a template file (index.tmp) and the text stream editor sed, as shown below.

File: update.sh

#!/bin/sh

IPADDR=`hostname -I | sed 's/ //g'`
cat index.tmp | sed "s/IPADDR/$IPADDR/g" > index.html

File: index.tmp

Final updated index.html file

The above shell script program update.sh is call using another shell script send-email.sh, which automatically tests to see if the IP address has changed. If it has the web page is updated and an email is sent to the user informing them of the new IP address. There are a number of different methods of sending emails from the Pi, to be honest this was the thing that caused the most problems. Details of how to install this on the Pi can be found here: (Link) (Local), (Link) (Local). The final ssmtp config files are shown below:

File: /etc/ssmtp/ssmtp.conf

#
# Config file for sSMTP sendmail
#

root=postmaster
mailhub=smtp.gmail.com:587
hostname=raspberrypi
AuthUser=3d.printer.cs.york@gmail.com
AuthPass=PASSWORD
FromLineOverride=YES
UseSTARTTLS=YES

File: /etc/ssmtp/revaliases

#
# sSMTP aliases
#

root:3d.printer.cs.york@gmail.com:smtp.gmail.com:587
pi:3d.printer.cs.york@gmail.com:smtp.gmail.com:587

The send-email.sh shell script (below) is executed every ten minutes using the previous crontab. This file compares the previous (10 minutes ago) and current IP addresses, if they match, it finishes, if they do not the system is updated.

#!/bin/sh

ip1=""
ip2=""

read ip1 < /home/pi/bin/ip.txt
ip2=`hostname -I`

#echo $ip1 : $ip2

DATE=`date`

#echo $DATE

if test $ip1 = $ip2
then
	echo $DATE : no change
else
	echo $DATE : update addr to $ip2
	echo "$ip2" > /home/pi/bin/ip.txt
	echo "new ipaddr: $ip2" | mail -s "ipaddr: $DATE" 3d.printer.cs.york@gmail.com 
	/var/www/update.sh
fi

To view the video streams the user just needs to enter the IP address into a web browser as shown in figure 5 (at this time 144.32.50.7). I would describe my hmtl style as minimalist :)

Figure 5 : Web page

Creative Commons Licence

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Contact details: email - mike.freeman@york.ac.uk, telephone - 01904 32(5473)

Back