System Setup

The Raspberry Pi 3 is not currently compatible with TTL printers. Our USB-capable “Tiny” printer can work with this board…or substitute any other model of Raspberry Pi (e.g. A+) instead. Pi 3 is a power-hungry board anyway and not the best choice this battery-powered project.
If you’re using a TTL printer (not USB), DO NOT connect it to the Raspberry Pi yet! It will spit paper like mad. Some system configuration is required first…

Basic Setup

Download the latest version of Raspbian Jessie Lite if you haven’t already. You do not need the regular full version; “Lite” is adequate for this project’s needs and can fit on a 2GB card with room to spare. You’ll also need a keyboard and monitor attached, and a network connection…if using a Pi Model A+, this may require a USB hub and a WiFi or Ethernet adapter…or, if you have another working Pi system, it’s often easier to borrow that for setup, then move the working card over to the A+ board.

Write the OS to an 2GB or larger SD card, insert in the Raspberry Pi and power it up. After a minute or so you’ll get a login prompt. Log in with the usual default pi/raspberry user and password, then run the raspi-config utility:

sudo raspi-config

The following options are required:

  • Expand Filesystem.
  • Enable Camera.
  • Under “Advanced Options,” select “Serial” and disable the serial console.

The following are optional but recommended:

  • Under “Internationalisation Options,” configure the keyboard (and optionally the locale and time zone) to match your needs. If you’re getting unexpected output from the keyboard, this is why.
  • Change User Password because everybody knows the default.
  • Advanced Options: Change Hostname to distinguish this system from other Raspberry Pi systems on the network.
  • Advanced Options: Enable SSH to allow remote administration via network.
  • Advanced Options: Disable Overscan gives a little more working space if using an HDMI monitor.

When you’re done, tab over to the button and reboot the system when prompted. Log in as the “pi” user again, using the password you assigned above (or the default “raspberry” if you opted not to change it).

Networking

This is most easily done with a wired Ethernet connection — usually just a matter of plug it in & go. The Model A+ board doesn’t have an Ethernet port, which is why we recommend doing the setup on a more capable Pi system, then moving the card over when finished. Alternately, a USB hub and WiFi adapter can work.

Connecting to wireless networks usually involves editing either /etc/network/interfaces or /etc/wpa_supplicant/wpa_supplicant.conf with your wireless network name and credentials. Beyond the scope of this guide, it’s explained in other guides here, or Googling can quickly turn up some reference material.

The network is only needed during setup…once the camera’s tested and working, it’s all self-contained.

Install Software

First we’ll install printer support (CUPS — the Common UNIX Printing System) and some related development tools…

sudo apt-get update
sudo apt-get install git cups wiringpi build-essential libcups2-dev libcupsimage2-dev

Then install the raster filter for CUPS. This processes bitmap images into the thermal printer’s native format…

cd
git clone https://github.com/adafruit/zj-58
cd zj-58
make
sudo ./install

Your thermal printer may have arrived with a test page in the box or the paper bay. If not, or if you threw that away, you can generate a new one by installing a roll of paper and holding the feed button (on printers that have one) while connecting power, or tapping the button on the back of the “Nano” printer or the “Printer Guts.”

Look for the baud rate that’s printed near the bottom of the page. This is typically either 9600 or 19200 baud. This is important…you’ll need to know the correct value for your printer.

The printer doesn’t need to be connected yet. We can prepare the system the same regardless.

To add the printer to the CUPS system and set it as the default, we’ll be typing two lines similar to the following (but not necessaryily identical…read on)…

sudo lpadmin -p ZJ-58 -E -v serial:/dev/ttyAMA0?baud=9600 -m zjiang/ZJ-58.ppd
sudo lpoptions -d ZJ-58

On the first line, change the “baud” value to 9600 or 19200 as required for your printer.

For a USB receipt printer, change the device name to /dev/ttyUSB0

For all other (TTL) printers, use /dev/ttyAMA0 for the device name.

The rest of the line should be typed exactly as it appears above. Likewise for the second line, which needs no changes.

Camera Script

The code that handles the shutter button and moves images from the Pi camera to the printer is included with our version of the CUPS filter software. You’ll find it in /home/pi/zj-58/extras/camera.sh (unless you’re using a different account or home directory, or downloaded the zj-58 repository elsewhere…the important bit is, look in the “extras” directory for this script.

It’s all written as a Bash shell script, no Python or anything else required:

#!/bin/bash

SHUTTER=16
HALT=21
LED=5

# Initialize GPIO states
gpio -g mode  $SHUTTER up
gpio -g mode  $HALT    up
gpio -g mode  $LED     out

# Flash LED on startup to indicate ready state
for i in `seq 1 5`;
do
	gpio -g write $LED 1
	sleep 0.2
	gpio -g write $LED 0
	sleep 0.2
done

while :
do
	# Check for shutter button
	if [ $(gpio -g read $SHUTTER) -eq 0 ]; then
		gpio -g write $LED 1
		raspistill -n -t 200 -w 512 -h 384 -o - | lp

		sleep 1
		# Wait for user to release button before resuming
		while [ $(gpio -g read $SHUTTER) -eq 0 ]; do continue; done
		gpio -g write $LED 0
	fi

	# Check for halt button
	if [ $(gpio -g read $HALT) -eq 0 ]; then
		# Must be held for 2+ seconds before shutdown is run...
		starttime=$(date +%s)
		while [ $(gpio -g read $HALT) -eq 0 ]; do
			if [ $(($(date +%s)-starttime)) -ge 2 ]; then
				gpio -g write $LED 1
				shutdown -h now
			fi
		done
	fi
done

A few variables at the top of the script may need editing depending how you assemble your camera…we’ll return to this on the “Connections” page.

Configure for Auto-Start

Let’s make the camera script launch automatically when the system boots…

sudo nano /etc/rc.local

Before the final “exit 0” line, insert:

sh /home/pi/zj-58/extras/camera.sh

It should look something like this:

If you’ve located the camera.sh script somewhere else, enter the full path here.

Save the changes to the file and exit the editor.

Once the system’s fully configured, you may want to back up the SD card. Like any battery-operated Pi project, there’s a small possibility of the filesystem getting corrupted. Many utilities that write SD card images can also read a card to create a backup.

Last updated on 2016-08-12 at 12.01.40 AM Published on 2016-05-10 at 09.04.21 PM