How Interesting...
January 5, 2019

Keep your printer healthy with an automated print-job via Raspberry Pi

Posted on January 5, 2019  •  6 minutes  • 1116 words
Table of contents

My Epson XP 312 gets snarky when I sporadically, then suddenly I need to print out a boarding past for an airline who still operates like it’s 1999, or some venue insists on physical tickets etc. When I finally want to use the printer, the cheap replacement ink I loaded it with all those months ago is now a dried mess all over the print head, clogging it right up - Every. Damn. Time. Cue the frantic hour or so running the head-clean program and spending a litre of ink, or at worst a new printer because it really isn’t worth the time over the £30-£50 for a replacement. Perhaps this is punishment for buying non-official (and overpriced) ink direct from the manufacturer.. 🤔

In my experience, the best way to keep the printer healthy - even with the knock-off ink from down the market - is to keep it in use rather than in semi-permanent stasis. Anyway I had a brainwave - could it be possible to automate a test page print-job? It certainly sounds plausible, and it’s the sort of thing Raspberry Pi's are made for.

The thing with Raspbian , the OS which comes with Raspberry Pi’s as standard, as great as it is for portability, you have to start pretty much from scratch every time you want to do even the most basic of tasks, and therefore something which would take me all of 5 minutes on fuller distros like Ubuntu or Windows images or whatever to script, the Pi takes a couple more steps. This is great for learning about the OS and great exposure to similar minimal distros such as Alpine however, so it’s important not to moan too much 😃.

This post will show what I did to set this automated test-page cron going.

Here’s what I started with:

CUPS

Cups are great. I had two cups of tea as I figured all this out. Also, CUPS stands for Common UNIX Printing System which will get us up and printing. If you’ve not updated your Pi recently it could be worth the obligatory update first:

1$ sudo apt-get update
2$ sudo apt-get install cups

Add your user (default is pi) to the correct group for permissions:

1$ sudo usermod -a -G lpadmin pi

lp is the actual application which will allow you to print - more on lp later.

Once cups is installed, we’ll need to configure it to work beyond the default of localhost.

Edit the following file:

1$ sudo vi /etc/cups/cupsd.conf

Replace this line

Listen localhost:631

With this line

Port 631

…and modify your config to look like the below. Some of this will already exist, so ensure you don’t repeat any lines:

1< Location / > # Restrict access to the server... Order allow,deny Allow @local<
2/Location > < Location /admin > # Restrict access to the admin pages... Order
3allow,deny Allow @local< /Location > < Location /admin/conf > AuthType Default
4Require user @SYSTEM # Restrict access to the configuration files... Order
5allow,deny Allow @local< /Location >

Save the file then restart cups to apply your changes:

1$ sudo /etc/init.d/cups restart

Now you should be able to get to the CUPS GUI by hitting your Raspberry Pi’s IP address on port 631 which we configured above. My Pi is on 192.168.1.247, so my access was http://192.168.1.247:631

You’ll get a screen like this all going well:

Navigate to the Administration Tab and then “Add Printer”

Expect a couple of things to happen:

Once that’s done, proceed to add your printer, which will hopefully appear on the discovered devices:

For some reason, mine was listed here twice, I just selected the top one. If your printer isn’t showing up here, ensure it’s switched on and connected to the network. Then try refreshing the page.

Once you’ve been able to select your printer, click continue.

Next CUPS wants you to confirm the printer name (which you’ll use shortly on the Pi itself). I just left it as the default. The sharing option here is if you wanted to share the printer through the Pi. This isn’t necessary as the printer is already a network printer.

Click Continue again.

Next CUPS wants you to select the driver for your printer. If the name of your printer is clearly listed here then go ahead and select it.

Drivers

My driver wasn’t listed, so I found a Github repository which contained the driver and used that. Here’s what I did. You’ll need to find the correct driver for your printer, but the process should hopefully be somewhat similar:

  1. Clone the Github repository
1$ git clone https://github.com/liberodark/Print-PPD
  1. Decompress the Zip using Gunzip
1$ gunzip Print-PPD/Epson/Epson-XP-312_313_315_Series-epson-driver.ppd.gz
  1. Select the file in the CUPS interface manually

Environment Variable

Next we need to set the default printer variable on the Raspberry Pi so it knows where to send your print jobs. This is especially important if you want to set up multiple printers.

SSH back onto the Pi and add the following to your bashrc file. You’ll need the name of the printer you specified in the CUPS GUI earlier.

1$ sudo vi ~/.bashrc

and add

1export PRINTER="EPSON_EPSON_XP-312_313_315_Series"

Replace “EPSON_EPSON_XP-312_313_315_Series” with your printer’s name from CUPS.

Time to send a test print job!

Create yourself a file if you don’t already have one, and use lp which came a part of the CUPS install to send it to the printer like so:

1$ lp <filename>

e.g.

1$ lp ~/Desktop/test-print-file.txt

All going well you should now see your printer spring into action and print the contents of your specified file.

Automate the print job

Finally, use cron to automate the print job 🖨️

1$ crontab -e

Don’t forget the -e flag if you already have other crons, or you’ll delete them.

Add something like the following line:

10 15 * * 3 lp ~/Desktop/test-print-file.txt

This cron runs every Wednesday at 3pm and prints out my test-print-file.txt file. It’ll need to contain all four colours to keep the printer head clean and healthy.

Tip 🚀

Each time you notice the print has occurred, you can just re-use the same piece of paper over and again so save paper and the environment 🌴

Socials / Links

I tweet tech, bad jokes and silly memes